BitInput.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. namespace SharpCompress.Compressors.Rar.VM
  2. {
  3. internal class BitInput
  4. {
  5. /// <summary> the max size of the input</summary>
  6. internal const int MAX_SIZE = 0x8000;
  7. public int inAddr;
  8. public int inBit;
  9. // TODO: rename var
  10. public int InAddr { get { return inAddr; } set { inAddr = value; } }
  11. public int InBit { get { return inBit; } set { inBit = value; } }
  12. public bool ExternalBuffer;
  13. /// <summary> </summary>
  14. internal BitInput()
  15. {
  16. InBuf = new byte[MAX_SIZE];
  17. }
  18. internal byte[] InBuf { get; }
  19. internal void InitBitInput()
  20. {
  21. inAddr = 0;
  22. inBit = 0;
  23. }
  24. internal void faddbits(uint bits) {
  25. // TODO uint
  26. AddBits((int)bits);
  27. }
  28. /// <summary>
  29. /// also named faddbits
  30. /// </summary>
  31. /// <param name="bits"></param>
  32. internal void AddBits(int bits)
  33. {
  34. bits += inBit;
  35. inAddr += (bits >> 3);
  36. inBit = bits & 7;
  37. }
  38. internal uint fgetbits() {
  39. // TODO uint
  40. return (uint)GetBits();
  41. }
  42. internal uint getbits() {
  43. // TODO uint
  44. return (uint)GetBits();
  45. }
  46. /// <summary>
  47. /// (also named fgetbits)
  48. /// </summary>
  49. /// <returns>
  50. /// the bits (unsigned short)
  51. /// </returns>
  52. internal int GetBits()
  53. {
  54. // int BitField=0;
  55. // BitField|=(int)(inBuf[inAddr] << 16)&0xFF0000;
  56. // BitField|=(int)(inBuf[inAddr+1] << 8)&0xff00;
  57. // BitField|=(int)(inBuf[inAddr+2])&0xFF;
  58. // BitField >>>= (8-inBit);
  59. // return (BitField & 0xffff);
  60. return ((Utility.URShift((((InBuf[inAddr] & 0xff) << 16)
  61. + ((InBuf[inAddr + 1] & 0xff) << 8)
  62. + ((InBuf[inAddr + 2] & 0xff))), (8 - inBit))) & 0xffff);
  63. }
  64. /// <summary> Indicates an Overfow</summary>
  65. /// <param name="IncPtr">how many bytes to inc
  66. /// </param>
  67. /// <returns> true if an Oververflow would occur
  68. /// </returns>
  69. internal bool Overflow(int IncPtr)
  70. {
  71. return (inAddr + IncPtr >= MAX_SIZE);
  72. }
  73. }
  74. }