stoneharry

Untitled

Oct 8th, 2013
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1.  
  2. #ifndef BITWRITER_H_BN
  3. #define BITWRITER_H_BN
  4.  
  5. class BitWriter_BN
  6. {
  7. private:
  8.     unsigned char * _buffer;
  9.     int32 _numBits;
  10.  
  11. public:
  12.     BitWriter_BN(int32 capacity)
  13.     {
  14.         _buffer = new unsigned char[capacity];
  15.         WritePos = 0;
  16.         _numBits = 0;
  17.     }
  18.  
  19.     ~BitWriter_BN()
  20.     {
  21.         delete _buffer;
  22.     }
  23.  
  24.     inline unsigned char * Buffer()
  25.     {
  26.         return _buffer;
  27.     }
  28.  
  29.     int32 NumBits()
  30.     {
  31.        return _numBits;
  32.     }
  33.  
  34.     int32 Length()
  35.     {
  36.         int32 pos7 = (WritePos & 7);
  37.         int32 bitsLeft = 8 - pos7;
  38.  
  39.         if (bitsLeft == 0)
  40.             return NumBits() / 8;
  41.         return (_numBits + bitsLeft)/8;
  42.     }
  43.  
  44.     template<typename T>
  45.     T Shift (T left, int32 right) {
  46.         return (T)0;
  47.     }
  48.  
  49.     template<>
  50.     uint32 Shift<uint32>(uint32 left, int32 right) {
  51.         return left >> right;
  52.     }
  53.  
  54.     template<>
  55.     int32 Shift<int32>(int32 left, int32 right) {
  56.         return static_cast<int32>(static_cast<uint32>(left) >> right);
  57.     }
  58.  
  59.     void AlignToNextByte()
  60.     {
  61.         WritePos = (WritePos + 7) & ~7;
  62.     }
  63.  
  64.     void WriteBytes( unsigned char * bytes, int32 length )
  65.     {
  66.         AlignToNextByte();
  67.         for (int32 i = 0; i < length; ++i)
  68.             WriteBits((int)bytes[i], 8);
  69.     }
  70.  
  71.     /*void WriteBytes( unsigned char * bytes, int32 length )
  72.     {
  73.         unsigned char* buf = new unsigned char[length];
  74.         //Array.Copy(bytes, 0, buf, 0, length);
  75.         memcpy(buf, bytes, length);
  76.         WriteBytes(buf);
  77.     }*/
  78.  
  79.     void WriteBuffer(unsigned char * bytes, int32 byteLength, int32 sizeBitCount)
  80.     {
  81.         WriteBits(byteLength, sizeBitCount);
  82.         WriteBytes(bytes, byteLength);
  83.     }
  84.  
  85.     void WriteInt32( int32 intVal )
  86.     {
  87.         WriteBits(intVal, 32);
  88.     }
  89.  
  90.     // passing 5730135 into function, which is equal to 'WoW' (WoW + chr(0) to make 4 chars)
  91.     template<typename T> void WriteBits(T valz, int numBits)
  92.     {
  93.         if (numBits > 0)
  94.         {
  95.             // becomes 43 (+ 32 to old result)
  96.             _numBits += numBits;
  97.             while (true)
  98.             {
  99.                 // WritePos = 11, pos7 becomes 3
  100.                 // second run, WritePos = 16, pos7 = 0
  101.                 // 3rd run, WritePos = 32, pos7 = 0
  102.                 // 4th run, WritePos = 40, pos7 = 0
  103.                 // 5th run, WritePos = 40, pos7 = 0
  104.                 int32 pos7 = (WritePos & 7);
  105.                 // unk = 5
  106.                 // second run, unk = 8
  107.                 // 3rd + 4th + 5th, unk = 8
  108.                 int32 unk = 8 - pos7;
  109.                 // lShift = 32
  110.                 // second run, 256
  111.                 // 3rd, 4th, 5th = 256
  112.                 uint16 lShift = (uint16) (1 << unk);
  113.                 int32 subNum;
  114.  
  115.                 if (unk < numBits)
  116.                     // = 5
  117.                     // second run, 8
  118.                     // 3rd = 8
  119.                     // 4th = 8
  120.                     subNum = unk;
  121.                 else
  122.                 {
  123.                     // 5th run
  124.                     // lshift = 8
  125.                     lShift = (uint16) (1 << numBits);
  126.                     // subnum = 3
  127.                     subNum = numBits;
  128.                 }
  129.                 // numbits = 27 now
  130.                 // second run, 19
  131.                 // 4th = 3
  132.                 // 5th = 0
  133.                 numBits -= subNum;
  134.                    
  135.                 // 65287
  136.                 // 65280 second run & 3rd & 4th
  137.                 // 65528 = 5th
  138.                 uint16 firstHalf = (uint16) (~((lShift - 1) << pos7));
  139.  
  140.                 // both 0
  141.                 // second run, both 10
  142.                 // 3rd run = 2797
  143.                 // 4th run = 60906
  144.                 // 5th run = 28503
  145.                 uint16 shifted = (uint16) Shift(valz, numBits);
  146.                 // 3rd run = 237
  147.                 // 4th run = 234
  148.                 // 5th run = 7
  149.                 uint16 secondHalf = (uint16) (((lShift - 1) & shifted) << pos7);
  150.  
  151.                 Buffer()[WritePos >> 3] = (unsigned char) (Buffer()[WritePos >> 3] & firstHalf | secondHalf); // was (Byte)
  152.  
  153.                 // writepos = 16
  154.                 // second run, writepos = 24
  155.                 // 3rd run, = 32
  156.                 // 4th run = 40
  157.                 // 5th run = 43
  158.                 WritePos += subNum;
  159.  
  160.                 // 5th run = 0
  161.                 if (numBits == 0)
  162.                     return;
  163.             }
  164.         }
  165.     }
  166.  
  167.     void WriteFourCC(string fourCC)
  168.     {
  169.         reverse(fourCC.begin(), fourCC.begin() + fourCC.length());
  170.         while (fourCC.length() < 4)
  171.             fourCC.push_back('\0');
  172.         WriteInt32(*(int32*)fourCC.c_str());
  173.     }
  174.  
  175.     void WriteHeader( int32 packetId, int32 channelId )
  176.     {
  177.         WriteBits(packetId, 6);
  178.         WriteBits(1, 1);
  179.         WriteBits(channelId, 4);
  180.     }
  181.  
  182.     string ToString()
  183.     {
  184.         string str = "";
  185.         int32 numBytes = _numBits/8;
  186.  
  187.         if (_numBits % 8 > 0)
  188.             numBytes++;
  189.  
  190.         for( int32 i = 0; i < numBytes; i++)
  191.         {
  192.             if (i > 0 && i % 16 == 0)
  193.                 str += "\r\n";
  194.  
  195.             //str += string.Format("{0:x2} ", Buffer[i]); // wut
  196.         }
  197.         return str;
  198.     }
  199.  
  200.     int32 WritePos;
  201.  
  202.     void HexadecimalToBinary(const string & hex, vector<unsigned char> & bin)
  203.     {
  204.         for(uint32 i = 0; i < hex.length() / 2; ++i)
  205.         {
  206.             stringstream strm;
  207.             strm << hex.substr(i * 2, 2);
  208.             unsigned int cur = 0;
  209.             strm >> std::hex >> cur;
  210.             bin.push_back((unsigned char)cur);
  211.         }
  212.     }
  213. };
  214.  
  215. #endif
Advertisement
Add Comment
Please, Sign In to add comment