3 namespace SevenZip
.Compression
.RangeCoder
7 public const uint kTopValue
= (1 << 24);
9 System
.IO
.Stream Stream
;
18 public void SetStream(System
.IO
.Stream stream
)
23 public void ReleaseStream()
30 StartPosition
= Stream
.Position
;
38 public void FlushData()
40 for (int i
= 0; i
< 5; i
++)
44 public void FlushStream()
49 public void CloseStream()
54 public void Encode(uint start
, uint size
, uint total
)
56 Low
+= start
* (Range
/= total
);
58 while (Range
< kTopValue
)
65 public void ShiftLow()
67 if ((uint)Low
< (uint)0xFF000000 || (uint)(Low
>> 32) == 1)
72 Stream
.WriteByte((byte)(temp
+ (Low
>> 32)));
75 while (--_cacheSize
!= 0);
76 _cache
= (byte)(((uint)Low
) >> 24);
79 Low
= ((uint)Low
) << 8;
82 public void EncodeDirectBits(uint v
, int numTotalBits
)
84 for (int i
= numTotalBits
- 1; i
>= 0; i
--)
87 if (((v
>> i
) & 1) == 1)
89 if (Range
< kTopValue
)
97 public void EncodeBit(uint size0
, int numTotalBits
, uint symbol
)
99 uint newBound
= (Range
>> numTotalBits
) * size0
;
107 while (Range
< kTopValue
)
114 public long GetProcessedSizeAdd()
117 Stream
.Position
- StartPosition
+ 4;
118 // (long)Stream.GetProcessedSize();
124 public const uint kTopValue
= (1 << 24);
127 // public Buffer.InBuffer Stream = new Buffer.InBuffer(1 << 16);
128 public System
.IO
.Stream Stream
;
130 public void Init(System
.IO
.Stream stream
)
132 // Stream.Init(stream);
137 for (int i
= 0; i
< 5; i
++)
138 Code
= (Code
<< 8) | (byte)Stream
.ReadByte();
141 public void ReleaseStream()
143 // Stream.ReleaseStream();
147 public void CloseStream()
152 public void Normalize()
154 while (Range
< kTopValue
)
156 Code
= (Code
<< 8) | (byte)Stream
.ReadByte();
161 public void Normalize2()
163 if (Range
< kTopValue
)
165 Code
= (Code
<< 8) | (byte)Stream
.ReadByte();
170 public uint GetThreshold(uint total
)
172 return Code
/ (Range
/= total
);
175 public void Decode(uint start
, uint size
, uint total
)
177 Code
-= start
* Range
;
182 public uint DecodeDirectBits(int numTotalBits
)
187 for (int i
= numTotalBits
; i
> 0; i
--)
198 uint t
= (code
- range
) >> 31;
199 code
-= range
& (t
- 1);
200 result
= (result
<< 1) | (1 - t
);
202 if (range
< kTopValue
)
204 code
= (code
<< 8) | (byte)Stream
.ReadByte();
213 public uint DecodeBit(uint size0
, int numTotalBits
)
215 uint newBound
= (Range
>> numTotalBits
) * size0
;
232 // ulong GetProcessedSize() {return Stream.GetProcessedSize(); }