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