namespace Holo { using System; using System.Text; public static class Encoding { public static int decodeB64(string Val) { char[] val = Val.ToCharArray(); int intTot = 0; int y = 0; for (int x = val.Length - 1; x >= 0; x--) { int intTmp = (byte) (val[x] - '@'); if (y > 0) { intTmp *= (int) Math.Pow(64.0, (double) y); } intTot += intTmp; y++; } return intTot; } public static int decodeVL64(char[] raw) { try { int pos = 0; int v = 0; bool negative = (raw[pos] & '\x0004') == 4; int totalBytes = (raw[pos] >> 3) & '\a'; v = raw[pos] & '\x0003'; pos++; int shiftAmount = 2; for (int b = 1; b < totalBytes; b++) { v |= (raw[pos] & 0x3f) << shiftAmount; shiftAmount = 2 + (6 * b); pos++; } if (negative) { v *= -1; } return v; } catch { return 0; } } public static int decodeVL64(string data) { return decodeVL64(data.ToCharArray()); } public static string encodeB64(string Val) { int value = Val.Length; int length = 2; string stack = ""; for (int x = 1; x <= length; x++) { int offset = 6 * (length - x); byte val = (byte) (0x40 + ((value >> offset) & 0x3f)); stack = stack + ((char) val); } return stack; } public static string encodeB64(int value, int length) { string stack = ""; for (int x = 1; x <= length; x++) { int offset = 6 * (length - x); byte val = (byte) (0x40 + ((value >> offset) & 0x3f)); stack = stack + ((char) val); } return stack; } public static string encodeVL64(int i) { byte[] wf = new byte[6]; int pos = 0; int startPos = pos; int bytes = 1; int negativeMask = (i >= 0) ? 0 : 4; i = Math.Abs(i); wf[pos++] = (byte) (0x40 + (i & 3)); i = i >> 2; while (i != 0) { bytes++; wf[pos++] = (byte) (0x40 + (i & 0x3f)); i = i >> 6; } wf[startPos] = (byte) ((wf[startPos] | (bytes << 3)) | negativeMask); ASCIIEncoding encoder = new ASCIIEncoding(); return encoder.GetString(wf).Replace("\0", ""); } } }