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. const MAXBITS = 15;
  21. const ENOUGH_LENS = 852;
  22. const ENOUGH_DISTS = 592;
  23. //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  24. const CODES = 0;
  25. const LENS = 1;
  26. const DISTS = 2;
  27. const lbase = new Uint16Array([ /* Length codes 257..285 base */
  28. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  29. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  30. ]);
  31. const lext = new Uint8Array([ /* Length codes 257..285 extra */
  32. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  33. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
  34. ]);
  35. const dbase = new Uint16Array([ /* Distance codes 0..29 base */
  36. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  37. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  38. 8193, 12289, 16385, 24577, 0, 0
  39. ]);
  40. const dext = new Uint8Array([ /* Distance codes 0..29 extra */
  41. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  42. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  43. 28, 28, 29, 29, 64, 64
  44. ]);
  45. const inflate_table = (type, lens, lens_index, codes, table, table_index, work, opts) =>
  46. {
  47. const bits = opts.bits;
  48. //here = opts.here; /* table entry for duplication */
  49. let len = 0; /* a code's length in bits */
  50. let sym = 0; /* index of code symbols */
  51. let min = 0, max = 0; /* minimum and maximum code lengths */
  52. let root = 0; /* number of index bits for root table */
  53. let curr = 0; /* number of index bits for current table */
  54. let drop = 0; /* code bits to drop for sub-table */
  55. let left = 0; /* number of prefix codes available */
  56. let used = 0; /* code entries in table used */
  57. let huff = 0; /* Huffman code */
  58. let incr; /* for incrementing code, index */
  59. let fill; /* index for replicating entries */
  60. let low; /* low bits for current root entry */
  61. let mask; /* mask for low root bits */
  62. let next; /* next available space in table */
  63. let base = null; /* base value table to use */
  64. let base_index = 0;
  65. // let shoextra; /* extra bits table to use */
  66. let end; /* use base and extra for symbol > end */
  67. const count = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */
  68. const offs = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */
  69. let extra = null;
  70. let extra_index = 0;
  71. let here_bits, here_op, here_val;
  72. /*
  73. Process a set of code lengths to create a canonical Huffman code. The
  74. code lengths are lens[0..codes-1]. Each length corresponds to the
  75. symbols 0..codes-1. The Huffman code is generated by first sorting the
  76. symbols by length from short to long, and retaining the symbol order
  77. for codes with equal lengths. Then the code starts with all zero bits
  78. for the first code of the shortest length, and the codes are integer
  79. increments for the same length, and zeros are appended as the length
  80. increases. For the deflate format, these bits are stored backwards
  81. from their more natural integer increment ordering, and so when the
  82. decoding tables are built in the large loop below, the integer codes
  83. are incremented backwards.
  84. This routine assumes, but does not check, that all of the entries in
  85. lens[] are in the range 0..MAXBITS. The caller must assure this.
  86. 1..MAXBITS is interpreted as that code length. zero means that that
  87. symbol does not occur in this code.
  88. The codes are sorted by computing a count of codes for each length,
  89. creating from that a table of starting indices for each length in the
  90. sorted table, and then entering the symbols in order in the sorted
  91. table. The sorted table is work[], with that space being provided by
  92. the caller.
  93. The length counts are used for other purposes as well, i.e. finding
  94. the minimum and maximum length codes, determining if there are any
  95. codes at all, checking for a valid set of lengths, and looking ahead
  96. at length counts to determine sub-table sizes when building the
  97. decoding tables.
  98. */
  99. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  100. for (len = 0; len <= MAXBITS; len++) {
  101. count[len] = 0;
  102. }
  103. for (sym = 0; sym < codes; sym++) {
  104. count[lens[lens_index + sym]]++;
  105. }
  106. /* bound code lengths, force root to be within code lengths */
  107. root = bits;
  108. for (max = MAXBITS; max >= 1; max--) {
  109. if (count[max] !== 0) { break; }
  110. }
  111. if (root > max) {
  112. root = max;
  113. }
  114. if (max === 0) { /* no symbols to code at all */
  115. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  116. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  117. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  118. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  119. //table.op[opts.table_index] = 64;
  120. //table.bits[opts.table_index] = 1;
  121. //table.val[opts.table_index++] = 0;
  122. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  123. opts.bits = 1;
  124. return 0; /* no symbols, but wait for decoding to report error */
  125. }
  126. for (min = 1; min < max; min++) {
  127. if (count[min] !== 0) { break; }
  128. }
  129. if (root < min) {
  130. root = min;
  131. }
  132. /* check for an over-subscribed or incomplete set of lengths */
  133. left = 1;
  134. for (len = 1; len <= MAXBITS; len++) {
  135. left <<= 1;
  136. left -= count[len];
  137. if (left < 0) {
  138. return -1;
  139. } /* over-subscribed */
  140. }
  141. if (left > 0 && (type === CODES || max !== 1)) {
  142. return -1; /* incomplete set */
  143. }
  144. /* generate offsets into symbol table for each length for sorting */
  145. offs[1] = 0;
  146. for (len = 1; len < MAXBITS; len++) {
  147. offs[len + 1] = offs[len] + count[len];
  148. }
  149. /* sort symbols by length, by symbol order within each length */
  150. for (sym = 0; sym < codes; sym++) {
  151. if (lens[lens_index + sym] !== 0) {
  152. work[offs[lens[lens_index + sym]]++] = sym;
  153. }
  154. }
  155. /*
  156. Create and fill in decoding tables. In this loop, the table being
  157. filled is at next and has curr index bits. The code being used is huff
  158. with length len. That code is converted to an index by dropping drop
  159. bits off of the bottom. For codes where len is less than drop + curr,
  160. those top drop + curr - len bits are incremented through all values to
  161. fill the table with replicated entries.
  162. root is the number of index bits for the root table. When len exceeds
  163. root, sub-tables are created pointed to by the root entry with an index
  164. of the low root bits of huff. This is saved in low to check for when a
  165. new sub-table should be started. drop is zero when the root table is
  166. being filled, and drop is root when sub-tables are being filled.
  167. When a new sub-table is needed, it is necessary to look ahead in the
  168. code lengths to determine what size sub-table is needed. The length
  169. counts are used for this, and so count[] is decremented as codes are
  170. entered in the tables.
  171. used keeps track of how many table entries have been allocated from the
  172. provided *table space. It is checked for LENS and DIST tables against
  173. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  174. the initial root table size constants. See the comments in inftrees.h
  175. for more information.
  176. sym increments through all symbols, and the loop terminates when
  177. all codes of length max, i.e. all codes, have been processed. This
  178. routine permits incomplete codes, so another loop after this one fills
  179. in the rest of the decoding tables with invalid code markers.
  180. */
  181. /* set up for code type */
  182. // poor man optimization - use if-else instead of switch,
  183. // to avoid deopts in old v8
  184. if (type === CODES) {
  185. base = extra = work; /* dummy value--not used */
  186. end = 19;
  187. } else if (type === LENS) {
  188. base = lbase;
  189. base_index -= 257;
  190. extra = lext;
  191. extra_index -= 257;
  192. end = 256;
  193. } else { /* DISTS */
  194. base = dbase;
  195. extra = dext;
  196. end = -1;
  197. }
  198. /* initialize opts for loop */
  199. huff = 0; /* starting code */
  200. sym = 0; /* starting code symbol */
  201. len = min; /* starting code length */
  202. next = table_index; /* current table to fill in */
  203. curr = root; /* current table index bits */
  204. drop = 0; /* current bits to drop from code for index */
  205. low = -1; /* trigger new sub-table when len > root */
  206. used = 1 << root; /* use root table entries */
  207. mask = used - 1; /* mask for comparing low */
  208. /* check available table space */
  209. if ((type === LENS && used > ENOUGH_LENS) ||
  210. (type === DISTS && used > ENOUGH_DISTS)) {
  211. return 1;
  212. }
  213. /* process all codes and make table entries */
  214. for (;;) {
  215. /* create table entry */
  216. here_bits = len - drop;
  217. if (work[sym] < end) {
  218. here_op = 0;
  219. here_val = work[sym];
  220. }
  221. else if (work[sym] > end) {
  222. here_op = extra[extra_index + work[sym]];
  223. here_val = base[base_index + work[sym]];
  224. }
  225. else {
  226. here_op = 32 + 64; /* end of block */
  227. here_val = 0;
  228. }
  229. /* replicate for those indices with low len bits equal to huff */
  230. incr = 1 << (len - drop);
  231. fill = 1 << curr;
  232. min = fill; /* save offset to next table */
  233. do {
  234. fill -= incr;
  235. table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
  236. } while (fill !== 0);
  237. /* backwards increment the len-bit code huff */
  238. incr = 1 << (len - 1);
  239. while (huff & incr) {
  240. incr >>= 1;
  241. }
  242. if (incr !== 0) {
  243. huff &= incr - 1;
  244. huff += incr;
  245. } else {
  246. huff = 0;
  247. }
  248. /* go to next symbol, update count, len */
  249. sym++;
  250. if (--count[len] === 0) {
  251. if (len === max) { break; }
  252. len = lens[lens_index + work[sym]];
  253. }
  254. /* create new sub-table if needed */
  255. if (len > root && (huff & mask) !== low) {
  256. /* if first time, transition to sub-tables */
  257. if (drop === 0) {
  258. drop = root;
  259. }
  260. /* increment past last table */
  261. next += min; /* here min is 1 << curr */
  262. /* determine length of next table */
  263. curr = len - drop;
  264. left = 1 << curr;
  265. while (curr + drop < max) {
  266. left -= count[curr + drop];
  267. if (left <= 0) { break; }
  268. curr++;
  269. left <<= 1;
  270. }
  271. /* check for enough space */
  272. used += 1 << curr;
  273. if ((type === LENS && used > ENOUGH_LENS) ||
  274. (type === DISTS && used > ENOUGH_DISTS)) {
  275. return 1;
  276. }
  277. /* point entry in root table to sub-table */
  278. low = huff & mask;
  279. /*table.op[low] = curr;
  280. table.bits[low] = root;
  281. table.val[low] = next - opts.table_index;*/
  282. table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
  283. }
  284. }
  285. /* fill in remaining table entry if code is incomplete (guaranteed to have
  286. at most one remaining entry, since if the code is incomplete, the
  287. maximum code length that was allowed to get this far is one bit) */
  288. if (huff !== 0) {
  289. //table.op[next + huff] = 64; /* invalid code marker */
  290. //table.bits[next + huff] = len - drop;
  291. //table.val[next + huff] = 0;
  292. table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
  293. }
  294. /* set return parameters */
  295. //opts.table_index += used;
  296. opts.bits = root;
  297. return 0;
  298. };
  299. module.exports = inflate_table;