lzma.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. var LZMA = LZMA || {};
  2. // browserify support
  3. if ( typeof module === 'object' ) {
  4. module.exports = LZMA;
  5. }
  6. LZMA.OutWindow = function() {
  7. this._windowSize = 0;
  8. };
  9. LZMA.OutWindow.prototype.create = function(windowSize) {
  10. if ( (!this._buffer) || (this._windowSize !== windowSize) ) {
  11. this._buffer = [];
  12. }
  13. this._windowSize = windowSize;
  14. this._pos = 0;
  15. this._streamPos = 0;
  16. };
  17. LZMA.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. LZMA.OutWindow.prototype.releaseStream = function() {
  30. this.flush();
  31. this._stream = null;
  32. };
  33. LZMA.OutWindow.prototype.setStream = function(stream) {
  34. this.releaseStream();
  35. this._stream = stream;
  36. };
  37. LZMA.OutWindow.prototype.init = function(solid) {
  38. if (!solid) {
  39. this._streamPos = 0;
  40. this._pos = 0;
  41. }
  42. };
  43. LZMA.OutWindow.prototype.copyBlock = function(distance, len) {
  44. var pos = this._pos - distance - 1;
  45. if (pos < 0) {
  46. pos += this._windowSize;
  47. }
  48. while (len --) {
  49. if (pos >= this._windowSize) {
  50. pos = 0;
  51. }
  52. this._buffer[this._pos ++] = this._buffer[pos ++];
  53. if (this._pos >= this._windowSize) {
  54. this.flush();
  55. }
  56. }
  57. };
  58. LZMA.OutWindow.prototype.putByte = function(b) {
  59. this._buffer[this._pos ++] = b;
  60. if (this._pos >= this._windowSize) {
  61. this.flush();
  62. }
  63. };
  64. LZMA.OutWindow.prototype.getByte = function(distance) {
  65. var pos = this._pos - distance - 1;
  66. if (pos < 0) {
  67. pos += this._windowSize;
  68. }
  69. return this._buffer[pos];
  70. };
  71. LZMA.RangeDecoder = function() {
  72. };
  73. LZMA.RangeDecoder.prototype.setStream = function(stream) {
  74. this._stream = stream;
  75. };
  76. LZMA.RangeDecoder.prototype.releaseStream = function() {
  77. this._stream = null;
  78. };
  79. LZMA.RangeDecoder.prototype.init = function() {
  80. var i = 5;
  81. this._code = 0;
  82. this._range = -1;
  83. while (i --) {
  84. this._code = (this._code << 8) | this._stream.readByte();
  85. }
  86. };
  87. LZMA.RangeDecoder.prototype.decodeDirectBits = function(numTotalBits) {
  88. var result = 0, i = numTotalBits, t;
  89. while (i --) {
  90. this._range >>>= 1;
  91. t = (this._code - this._range) >>> 31;
  92. this._code -= this._range & (t - 1);
  93. result = (result << 1) | (1 - t);
  94. if ( (this._range & 0xff000000) === 0) {
  95. this._code = (this._code << 8) | this._stream.readByte();
  96. this._range <<= 8;
  97. }
  98. }
  99. return result;
  100. };
  101. LZMA.RangeDecoder.prototype.decodeBit = function(probs, index) {
  102. var prob = probs[index],
  103. newBound = (this._range >>> 11) * prob;
  104. if ( (this._code ^ 0x80000000) < (newBound ^ 0x80000000) ) {
  105. this._range = newBound;
  106. probs[index] += (2048 - prob) >>> 5;
  107. if ( (this._range & 0xff000000) === 0) {
  108. this._code = (this._code << 8) | this._stream.readByte();
  109. this._range <<= 8;
  110. }
  111. return 0;
  112. }
  113. this._range -= newBound;
  114. this._code -= newBound;
  115. probs[index] -= prob >>> 5;
  116. if ( (this._range & 0xff000000) === 0) {
  117. this._code = (this._code << 8) | this._stream.readByte();
  118. this._range <<= 8;
  119. }
  120. return 1;
  121. };
  122. LZMA.initBitModels = function(probs, len) {
  123. while (len --) {
  124. probs[len] = 1024;
  125. }
  126. };
  127. LZMA.BitTreeDecoder = function(numBitLevels) {
  128. this._models = [];
  129. this._numBitLevels = numBitLevels;
  130. };
  131. LZMA.BitTreeDecoder.prototype.init = function() {
  132. LZMA.initBitModels(this._models, 1 << this._numBitLevels);
  133. };
  134. LZMA.BitTreeDecoder.prototype.decode = function(rangeDecoder) {
  135. var m = 1, i = this._numBitLevels;
  136. while (i --) {
  137. m = (m << 1) | rangeDecoder.decodeBit(this._models, m);
  138. }
  139. return m - (1 << this._numBitLevels);
  140. };
  141. LZMA.BitTreeDecoder.prototype.reverseDecode = function(rangeDecoder) {
  142. var m = 1, symbol = 0, i = 0, bit;
  143. for (; i < this._numBitLevels; ++ i) {
  144. bit = rangeDecoder.decodeBit(this._models, m);
  145. m = (m << 1) | bit;
  146. symbol |= bit << i;
  147. }
  148. return symbol;
  149. };
  150. LZMA.reverseDecode2 = function(models, startIndex, rangeDecoder, numBitLevels) {
  151. var m = 1, symbol = 0, i = 0, bit;
  152. for (; i < numBitLevels; ++ i) {
  153. bit = rangeDecoder.decodeBit(models, startIndex + m);
  154. m = (m << 1) | bit;
  155. symbol |= bit << i;
  156. }
  157. return symbol;
  158. };
  159. LZMA.LenDecoder = function() {
  160. this._choice = [];
  161. this._lowCoder = [];
  162. this._midCoder = [];
  163. this._highCoder = new LZMA.BitTreeDecoder(8);
  164. this._numPosStates = 0;
  165. };
  166. LZMA.LenDecoder.prototype.create = function(numPosStates) {
  167. for (; this._numPosStates < numPosStates; ++ this._numPosStates) {
  168. this._lowCoder[this._numPosStates] = new LZMA.BitTreeDecoder(3);
  169. this._midCoder[this._numPosStates] = new LZMA.BitTreeDecoder(3);
  170. }
  171. };
  172. LZMA.LenDecoder.prototype.init = function() {
  173. var i = this._numPosStates;
  174. LZMA.initBitModels(this._choice, 2);
  175. while (i --) {
  176. this._lowCoder[i].init();
  177. this._midCoder[i].init();
  178. }
  179. this._highCoder.init();
  180. };
  181. LZMA.LenDecoder.prototype.decode = function(rangeDecoder, posState) {
  182. if (rangeDecoder.decodeBit(this._choice, 0) === 0) {
  183. return this._lowCoder[posState].decode(rangeDecoder);
  184. }
  185. if (rangeDecoder.decodeBit(this._choice, 1) === 0) {
  186. return 8 + this._midCoder[posState].decode(rangeDecoder);
  187. }
  188. return 16 + this._highCoder.decode(rangeDecoder);
  189. };
  190. LZMA.Decoder2 = function() {
  191. this._decoders = [];
  192. };
  193. LZMA.Decoder2.prototype.init = function() {
  194. LZMA.initBitModels(this._decoders, 0x300);
  195. };
  196. LZMA.Decoder2.prototype.decodeNormal = function(rangeDecoder) {
  197. var symbol = 1;
  198. do {
  199. symbol = (symbol << 1) | rangeDecoder.decodeBit(this._decoders, symbol);
  200. }while (symbol < 0x100);
  201. return symbol & 0xff;
  202. };
  203. LZMA.Decoder2.prototype.decodeWithMatchByte = function(rangeDecoder, matchByte) {
  204. var symbol = 1, matchBit, bit;
  205. do {
  206. matchBit = (matchByte >> 7) & 1;
  207. matchByte <<= 1;
  208. bit = rangeDecoder.decodeBit(this._decoders, ( (1 + matchBit) << 8) + symbol);
  209. symbol = (symbol << 1) | bit;
  210. if (matchBit !== bit) {
  211. while (symbol < 0x100) {
  212. symbol = (symbol << 1) | rangeDecoder.decodeBit(this._decoders, symbol);
  213. }
  214. break;
  215. }
  216. }while (symbol < 0x100);
  217. return symbol & 0xff;
  218. };
  219. LZMA.LiteralDecoder = function() {
  220. };
  221. LZMA.LiteralDecoder.prototype.create = function(numPosBits, numPrevBits) {
  222. var i;
  223. if (this._coders
  224. && (this._numPrevBits === numPrevBits)
  225. && (this._numPosBits === numPosBits) ) {
  226. return;
  227. }
  228. this._numPosBits = numPosBits;
  229. this._posMask = (1 << numPosBits) - 1;
  230. this._numPrevBits = numPrevBits;
  231. this._coders = [];
  232. i = 1 << (this._numPrevBits + this._numPosBits);
  233. while (i --) {
  234. this._coders[i] = new LZMA.Decoder2();
  235. }
  236. };
  237. LZMA.LiteralDecoder.prototype.init = function() {
  238. var i = 1 << (this._numPrevBits + this._numPosBits);
  239. while (i --) {
  240. this._coders[i].init();
  241. }
  242. };
  243. LZMA.LiteralDecoder.prototype.getDecoder = function(pos, prevByte) {
  244. return this._coders[( (pos & this._posMask) << this._numPrevBits)
  245. + ( (prevByte & 0xff) >>> (8 - this._numPrevBits) )];
  246. };
  247. LZMA.Decoder = function() {
  248. this._outWindow = new LZMA.OutWindow();
  249. this._rangeDecoder = new LZMA.RangeDecoder();
  250. this._isMatchDecoders = [];
  251. this._isRepDecoders = [];
  252. this._isRepG0Decoders = [];
  253. this._isRepG1Decoders = [];
  254. this._isRepG2Decoders = [];
  255. this._isRep0LongDecoders = [];
  256. this._posSlotDecoder = [];
  257. this._posDecoders = [];
  258. this._posAlignDecoder = new LZMA.BitTreeDecoder(4);
  259. this._lenDecoder = new LZMA.LenDecoder();
  260. this._repLenDecoder = new LZMA.LenDecoder();
  261. this._literalDecoder = new LZMA.LiteralDecoder();
  262. this._dictionarySize = -1;
  263. this._dictionarySizeCheck = -1;
  264. this._posSlotDecoder[0] = new LZMA.BitTreeDecoder(6);
  265. this._posSlotDecoder[1] = new LZMA.BitTreeDecoder(6);
  266. this._posSlotDecoder[2] = new LZMA.BitTreeDecoder(6);
  267. this._posSlotDecoder[3] = new LZMA.BitTreeDecoder(6);
  268. };
  269. LZMA.Decoder.prototype.setDictionarySize = function(dictionarySize) {
  270. if (dictionarySize < 0) {
  271. return false;
  272. }
  273. if (this._dictionarySize !== dictionarySize) {
  274. this._dictionarySize = dictionarySize;
  275. this._dictionarySizeCheck = Math.max(this._dictionarySize, 1);
  276. this._outWindow.create( Math.max(this._dictionarySizeCheck, 4096) );
  277. }
  278. return true;
  279. };
  280. LZMA.Decoder.prototype.setLcLpPb = function(lc, lp, pb) {
  281. var numPosStates = 1 << pb;
  282. if (lc > 8 || lp > 4 || pb > 4) {
  283. return false;
  284. }
  285. this._literalDecoder.create(lp, lc);
  286. this._lenDecoder.create(numPosStates);
  287. this._repLenDecoder.create(numPosStates);
  288. this._posStateMask = numPosStates - 1;
  289. return true;
  290. };
  291. LZMA.Decoder.prototype.init = function() {
  292. var i = 4;
  293. this._outWindow.init(false);
  294. LZMA.initBitModels(this._isMatchDecoders, 192);
  295. LZMA.initBitModels(this._isRep0LongDecoders, 192);
  296. LZMA.initBitModels(this._isRepDecoders, 12);
  297. LZMA.initBitModels(this._isRepG0Decoders, 12);
  298. LZMA.initBitModels(this._isRepG1Decoders, 12);
  299. LZMA.initBitModels(this._isRepG2Decoders, 12);
  300. LZMA.initBitModels(this._posDecoders, 114);
  301. this._literalDecoder.init();
  302. while (i --) {
  303. this._posSlotDecoder[i].init();
  304. }
  305. this._lenDecoder.init();
  306. this._repLenDecoder.init();
  307. this._posAlignDecoder.init();
  308. this._rangeDecoder.init();
  309. };
  310. LZMA.Decoder.prototype.decode = function(inStream, outStream, outSize) {
  311. var state = 0, rep0 = 0, rep1 = 0, rep2 = 0, rep3 = 0, nowPos64 = 0, prevByte = 0,
  312. posState, decoder2, len, distance, posSlot, numDirectBits;
  313. this._rangeDecoder.setStream(inStream);
  314. this._outWindow.setStream(outStream);
  315. this.init();
  316. while (outSize < 0 || nowPos64 < outSize) {
  317. posState = nowPos64 & this._posStateMask;
  318. if (this._rangeDecoder.decodeBit(this._isMatchDecoders, (state << 4) + posState) === 0) {
  319. decoder2 = this._literalDecoder.getDecoder(nowPos64 ++, prevByte);
  320. if (state >= 7) {
  321. prevByte = decoder2.decodeWithMatchByte(this._rangeDecoder, this._outWindow.getByte(rep0) );
  322. }else {
  323. prevByte = decoder2.decodeNormal(this._rangeDecoder);
  324. }
  325. this._outWindow.putByte(prevByte);
  326. state = state < 4 ? 0 : state - (state < 10 ? 3 : 6);
  327. }else {
  328. if (this._rangeDecoder.decodeBit(this._isRepDecoders, state) === 1) {
  329. len = 0;
  330. if (this._rangeDecoder.decodeBit(this._isRepG0Decoders, state) === 0) {
  331. if (this._rangeDecoder.decodeBit(this._isRep0LongDecoders, (state << 4) + posState) === 0) {
  332. state = state < 7 ? 9 : 11;
  333. len = 1;
  334. }
  335. }else {
  336. if (this._rangeDecoder.decodeBit(this._isRepG1Decoders, state) === 0) {
  337. distance = rep1;
  338. }else {
  339. if (this._rangeDecoder.decodeBit(this._isRepG2Decoders, state) === 0) {
  340. distance = rep2;
  341. }else {
  342. distance = rep3;
  343. rep3 = rep2;
  344. }
  345. rep2 = rep1;
  346. }
  347. rep1 = rep0;
  348. rep0 = distance;
  349. }
  350. if (len === 0) {
  351. len = 2 + this._repLenDecoder.decode(this._rangeDecoder, posState);
  352. state = state < 7 ? 8 : 11;
  353. }
  354. }else {
  355. rep3 = rep2;
  356. rep2 = rep1;
  357. rep1 = rep0;
  358. len = 2 + this._lenDecoder.decode(this._rangeDecoder, posState);
  359. state = state < 7 ? 7 : 10;
  360. posSlot = this._posSlotDecoder[len <= 5 ? len - 2 : 3].decode(this._rangeDecoder);
  361. if (posSlot >= 4) {
  362. numDirectBits = (posSlot >> 1) - 1;
  363. rep0 = (2 | (posSlot & 1) ) << numDirectBits;
  364. if (posSlot < 14) {
  365. rep0 += LZMA.reverseDecode2(this._posDecoders,
  366. rep0 - posSlot - 1, this._rangeDecoder, numDirectBits);
  367. }else {
  368. rep0 += this._rangeDecoder.decodeDirectBits(numDirectBits - 4) << 4;
  369. rep0 += this._posAlignDecoder.reverseDecode(this._rangeDecoder);
  370. if (rep0 < 0) {
  371. if (rep0 === -1) {
  372. break;
  373. }
  374. return false;
  375. }
  376. }
  377. }else {
  378. rep0 = posSlot;
  379. }
  380. }
  381. if (rep0 >= nowPos64 || rep0 >= this._dictionarySizeCheck) {
  382. return false;
  383. }
  384. this._outWindow.copyBlock(rep0, len);
  385. nowPos64 += len;
  386. prevByte = this._outWindow.getByte(0);
  387. }
  388. }
  389. this._outWindow.flush();
  390. this._outWindow.releaseStream();
  391. this._rangeDecoder.releaseStream();
  392. return true;
  393. };
  394. LZMA.Decoder.prototype.setDecoderProperties = function(properties) {
  395. var value, lc, lp, pb, dictionarySize;
  396. if (properties.size < 5) {
  397. return false;
  398. }
  399. value = properties.readByte();
  400. lc = value % 9;
  401. value = ~~(value / 9);
  402. lp = value % 5;
  403. pb = ~~(value / 5);
  404. if ( !this.setLcLpPb(lc, lp, pb) ) {
  405. return false;
  406. }
  407. dictionarySize = properties.readByte();
  408. dictionarySize |= properties.readByte() << 8;
  409. dictionarySize |= properties.readByte() << 16;
  410. dictionarySize += properties.readByte() * 16777216;
  411. return this.setDictionarySize(dictionarySize);
  412. };
  413. LZMA.decompress = function(properties, inStream, outStream, outSize) {
  414. var decoder = new LZMA.Decoder();
  415. if ( !decoder.setDecoderProperties(properties) ) {
  416. throw "Incorrect stream properties";
  417. }
  418. if ( !decoder.decode(inStream, outStream, outSize) ) {
  419. throw "Error in data stream";
  420. }
  421. return true;
  422. };