BitStream.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. namespace cyh.game
  3. {
  4. public unsafe class BitStream
  5. {
  6. byte[] array;
  7. ulong length;
  8. ulong read_position;
  9. ulong write_position;
  10. public ulong Length { get { return this.length; } }
  11. public ulong Count { get { return (this.write_position - this.read_position) >> 3; } }
  12. public BitStream()
  13. {
  14. this.length = 128;
  15. this.array = new byte[this.length];
  16. }
  17. public BitStream(byte[] data)
  18. {
  19. this.array = data;
  20. this.length = (ulong)data.Length;
  21. this.write_position = this.length << 3;
  22. }
  23. void resize()
  24. {
  25. Array.Resize<byte>(ref this.array, (int)this.length << 1);
  26. }
  27. public void Write(ulong value, int bitCount)
  28. {
  29. if (bitCount > 64)
  30. bitCount = 64;
  31. if (this.write_position >= this.length - 8)
  32. this.resize();
  33. byte offset = (byte)(this.write_position & 7);
  34. ulong pos = (this.write_position >> 3);
  35. if (offset != 0)
  36. {
  37. this.array[pos] |= (byte)(value << offset);
  38. offset = (byte)(8 - offset);
  39. value >>= offset;
  40. fixed (byte* ptr = &this.array[pos + 1])
  41. {
  42. *(ulong*)ptr = value;
  43. }
  44. }
  45. else
  46. {
  47. fixed (byte* ptr = &this.array[pos])
  48. {
  49. *(ulong*)ptr = value;
  50. }
  51. }
  52. this.write_position += (ulong)bitCount;
  53. }
  54. public ulong Read(int bitCount)
  55. {
  56. if (bitCount > 64)
  57. bitCount = 64;
  58. ulong bitsLeft = this.write_position - this.read_position;
  59. if (bitsLeft >= (uint)bitCount)
  60. {
  61. ulong value;
  62. byte offset = (byte)(this.read_position & 7);
  63. ulong pos = (this.read_position >> 3);
  64. if (offset != 0)
  65. {
  66. fixed (byte* ptr = &this.array[pos])
  67. {
  68. value = *(ulong*)ptr;
  69. }
  70. value = value >> offset;
  71. this.read_position += (uint)bitCount;
  72. offset = (byte)(64 - offset);
  73. if (bitCount > offset)
  74. {
  75. pos = (this.read_position >> 3);
  76. fixed (byte* ptr = &this.array[pos])
  77. {
  78. value |= ((ulong)*ptr) << offset;
  79. }
  80. }
  81. }
  82. else
  83. {
  84. fixed (byte* ptr = &this.array[pos])
  85. {
  86. value = *(ulong*)ptr;
  87. }
  88. this.read_position += (uint)bitCount;
  89. }
  90. value &= 0xFFFFFFFFFFFFFFFF >> (64 - bitCount);
  91. return value;
  92. }
  93. return 0;
  94. }
  95. }
  96. }