public static string Encrypt <P>(P prin, byte proj) where P : IData { var cnt = new JsonContent(true, 4096); try { cnt.Put(null, prin, proj); byte[] bytebuf = cnt.ByteBuffer; int count = cnt.Size; int mask = sign; int[] masks = { (mask >> 24) & 0xff, (mask >> 16) & 0xff, (mask >> 8) & 0xff, mask & 0xff }; char[] charbuf = new char[count * 2]; // the target int p = 0; for (int i = 0; i < count; i++) { // masking int b = bytebuf[i] ^ masks[i % 4]; //transform charbuf[p++] = HEX[(b >> 4) & 0x0f]; charbuf[p++] = HEX[(b) & 0x0f]; // reordering } return(new string(charbuf, 0, charbuf.Length)); } finally { // return pool BufferUtility.Return(cnt); } }
protected DynamicContent(bool binary, int capacity) { if (binary) { bytebuf = BufferUtility.GetByteBuffer(capacity); } else { charbuf = BufferUtility.GetCharBuffer(capacity); } count = 0; }
public override string ToString() { JsonContent cnt = new JsonContent(false, 4096); try { cnt.PutFromSource(this); return(cnt.ToString()); } finally { BufferUtility.Return(cnt); } }
public static string ToString <D>(List <D> v, byte proj = 0x0f) where D : IData { var cnt = new JsonContent(false, 4 * 1024); try { cnt.Put(null, v, proj); return(cnt.ToString()); } finally { BufferUtility.Return(cnt); // return buffer to pool } }
void AddByte(byte b) { // ensure capacity int olen = bytebuf.Length; // old length if (count >= olen) { int nlen = olen * 4; // new length byte[] obuf = bytebuf; bytebuf = BufferUtility.GetByteBuffer(nlen); Array.Copy(obuf, 0, bytebuf, 0, olen); BufferUtility.Return(obuf); } bytebuf[count++] = b; // calculate checksum ulong cs = checksum; cs ^= b; // XOR checksum = cs >> 57 | cs << 7; // circular left shift 7 bit }
public void Add(char c) { if (IsBinary) // byte-oriented { // UTF-8 encoding but without surrogate support if (c < 0x80) { // have at most seven bits AddByte((byte)c); } else if (c < 0x800) { // 2 char, 11 bits AddByte((byte)(0xc0 | (c >> 6))); AddByte((byte)(0x80 | (c & 0x3f))); } else { // 3 char, 16 bits AddByte((byte)(0xe0 | ((c >> 12)))); AddByte((byte)(0x80 | ((c >> 6) & 0x3f))); AddByte((byte)(0x80 | (c & 0x3f))); } } else // char-oriented { // ensure capacity int olen = charbuf.Length; // old length if (count >= olen) { int nlen = olen * 4; // new length char[] obuf = charbuf; charbuf = BufferUtility.GetCharBuffer(nlen); Array.Copy(obuf, 0, charbuf, 0, olen); BufferUtility.Return(obuf); } charbuf[count++] = c; } }