Adilol

JeaxEncoding

Jul 9th, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Holo
  5. {
  6.     /// <summary>
  7.     /// Provides number encoding/decoding for the Habbo client. Features VL64 and Base64. Class written by Josh Comery. (Jeax)
  8.     /// </summary>
  9.     public static class Encoding
  10.     {
  11.         #region Base64
  12.         public static string encodeB64(int value, int length)
  13.         {
  14.             string stack = "";
  15.             for (int x = 1; x <= length; x++)
  16.             {
  17.                 int offset = 6 * (length - x);
  18.                 byte val = (byte)(64 + (value >> offset & 0x3f));
  19.                 stack += (char)val;
  20.             }
  21.             return stack;
  22.         }
  23.         public static string encodeB64(string Val)
  24.         {
  25.             int value = Val.Length;
  26.             int length = 2;
  27.             string stack = "";
  28.             for (int x = 1; x <= length; x++)
  29.             {
  30.                 int offset = 6 * (length - x);
  31.                 byte val = (byte)(64 + (value >> offset & 0x3f));
  32.                 stack += (char)val;
  33.             }
  34.             return stack;
  35.         }
  36.         public static int decodeB64(string Val)
  37.         {
  38.             char[] val = Val.ToCharArray();
  39.             int intTot = 0;
  40.             int y = 0;
  41.             for (int x = (val.Length - 1); x >= 0; x--)
  42.             {
  43.                 int intTmp = (int)(byte)((val[x] - 64));
  44.                 if (y > 0)
  45.                 {
  46.                     intTmp = intTmp * (int)(Math.Pow(64, y));
  47.                 }
  48.                 intTot += intTmp;
  49.                 y++;
  50.             }
  51.             return intTot;
  52.         }
  53.         #endregion
  54.         #region VL64
  55.         public static string encodeVL64(int i)
  56.         {
  57.             byte[] wf = new byte[6];
  58.             int pos = 0;
  59.             int startPos = pos;
  60.             int bytes = 1;
  61.             int negativeMask = i >= 0 ? 0 : 4;
  62.             i = Math.Abs(i);
  63.             wf[pos++] = (byte)(64 + (i & 3));
  64.             for (i >>= 2; i != 0; i >>= 6)
  65.             {
  66.                 bytes++;
  67.                 wf[pos++] = (byte)(64 + (i & 0x3f));
  68.             }
  69.  
  70.             wf[startPos] = (byte)(wf[startPos] | bytes << 3 | negativeMask);
  71.  
  72.             System.Text.ASCIIEncoding encoder = new ASCIIEncoding();
  73.             string tmp = encoder.GetString(wf);
  74.             return tmp.Replace("\0", "");
  75.         }
  76.         public static int decodeVL64(string data)
  77.         {
  78.             return decodeVL64(data.ToCharArray());
  79.         }
  80.         public static int decodeVL64(char[] raw)
  81.         {
  82.             try
  83.             {
  84.                 int pos = 0;
  85.                 int v = 0;
  86.                 bool negative = (raw[pos] & 4) == 4;
  87.                 int totalBytes = raw[pos] >> 3 & 7;
  88.                 v = raw[pos] & 3;
  89.                 pos++;
  90.                 int shiftAmount = 2;
  91.                 for (int b = 1; b < totalBytes; b++)
  92.                 {
  93.                     v |= (raw[pos] & 0x3f) << shiftAmount;
  94.                     shiftAmount = 2 + 6 * b;
  95.                     pos++;
  96.                 }
  97.  
  98.                 if (negative)
  99.                     v *= -1;
  100.  
  101.                 return v;
  102.             }
  103.             catch
  104.             {
  105.                 return 0;
  106.             }
  107.         }
  108.         #endregion
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment