getbits.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef _RAR_GETBITS_
  2. #define _RAR_GETBITS_
  3. class BitInput
  4. {
  5. public:
  6. enum BufferSize {MAX_SIZE=0x8000}; // Size of input buffer.
  7. protected:
  8. int InAddr; // Curent byte position in the buffer.
  9. int InBit; // Current bit position in the current byte.
  10. public:
  11. BitInput();
  12. ~BitInput();
  13. byte *InBuf; // Dynamically allocated input buffer.
  14. void InitBitInput()
  15. {
  16. InAddr=InBit=0;
  17. }
  18. // Move forward by 'Bits' bits.
  19. void addbits(uint Bits)
  20. {
  21. Bits+=InBit;
  22. InAddr+=Bits>>3;
  23. InBit=Bits&7;
  24. }
  25. // Return 16 bits from current position in the buffer.
  26. // Bit at (InAddr,InBit) has the highest position in returning data.
  27. uint getbits()
  28. {
  29. uint BitField=(uint)InBuf[InAddr] << 16;
  30. BitField|=(uint)InBuf[InAddr+1] << 8;
  31. BitField|=(uint)InBuf[InAddr+2];
  32. BitField >>= (8-InBit);
  33. return(BitField & 0xffff);
  34. }
  35. void faddbits(uint Bits);
  36. uint fgetbits();
  37. // Check if buffer has enough space for IncPtr bytes. Returns 'true'
  38. // if buffer will be overflown.
  39. bool Overflow(uint IncPtr)
  40. {
  41. return(InAddr+IncPtr>=MAX_SIZE);
  42. }
  43. };
  44. #endif