Advertisement
stoneharry

Untitled

Sep 22nd, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1.  
  2. class BitReader
  3. {
  4. private:
  5.     unsigned char * _buffer;
  6.     int32 _numBits;
  7.     int32 _readPos;
  8.  
  9. public:
  10.     BitReader::BitReader(unsigned char buffer[], uint32 Length)
  11.     {
  12.         _buffer = buffer;
  13.         _numBits = Length*8;
  14.         _readPos = 0;
  15.     }
  16.  
  17.     unsigned char * Buffer()
  18.     {
  19.         return _buffer;
  20.     }
  21.  
  22.     int32 NumBits()
  23.     {
  24.         return _numBits;
  25.     }
  26.  
  27.     int32 ByteLength()
  28.     {
  29.         return NumBits()/8;
  30.     }
  31.  
  32.     int32 ReadPos()
  33.     {
  34.         return _readPos;
  35.     }
  36.  
  37.     void AlignToNextByte()
  38.     {
  39.         _readPos = (ReadPos() + 7) & ~7;
  40.     }
  41.  
  42.     unsigned char * ReadBytes(int count)
  43.     {
  44.         AlignToNextByte();
  45.         unsigned char * buf = new unsigned char[count];
  46.         memcpy(_buffer + (ReadPos() >> 3), buf, count);
  47.         _readPos = ReadPos() + count*8;
  48.         return buf;
  49.     }
  50.  
  51.     unsigned char * ReadBuffer(int sizeBitCount)
  52.     {
  53.         int blobSize = ReadInt32(sizeBitCount);
  54.         return ReadBytes(blobSize);
  55.     }
  56.  
  57.     bool ReadBoolean()
  58.     {
  59.         return ReadInt32(1) != 0;
  60.     }
  61.  
  62.     bool ReadOptional()
  63.     {
  64.         return ReadBoolean();
  65.     }
  66.  
  67.     float ReadFloat()
  68.     {
  69.         //return BitConverter.ToSingle(BitConverter.GetBytes(ReadInt32(32)), 0);
  70.         return float(ReadInt32(32));
  71.     }
  72.  
  73.     string ReadAsciiString(int32 bitCount)
  74.     {
  75.         int32 len = ReadInt32(bitCount);
  76.         //return Encoding.ASCII.GetString(ReadBytes(len));
  77.         return reinterpret_cast<char*>(ReadBytes(len));
  78.     }
  79.  
  80.     string ReadAsciiString(int32 bitCount, int32 min)
  81.     {
  82.         int32 len = ReadInt32(bitCount, min);
  83.         //return Encoding.ASCII.GetString(ReadBytes(len));
  84.         return reinterpret_cast<char*>(ReadBytes(len));
  85.     }
  86.  
  87.     string ReadUTFString(int32 bitCount)
  88.     {
  89.         int32 len = ReadInt32(bitCount + 2);
  90.         //return Encoding.UTF8.GetString(ReadBytes(len));
  91.         return reinterpret_cast<char*>(ReadBytes(len));
  92.     }
  93.  
  94.     uint32 ReadUInt32(int32 numBits)
  95.     {
  96.         if (numBits <= 0)
  97.             return 0;
  98.         uint32 ret = 0;
  99.         while (true)
  100.         {
  101.             if (ReadPos() >= _numBits)
  102.             {
  103.                 return ret;
  104.             }
  105.  
  106.             int32 pos7 = (ReadPos() & 7);
  107.             int32 bitsLeftInByte = 8 - pos7;
  108.  
  109.             int32 subNum = bitsLeftInByte;
  110.             if (bitsLeftInByte >= numBits)
  111.                 subNum = numBits;
  112.  
  113.             Byte lShift = (Byte)(1 << subNum);
  114.  
  115.             int v10 = _buffer[ReadPos() >> 3] >> (ReadPos() & 7);
  116.             numBits -= subNum;
  117.  
  118.             ret |= (uint32) (((lShift - 1) & v10) << numBits);
  119.             _readPos = ReadPos() + subNum;
  120.  
  121.             if (numBits == 0)
  122.                 return ret;
  123.         }
  124.     }
  125.  
  126.     int32 ReadInt32(int32 numBits)
  127.     {
  128.         if (numBits <= 0)
  129.             return 0;
  130.         int32 ret = 0;
  131.         while (true)
  132.         {
  133.             if (ReadPos() >= _numBits)
  134.             {
  135.                 return ret;
  136.             }
  137.  
  138.             int32 pos7 = (ReadPos() & 7);
  139.             int32 bitsLeftInByte = 8 - pos7;
  140.  
  141.             int32 subNum = bitsLeftInByte;
  142.             if (bitsLeftInByte >= numBits)
  143.                 subNum = numBits;
  144.  
  145.             Byte lShift = (Byte) (1 << subNum);
  146.  
  147.             int32 v10 = _buffer[ReadPos() >> 3] >> (ReadPos() & 7);
  148.             numBits -= subNum;
  149.  
  150.             ret |= ((lShift - 1) & v10) << numBits;
  151.             _readPos = ReadPos() + subNum;
  152.  
  153.             if (numBits == 0)
  154.                 return ret;
  155.         }
  156.     }
  157.  
  158.  
  159.     int32 ReadInt32(int32 numBits, int32 min)
  160.     {
  161.         return ReadInt32(numBits) + min;
  162.     }
  163.  
  164.  
  165.     long ReadInt64(int32 numBits)
  166.     {
  167.         if (numBits <= 0)
  168.             return 0;
  169.         long ret = 0;
  170.         while (true)
  171.         {
  172.             if (ReadPos() >= _numBits)
  173.             {
  174.                 return ret;
  175.             }
  176.  
  177.             int32 pos7 = (ReadPos() & 7);
  178.             int32 bitsLeftInByte = 8 - pos7;
  179.  
  180.             int subNum = bitsLeftInByte;
  181.             if (bitsLeftInByte >= numBits)
  182.                 subNum = numBits;
  183.  
  184.             Byte lShift = (Byte)(1 << subNum);
  185.  
  186.             int32 v10 = _buffer[ReadPos() >> 3] >> (ReadPos() & 7);
  187.             numBits -= subNum;
  188.  
  189.             ret |= ((lShift - 1) & v10) << numBits;
  190.             _readPos = ReadPos() + subNum;
  191.  
  192.             if (numBits == 0)
  193.                 return ret;
  194.         }
  195.     }
  196. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement