BitInput.getbits_hpp.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. namespace SharpCompress.Compressors.Rar.UnpackV2017
  2. {
  3. internal partial class BitInput
  4. {
  5. public const int MAX_SIZE=0x8000; // Size of input buffer.
  6. public int InAddr; // Curent byte position in the buffer.
  7. public int InBit; // Current bit position in the current byte.
  8. public bool ExternalBuffer;
  9. //BitInput(bool AllocBuffer);
  10. //~BitInput();
  11. public byte[] InBuf; // Dynamically allocated input buffer.
  12. public
  13. void InitBitInput()
  14. {
  15. InAddr=InBit=0;
  16. }
  17. // Move forward by 'Bits' bits.
  18. public void addbits(uint _Bits)
  19. {
  20. var Bits = checked((int)_Bits);
  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. public 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. public 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. private bool Overflow(uint IncPtr)
  52. {
  53. return InAddr+IncPtr>=MAX_SIZE;
  54. }
  55. //void SetExternalBuffer(byte *Buf);
  56. }
  57. }