public Int32 GetByteCount() { if (mSize != -2) { return(mSize); } mTree = new Node[T]; mLeaf = new Int32[N_SYM]; InitTree(); BitReaderB R = new BitReaderB(mData, mOffset); Int32 ct = 0; Int32 n; while ((n = GetSymbol(R)) != -1) { UpdateNode(n); // this is what makes the coding "Adaptive" if (n < 256) { ct++; // symbol encodes a single byte } else { ct += n - (256 - 3); // symbol encodes a length if (GetPosition(R) == -1) { return(mSize = -1); } } } return(mSize = ct); }
private Int32 GetSymbol(BitReaderB reader) { Int32 code = 0, len = 0; Int32 p = T - 1; // start at root while (mTree[p].L != -1) { Int32 bit = reader.Next(1); if (bit == -1) { return(-1); } p = (bit == 0) ? mTree[p].L : mTree[p].R; code <<= 1; code += bit; len++; } return(mTree[p].R); }
private Int32 GetPosition(BitReaderB reader) { switch (reader.Next(4)) { case 0: return(0x000 | reader.Next(5)); case 1: return(0x020 | reader.Next(5)); case 2: return(0x040 | reader.Next(6)); case 3: return(0x080 | reader.Next(6)); case 4: return(0x0c0 | reader.Next(6)); case 5: return(0x100 | reader.Next(7)); case 6: return(0x180 | reader.Next(7)); case 7: return(0x200 | reader.Next(7)); case 8: return(0x280 | reader.Next(7)); case 9: return(0x300 | reader.Next(8)); case 10: return(0x400 | reader.Next(8)); case 11: return(0x500 | reader.Next(8)); case 12: return(0x600 | reader.Next(9)); case 13: return(0x800 | reader.Next(9)); case 14: return(0xa00 | reader.Next(9)); case 15: return(0xc00 | reader.Next(10)); } return(-1); }
public Byte[] GetBytes() { Int32 q = GetByteCount(); if (q == -1) { return(null); } Byte[] data = new Byte[q]; q = 0; Byte[] buf = new Byte[N]; Int32 p = N - F; for (Int32 i = 0; i < p; i++) { buf[i] = 32; } InitTree(); BitReaderB R = new BitReaderB(mData, mOffset); Int32 n; while ((n = GetSymbol(R)) != -1) { UpdateNode(n); // this is what makes the coding "Adaptive" if (n < 256) { data[q++] = (Byte)n; // symbol encodes a single byte buf[p++] = (Byte)n; if (p >= buf.Length) { p = 0; } } else { n -= (256 - 3); // symbol encodes a length Int32 m = GetPosition(R); // (position follows) Int32 k = p - 1 - m; if (k < 0) { k += buf.Length; } for (Int32 i = 0; i < n; i++) { data[q++] = buf[k]; buf[p++] = buf[k++]; if (p >= buf.Length) { p = 0; } if (k >= buf.Length) { k = 0; } } } } return(data); }