1
0

getbits.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. int InAddr; // Curent byte position in the buffer.
  8. int InBit; // Current bit position in the current byte.
  9. bool ExternalBuffer;
  10. public:
  11. BitInput(bool AllocBuffer);
  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. // Return 32 bits from current position in the buffer.
  36. // Bit at (InAddr,InBit) has the highest position in returning data.
  37. uint getbits32()
  38. {
  39. uint BitField=(uint)InBuf[InAddr] << 24;
  40. BitField|=(uint)InBuf[InAddr+1] << 16;
  41. BitField|=(uint)InBuf[InAddr+2] << 8;
  42. BitField|=(uint)InBuf[InAddr+3];
  43. BitField <<= InBit;
  44. BitField|=(uint)InBuf[InAddr+4] >> (8-InBit);
  45. return BitField & 0xffffffff;
  46. }
  47. void faddbits(uint Bits);
  48. uint fgetbits();
  49. // Check if buffer has enough space for IncPtr bytes. Returns 'true'
  50. // if buffer will be overflown.
  51. bool Overflow(uint IncPtr)
  52. {
  53. return InAddr+IncPtr>=MAX_SIZE;
  54. }
  55. void SetExternalBuffer(byte *Buf);
  56. };
  57. #endif