RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / scripts / squashfs / lzma / CS / 7zip / Compress / RangeCoder / RangeCoder.cs
blob4ced2477e49c4291884181bcd3221c580b0e8889
1 using System;
3 namespace SevenZip.Compression.RangeCoder
5 class Encoder
7 public const uint kTopValue = (1 << 24);
9 System.IO.Stream Stream;
11 public UInt64 Low;
12 public uint Range;
13 uint _cacheSize;
14 byte _cache;
16 long StartPosition;
18 public void SetStream(System.IO.Stream stream)
20 Stream = stream;
23 public void ReleaseStream()
25 Stream = null;
28 public void Init()
30 StartPosition = Stream.Position;
32 Low = 0;
33 Range = 0xFFFFFFFF;
34 _cacheSize = 1;
35 _cache = 0;
38 public void FlushData()
40 for (int i = 0; i < 5; i++)
41 ShiftLow();
44 public void FlushStream()
46 Stream.Flush();
49 public void CloseStream()
51 Stream.Close();
54 public void Encode(uint start, uint size, uint total)
56 Low += start * (Range /= total);
57 Range *= size;
58 while (Range < kTopValue)
60 Range <<= 8;
61 ShiftLow();
65 public void ShiftLow()
67 if ((uint)Low < (uint)0xFF000000 || (uint)(Low >> 32) == 1)
69 byte temp = _cache;
72 Stream.WriteByte((byte)(temp + (Low >> 32)));
73 temp = 0xFF;
75 while (--_cacheSize != 0);
76 _cache = (byte)(((uint)Low) >> 24);
78 _cacheSize++;
79 Low = ((uint)Low) << 8;
82 public void EncodeDirectBits(uint v, int numTotalBits)
84 for (int i = numTotalBits - 1; i >= 0; i--)
86 Range >>= 1;
87 if (((v >> i) & 1) == 1)
88 Low += Range;
89 if (Range < kTopValue)
91 Range <<= 8;
92 ShiftLow();
97 public void EncodeBit(uint size0, int numTotalBits, uint symbol)
99 uint newBound = (Range >> numTotalBits) * size0;
100 if (symbol == 0)
101 Range = newBound;
102 else
104 Low += newBound;
105 Range -= newBound;
107 while (Range < kTopValue)
109 Range <<= 8;
110 ShiftLow();
114 public long GetProcessedSizeAdd()
116 return _cacheSize +
117 Stream.Position - StartPosition + 4;
118 // (long)Stream.GetProcessedSize();
122 class Decoder
124 public const uint kTopValue = (1 << 24);
125 public uint Range;
126 public uint Code;
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);
133 Stream = stream;
135 Code = 0;
136 Range = 0xFFFFFFFF;
137 for (int i = 0; i < 5; i++)
138 Code = (Code << 8) | (byte)Stream.ReadByte();
141 public void ReleaseStream()
143 // Stream.ReleaseStream();
144 Stream = null;
147 public void CloseStream()
149 Stream.Close();
152 public void Normalize()
154 while (Range < kTopValue)
156 Code = (Code << 8) | (byte)Stream.ReadByte();
157 Range <<= 8;
161 public void Normalize2()
163 if (Range < kTopValue)
165 Code = (Code << 8) | (byte)Stream.ReadByte();
166 Range <<= 8;
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;
178 Range *= size;
179 Normalize();
182 public uint DecodeDirectBits(int numTotalBits)
184 uint range = Range;
185 uint code = Code;
186 uint result = 0;
187 for (int i = numTotalBits; i > 0; i--)
189 range >>= 1;
191 result <<= 1;
192 if (code >= range)
194 code -= range;
195 result |= 1;
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();
205 range <<= 8;
208 Range = range;
209 Code = code;
210 return result;
213 public uint DecodeBit(uint size0, int numTotalBits)
215 uint newBound = (Range >> numTotalBits) * size0;
216 uint symbol;
217 if (Code < newBound)
219 symbol = 0;
220 Range = newBound;
222 else
224 symbol = 1;
225 Code -= newBound;
226 Range -= newBound;
228 Normalize();
229 return symbol;
232 // ulong GetProcessedSize() {return Stream.GetProcessedSize(); }