inftrees.js 11 KB

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