inffast.js 11 KB

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