7z-iterator 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ BSD License
2
+
3
+ lzma-purejs - Pure JavaScript LZMA de/compression
4
+ https://github.com/cscott/lzma-purejs
5
+
6
+ Copyright (c) Gary Linscott, Juan Mellado, C. Scott Ananian
7
+
8
+ This vendored copy includes modifications to support LZMA2 solid mode
9
+ (setSolid method for state preservation across chunks).
10
+
11
+ Original package: https://www.npmjs.com/package/lzma-purejs
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+ var LZ = require('./lib/LZ');
3
+ var LZMA = require('./lib/LZMA');
4
+ var RangeCoder = require('./lib/RangeCoder');
5
+ var Stream = require('./lib/Stream');
6
+ var Util = require('./lib/Util');
7
+
8
+ module.exports = {
9
+ version: "0.9.0",
10
+ LZ: LZ,
11
+ LZMA: LZMA,
12
+ RangeCoder: RangeCoder,
13
+ Stream: Stream,
14
+ Util: Util,
15
+ compress: Util.compress,
16
+ compressFile: Util.compressFile,
17
+ decompress: Util.decompress,
18
+ decompressFile: Util.decompressFile
19
+ };
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+ var makeBuffer = require('../makeBuffer');
3
+
4
+ var OutWindow = function(){
5
+ this._windowSize = 0;
6
+ };
7
+
8
+ OutWindow.prototype.create = function(windowSize){
9
+ if ((!this._buffer) || (this._windowSize !== windowSize)){
10
+ this._buffer = makeBuffer(windowSize);
11
+ }
12
+ this._windowSize = windowSize;
13
+ this._pos = 0;
14
+ this._streamPos = 0;
15
+ };
16
+
17
+ OutWindow.prototype.flush = function(){
18
+ var size = this._pos - this._streamPos;
19
+ if (size !== 0){
20
+ while(size--){
21
+ this._stream.writeByte(this._buffer[this._streamPos++]);
22
+ }
23
+ if (this._pos >= this._windowSize){
24
+ this._pos = 0;
25
+ }
26
+ this._streamPos = this._pos;
27
+ }
28
+ };
29
+
30
+ OutWindow.prototype.releaseStream = function(){
31
+ this.flush();
32
+ this._stream = null;
33
+ };
34
+
35
+ OutWindow.prototype.setStream = function(stream){
36
+ this.releaseStream();
37
+ this._stream = stream;
38
+ };
39
+
40
+ OutWindow.prototype.init = function(solid){
41
+ if (!solid){
42
+ this._streamPos = 0;
43
+ this._pos = 0;
44
+ }
45
+ };
46
+
47
+ OutWindow.prototype.copyBlock = function(distance, len){
48
+ var pos = this._pos - distance - 1;
49
+ if (pos < 0){
50
+ pos += this._windowSize;
51
+ }
52
+ while(len--){
53
+ if (pos >= this._windowSize){
54
+ pos = 0;
55
+ }
56
+ this._buffer[this._pos++] = this._buffer[pos++];
57
+ if (this._pos >= this._windowSize){
58
+ this.flush();
59
+ }
60
+ }
61
+ };
62
+
63
+ OutWindow.prototype.putByte = function(b){
64
+ this._buffer[this._pos++] = b;
65
+ if (this._pos >= this._windowSize){
66
+ this.flush();
67
+ }
68
+ };
69
+
70
+ OutWindow.prototype.getByte = function(distance){
71
+ var pos = this._pos - distance - 1;
72
+ if (pos < 0){
73
+ pos += this._windowSize;
74
+ }
75
+ return this._buffer[pos];
76
+ };
77
+
78
+ module.exports = OutWindow;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+ var OutWindow = require('./LZ/OutWindow');
3
+
4
+ module.exports = {
5
+ OutWindow: OutWindow
6
+ };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ var Base = {};
4
+ Base.kNumRepDistances = 4;
5
+ Base.kNumStates = 12;
6
+
7
+ Base.stateInit = function() { return 0; };
8
+ Base.stateUpdateChar = function(index) {
9
+ if (index < 4) return 0;
10
+ if (index < 10) return index - 3;
11
+ return index - 6;
12
+ };
13
+ Base.stateUpdateMatch = function(index) { return (index < 7 ? 7 : 10); };
14
+ Base.stateUpdateRep = function(index) { return (index < 7 ? 8 : 11); };
15
+ Base.stateUpdateShortRep = function(index) { return (index < 7 ? 9 : 11); };
16
+ Base.stateIsCharState = function(index) { return index < 7; };
17
+
18
+ Base.kNumPosSlotBits = 6;
19
+ Base.kDicLogSizeMin = 0;
20
+ Base.kNumLenToPosStatesBits = 2;
21
+ Base.kNumLenToPosStates = 1 << Base.kNumLenToPosStatesBits;
22
+ Base.kMatchMinLen = 2;
23
+ Base.getLenToPosState = function(len) {
24
+ len -= Base.kMatchMinLen;
25
+ return len < Base.kNumLenToPosStates ? len : (Base.kNumLenToPosStates - 1);
26
+ };
27
+ Base.kNumAlignBits = 4;
28
+ Base.kAlignTableSize = 1 << Base.kNumAlignBits;
29
+ Base.kAlignMask = (Base.kAlignTableSize - 1);
30
+ Base.kStartPosModelIndex = 4;
31
+ Base.kEndPosModelIndex = 14;
32
+ Base.kNumPosModels = Base.kEndPosModelIndex - Base.kStartPosModelIndex;
33
+ Base.kNumFullDistances = 1 << (Base.kEndPosModelIndex / 2);
34
+ Base.kNumLitPosStatesBitsEncodingMax = 4;
35
+ Base.kNumLitContextBitsMax = 8;
36
+ Base.kNumPosStatesBitsMax = 4;
37
+ Base.kNumPosStatesMax = (1 << Base.kNumPosStatesBitsMax);
38
+ Base.kNumPosStatesBitsEncodingMax = 4;
39
+ Base.kNumPosStatesEncodingMax = (1 << Base.kNumPosStatesBitsEncodingMax);
40
+ Base.kNumLowLenBits = 3;
41
+ Base.kNumMidLenBits = 3;
42
+ Base.kNumHighLenBits = 8;
43
+ Base.kNumLowLenSymbols = 1 << Base.kNumLowLenBits;
44
+ Base.kNumMidLenSymbols = 1 << Base.kNumMidLenBits;
45
+ Base.kNumLenSymbols = Base.kNumLowLenSymbols + Base.kNumMidLenSymbols + (1 << Base.kNumHighLenBits);
46
+ Base.kMatchMaxLen = Base.kMatchMinLen + Base.kNumLenSymbols - 1;
47
+
48
+ module.exports = Base;
@@ -0,0 +1,296 @@
1
+ 'use strict';
2
+ var Base = require('./Base');
3
+ var LZ = require('../LZ');
4
+ var RangeCoder = require('../RangeCoder');
5
+
6
+ var initBitModels = RangeCoder.Encoder.initBitModels;
7
+
8
+ var LenDecoder = function(){
9
+ this._choice = initBitModels(null, 2);
10
+ this._lowCoder = [];
11
+ this._midCoder = [];
12
+ this._highCoder = new RangeCoder.BitTreeDecoder(8);
13
+ this._numPosStates = 0;
14
+ };
15
+
16
+ LenDecoder.prototype.create = function(numPosStates){
17
+ for (; this._numPosStates < numPosStates; ++this._numPosStates){
18
+ this._lowCoder[this._numPosStates] = new RangeCoder.BitTreeDecoder(3);
19
+ this._midCoder[this._numPosStates] = new RangeCoder.BitTreeDecoder(3);
20
+ }
21
+ };
22
+
23
+ LenDecoder.prototype.init = function(){
24
+ initBitModels(this._choice);
25
+ for (var i = this._numPosStates - 1; i >= 0; i--){
26
+ this._lowCoder[i].init();
27
+ this._midCoder[i].init();
28
+ }
29
+ this._highCoder.init();
30
+ };
31
+
32
+ LenDecoder.prototype.decode = function(rangeDecoder, posState){
33
+ if (rangeDecoder.decodeBit(this._choice, 0) === 0){
34
+ return this._lowCoder[posState].decode(rangeDecoder);
35
+ }
36
+ if (rangeDecoder.decodeBit(this._choice, 1) === 0){
37
+ return 8 + this._midCoder[posState].decode(rangeDecoder);
38
+ }
39
+ return 16 + this._highCoder.decode(rangeDecoder);
40
+ };
41
+
42
+ var LiteralDecoder = function(){};
43
+
44
+ LiteralDecoder.Decoder2 = function(){
45
+ this._decoders = initBitModels(null, 0x300);
46
+ };
47
+
48
+ LiteralDecoder.Decoder2.prototype.init = function(){
49
+ initBitModels(this._decoders);
50
+ };
51
+
52
+ LiteralDecoder.Decoder2.prototype.decodeNormal = function(rangeDecoder){
53
+ var symbol = 1;
54
+ do {
55
+ symbol = (symbol << 1) | rangeDecoder.decodeBit(this._decoders, symbol);
56
+ } while(symbol < 0x100);
57
+ return symbol & 0xff;
58
+ };
59
+
60
+ LiteralDecoder.Decoder2.prototype.decodeWithMatchByte = function(rangeDecoder, matchByte){
61
+ var symbol = 1;
62
+ do {
63
+ var matchBit = (matchByte >> 7) & 1;
64
+ matchByte <<= 1;
65
+ var bit = rangeDecoder.decodeBit(this._decoders, ((1 + matchBit) << 8) + symbol);
66
+ symbol = (symbol << 1) | bit;
67
+ if (matchBit !== bit){
68
+ while(symbol < 0x100){
69
+ symbol = (symbol << 1) | rangeDecoder.decodeBit(this._decoders, symbol);
70
+ }
71
+ break;
72
+ }
73
+ } while(symbol < 0x100);
74
+ return symbol & 0xff;
75
+ };
76
+
77
+ LiteralDecoder.prototype.create = function(numPosBits, numPrevBits){
78
+ if (this._coders && this._numPrevBits === numPrevBits && this._numPosBits === numPosBits) return;
79
+ this._numPosBits = numPosBits;
80
+ this._posMask = (1 << numPosBits) - 1;
81
+ this._numPrevBits = numPrevBits;
82
+ this._coders = [];
83
+ var count = 1 << (this._numPrevBits + this._numPosBits);
84
+ for (var i = 0; i < count; i++){
85
+ this._coders[i] = new LiteralDecoder.Decoder2();
86
+ }
87
+ };
88
+
89
+ LiteralDecoder.prototype.init = function(){
90
+ var count = 1 << (this._numPrevBits + this._numPosBits);
91
+ for (var i = 0; i < count; i++){
92
+ this._coders[i].init();
93
+ }
94
+ };
95
+
96
+ LiteralDecoder.prototype.getDecoder = function(pos, prevByte){
97
+ return this._coders[((pos & this._posMask) << this._numPrevBits) +
98
+ ((prevByte & 0xff) >>> (8 - this._numPrevBits))];
99
+ };
100
+
101
+ var Decoder = function(){
102
+ this._outWindow = new LZ.OutWindow();
103
+ this._rangeDecoder = new RangeCoder.Decoder();
104
+ this._isMatchDecoders = initBitModels(null, Base.kNumStates << Base.kNumPosStatesBitsMax);
105
+ this._isRepDecoders = initBitModels(null, Base.kNumStates);
106
+ this._isRepG0Decoders = initBitModels(null, Base.kNumStates);
107
+ this._isRepG1Decoders = initBitModels(null, Base.kNumStates);
108
+ this._isRepG2Decoders = initBitModels(null, Base.kNumStates);
109
+ this._isRep0LongDecoders = initBitModels(null, Base.kNumStates << Base.kNumPosStatesBitsMax);
110
+ this._posSlotDecoder = [];
111
+ this._posDecoders = initBitModels(null, Base.kNumFullDistances - Base.kEndPosModelIndex);
112
+ this._posAlignDecoder = new RangeCoder.BitTreeDecoder(Base.kNumAlignBits);
113
+ this._lenDecoder = new LenDecoder();
114
+ this._repLenDecoder = new LenDecoder();
115
+ this._literalDecoder = new LiteralDecoder();
116
+ this._dictionarySize = -1;
117
+ this._dictionarySizeCheck = -1;
118
+ this._posStateMask = 0;
119
+
120
+ // LZMA2 state preservation
121
+ this._state = 0;
122
+ this._rep0 = 0;
123
+ this._rep1 = 0;
124
+ this._rep2 = 0;
125
+ this._rep3 = 0;
126
+ this._prevByte = 0;
127
+ this._nowPos64 = 0;
128
+ this._solid = false;
129
+
130
+ for (var i = 0; i < Base.kNumLenToPosStates; i++){
131
+ this._posSlotDecoder[i] = new RangeCoder.BitTreeDecoder(Base.kNumPosSlotBits);
132
+ }
133
+ };
134
+
135
+ Decoder.prototype.setSolid = function(solid) {
136
+ this._solid = solid;
137
+ };
138
+
139
+ Decoder.prototype.setDictionarySize = function(dictionarySize){
140
+ if (dictionarySize < 0) return false;
141
+ if (this._dictionarySize !== dictionarySize){
142
+ this._dictionarySize = dictionarySize;
143
+ this._dictionarySizeCheck = Math.max(this._dictionarySize, 1);
144
+ this._outWindow.create(Math.max(this._dictionarySizeCheck, (1 << 12)));
145
+ }
146
+ return true;
147
+ };
148
+
149
+ Decoder.prototype.setLcLpPb = function(lc, lp, pb){
150
+ if (lc > Base.kNumLitContextBitsMax || lp > 4 || pb > Base.kNumPosStatesBitsMax) return false;
151
+ var numPosStates = 1 << pb;
152
+ this._literalDecoder.create(lp, lc);
153
+ this._lenDecoder.create(numPosStates);
154
+ this._repLenDecoder.create(numPosStates);
155
+ this._posStateMask = numPosStates - 1;
156
+ return true;
157
+ };
158
+
159
+ Decoder.prototype.init = function(){
160
+ this._outWindow.init(false);
161
+ initBitModels(this._isMatchDecoders);
162
+ initBitModels(this._isRepDecoders);
163
+ initBitModels(this._isRepG0Decoders);
164
+ initBitModels(this._isRepG1Decoders);
165
+ initBitModels(this._isRepG2Decoders);
166
+ initBitModels(this._isRep0LongDecoders);
167
+ initBitModels(this._posDecoders);
168
+ this._literalDecoder.init();
169
+ for (var i = Base.kNumLenToPosStates - 1; i >= 0; i--){
170
+ this._posSlotDecoder[i].init();
171
+ }
172
+ this._lenDecoder.init();
173
+ this._repLenDecoder.init();
174
+ this._posAlignDecoder.init();
175
+ this._rangeDecoder.init();
176
+ };
177
+
178
+ Decoder.prototype.code = function(inStream, outStream, outSize){
179
+ var chunkPos = 0, posState, decoder2, len, distance, posSlot, numDirectBits;
180
+ this._rangeDecoder.setStream(inStream);
181
+ this._outWindow.setStream(outStream);
182
+
183
+ if (!this._solid) {
184
+ this.init();
185
+ this._state = Base.stateInit();
186
+ this._rep0 = 0;
187
+ this._rep1 = 0;
188
+ this._rep2 = 0;
189
+ this._rep3 = 0;
190
+ this._prevByte = 0;
191
+ this._nowPos64 = 0;
192
+ } else {
193
+ this._outWindow.init(true);
194
+ this._rangeDecoder.init();
195
+ }
196
+ var cumPos = this._nowPos64;
197
+
198
+ while(outSize < 0 || chunkPos < outSize){
199
+ posState = cumPos & this._posStateMask;
200
+ if (this._rangeDecoder.decodeBit(this._isMatchDecoders, (this._state << Base.kNumPosStatesBitsMax) + posState) === 0){
201
+ decoder2 = this._literalDecoder.getDecoder(cumPos, this._prevByte);
202
+ if (!Base.stateIsCharState(this._state)){
203
+ this._prevByte = decoder2.decodeWithMatchByte(this._rangeDecoder, this._outWindow.getByte(this._rep0));
204
+ } else {
205
+ this._prevByte = decoder2.decodeNormal(this._rangeDecoder);
206
+ }
207
+ this._outWindow.putByte(this._prevByte);
208
+ this._state = Base.stateUpdateChar(this._state);
209
+ chunkPos++; cumPos++;
210
+ } else {
211
+ if (this._rangeDecoder.decodeBit(this._isRepDecoders, this._state) === 1){
212
+ len = 0;
213
+ if (this._rangeDecoder.decodeBit(this._isRepG0Decoders, this._state) === 0){
214
+ if (this._rangeDecoder.decodeBit(this._isRep0LongDecoders, (this._state << Base.kNumPosStatesBitsMax) + posState) === 0){
215
+ this._state = Base.stateUpdateShortRep(this._state);
216
+ len = 1;
217
+ }
218
+ } else {
219
+ if (this._rangeDecoder.decodeBit(this._isRepG1Decoders, this._state) === 0){
220
+ distance = this._rep1;
221
+ } else {
222
+ if (this._rangeDecoder.decodeBit(this._isRepG2Decoders, this._state) === 0){
223
+ distance = this._rep2;
224
+ } else {
225
+ distance = this._rep3;
226
+ this._rep3 = this._rep2;
227
+ }
228
+ this._rep2 = this._rep1;
229
+ }
230
+ this._rep1 = this._rep0;
231
+ this._rep0 = distance;
232
+ }
233
+ if (len === 0){
234
+ len = Base.kMatchMinLen + this._repLenDecoder.decode(this._rangeDecoder, posState);
235
+ this._state = Base.stateUpdateRep(this._state);
236
+ }
237
+ } else {
238
+ this._rep3 = this._rep2;
239
+ this._rep2 = this._rep1;
240
+ this._rep1 = this._rep0;
241
+ len = Base.kMatchMinLen + this._lenDecoder.decode(this._rangeDecoder, posState);
242
+ this._state = Base.stateUpdateMatch(this._state);
243
+ posSlot = this._posSlotDecoder[Base.getLenToPosState(len)].decode(this._rangeDecoder);
244
+ if (posSlot >= Base.kStartPosModelIndex){
245
+ numDirectBits = (posSlot >> 1) - 1;
246
+ this._rep0 = (2 | (posSlot & 1)) << numDirectBits;
247
+ if (posSlot < Base.kEndPosModelIndex){
248
+ this._rep0 += RangeCoder.BitTreeDecoder.reverseDecode(this._posDecoders,
249
+ this._rep0 - posSlot - 1, this._rangeDecoder, numDirectBits);
250
+ } else {
251
+ this._rep0 += this._rangeDecoder.decodeDirectBits(numDirectBits - Base.kNumAlignBits) << Base.kNumAlignBits;
252
+ this._rep0 += this._posAlignDecoder.reverseDecode(this._rangeDecoder);
253
+ if (this._rep0 < 0){
254
+ if (this._rep0 === -1) break;
255
+ return false;
256
+ }
257
+ }
258
+ } else {
259
+ this._rep0 = posSlot;
260
+ }
261
+ }
262
+ if (this._rep0 >= cumPos || this._rep0 >= this._dictionarySizeCheck) return false;
263
+ this._outWindow.copyBlock(this._rep0, len);
264
+ chunkPos += len; cumPos += len;
265
+ this._prevByte = this._outWindow.getByte(0);
266
+ }
267
+ }
268
+ this._nowPos64 = cumPos;
269
+ this._outWindow.flush();
270
+ this._outWindow.releaseStream();
271
+ this._rangeDecoder.releaseStream();
272
+ return true;
273
+ };
274
+
275
+ Decoder.prototype.setDecoderProperties = function(properties){
276
+ if (properties.length < 5) return false;
277
+ var value = properties[0] & 0xFF;
278
+ var lc = value % 9;
279
+ value = ~~(value / 9);
280
+ var lp = value % 5;
281
+ var pb = ~~(value / 5);
282
+ if (!this.setLcLpPb(lc, lp, pb)) return false;
283
+ var dictionarySize = 0;
284
+ for (var i = 0, shift = 1; i < 4; i++, shift *= 256){
285
+ dictionarySize += (properties[1+i] & 0xFF) * shift;
286
+ }
287
+ return this.setDictionarySize(dictionarySize);
288
+ };
289
+
290
+ Decoder.prototype.setDecoderPropertiesFromStream = function(stream) {
291
+ var buffer = [];
292
+ for (var i = 0; i < 5; i++) { buffer[i] = stream.readByte(); }
293
+ return this.setDecoderProperties(buffer);
294
+ };
295
+
296
+ module.exports = Decoder;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+ var Decoder = require('./LZMA/Decoder');
3
+
4
+ module.exports = {
5
+ Decoder: Decoder
6
+ };
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+ var Encoder = require('./Encoder');
3
+
4
+ var BitTreeDecoder = function(numBitLevels){
5
+ this._numBitLevels = numBitLevels;
6
+ this.init();
7
+ };
8
+
9
+ BitTreeDecoder.prototype.init = function(){
10
+ this._models = Encoder.initBitModels(null, 1 << this._numBitLevels);
11
+ };
12
+
13
+ BitTreeDecoder.prototype.decode = function(rangeDecoder){
14
+ var m = 1;
15
+ for (var i = this._numBitLevels; i > 0; i--){
16
+ m = (m << 1) | rangeDecoder.decodeBit(this._models, m);
17
+ }
18
+ return m - (1 << this._numBitLevels);
19
+ };
20
+
21
+ BitTreeDecoder.prototype.reverseDecode = function(rangeDecoder){
22
+ var m = 1, symbol = 0;
23
+ for (var i = 0; i < this._numBitLevels; i++){
24
+ var bit = rangeDecoder.decodeBit(this._models, m);
25
+ m = (m << 1) | bit;
26
+ symbol |= (bit << i);
27
+ }
28
+ return symbol;
29
+ };
30
+
31
+ BitTreeDecoder.reverseDecode = function(models, startIndex, rangeDecoder, numBitLevels) {
32
+ var m = 1, symbol = 0;
33
+ for (var i = 0; i < numBitLevels; i++){
34
+ var bit = rangeDecoder.decodeBit(models, startIndex + m);
35
+ m = (m << 1) | bit;
36
+ symbol |= (bit << i);
37
+ }
38
+ return symbol;
39
+ };
40
+
41
+ module.exports = BitTreeDecoder;
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ var Decoder = function(stream){
4
+ if (stream) {
5
+ this.setStream(stream);
6
+ this.init();
7
+ }
8
+ };
9
+
10
+ Decoder.prototype.setStream = function(stream){ this._stream = stream; };
11
+ Decoder.prototype.releaseStream = function(){ this._stream = null; };
12
+
13
+ Decoder.prototype.init = function(){
14
+ this._code = 0;
15
+ this._range = -1;
16
+ for (var i = 0; i < 5; i++){
17
+ this._code = (this._code << 8) | this._stream.readByte();
18
+ }
19
+ };
20
+
21
+ Decoder.prototype.decodeDirectBits = function(numTotalBits){
22
+ var result = 0;
23
+ for (var i = numTotalBits; i > 0; i--){
24
+ this._range >>>= 1;
25
+ var t = (this._code - this._range) >>> 31;
26
+ this._code -= this._range & (t - 1);
27
+ result = (result << 1) | (1 - t);
28
+ if ((this._range & 0xff000000) === 0){
29
+ this._code = (this._code << 8) | this._stream.readByte();
30
+ this._range <<= 8;
31
+ }
32
+ }
33
+ return result;
34
+ };
35
+
36
+ Decoder.prototype.decodeBit = function(probs, index){
37
+ var prob = probs[index];
38
+ var newBound = (this._range >>> 11) * prob;
39
+ if ((this._code ^ 0x80000000) < (newBound ^ 0x80000000)){
40
+ this._range = newBound;
41
+ probs[index] += (2048 - prob) >>> 5;
42
+ if ((this._range & 0xff000000) === 0){
43
+ this._code = (this._code << 8) | this._stream.readByte();
44
+ this._range <<= 8;
45
+ }
46
+ return 0;
47
+ }
48
+ this._range -= newBound;
49
+ this._code -= newBound;
50
+ probs[index] -= prob >>> 5;
51
+ if ((this._range & 0xff000000) === 0){
52
+ this._code = (this._code << 8) | this._stream.readByte();
53
+ this._range <<= 8;
54
+ }
55
+ return 1;
56
+ };
57
+
58
+ module.exports = Decoder;
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ var MAX32 = 0xFFFFFFFF;
4
+ var MAX24 = 0x00FFFFFF;
5
+ var MAX8 = 0x000000FF;
6
+ var MASK24= 0xFF000000;
7
+
8
+ var kNumBitModelTotalBits = 11;
9
+ var kBitModelTotal = (1 << kNumBitModelTotalBits);
10
+ var kNumMoveBits = 5;
11
+ var kNumMoveReducingBits = 2;
12
+ var kNumBitPriceShiftBits = 6;
13
+
14
+ var Encoder = function(stream){
15
+ this.init();
16
+ if (stream) { this.setStream(stream); }
17
+ };
18
+ Encoder.prototype.setStream = function(stream) { this._stream = stream; };
19
+ Encoder.prototype.releaseStream = function() { this._stream = null; };
20
+ Encoder.prototype.init = function() {
21
+ this._position = 0;
22
+ this.low = 0;
23
+ this.range = MAX32;
24
+ this._cacheSize = 1;
25
+ this._cache = 0;
26
+ };
27
+ Encoder.prototype.flushData = function() {
28
+ for (var i=0; i<5; i++) { this.shiftLow(); }
29
+ };
30
+ Encoder.prototype.flushStream = function() {
31
+ if (this._stream.flush) { this._stream.flush(); }
32
+ };
33
+ Encoder.prototype.shiftLow = function() {
34
+ var overflow = (this.low > MAX32) ? 1 : 0;
35
+ if (this.low < MASK24 || overflow) {
36
+ this._position += this._cacheSize;
37
+ var temp = this._cache;
38
+ do {
39
+ this._stream.writeByte((temp + overflow) & MAX8);
40
+ temp = MAX8;
41
+ } while (--this._cacheSize !== 0);
42
+ this._cache = this.low >>> 24;
43
+ }
44
+ this._cacheSize++;
45
+ this.low = (this.low & MAX24) * 256;
46
+ };
47
+ Encoder.prototype.encodeDirectBits = function(v, numTotalBits) {
48
+ var mask = 1 << (numTotalBits-1);
49
+ for (var i = numTotalBits - 1; i >= 0; i--, mask>>>=1) {
50
+ this.range >>>= 1;
51
+ if (v & mask) { this.low += this.range; }
52
+ if (this.range <= MAX24) {
53
+ this.range *= 256;
54
+ this.shiftLow();
55
+ }
56
+ }
57
+ };
58
+ Encoder.prototype.getProcessedSizeAdd = function() {
59
+ return this._cacheSize + this._position + 4;
60
+ };
61
+ Encoder.initBitModels = function(probs, len) {
62
+ if (len && !probs) {
63
+ probs = typeof(Uint16Array)!=='undefined' ? new Uint16Array(len) : new Array(len);
64
+ }
65
+ for (var i=0; i < probs.length; i++) { probs[i] = (kBitModelTotal >>> 1); }
66
+ return probs;
67
+ };
68
+ Encoder.prototype.encode = function(probs, index, symbol) {
69
+ var prob = probs[index];
70
+ var newBound = (this.range >>> kNumBitModelTotalBits) * prob;
71
+ if (symbol === 0) {
72
+ this.range = newBound;
73
+ probs[index] = prob + ((kBitModelTotal - prob) >>> kNumMoveBits);
74
+ } else {
75
+ this.low += newBound;
76
+ this.range -= newBound;
77
+ probs[index] = prob - (prob >>> kNumMoveBits);
78
+ }
79
+ if (this.range <= MAX24) {
80
+ this.range *= 256;
81
+ this.shiftLow();
82
+ }
83
+ };
84
+
85
+ var ProbPrices = typeof(Uint32Array)!=='undefined' ?
86
+ new Uint32Array(kBitModelTotal >>> kNumMoveReducingBits) : [];
87
+ (function() {
88
+ var kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
89
+ for (var i = kNumBits - 1; i >= 0; i--) {
90
+ var start = 1 << (kNumBits - i - 1);
91
+ var end = 1 << (kNumBits - i);
92
+ for (var j = start; j < end; j++) {
93
+ ProbPrices[j] = (i << kNumBitPriceShiftBits) +
94
+ (((end - j) << kNumBitPriceShiftBits) >>> (kNumBits - i - 1));
95
+ }
96
+ }
97
+ })();
98
+
99
+ Encoder.getPrice = function(prob, symbol) {
100
+ return ProbPrices[(((prob - symbol) ^ ((-symbol))) & (kBitModelTotal - 1)) >>> kNumMoveReducingBits];
101
+ };
102
+ Encoder.getPrice0 = function(prob) { return ProbPrices[prob >>> kNumMoveReducingBits]; };
103
+ Encoder.getPrice1 = function(prob) { return ProbPrices[(kBitModelTotal - prob) >>> kNumMoveReducingBits]; };
104
+ Encoder.kNumBitPriceShiftBits = kNumBitPriceShiftBits;
105
+
106
+ module.exports = Encoder;
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+ var BitTreeDecoder = require('./RangeCoder/BitTreeDecoder');
3
+ var Decoder = require('./RangeCoder/Decoder');
4
+ var Encoder = require('./RangeCoder/Encoder');
5
+
6
+ module.exports = {
7
+ BitTreeDecoder: BitTreeDecoder,
8
+ Decoder: Decoder,
9
+ Encoder: Encoder
10
+ };