Counter Strike : Global Offensive Source Code
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.

324 lines
13 KiB

  1. /* inftree9.c -- generate Huffman trees for efficient decoding
  2. * Copyright (C) 1995-2010 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "zutil.h"
  6. #include "inftree9.h"
  7. #define MAXBITS 15
  8. const char inflate9_copyright[] =
  9. " inflate9 1.2.5 Copyright 1995-2010 Mark Adler ";
  10. /*
  11. If you use the zlib library in a product, an acknowledgment is welcome
  12. in the documentation of your product. If for some reason you cannot
  13. include such an acknowledgment, I would appreciate that you keep this
  14. copyright string in the executable of your product.
  15. */
  16. /*
  17. Build a set of tables to decode the provided canonical Huffman code.
  18. The code lengths are lens[0..codes-1]. The result starts at *table,
  19. whose indices are 0..2^bits-1. work is a writable array of at least
  20. lens shorts, which is used as a work area. type is the type of code
  21. to be generated, CODES, LENS, or DISTS. On return, zero is success,
  22. -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
  23. on return points to the next available entry's address. bits is the
  24. requested root table index bits, and on return it is the actual root
  25. table index bits. It will differ if the request is greater than the
  26. longest code or if it is less than the shortest code.
  27. */
  28. int inflate_table9(type, lens, codes, table, bits, work)
  29. codetype type;
  30. unsigned short FAR *lens;
  31. unsigned codes;
  32. code FAR * FAR *table;
  33. unsigned FAR *bits;
  34. unsigned short FAR *work;
  35. {
  36. unsigned len; /* a code's length in bits */
  37. unsigned sym; /* index of code symbols */
  38. unsigned min, max; /* minimum and maximum code lengths */
  39. unsigned root; /* number of index bits for root table */
  40. unsigned curr; /* number of index bits for current table */
  41. unsigned drop; /* code bits to drop for sub-table */
  42. int left; /* number of prefix codes available */
  43. unsigned used; /* code entries in table used */
  44. unsigned huff; /* Huffman code */
  45. unsigned incr; /* for incrementing code, index */
  46. unsigned fill; /* index for replicating entries */
  47. unsigned low; /* low bits for current root entry */
  48. unsigned mask; /* mask for low root bits */
  49. code this; /* table entry for duplication */
  50. code FAR *next; /* next available space in table */
  51. const unsigned short FAR *base; /* base value table to use */
  52. const unsigned short FAR *extra; /* extra bits table to use */
  53. int end; /* use base and extra for symbol > end */
  54. unsigned short count[MAXBITS+1]; /* number of codes of each length */
  55. unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
  56. static const unsigned short lbase[31] = { /* Length codes 257..285 base */
  57. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17,
  58. 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115,
  59. 131, 163, 195, 227, 3, 0, 0};
  60. static const unsigned short lext[31] = { /* Length codes 257..285 extra */
  61. 128, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129,
  62. 130, 130, 130, 130, 131, 131, 131, 131, 132, 132, 132, 132,
  63. 133, 133, 133, 133, 144, 73, 195};
  64. static const unsigned short dbase[32] = { /* Distance codes 0..31 base */
  65. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49,
  66. 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073,
  67. 4097, 6145, 8193, 12289, 16385, 24577, 32769, 49153};
  68. static const unsigned short dext[32] = { /* Distance codes 0..31 extra */
  69. 128, 128, 128, 128, 129, 129, 130, 130, 131, 131, 132, 132,
  70. 133, 133, 134, 134, 135, 135, 136, 136, 137, 137, 138, 138,
  71. 139, 139, 140, 140, 141, 141, 142, 142};
  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. for (sym = 0; sym < codes; sym++)
  103. count[lens[sym]]++;
  104. /* bound code lengths, force root to be within code lengths */
  105. root = *bits;
  106. for (max = MAXBITS; max >= 1; max--)
  107. if (count[max] != 0) break;
  108. if (root > max) root = max;
  109. if (max == 0) return -1; /* no codes! */
  110. for (min = 1; min <= MAXBITS; min++)
  111. if (count[min] != 0) break;
  112. if (root < min) root = min;
  113. /* check for an over-subscribed or incomplete set of lengths */
  114. left = 1;
  115. for (len = 1; len <= MAXBITS; len++) {
  116. left <<= 1;
  117. left -= count[len];
  118. if (left < 0) return -1; /* over-subscribed */
  119. }
  120. if (left > 0 && (type == CODES || max != 1))
  121. return -1; /* incomplete set */
  122. /* generate offsets into symbol table for each length for sorting */
  123. offs[1] = 0;
  124. for (len = 1; len < MAXBITS; len++)
  125. offs[len + 1] = offs[len] + count[len];
  126. /* sort symbols by length, by symbol order within each length */
  127. for (sym = 0; sym < codes; sym++)
  128. if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
  129. /*
  130. Create and fill in decoding tables. In this loop, the table being
  131. filled is at next and has curr index bits. The code being used is huff
  132. with length len. That code is converted to an index by dropping drop
  133. bits off of the bottom. For codes where len is less than drop + curr,
  134. those top drop + curr - len bits are incremented through all values to
  135. fill the table with replicated entries.
  136. root is the number of index bits for the root table. When len exceeds
  137. root, sub-tables are created pointed to by the root entry with an index
  138. of the low root bits of huff. This is saved in low to check for when a
  139. new sub-table should be started. drop is zero when the root table is
  140. being filled, and drop is root when sub-tables are being filled.
  141. When a new sub-table is needed, it is necessary to look ahead in the
  142. code lengths to determine what size sub-table is needed. The length
  143. counts are used for this, and so count[] is decremented as codes are
  144. entered in the tables.
  145. used keeps track of how many table entries have been allocated from the
  146. provided *table space. It is checked for LENS and DIST tables against
  147. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  148. the initial root table size constants. See the comments in inftree9.h
  149. for more information.
  150. sym increments through all symbols, and the loop terminates when
  151. all codes of length max, i.e. all codes, have been processed. This
  152. routine permits incomplete codes, so another loop after this one fills
  153. in the rest of the decoding tables with invalid code markers.
  154. */
  155. /* set up for code type */
  156. switch (type) {
  157. case CODES:
  158. base = extra = work; /* dummy value--not used */
  159. end = 19;
  160. break;
  161. case LENS:
  162. base = lbase;
  163. base -= 257;
  164. extra = lext;
  165. extra -= 257;
  166. end = 256;
  167. break;
  168. default: /* DISTS */
  169. base = dbase;
  170. extra = dext;
  171. end = -1;
  172. }
  173. /* initialize state for loop */
  174. huff = 0; /* starting code */
  175. sym = 0; /* starting code symbol */
  176. len = min; /* starting code length */
  177. next = *table; /* current table to fill in */
  178. curr = root; /* current table index bits */
  179. drop = 0; /* current bits to drop from code for index */
  180. low = (unsigned)(-1); /* trigger new sub-table when len > root */
  181. used = 1U << root; /* use root table entries */
  182. mask = used - 1; /* mask for comparing low */
  183. /* check available table space */
  184. if ((type == LENS && used >= ENOUGH_LENS) ||
  185. (type == DISTS && used >= ENOUGH_DISTS))
  186. return 1;
  187. /* process all codes and make table entries */
  188. for (;;) {
  189. /* create table entry */
  190. this.bits = (unsigned char)(len - drop);
  191. if ((int)(work[sym]) < end) {
  192. this.op = (unsigned char)0;
  193. this.val = work[sym];
  194. }
  195. else if ((int)(work[sym]) > end) {
  196. this.op = (unsigned char)(extra[work[sym]]);
  197. this.val = base[work[sym]];
  198. }
  199. else {
  200. this.op = (unsigned char)(32 + 64); /* end of block */
  201. this.val = 0;
  202. }
  203. /* replicate for those indices with low len bits equal to huff */
  204. incr = 1U << (len - drop);
  205. fill = 1U << curr;
  206. do {
  207. fill -= incr;
  208. next[(huff >> drop) + fill] = this;
  209. } while (fill != 0);
  210. /* backwards increment the len-bit code huff */
  211. incr = 1U << (len - 1);
  212. while (huff & incr)
  213. incr >>= 1;
  214. if (incr != 0) {
  215. huff &= incr - 1;
  216. huff += incr;
  217. }
  218. else
  219. huff = 0;
  220. /* go to next symbol, update count, len */
  221. sym++;
  222. if (--(count[len]) == 0) {
  223. if (len == max) break;
  224. len = lens[work[sym]];
  225. }
  226. /* create new sub-table if needed */
  227. if (len > root && (huff & mask) != low) {
  228. /* if first time, transition to sub-tables */
  229. if (drop == 0)
  230. drop = root;
  231. /* increment past last table */
  232. next += 1U << curr;
  233. /* determine length of next table */
  234. curr = len - drop;
  235. left = (int)(1 << curr);
  236. while (curr + drop < max) {
  237. left -= count[curr + drop];
  238. if (left <= 0) break;
  239. curr++;
  240. left <<= 1;
  241. }
  242. /* check for enough space */
  243. used += 1U << curr;
  244. if ((type == LENS && used >= ENOUGH_LENS) ||
  245. (type == DISTS && used >= ENOUGH_DISTS))
  246. return 1;
  247. /* point entry in root table to sub-table */
  248. low = huff & mask;
  249. (*table)[low].op = (unsigned char)curr;
  250. (*table)[low].bits = (unsigned char)root;
  251. (*table)[low].val = (unsigned short)(next - *table);
  252. }
  253. }
  254. /*
  255. Fill in rest of table for incomplete codes. This loop is similar to the
  256. loop above in incrementing huff for table indices. It is assumed that
  257. len is equal to curr + drop, so there is no loop needed to increment
  258. through high index bits. When the current sub-table is filled, the loop
  259. drops back to the root table to fill in any remaining entries there.
  260. */
  261. this.op = (unsigned char)64; /* invalid code marker */
  262. this.bits = (unsigned char)(len - drop);
  263. this.val = (unsigned short)0;
  264. while (huff != 0) {
  265. /* when done with sub-table, drop back to root table */
  266. if (drop != 0 && (huff & mask) != low) {
  267. drop = 0;
  268. len = root;
  269. next = *table;
  270. curr = root;
  271. this.bits = (unsigned char)len;
  272. }
  273. /* put invalid code marker in table */
  274. next[huff >> drop] = this;
  275. /* backwards increment the len-bit code huff */
  276. incr = 1U << (len - 1);
  277. while (huff & incr)
  278. incr >>= 1;
  279. if (incr != 0) {
  280. huff &= incr - 1;
  281. huff += incr;
  282. }
  283. else
  284. huff = 0;
  285. }
  286. /* set return parameters */
  287. *table += used;
  288. *bits = root;
  289. return 0;
  290. }