RangeCoder.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using SharpCompress.Compressors.Rar;
  5. namespace SharpCompress.Compressors.PPMd.H
  6. {
  7. internal class RangeCoder
  8. {
  9. internal const int TOP = 1 << 24;
  10. internal const int BOT = 1 << 15;
  11. internal const long UINT_MASK = 0xFFFFffffL;
  12. // uint low, code, range;
  13. private long _low, _code, _range;
  14. private readonly IRarUnpack _unpackRead;
  15. private readonly Stream _stream;
  16. internal RangeCoder(IRarUnpack unpackRead)
  17. {
  18. _unpackRead = unpackRead;
  19. Init();
  20. }
  21. internal RangeCoder(Stream stream)
  22. {
  23. _stream = stream;
  24. Init();
  25. }
  26. private void Init()
  27. {
  28. SubRange = new SubRange();
  29. _low = _code = 0L;
  30. _range = 0xFFFFffffL;
  31. for (int i = 0; i < 4; i++)
  32. {
  33. _code = ((_code << 8) | Char) & UINT_MASK;
  34. }
  35. }
  36. internal int CurrentCount
  37. {
  38. get
  39. {
  40. _range = (_range / SubRange.Scale) & UINT_MASK;
  41. return (int)((_code - _low) / (_range));
  42. }
  43. }
  44. private long Char
  45. {
  46. get
  47. {
  48. if (_unpackRead != null)
  49. {
  50. return (_unpackRead.Char);
  51. }
  52. if (_stream != null)
  53. {
  54. return _stream.ReadByte();
  55. }
  56. return -1;
  57. }
  58. }
  59. internal SubRange SubRange { get; private set; }
  60. internal long GetCurrentShiftCount(int shift)
  61. {
  62. _range = Utility.URShift(_range, shift);
  63. return ((_code - _low) / (_range)) & UINT_MASK;
  64. }
  65. internal void Decode()
  66. {
  67. _low = (_low + (_range * SubRange.LowCount)) & UINT_MASK;
  68. _range = (_range * (SubRange.HighCount - SubRange.LowCount)) & UINT_MASK;
  69. }
  70. internal void AriDecNormalize()
  71. {
  72. // while ((low ^ (low + range)) < TOP || range < BOT && ((range = -low & (BOT - 1)) != 0 ? true : true))
  73. // {
  74. // code = ((code << 8) | unpackRead.getChar()&0xff)&uintMask;
  75. // range = (range << 8)&uintMask;
  76. // low = (low << 8)&uintMask;
  77. // }
  78. // Rewrote for clarity
  79. bool c2 = false;
  80. while ((_low ^ (_low + _range)) < TOP || (c2 = _range < BOT))
  81. {
  82. if (c2)
  83. {
  84. _range = (-_low & (BOT - 1)) & UINT_MASK;
  85. c2 = false;
  86. }
  87. _code = ((_code << 8) | Char) & UINT_MASK;
  88. _range = (_range << 8) & UINT_MASK;
  89. _low = (_low << 8) & UINT_MASK;
  90. }
  91. }
  92. // Debug
  93. public override String ToString()
  94. {
  95. StringBuilder buffer = new StringBuilder();
  96. buffer.Append("RangeCoder[");
  97. buffer.Append("\n low=");
  98. buffer.Append(_low);
  99. buffer.Append("\n code=");
  100. buffer.Append(_code);
  101. buffer.Append("\n range=");
  102. buffer.Append(_range);
  103. buffer.Append("\n subrange=");
  104. buffer.Append(SubRange);
  105. buffer.Append("]");
  106. return buffer.ToString();
  107. }
  108. }
  109. internal class SubRange
  110. {
  111. // uint LowCount, HighCount, scale;
  112. private long _lowCount, _highCount, _scale;
  113. internal void IncScale(int dScale)
  114. {
  115. Scale = Scale + dScale;
  116. }
  117. internal long HighCount { get => _highCount; set => _highCount = value & RangeCoder.UINT_MASK; }
  118. internal long LowCount { get => _lowCount & RangeCoder.UINT_MASK; set => _lowCount = value & RangeCoder.UINT_MASK; }
  119. internal long Scale { get => _scale; set => _scale = value & RangeCoder.UINT_MASK; }
  120. // Debug
  121. public override String ToString()
  122. {
  123. StringBuilder buffer = new StringBuilder();
  124. buffer.Append("SubRange[");
  125. buffer.Append("\n lowCount=");
  126. buffer.Append(_lowCount);
  127. buffer.Append("\n highCount=");
  128. buffer.Append(_highCount);
  129. buffer.Append("\n scale=");
  130. buffer.Append(_scale);
  131. buffer.Append("]");
  132. return buffer.ToString();
  133. }
  134. }
  135. }