lzma-purejs-requirejs
Version:
pure JavaScript LZMA de/compression, for node.js, volo, and the browser.
75 lines (59 loc) • 1.61 kB
JavaScript
'use strict';
const freeze = require('../freeze');
var Decoder = function(stream){
if (stream) {
this.setStream(stream);
this.init();
}
};
Decoder.prototype.setStream = function(stream){
this._stream = stream;
};
Decoder.prototype.releaseStream = function(){
this._stream = null;
};
Decoder.prototype.init = function(){
var i = 5;
this._code = 0;
this._range = -1;
while(i --){
this._code = (this._code << 8) | this._stream.readByte();
}
};
Decoder.prototype.decodeDirectBits = function(numTotalBits){
var result = 0, i = numTotalBits, t;
while(i --){
this._range >>>= 1;
t = (this._code - this._range) >>> 31;
this._code -= this._range & (t - 1);
result = (result << 1) | (1 - t);
if ( (this._range & 0xff000000) === 0){
this._code = (this._code << 8) | this._stream.readByte();
this._range <<= 8;
}
}
return result;
};
Decoder.prototype.decodeBit = function(probs, index){
var prob = probs[index],
newBound = (this._range >>> 11) * prob;
if ( (this._code ^ 0x80000000) < (newBound ^ 0x80000000) ){
this._range = newBound;
probs[index] += (2048 - prob) >>> 5;
if ( (this._range & 0xff000000) === 0){
this._code = (this._code << 8) | this._stream.readByte();
this._range <<= 8;
}
return 0;
}
this._range -= newBound;
this._code -= newBound;
probs[index] -= prob >>> 5;
if ( (this._range & 0xff000000) === 0){
this._code = (this._code << 8) | this._stream.readByte();
this._range <<= 8;
}
return 1;
};
freeze(Decoder.prototype);
module.exports = freeze(Decoder);