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.

344 lines
12 KiB

1 year ago
  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. const BAD = 30; /* got a data error -- remain here until reset */
  22. const 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. let _in; /* local strm.input */
  53. let last; /* have enough input while in < last */
  54. let _out; /* local strm.output */
  55. let beg; /* inflate()'s initial strm.output */
  56. let end; /* while out < end, enough space available */
  57. //#ifdef INFLATE_STRICT
  58. let dmax; /* maximum distance from zlib header */
  59. //#endif
  60. let wsize; /* window size or zero if not using window */
  61. let whave; /* valid bytes in the window */
  62. let wnext; /* window write index */
  63. // Use `s_window` instead `window`, avoid conflict with instrumentation tools
  64. let s_window; /* allocated sliding window, if wsize != 0 */
  65. let hold; /* local strm.hold */
  66. let bits; /* local strm.bits */
  67. let lcode; /* local strm.lencode */
  68. let dcode; /* local strm.distcode */
  69. let lmask; /* mask for first level of length codes */
  70. let dmask; /* mask for first level of distance codes */
  71. let here; /* retrieved table entry */
  72. let op; /* code bits, operation, extra bits, or */
  73. /* window position, window bytes to copy */
  74. let len; /* match length, unused bytes */
  75. let dist; /* match distance */
  76. let from; /* where to copy match from */
  77. let from_source;
  78. let input, output; // JS specific, because we have no pointers
  79. /* copy state to local variables */
  80. const state = strm.state;
  81. //here = state.here;
  82. _in = strm.next_in;
  83. input = strm.input;
  84. last = _in + (strm.avail_in - 5);
  85. _out = strm.next_out;
  86. output = strm.output;
  87. beg = _out - (start - strm.avail_out);
  88. end = _out + (strm.avail_out - 257);
  89. //#ifdef INFLATE_STRICT
  90. dmax = state.dmax;
  91. //#endif
  92. wsize = state.wsize;
  93. whave = state.whave;
  94. wnext = state.wnext;
  95. s_window = state.window;
  96. hold = state.hold;
  97. bits = state.bits;
  98. lcode = state.lencode;
  99. dcode = state.distcode;
  100. lmask = (1 << state.lenbits) - 1;
  101. dmask = (1 << state.distbits) - 1;
  102. /* decode literals and length/distances until end-of-block or not enough
  103. input data or output space */
  104. top:
  105. do {
  106. if (bits < 15) {
  107. hold += input[_in++] << bits;
  108. bits += 8;
  109. hold += input[_in++] << bits;
  110. bits += 8;
  111. }
  112. here = lcode[hold & lmask];
  113. dolen:
  114. for (;;) { // Goto emulation
  115. op = here >>> 24/*here.bits*/;
  116. hold >>>= op;
  117. bits -= op;
  118. op = (here >>> 16) & 0xff/*here.op*/;
  119. if (op === 0) { /* literal */
  120. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  121. // "inflate: literal '%c'\n" :
  122. // "inflate: literal 0x%02x\n", here.val));
  123. output[_out++] = here & 0xffff/*here.val*/;
  124. }
  125. else if (op & 16) { /* length base */
  126. len = here & 0xffff/*here.val*/;
  127. op &= 15; /* number of extra bits */
  128. if (op) {
  129. if (bits < op) {
  130. hold += input[_in++] << bits;
  131. bits += 8;
  132. }
  133. len += hold & ((1 << op) - 1);
  134. hold >>>= op;
  135. bits -= op;
  136. }
  137. //Tracevv((stderr, "inflate: length %u\n", len));
  138. if (bits < 15) {
  139. hold += input[_in++] << bits;
  140. bits += 8;
  141. hold += input[_in++] << bits;
  142. bits += 8;
  143. }
  144. here = dcode[hold & dmask];
  145. dodist:
  146. for (;;) { // goto emulation
  147. op = here >>> 24/*here.bits*/;
  148. hold >>>= op;
  149. bits -= op;
  150. op = (here >>> 16) & 0xff/*here.op*/;
  151. if (op & 16) { /* distance base */
  152. dist = here & 0xffff/*here.val*/;
  153. op &= 15; /* number of extra bits */
  154. if (bits < op) {
  155. hold += input[_in++] << bits;
  156. bits += 8;
  157. if (bits < op) {
  158. hold += input[_in++] << bits;
  159. bits += 8;
  160. }
  161. }
  162. dist += hold & ((1 << op) - 1);
  163. //#ifdef INFLATE_STRICT
  164. if (dist > dmax) {
  165. strm.msg = 'invalid distance too far back';
  166. state.mode = BAD;
  167. break top;
  168. }
  169. //#endif
  170. hold >>>= op;
  171. bits -= op;
  172. //Tracevv((stderr, "inflate: distance %u\n", dist));
  173. op = _out - beg; /* max distance in output */
  174. if (dist > op) { /* see if copy from window */
  175. op = dist - op; /* distance back in window */
  176. if (op > whave) {
  177. if (state.sane) {
  178. strm.msg = 'invalid distance too far back';
  179. state.mode = BAD;
  180. break top;
  181. }
  182. // (!) This block is disabled in zlib defaults,
  183. // don't enable it for binary compatibility
  184. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  185. // if (len <= op - whave) {
  186. // do {
  187. // output[_out++] = 0;
  188. // } while (--len);
  189. // continue top;
  190. // }
  191. // len -= op - whave;
  192. // do {
  193. // output[_out++] = 0;
  194. // } while (--op > whave);
  195. // if (op === 0) {
  196. // from = _out - dist;
  197. // do {
  198. // output[_out++] = output[from++];
  199. // } while (--len);
  200. // continue top;
  201. // }
  202. //#endif
  203. }
  204. from = 0; // window index
  205. from_source = s_window;
  206. if (wnext === 0) { /* very common case */
  207. from += wsize - op;
  208. if (op < len) { /* some from window */
  209. len -= op;
  210. do {
  211. output[_out++] = s_window[from++];
  212. } while (--op);
  213. from = _out - dist; /* rest from output */
  214. from_source = output;
  215. }
  216. }
  217. else if (wnext < op) { /* wrap around window */
  218. from += wsize + wnext - op;
  219. op -= wnext;
  220. if (op < len) { /* some from end of window */
  221. len -= op;
  222. do {
  223. output[_out++] = s_window[from++];
  224. } while (--op);
  225. from = 0;
  226. if (wnext < len) { /* some from start of window */
  227. op = wnext;
  228. len -= op;
  229. do {
  230. output[_out++] = s_window[from++];
  231. } while (--op);
  232. from = _out - dist; /* rest from output */
  233. from_source = output;
  234. }
  235. }
  236. }
  237. else { /* contiguous in window */
  238. from += wnext - op;
  239. if (op < len) { /* some from window */
  240. len -= op;
  241. do {
  242. output[_out++] = s_window[from++];
  243. } while (--op);
  244. from = _out - dist; /* rest from output */
  245. from_source = output;
  246. }
  247. }
  248. while (len > 2) {
  249. output[_out++] = from_source[from++];
  250. output[_out++] = from_source[from++];
  251. output[_out++] = from_source[from++];
  252. len -= 3;
  253. }
  254. if (len) {
  255. output[_out++] = from_source[from++];
  256. if (len > 1) {
  257. output[_out++] = from_source[from++];
  258. }
  259. }
  260. }
  261. else {
  262. from = _out - dist; /* copy direct from output */
  263. do { /* minimum length is three */
  264. output[_out++] = output[from++];
  265. output[_out++] = output[from++];
  266. output[_out++] = output[from++];
  267. len -= 3;
  268. } while (len > 2);
  269. if (len) {
  270. output[_out++] = output[from++];
  271. if (len > 1) {
  272. output[_out++] = output[from++];
  273. }
  274. }
  275. }
  276. }
  277. else if ((op & 64) === 0) { /* 2nd level distance code */
  278. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  279. continue dodist;
  280. }
  281. else {
  282. strm.msg = 'invalid distance code';
  283. state.mode = BAD;
  284. break top;
  285. }
  286. break; // need to emulate goto via "continue"
  287. }
  288. }
  289. else if ((op & 64) === 0) { /* 2nd level length code */
  290. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  291. continue dolen;
  292. }
  293. else if (op & 32) { /* end-of-block */
  294. //Tracevv((stderr, "inflate: end of block\n"));
  295. state.mode = TYPE;
  296. break top;
  297. }
  298. else {
  299. strm.msg = 'invalid literal/length code';
  300. state.mode = BAD;
  301. break top;
  302. }
  303. break; // need to emulate goto via "continue"
  304. }
  305. } while (_in < last && _out < end);
  306. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  307. len = bits >> 3;
  308. _in -= len;
  309. bits -= len << 3;
  310. hold &= (1 << bits) - 1;
  311. /* update state and return */
  312. strm.next_in = _in;
  313. strm.next_out = _out;
  314. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  315. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  316. state.hold = hold;
  317. state.bits = bits;
  318. return;
  319. };