RangeCoder.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* This file is part of SevenZipSharp.
  2. SevenZipSharp is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Lesser General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. SevenZipSharp is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with SevenZipSharp. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. using System;
  14. using System.IO;
  15. namespace SevenZip.Sdk.Compression.RangeCoder
  16. {
  17. internal class Encoder
  18. {
  19. public const uint kTopValue = (1 << 24);
  20. private byte _cache;
  21. private uint _cacheSize;
  22. public UInt64 Low;
  23. public uint Range;
  24. private long StartPosition;
  25. private Stream Stream;
  26. public void SetStream(Stream stream)
  27. {
  28. Stream = stream;
  29. }
  30. public void ReleaseStream()
  31. {
  32. Stream = null;
  33. }
  34. public void Init()
  35. {
  36. StartPosition = Stream.Position;
  37. Low = 0;
  38. Range = 0xFFFFFFFF;
  39. _cacheSize = 1;
  40. _cache = 0;
  41. }
  42. public void FlushData()
  43. {
  44. for (int i = 0; i < 5; i++)
  45. ShiftLow();
  46. }
  47. public void FlushStream()
  48. {
  49. Stream.Flush();
  50. }
  51. /*public void CloseStream()
  52. {
  53. Stream.Close();
  54. }*/
  55. /*public void Encode(uint start, uint size, uint total)
  56. {
  57. Low += start * (Range /= total);
  58. Range *= size;
  59. while (Range < kTopValue)
  60. {
  61. Range <<= 8;
  62. ShiftLow();
  63. }
  64. }*/
  65. public void ShiftLow()
  66. {
  67. if ((uint) Low < 0xFF000000 || (uint) (Low >> 32) == 1)
  68. {
  69. byte temp = _cache;
  70. do
  71. {
  72. Stream.WriteByte((byte) (temp + (Low >> 32)));
  73. temp = 0xFF;
  74. } while (--_cacheSize != 0);
  75. _cache = (byte) (((uint) Low) >> 24);
  76. }
  77. _cacheSize++;
  78. Low = ((uint) Low) << 8;
  79. }
  80. public void EncodeDirectBits(uint v, int numTotalBits)
  81. {
  82. for (int i = numTotalBits - 1; i >= 0; i--)
  83. {
  84. Range >>= 1;
  85. if (((v >> i) & 1) == 1)
  86. Low += Range;
  87. if (Range < kTopValue)
  88. {
  89. Range <<= 8;
  90. ShiftLow();
  91. }
  92. }
  93. }
  94. /*public void EncodeBit(uint size0, int numTotalBits, uint symbol)
  95. {
  96. uint newBound = (Range >> numTotalBits) * size0;
  97. if (symbol == 0)
  98. Range = newBound;
  99. else
  100. {
  101. Low += newBound;
  102. Range -= newBound;
  103. }
  104. while (Range < kTopValue)
  105. {
  106. Range <<= 8;
  107. ShiftLow();
  108. }
  109. }*/
  110. public long GetProcessedSizeAdd()
  111. {
  112. return _cacheSize +
  113. Stream.Position - StartPosition + 4;
  114. // (long)Stream.GetProcessedSize();
  115. }
  116. }
  117. internal class Decoder
  118. {
  119. public const uint kTopValue = (1 << 24);
  120. public uint Code;
  121. public uint Range;
  122. // public Buffer.InBuffer Stream = new Buffer.InBuffer(1 << 16);
  123. public Stream Stream;
  124. public void Init(Stream stream)
  125. {
  126. // Stream.Init(stream);
  127. Stream = stream;
  128. Code = 0;
  129. Range = 0xFFFFFFFF;
  130. for (int i = 0; i < 5; i++)
  131. Code = (Code << 8) | (byte) Stream.ReadByte();
  132. }
  133. public void ReleaseStream()
  134. {
  135. // Stream.ReleaseStream();
  136. Stream = null;
  137. }
  138. /*public void CloseStream()
  139. {
  140. Stream.Close();
  141. }*/
  142. /*public void Normalize()
  143. {
  144. while (Range < kTopValue)
  145. {
  146. Code = (Code << 8) | (byte)Stream.ReadByte();
  147. Range <<= 8;
  148. }
  149. }*/
  150. /*public void Normalize2()
  151. {
  152. if (Range < kTopValue)
  153. {
  154. Code = (Code << 8) | (byte)Stream.ReadByte();
  155. Range <<= 8;
  156. }
  157. }*/
  158. /*public uint GetThreshold(uint total)
  159. {
  160. return Code / (Range /= total);
  161. }*/
  162. /*public void Decode(uint start, uint size, uint total)
  163. {
  164. Code -= start * Range;
  165. Range *= size;
  166. Normalize();
  167. }*/
  168. public uint DecodeDirectBits(int numTotalBits)
  169. {
  170. uint range = Range;
  171. uint code = Code;
  172. uint result = 0;
  173. for (int i = numTotalBits; i > 0; i--)
  174. {
  175. range >>= 1;
  176. /*
  177. result <<= 1;
  178. if (code >= range)
  179. {
  180. code -= range;
  181. result |= 1;
  182. }
  183. */
  184. uint t = (code - range) >> 31;
  185. code -= range & (t - 1);
  186. result = (result << 1) | (1 - t);
  187. if (range < kTopValue)
  188. {
  189. code = (code << 8) | (byte) Stream.ReadByte();
  190. range <<= 8;
  191. }
  192. }
  193. Range = range;
  194. Code = code;
  195. return result;
  196. }
  197. /*public uint DecodeBit(uint size0, int numTotalBits)
  198. {
  199. uint newBound = (Range >> numTotalBits) * size0;
  200. uint symbol;
  201. if (Code < newBound)
  202. {
  203. symbol = 0;
  204. Range = newBound;
  205. }
  206. else
  207. {
  208. symbol = 1;
  209. Code -= newBound;
  210. Range -= newBound;
  211. }
  212. Normalize();
  213. return symbol;
  214. }*/
  215. // ulong GetProcessedSize() {return Stream.GetProcessedSize(); }
  216. }
  217. }