You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inffast.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. 'use strict';
  2. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  3. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. // 2. Altered source versions must be plainly marked as such, and must not be
  18. // misrepresented as being the original software.
  19. // 3. This notice may not be removed or altered from any source distribution.
  20. // See state defs from inflate.js
  21. var BAD = 30; /* got a data error -- remain here until reset */
  22. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  23. /*
  24. Decode literal, length, and distance codes and write out the resulting
  25. literal and match bytes until either not enough input or output is
  26. available, an end-of-block is encountered, or a data error is encountered.
  27. When large enough input and output buffers are supplied to inflate(), for
  28. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  29. inflate execution time is spent in this routine.
  30. Entry assumptions:
  31. state.mode === LEN
  32. strm.avail_in >= 6
  33. strm.avail_out >= 258
  34. start >= strm.avail_out
  35. state.bits < 8
  36. On return, state.mode is one of:
  37. LEN -- ran out of enough output space or enough available input
  38. TYPE -- reached end of block code, inflate() to interpret next block
  39. BAD -- error in block data
  40. Notes:
  41. - The maximum input bits used by a length/distance pair is 15 bits for the
  42. length code, 5 bits for the length extra, 15 bits for the distance code,
  43. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  44. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  45. checking for available input while decoding.
  46. - The maximum bytes that a single length/distance pair can output is 258
  47. bytes, which is the maximum length that can be coded. inflate_fast()
  48. requires strm.avail_out >= 258 for each loop to avoid checking for
  49. output space.
  50. */
  51. module.exports = function inflate_fast(strm, start) {
  52. var state;
  53. var _in; /* local strm.input */
  54. var last; /* have enough input while in < last */
  55. var _out; /* local strm.output */
  56. var beg; /* inflate()'s initial strm.output */
  57. var end; /* while out < end, enough space available */
  58. //#ifdef INFLATE_STRICT
  59. var dmax; /* maximum distance from zlib header */
  60. //#endif
  61. var wsize; /* window size or zero if not using window */
  62. var whave; /* valid bytes in the window */
  63. var wnext; /* window write index */
  64. // Use `s_window` instead `window`, avoid conflict with instrumentation tools
  65. var s_window; /* allocated sliding window, if wsize != 0 */
  66. var hold; /* local strm.hold */
  67. var bits; /* local strm.bits */
  68. var lcode; /* local strm.lencode */
  69. var dcode; /* local strm.distcode */
  70. var lmask; /* mask for first level of length codes */
  71. var dmask; /* mask for first level of distance codes */
  72. var here; /* retrieved table entry */
  73. var op; /* code bits, operation, extra bits, or */
  74. /* window position, window bytes to copy */
  75. var len; /* match length, unused bytes */
  76. var dist; /* match distance */
  77. var from; /* where to copy match from */
  78. var from_source;
  79. var input, output; // JS specific, because we have no pointers
  80. /* copy state to local variables */
  81. state = strm.state;
  82. //here = state.here;
  83. _in = strm.next_in;
  84. input = strm.input;
  85. last = _in + (strm.avail_in - 5);
  86. _out = strm.next_out;
  87. output = strm.output;
  88. beg = _out - (start - strm.avail_out);
  89. end = _out + (strm.avail_out - 257);
  90. //#ifdef INFLATE_STRICT
  91. dmax = state.dmax;
  92. //#endif
  93. wsize = state.wsize;
  94. whave = state.whave;
  95. wnext = state.wnext;
  96. s_window = state.window;
  97. hold = state.hold;
  98. bits = state.bits;
  99. lcode = state.lencode;
  100. dcode = state.distcode;
  101. lmask = (1 << state.lenbits) - 1;
  102. dmask = (1 << state.distbits) - 1;
  103. /* decode literals and length/distances until end-of-block or not enough
  104. input data or output space */
  105. top:
  106. do {
  107. if (bits < 15) {
  108. hold += input[_in++] << bits;
  109. bits += 8;
  110. hold += input[_in++] << bits;
  111. bits += 8;
  112. }
  113. here = lcode[hold & lmask];
  114. dolen:
  115. for (;;) { // Goto emulation
  116. op = here >>> 24/*here.bits*/;
  117. hold >>>= op;
  118. bits -= op;
  119. op = (here >>> 16) & 0xff/*here.op*/;
  120. if (op === 0) { /* literal */
  121. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  122. // "inflate: literal '%c'\n" :
  123. // "inflate: literal 0x%02x\n", here.val));
  124. output[_out++] = here & 0xffff/*here.val*/;
  125. }
  126. else if (op & 16) { /* length base */
  127. len = here & 0xffff/*here.val*/;
  128. op &= 15; /* number of extra bits */
  129. if (op) {
  130. if (bits < op) {
  131. hold += input[_in++] << bits;
  132. bits += 8;
  133. }
  134. len += hold & ((1 << op) - 1);
  135. hold >>>= op;
  136. bits -= op;
  137. }
  138. //Tracevv((stderr, "inflate: length %u\n", len));
  139. if (bits < 15) {
  140. hold += input[_in++] << bits;
  141. bits += 8;
  142. hold += input[_in++] << bits;
  143. bits += 8;
  144. }
  145. here = dcode[hold & dmask];
  146. dodist:
  147. for (;;) { // goto emulation
  148. op = here >>> 24/*here.bits*/;
  149. hold >>>= op;
  150. bits -= op;
  151. op = (here >>> 16) & 0xff/*here.op*/;
  152. if (op & 16) { /* distance base */
  153. dist = here & 0xffff/*here.val*/;
  154. op &= 15; /* number of extra bits */
  155. if (bits < op) {
  156. hold += input[_in++] << bits;
  157. bits += 8;
  158. if (bits < op) {
  159. hold += input[_in++] << bits;
  160. bits += 8;
  161. }
  162. }
  163. dist += hold & ((1 << op) - 1);
  164. //#ifdef INFLATE_STRICT
  165. if (dist > dmax) {
  166. strm.msg = 'invalid distance too far back';
  167. state.mode = BAD;
  168. break top;
  169. }
  170. //#endif
  171. hold >>>= op;
  172. bits -= op;
  173. //Tracevv((stderr, "inflate: distance %u\n", dist));
  174. op = _out - beg; /* max distance in output */
  175. if (dist > op) { /* see if copy from window */
  176. op = dist - op; /* distance back in window */
  177. if (op > whave) {
  178. if (state.sane) {
  179. strm.msg = 'invalid distance too far back';
  180. state.mode = BAD;
  181. break top;
  182. }
  183. // (!) This block is disabled in zlib defaults,
  184. // don't enable it for binary compatibility
  185. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  186. // if (len <= op - whave) {
  187. // do {
  188. // output[_out++] = 0;
  189. // } while (--len);
  190. // continue top;
  191. // }
  192. // len -= op - whave;
  193. // do {
  194. // output[_out++] = 0;
  195. // } while (--op > whave);
  196. // if (op === 0) {
  197. // from = _out - dist;
  198. // do {
  199. // output[_out++] = output[from++];
  200. // } while (--len);
  201. // continue top;
  202. // }
  203. //#endif
  204. }
  205. from = 0; // window index
  206. from_source = s_window;
  207. if (wnext === 0) { /* very common case */
  208. from += wsize - op;
  209. if (op < len) { /* some from window */
  210. len -= op;
  211. do {
  212. output[_out++] = s_window[from++];
  213. } while (--op);
  214. from = _out - dist; /* rest from output */
  215. from_source = output;
  216. }
  217. }
  218. else if (wnext < op) { /* wrap around window */
  219. from += wsize + wnext - op;
  220. op -= wnext;
  221. if (op < len) { /* some from end of window */
  222. len -= op;
  223. do {
  224. output[_out++] = s_window[from++];
  225. } while (--op);
  226. from = 0;
  227. if (wnext < len) { /* some from start of window */
  228. op = wnext;
  229. len -= op;
  230. do {
  231. output[_out++] = s_window[from++];
  232. } while (--op);
  233. from = _out - dist; /* rest from output */
  234. from_source = output;
  235. }
  236. }
  237. }
  238. else { /* contiguous in window */
  239. from += wnext - op;
  240. if (op < len) { /* some from window */
  241. len -= op;
  242. do {
  243. output[_out++] = s_window[from++];
  244. } while (--op);
  245. from = _out - dist; /* rest from output */
  246. from_source = output;
  247. }
  248. }
  249. while (len > 2) {
  250. output[_out++] = from_source[from++];
  251. output[_out++] = from_source[from++];
  252. output[_out++] = from_source[from++];
  253. len -= 3;
  254. }
  255. if (len) {
  256. output[_out++] = from_source[from++];
  257. if (len > 1) {
  258. output[_out++] = from_source[from++];
  259. }
  260. }
  261. }
  262. else {
  263. from = _out - dist; /* copy direct from output */
  264. do { /* minimum length is three */
  265. output[_out++] = output[from++];
  266. output[_out++] = output[from++];
  267. output[_out++] = output[from++];
  268. len -= 3;
  269. } while (len > 2);
  270. if (len) {
  271. output[_out++] = output[from++];
  272. if (len > 1) {
  273. output[_out++] = output[from++];
  274. }
  275. }
  276. }
  277. }
  278. else if ((op & 64) === 0) { /* 2nd level distance code */
  279. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  280. continue dodist;
  281. }
  282. else {
  283. strm.msg = 'invalid distance code';
  284. state.mode = BAD;
  285. break top;
  286. }
  287. break; // need to emulate goto via "continue"
  288. }
  289. }
  290. else if ((op & 64) === 0) { /* 2nd level length code */
  291. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  292. continue dolen;
  293. }
  294. else if (op & 32) { /* end-of-block */
  295. //Tracevv((stderr, "inflate: end of block\n"));
  296. state.mode = TYPE;
  297. break top;
  298. }
  299. else {
  300. strm.msg = 'invalid literal/length code';
  301. state.mode = BAD;
  302. break top;
  303. }
  304. break; // need to emulate goto via "continue"
  305. }
  306. } while (_in < last && _out < end);
  307. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  308. len = bits >> 3;
  309. _in -= len;
  310. bits -= len << 3;
  311. hold &= (1 << bits) - 1;
  312. /* update state and return */
  313. strm.next_in = _in;
  314. strm.next_out = _out;
  315. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  316. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  317. state.hold = hold;
  318. state.bits = bits;
  319. return;
  320. };