inffast.js 11 KB

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