puff.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * puff.c
  3. * Copyright (C) 2002-2010 Mark Adler
  4. * For conditions of distribution and use, see copyright notice in puff.h
  5. * version 2.1, 4 Apr 2010
  6. *
  7. * puff.c is a simple inflate written to be an unambiguous way to specify the
  8. * deflate format. It is not written for speed but rather simplicity. As a
  9. * side benefit, this code might actually be useful when small code is more
  10. * important than speed, such as bootstrap applications. For typical deflate
  11. * data, zlib's inflate() is about four times as fast as puff(). zlib's
  12. * inflate compiles to around 20K on my machine, whereas puff.c compiles to
  13. * around 4K on my machine (a PowerPC using GNU cc). If the faster decode()
  14. * function here is used, then puff() is only twice as slow as zlib's
  15. * inflate().
  16. *
  17. * All dynamically allocated memory comes from the stack. The stack required
  18. * is less than 2K bytes. This code is compatible with 16-bit int's and
  19. * assumes that long's are at least 32 bits. puff.c uses the short data type,
  20. * assumed to be 16 bits, for arrays in order to to conserve memory. The code
  21. * works whether integers are stored big endian or little endian.
  22. *
  23. * In the comments below are "Format notes" that describe the inflate process
  24. * and document some of the less obvious aspects of the format. This source
  25. * code is meant to supplement RFC 1951, which formally describes the deflate
  26. * format:
  27. *
  28. * http://www.zlib.org/rfc-deflate.html
  29. */
  30. /*
  31. * Change history:
  32. *
  33. * 1.0 10 Feb 2002 - First version
  34. * 1.1 17 Feb 2002 - Clarifications of some comments and notes
  35. * - Update puff() dest and source pointers on negative
  36. * errors to facilitate debugging deflators
  37. * - Remove longest from struct huffman -- not needed
  38. * - Simplify offs[] index in construct()
  39. * - Add input size and checking, using longjmp() to
  40. * maintain easy readability
  41. * - Use short data type for large arrays
  42. * - Use pointers instead of long to specify source and
  43. * destination sizes to avoid arbitrary 4 GB limits
  44. * 1.2 17 Mar 2002 - Add faster version of decode(), doubles speed (!),
  45. * but leave simple version for readabilty
  46. * - Make sure invalid distances detected if pointers
  47. * are 16 bits
  48. * - Fix fixed codes table error
  49. * - Provide a scanning mode for determining size of
  50. * uncompressed data
  51. * 1.3 20 Mar 2002 - Go back to lengths for puff() parameters [Jean-loup]
  52. * - Add a puff.h file for the interface
  53. * - Add braces in puff() for else do [Jean-loup]
  54. * - Use indexes instead of pointers for readability
  55. * 1.4 31 Mar 2002 - Simplify construct() code set check
  56. * - Fix some comments
  57. * - Add FIXLCODES #define
  58. * 1.5 6 Apr 2002 - Minor comment fixes
  59. * 1.6 7 Aug 2002 - Minor format changes
  60. * 1.7 3 Mar 2003 - Added test code for distribution
  61. * - Added zlib-like license
  62. * 1.8 9 Jan 2004 - Added some comments on no distance codes case
  63. * 1.9 21 Feb 2008 - Fix bug on 16-bit integer architectures [Pohland]
  64. * - Catch missing end-of-block symbol error
  65. * 2.0 25 Jul 2008 - Add #define to permit distance too far back
  66. * - Add option in TEST code for puff to write the data
  67. * - Add option in TEST code to skip input bytes
  68. * - Allow TEST code to read from piped stdin
  69. * 2.1 4 Apr 2010 - Avoid variable initialization for happier compilers
  70. * - Avoid unsigned comparisons for even happier compilers
  71. */
  72. #include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
  73. #include "puff.h" /* prototype for puff() */
  74. #define local static /* for local function definitions */
  75. #define NIL ((unsigned char *)0) /* for no output option */
  76. /*
  77. * Maximums for allocations and loops. It is not useful to change these --
  78. * they are fixed by the deflate format.
  79. */
  80. #define MAXBITS 15 /* maximum bits in a code */
  81. #define MAXLCODES 286 /* maximum number of literal/length codes */
  82. #define MAXDCODES 30 /* maximum number of distance codes */
  83. #define MAXCODES (MAXLCODES+MAXDCODES) /* maximum codes lengths to read */
  84. #define FIXLCODES 288 /* number of fixed literal/length codes */
  85. /* input and output state */
  86. struct state {
  87. /* output state */
  88. unsigned char *out; /* output buffer */
  89. unsigned long outlen; /* available space at out */
  90. unsigned long outcnt; /* bytes written to out so far */
  91. /* input state */
  92. unsigned char *in; /* input buffer */
  93. unsigned long inlen; /* available input at in */
  94. unsigned long incnt; /* bytes read so far */
  95. int bitbuf; /* bit buffer */
  96. int bitcnt; /* number of bits in bit buffer */
  97. /* input limit error return state for bits() and decode() */
  98. jmp_buf env;
  99. };
  100. /*
  101. * Return need bits from the input stream. This always leaves less than
  102. * eight bits in the buffer. bits() works properly for need == 0.
  103. *
  104. * Format notes:
  105. *
  106. * - Bits are stored in bytes from the least significant bit to the most
  107. * significant bit. Therefore bits are dropped from the bottom of the bit
  108. * buffer, using shift right, and new bytes are appended to the top of the
  109. * bit buffer, using shift left.
  110. */
  111. local int bits(struct state *s, int need)
  112. {
  113. long val; /* bit accumulator (can use up to 20 bits) */
  114. /* load at least need bits into val */
  115. val = s->bitbuf;
  116. while (s->bitcnt < need) {
  117. if (s->incnt == s->inlen) longjmp(s->env, 1); /* out of input */
  118. val |= (long)(s->in[s->incnt++]) << s->bitcnt; /* load eight bits */
  119. s->bitcnt += 8;
  120. }
  121. /* drop need bits and update buffer, always zero to seven bits left */
  122. s->bitbuf = (int)(val >> need);
  123. s->bitcnt -= need;
  124. /* return need bits, zeroing the bits above that */
  125. return (int)(val & ((1L << need) - 1));
  126. }
  127. /*
  128. * Process a stored block.
  129. *
  130. * Format notes:
  131. *
  132. * - After the two-bit stored block type (00), the stored block length and
  133. * stored bytes are byte-aligned for fast copying. Therefore any leftover
  134. * bits in the byte that has the last bit of the type, as many as seven, are
  135. * discarded. The value of the discarded bits are not defined and should not
  136. * be checked against any expectation.
  137. *
  138. * - The second inverted copy of the stored block length does not have to be
  139. * checked, but it's probably a good idea to do so anyway.
  140. *
  141. * - A stored block can have zero length. This is sometimes used to byte-align
  142. * subsets of the compressed data for random access or partial recovery.
  143. */
  144. local int stored(struct state *s)
  145. {
  146. unsigned len; /* length of stored block */
  147. /* discard leftover bits from current byte (assumes s->bitcnt < 8) */
  148. s->bitbuf = 0;
  149. s->bitcnt = 0;
  150. /* get length and check against its one's complement */
  151. if (s->incnt + 4 > s->inlen) return 2; /* not enough input */
  152. len = s->in[s->incnt++];
  153. len |= s->in[s->incnt++] << 8;
  154. if (s->in[s->incnt++] != (~len & 0xff) ||
  155. s->in[s->incnt++] != ((~len >> 8) & 0xff))
  156. return -2; /* didn't match complement! */
  157. /* copy len bytes from in to out */
  158. if (s->incnt + len > s->inlen) return 2; /* not enough input */
  159. if (s->out != NIL) {
  160. if (s->outcnt + len > s->outlen)
  161. return 1; /* not enough output space */
  162. while (len--)
  163. s->out[s->outcnt++] = s->in[s->incnt++];
  164. }
  165. else { /* just scanning */
  166. s->outcnt += len;
  167. s->incnt += len;
  168. }
  169. /* done with a valid stored block */
  170. return 0;
  171. }
  172. /*
  173. * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
  174. * each length, which for a canonical code are stepped through in order.
  175. * symbol[] are the symbol values in canonical order, where the number of
  176. * entries is the sum of the counts in count[]. The decoding process can be
  177. * seen in the function decode() below.
  178. */
  179. struct huffman {
  180. short *count; /* number of symbols of each length */
  181. short *symbol; /* canonically ordered symbols */
  182. };
  183. /*
  184. * Decode a code from the stream s using huffman table h. Return the symbol or
  185. * a negative value if there is an error. If all of the lengths are zero, i.e.
  186. * an empty code, or if the code is incomplete and an invalid code is received,
  187. * then -10 is returned after reading MAXBITS bits.
  188. *
  189. * Format notes:
  190. *
  191. * - The codes as stored in the compressed data are bit-reversed relative to
  192. * a simple integer ordering of codes of the same lengths. Hence below the
  193. * bits are pulled from the compressed data one at a time and used to
  194. * build the code value reversed from what is in the stream in order to
  195. * permit simple integer comparisons for decoding. A table-based decoding
  196. * scheme (as used in zlib) does not need to do this reversal.
  197. *
  198. * - The first code for the shortest length is all zeros. Subsequent codes of
  199. * the same length are simply integer increments of the previous code. When
  200. * moving up a length, a zero bit is appended to the code. For a complete
  201. * code, the last code of the longest length will be all ones.
  202. *
  203. * - Incomplete codes are handled by this decoder, since they are permitted
  204. * in the deflate format. See the format notes for fixed() and dynamic().
  205. */
  206. #ifdef SLOW
  207. local int decode(struct state *s, struct huffman *h)
  208. {
  209. int len; /* current number of bits in code */
  210. int code; /* len bits being decoded */
  211. int first; /* first code of length len */
  212. int count; /* number of codes of length len */
  213. int index; /* index of first code of length len in symbol table */
  214. code = first = index = 0;
  215. for (len = 1; len <= MAXBITS; len++) {
  216. code |= bits(s, 1); /* get next bit */
  217. count = h->count[len];
  218. if (code - count < first) /* if length len, return symbol */
  219. return h->symbol[index + (code - first)];
  220. index += count; /* else update for next length */
  221. first += count;
  222. first <<= 1;
  223. code <<= 1;
  224. }
  225. return -10; /* ran out of codes */
  226. }
  227. /*
  228. * A faster version of decode() for real applications of this code. It's not
  229. * as readable, but it makes puff() twice as fast. And it only makes the code
  230. * a few percent larger.
  231. */
  232. #else /* !SLOW */
  233. local int decode(struct state *s, struct huffman *h)
  234. {
  235. int len; /* current number of bits in code */
  236. int code; /* len bits being decoded */
  237. int first; /* first code of length len */
  238. int count; /* number of codes of length len */
  239. int index; /* index of first code of length len in symbol table */
  240. int bitbuf; /* bits from stream */
  241. int left; /* bits left in next or left to process */
  242. short *next; /* next number of codes */
  243. bitbuf = s->bitbuf;
  244. left = s->bitcnt;
  245. code = first = index = 0;
  246. len = 1;
  247. next = h->count + 1;
  248. while (1) {
  249. while (left--) {
  250. code |= bitbuf & 1;
  251. bitbuf >>= 1;
  252. count = *next++;
  253. if (code - count < first) { /* if length len, return symbol */
  254. s->bitbuf = bitbuf;
  255. s->bitcnt = (s->bitcnt - len) & 7;
  256. return h->symbol[index + (code - first)];
  257. }
  258. index += count; /* else update for next length */
  259. first += count;
  260. first <<= 1;
  261. code <<= 1;
  262. len++;
  263. }
  264. left = (MAXBITS+1) - len;
  265. if (left == 0) break;
  266. if (s->incnt == s->inlen) longjmp(s->env, 1); /* out of input */
  267. bitbuf = s->in[s->incnt++];
  268. if (left > 8) left = 8;
  269. }
  270. return -10; /* ran out of codes */
  271. }
  272. #endif /* SLOW */
  273. /*
  274. * Given the list of code lengths length[0..n-1] representing a canonical
  275. * Huffman code for n symbols, construct the tables required to decode those
  276. * codes. Those tables are the number of codes of each length, and the symbols
  277. * sorted by length, retaining their original order within each length. The
  278. * return value is zero for a complete code set, negative for an over-
  279. * subscribed code set, and positive for an incomplete code set. The tables
  280. * can be used if the return value is zero or positive, but they cannot be used
  281. * if the return value is negative. If the return value is zero, it is not
  282. * possible for decode() using that table to return an error--any stream of
  283. * enough bits will resolve to a symbol. If the return value is positive, then
  284. * it is possible for decode() using that table to return an error for received
  285. * codes past the end of the incomplete lengths.
  286. *
  287. * Not used by decode(), but used for error checking, h->count[0] is the number
  288. * of the n symbols not in the code. So n - h->count[0] is the number of
  289. * codes. This is useful for checking for incomplete codes that have more than
  290. * one symbol, which is an error in a dynamic block.
  291. *
  292. * Assumption: for all i in 0..n-1, 0 <= length[i] <= MAXBITS
  293. * This is assured by the construction of the length arrays in dynamic() and
  294. * fixed() and is not verified by construct().
  295. *
  296. * Format notes:
  297. *
  298. * - Permitted and expected examples of incomplete codes are one of the fixed
  299. * codes and any code with a single symbol which in deflate is coded as one
  300. * bit instead of zero bits. See the format notes for fixed() and dynamic().
  301. *
  302. * - Within a given code length, the symbols are kept in ascending order for
  303. * the code bits definition.
  304. */
  305. local int construct(struct huffman *h, short *length, int n)
  306. {
  307. int symbol; /* current symbol when stepping through length[] */
  308. int len; /* current length when stepping through h->count[] */
  309. int left; /* number of possible codes left of current length */
  310. short offs[MAXBITS+1]; /* offsets in symbol table for each length */
  311. /* count number of codes of each length */
  312. for (len = 0; len <= MAXBITS; len++)
  313. h->count[len] = 0;
  314. for (symbol = 0; symbol < n; symbol++)
  315. (h->count[length[symbol]])++; /* assumes lengths are within bounds */
  316. if (h->count[0] == n) /* no codes! */
  317. return 0; /* complete, but decode() will fail */
  318. /* check for an over-subscribed or incomplete set of lengths */
  319. left = 1; /* one possible code of zero length */
  320. for (len = 1; len <= MAXBITS; len++) {
  321. left <<= 1; /* one more bit, double codes left */
  322. left -= h->count[len]; /* deduct count from possible codes */
  323. if (left < 0) return left; /* over-subscribed--return negative */
  324. } /* left > 0 means incomplete */
  325. /* generate offsets into symbol table for each length for sorting */
  326. offs[1] = 0;
  327. for (len = 1; len < MAXBITS; len++)
  328. offs[len + 1] = offs[len] + h->count[len];
  329. /*
  330. * put symbols in table sorted by length, by symbol order within each
  331. * length
  332. */
  333. for (symbol = 0; symbol < n; symbol++)
  334. if (length[symbol] != 0)
  335. h->symbol[offs[length[symbol]]++] = symbol;
  336. /* return zero for complete set, positive for incomplete set */
  337. return left;
  338. }
  339. /*
  340. * Decode literal/length and distance codes until an end-of-block code.
  341. *
  342. * Format notes:
  343. *
  344. * - Compressed data that is after the block type if fixed or after the code
  345. * description if dynamic is a combination of literals and length/distance
  346. * pairs terminated by and end-of-block code. Literals are simply Huffman
  347. * coded bytes. A length/distance pair is a coded length followed by a
  348. * coded distance to represent a string that occurs earlier in the
  349. * uncompressed data that occurs again at the current location.
  350. *
  351. * - Literals, lengths, and the end-of-block code are combined into a single
  352. * code of up to 286 symbols. They are 256 literals (0..255), 29 length
  353. * symbols (257..285), and the end-of-block symbol (256).
  354. *
  355. * - There are 256 possible lengths (3..258), and so 29 symbols are not enough
  356. * to represent all of those. Lengths 3..10 and 258 are in fact represented
  357. * by just a length symbol. Lengths 11..257 are represented as a symbol and
  358. * some number of extra bits that are added as an integer to the base length
  359. * of the length symbol. The number of extra bits is determined by the base
  360. * length symbol. These are in the static arrays below, lens[] for the base
  361. * lengths and lext[] for the corresponding number of extra bits.
  362. *
  363. * - The reason that 258 gets its own symbol is that the longest length is used
  364. * often in highly redundant files. Note that 258 can also be coded as the
  365. * base value 227 plus the maximum extra value of 31. While a good deflate
  366. * should never do this, it is not an error, and should be decoded properly.
  367. *
  368. * - If a length is decoded, including its extra bits if any, then it is
  369. * followed a distance code. There are up to 30 distance symbols. Again
  370. * there are many more possible distances (1..32768), so extra bits are added
  371. * to a base value represented by the symbol. The distances 1..4 get their
  372. * own symbol, but the rest require extra bits. The base distances and
  373. * corresponding number of extra bits are below in the static arrays dist[]
  374. * and dext[].
  375. *
  376. * - Literal bytes are simply written to the output. A length/distance pair is
  377. * an instruction to copy previously uncompressed bytes to the output. The
  378. * copy is from distance bytes back in the output stream, copying for length
  379. * bytes.
  380. *
  381. * - Distances pointing before the beginning of the output data are not
  382. * permitted.
  383. *
  384. * - Overlapped copies, where the length is greater than the distance, are
  385. * allowed and common. For example, a distance of one and a length of 258
  386. * simply copies the last byte 258 times. A distance of four and a length of
  387. * twelve copies the last four bytes three times. A simple forward copy
  388. * ignoring whether the length is greater than the distance or not implements
  389. * this correctly. You should not use memcpy() since its behavior is not
  390. * defined for overlapped arrays. You should not use memmove() or bcopy()
  391. * since though their behavior -is- defined for overlapping arrays, it is
  392. * defined to do the wrong thing in this case.
  393. */
  394. local int codes(struct state *s,
  395. struct huffman *lencode,
  396. struct huffman *distcode)
  397. {
  398. int symbol; /* decoded symbol */
  399. int len; /* length for copy */
  400. unsigned dist; /* distance for copy */
  401. static const short lens[29] = { /* Size base for length codes 257..285 */
  402. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  403. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
  404. static const short lext[29] = { /* Extra bits for length codes 257..285 */
  405. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  406. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
  407. static const short dists[30] = { /* Offset base for distance codes 0..29 */
  408. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  409. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  410. 8193, 12289, 16385, 24577};
  411. static const short dext[30] = { /* Extra bits for distance codes 0..29 */
  412. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  413. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  414. 12, 12, 13, 13};
  415. /* decode literals and length/distance pairs */
  416. do {
  417. symbol = decode(s, lencode);
  418. if (symbol < 0) return symbol; /* invalid symbol */
  419. if (symbol < 256) { /* literal: symbol is the byte */
  420. /* write out the literal */
  421. if (s->out != NIL) {
  422. if (s->outcnt == s->outlen) return 1;
  423. s->out[s->outcnt] = symbol;
  424. }
  425. s->outcnt++;
  426. }
  427. else if (symbol > 256) { /* length */
  428. /* get and compute length */
  429. symbol -= 257;
  430. if (symbol >= 29) return -10; /* invalid fixed code */
  431. len = lens[symbol] + bits(s, lext[symbol]);
  432. /* get and check distance */
  433. symbol = decode(s, distcode);
  434. if (symbol < 0) return symbol; /* invalid symbol */
  435. dist = dists[symbol] + bits(s, dext[symbol]);
  436. #ifndef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  437. if (dist > s->outcnt)
  438. return -11; /* distance too far back */
  439. #endif
  440. /* copy length bytes from distance bytes back */
  441. if (s->out != NIL) {
  442. if (s->outcnt + len > s->outlen) return 1;
  443. while (len--) {
  444. s->out[s->outcnt] =
  445. #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  446. dist > s->outcnt ? 0 :
  447. #endif
  448. s->out[s->outcnt - dist];
  449. s->outcnt++;
  450. }
  451. }
  452. else
  453. s->outcnt += len;
  454. }
  455. } while (symbol != 256); /* end of block symbol */
  456. /* done with a valid fixed or dynamic block */
  457. return 0;
  458. }
  459. /*
  460. * Process a fixed codes block.
  461. *
  462. * Format notes:
  463. *
  464. * - This block type can be useful for compressing small amounts of data for
  465. * which the size of the code descriptions in a dynamic block exceeds the
  466. * benefit of custom codes for that block. For fixed codes, no bits are
  467. * spent on code descriptions. Instead the code lengths for literal/length
  468. * codes and distance codes are fixed. The specific lengths for each symbol
  469. * can be seen in the "for" loops below.
  470. *
  471. * - The literal/length code is complete, but has two symbols that are invalid
  472. * and should result in an error if received. This cannot be implemented
  473. * simply as an incomplete code since those two symbols are in the "middle"
  474. * of the code. They are eight bits long and the longest literal/length\
  475. * code is nine bits. Therefore the code must be constructed with those
  476. * symbols, and the invalid symbols must be detected after decoding.
  477. *
  478. * - The fixed distance codes also have two invalid symbols that should result
  479. * in an error if received. Since all of the distance codes are the same
  480. * length, this can be implemented as an incomplete code. Then the invalid
  481. * codes are detected while decoding.
  482. */
  483. local int fixed(struct state *s)
  484. {
  485. static int virgin = 1;
  486. static short lencnt[MAXBITS+1], lensym[FIXLCODES];
  487. static short distcnt[MAXBITS+1], distsym[MAXDCODES];
  488. static struct huffman lencode, distcode;
  489. /* build fixed huffman tables if first call (may not be thread safe) */
  490. if (virgin) {
  491. int symbol;
  492. short lengths[FIXLCODES];
  493. /* literal/length table */
  494. for (symbol = 0; symbol < 144; symbol++)
  495. lengths[symbol] = 8;
  496. for (; symbol < 256; symbol++)
  497. lengths[symbol] = 9;
  498. for (; symbol < 280; symbol++)
  499. lengths[symbol] = 7;
  500. for (; symbol < FIXLCODES; symbol++)
  501. lengths[symbol] = 8;
  502. construct(&lencode, lengths, FIXLCODES);
  503. /* distance table */
  504. for (symbol = 0; symbol < MAXDCODES; symbol++)
  505. lengths[symbol] = 5;
  506. construct(&distcode, lengths, MAXDCODES);
  507. /* construct lencode and distcode */
  508. lencode.count = lencnt;
  509. lencode.symbol = lensym;
  510. distcode.count = distcnt;
  511. distcode.symbol = distsym;
  512. /* do this just once */
  513. virgin = 0;
  514. }
  515. /* decode data until end-of-block code */
  516. return codes(s, &lencode, &distcode);
  517. }
  518. /*
  519. * Process a dynamic codes block.
  520. *
  521. * Format notes:
  522. *
  523. * - A dynamic block starts with a description of the literal/length and
  524. * distance codes for that block. New dynamic blocks allow the compressor to
  525. * rapidly adapt to changing data with new codes optimized for that data.
  526. *
  527. * - The codes used by the deflate format are "canonical", which means that
  528. * the actual bits of the codes are generated in an unambiguous way simply
  529. * from the number of bits in each code. Therefore the code descriptions
  530. * are simply a list of code lengths for each symbol.
  531. *
  532. * - The code lengths are stored in order for the symbols, so lengths are
  533. * provided for each of the literal/length symbols, and for each of the
  534. * distance symbols.
  535. *
  536. * - If a symbol is not used in the block, this is represented by a zero as
  537. * as the code length. This does not mean a zero-length code, but rather
  538. * that no code should be created for this symbol. There is no way in the
  539. * deflate format to represent a zero-length code.
  540. *
  541. * - The maximum number of bits in a code is 15, so the possible lengths for
  542. * any code are 1..15.
  543. *
  544. * - The fact that a length of zero is not permitted for a code has an
  545. * interesting consequence. Normally if only one symbol is used for a given
  546. * code, then in fact that code could be represented with zero bits. However
  547. * in deflate, that code has to be at least one bit. So for example, if
  548. * only a single distance base symbol appears in a block, then it will be
  549. * represented by a single code of length one, in particular one 0 bit. This
  550. * is an incomplete code, since if a 1 bit is received, it has no meaning,
  551. * and should result in an error. So incomplete distance codes of one symbol
  552. * should be permitted, and the receipt of invalid codes should be handled.
  553. *
  554. * - It is also possible to have a single literal/length code, but that code
  555. * must be the end-of-block code, since every dynamic block has one. This
  556. * is not the most efficient way to create an empty block (an empty fixed
  557. * block is fewer bits), but it is allowed by the format. So incomplete
  558. * literal/length codes of one symbol should also be permitted.
  559. *
  560. * - If there are only literal codes and no lengths, then there are no distance
  561. * codes. This is represented by one distance code with zero bits.
  562. *
  563. * - The list of up to 286 length/literal lengths and up to 30 distance lengths
  564. * are themselves compressed using Huffman codes and run-length encoding. In
  565. * the list of code lengths, a 0 symbol means no code, a 1..15 symbol means
  566. * that length, and the symbols 16, 17, and 18 are run-length instructions.
  567. * Each of 16, 17, and 18 are follwed by extra bits to define the length of
  568. * the run. 16 copies the last length 3 to 6 times. 17 represents 3 to 10
  569. * zero lengths, and 18 represents 11 to 138 zero lengths. Unused symbols
  570. * are common, hence the special coding for zero lengths.
  571. *
  572. * - The symbols for 0..18 are Huffman coded, and so that code must be
  573. * described first. This is simply a sequence of up to 19 three-bit values
  574. * representing no code (0) or the code length for that symbol (1..7).
  575. *
  576. * - A dynamic block starts with three fixed-size counts from which is computed
  577. * the number of literal/length code lengths, the number of distance code
  578. * lengths, and the number of code length code lengths (ok, you come up with
  579. * a better name!) in the code descriptions. For the literal/length and
  580. * distance codes, lengths after those provided are considered zero, i.e. no
  581. * code. The code length code lengths are received in a permuted order (see
  582. * the order[] array below) to make a short code length code length list more
  583. * likely. As it turns out, very short and very long codes are less likely
  584. * to be seen in a dynamic code description, hence what may appear initially
  585. * to be a peculiar ordering.
  586. *
  587. * - Given the number of literal/length code lengths (nlen) and distance code
  588. * lengths (ndist), then they are treated as one long list of nlen + ndist
  589. * code lengths. Therefore run-length coding can and often does cross the
  590. * boundary between the two sets of lengths.
  591. *
  592. * - So to summarize, the code description at the start of a dynamic block is
  593. * three counts for the number of code lengths for the literal/length codes,
  594. * the distance codes, and the code length codes. This is followed by the
  595. * code length code lengths, three bits each. This is used to construct the
  596. * code length code which is used to read the remainder of the lengths. Then
  597. * the literal/length code lengths and distance lengths are read as a single
  598. * set of lengths using the code length codes. Codes are constructed from
  599. * the resulting two sets of lengths, and then finally you can start
  600. * decoding actual compressed data in the block.
  601. *
  602. * - For reference, a "typical" size for the code description in a dynamic
  603. * block is around 80 bytes.
  604. */
  605. local int dynamic(struct state *s)
  606. {
  607. int nlen, ndist, ncode; /* number of lengths in descriptor */
  608. int index; /* index of lengths[] */
  609. int err; /* construct() return value */
  610. short lengths[MAXCODES]; /* descriptor code lengths */
  611. short lencnt[MAXBITS+1], lensym[MAXLCODES]; /* lencode memory */
  612. short distcnt[MAXBITS+1], distsym[MAXDCODES]; /* distcode memory */
  613. struct huffman lencode, distcode; /* length and distance codes */
  614. static const short order[19] = /* permutation of code length codes */
  615. {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  616. /* construct lencode and distcode */
  617. lencode.count = lencnt;
  618. lencode.symbol = lensym;
  619. distcode.count = distcnt;
  620. distcode.symbol = distsym;
  621. /* get number of lengths in each table, check lengths */
  622. nlen = bits(s, 5) + 257;
  623. ndist = bits(s, 5) + 1;
  624. ncode = bits(s, 4) + 4;
  625. if (nlen > MAXLCODES || ndist > MAXDCODES)
  626. return -3; /* bad counts */
  627. /* read code length code lengths (really), missing lengths are zero */
  628. for (index = 0; index < ncode; index++)
  629. lengths[order[index]] = bits(s, 3);
  630. for (; index < 19; index++)
  631. lengths[order[index]] = 0;
  632. /* build huffman table for code lengths codes (use lencode temporarily) */
  633. err = construct(&lencode, lengths, 19);
  634. if (err != 0) return -4; /* require complete code set here */
  635. /* read length/literal and distance code length tables */
  636. index = 0;
  637. while (index < nlen + ndist) {
  638. int symbol; /* decoded value */
  639. int len; /* last length to repeat */
  640. symbol = decode(s, &lencode);
  641. if (symbol < 16) /* length in 0..15 */
  642. lengths[index++] = symbol;
  643. else { /* repeat instruction */
  644. len = 0; /* assume repeating zeros */
  645. if (symbol == 16) { /* repeat last length 3..6 times */
  646. if (index == 0) return -5; /* no last length! */
  647. len = lengths[index - 1]; /* last length */
  648. symbol = 3 + bits(s, 2);
  649. }
  650. else if (symbol == 17) /* repeat zero 3..10 times */
  651. symbol = 3 + bits(s, 3);
  652. else /* == 18, repeat zero 11..138 times */
  653. symbol = 11 + bits(s, 7);
  654. if (index + symbol > nlen + ndist)
  655. return -6; /* too many lengths! */
  656. while (symbol--) /* repeat last or zero symbol times */
  657. lengths[index++] = len;
  658. }
  659. }
  660. /* check for end-of-block code -- there better be one! */
  661. if (lengths[256] == 0)
  662. return -9;
  663. /* build huffman table for literal/length codes */
  664. err = construct(&lencode, lengths, nlen);
  665. if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
  666. return -7; /* only allow incomplete codes if just one code */
  667. /* build huffman table for distance codes */
  668. err = construct(&distcode, lengths + nlen, ndist);
  669. if (err < 0 || (err > 0 && ndist - distcode.count[0] != 1))
  670. return -8; /* only allow incomplete codes if just one code */
  671. /* decode data until end-of-block code */
  672. return codes(s, &lencode, &distcode);
  673. }
  674. /*
  675. * Inflate source to dest. On return, destlen and sourcelen are updated to the
  676. * size of the uncompressed data and the size of the deflate data respectively.
  677. * On success, the return value of puff() is zero. If there is an error in the
  678. * source data, i.e. it is not in the deflate format, then a negative value is
  679. * returned. If there is not enough input available or there is not enough
  680. * output space, then a positive error is returned. In that case, destlen and
  681. * sourcelen are not updated to facilitate retrying from the beginning with the
  682. * provision of more input data or more output space. In the case of invalid
  683. * inflate data (a negative error), the dest and source pointers are updated to
  684. * facilitate the debugging of deflators.
  685. *
  686. * puff() also has a mode to determine the size of the uncompressed output with
  687. * no output written. For this dest must be (unsigned char *)0. In this case,
  688. * the input value of *destlen is ignored, and on return *destlen is set to the
  689. * size of the uncompressed output.
  690. *
  691. * The return codes are:
  692. *
  693. * 2: available inflate data did not terminate
  694. * 1: output space exhausted before completing inflate
  695. * 0: successful inflate
  696. * -1: invalid block type (type == 3)
  697. * -2: stored block length did not match one's complement
  698. * -3: dynamic block code description: too many length or distance codes
  699. * -4: dynamic block code description: code lengths codes incomplete
  700. * -5: dynamic block code description: repeat lengths with no first length
  701. * -6: dynamic block code description: repeat more than specified lengths
  702. * -7: dynamic block code description: invalid literal/length code lengths
  703. * -8: dynamic block code description: invalid distance code lengths
  704. * -9: dynamic block code description: missing end-of-block code
  705. * -10: invalid literal/length or distance code in fixed or dynamic block
  706. * -11: distance is too far back in fixed or dynamic block
  707. *
  708. * Format notes:
  709. *
  710. * - Three bits are read for each block to determine the kind of block and
  711. * whether or not it is the last block. Then the block is decoded and the
  712. * process repeated if it was not the last block.
  713. *
  714. * - The leftover bits in the last byte of the deflate data after the last
  715. * block (if it was a fixed or dynamic block) are undefined and have no
  716. * expected values to check.
  717. */
  718. int puff(unsigned char *dest, /* pointer to destination pointer */
  719. unsigned long *destlen, /* amount of output space */
  720. unsigned char *source, /* pointer to source data pointer */
  721. unsigned long *sourcelen) /* amount of input available */
  722. {
  723. struct state s; /* input/output state */
  724. int last, type; /* block information */
  725. int err; /* return value */
  726. /* initialize output state */
  727. s.out = dest;
  728. s.outlen = *destlen; /* ignored if dest is NIL */
  729. s.outcnt = 0;
  730. /* initialize input state */
  731. s.in = source;
  732. s.inlen = *sourcelen;
  733. s.incnt = 0;
  734. s.bitbuf = 0;
  735. s.bitcnt = 0;
  736. /* return if bits() or decode() tries to read past available input */
  737. if (setjmp(s.env) != 0) /* if came back here via longjmp() */
  738. err = 2; /* then skip do-loop, return error */
  739. else {
  740. /* process blocks until last block or error */
  741. do {
  742. last = bits(&s, 1); /* one if last block */
  743. type = bits(&s, 2); /* block type 0..3 */
  744. err = type == 0 ? stored(&s) :
  745. (type == 1 ? fixed(&s) :
  746. (type == 2 ? dynamic(&s) :
  747. -1)); /* type == 3, invalid */
  748. if (err != 0) break; /* return with error */
  749. } while (!last);
  750. }
  751. /* update the lengths and return */
  752. if (err <= 0) {
  753. *destlen = s.outcnt;
  754. *sourcelen = s.incnt;
  755. }
  756. return err;
  757. }
  758. #ifdef TEST
  759. /* Examples of how to use puff().
  760. Usage: puff [-w] [-nnn] file
  761. ... | puff [-w] [-nnn]
  762. where file is the input file with deflate data, nnn is the number of bytes
  763. of input to skip before inflating (e.g. to skip a zlib or gzip header), and
  764. -w is used to write the decompressed data to stdout */
  765. #include <stdio.h>
  766. #include <stdlib.h>
  767. /* Return size times approximately the cube root of 2, keeping the result as 1,
  768. 3, or 5 times a power of 2 -- the result is always > size, until the result
  769. is the maximum value of an unsigned long, where it remains. This is useful
  770. to keep reallocations less than ~33% over the actual data. */
  771. local size_t bythirds(size_t size)
  772. {
  773. int n;
  774. size_t m;
  775. m = size;
  776. for (n = 0; m; n++)
  777. m >>= 1;
  778. if (n < 3)
  779. return size + 1;
  780. n -= 3;
  781. m = size >> n;
  782. m += m == 6 ? 2 : 1;
  783. m <<= n;
  784. return m > size ? m : (size_t)(-1);
  785. }
  786. /* Read the input file *name, or stdin if name is NULL, into allocated memory.
  787. Reallocate to larger buffers until the entire file is read in. Return a
  788. pointer to the allocated data, or NULL if there was a memory allocation
  789. failure. *len is the number of bytes of data read from the input file (even
  790. if load() returns NULL). If the input file was empty or could not be opened
  791. or read, *len is zero. */
  792. local void *load(char *name, size_t *len)
  793. {
  794. size_t size;
  795. void *buf, *swap;
  796. FILE *in;
  797. *len = 0;
  798. buf = malloc(size = 4096);
  799. if (buf == NULL)
  800. return NULL;
  801. in = name == NULL ? stdin : fopen(name, "rb");
  802. if (in != NULL) {
  803. for (;;) {
  804. *len += fread((char *)buf + *len, 1, size - *len, in);
  805. if (*len < size) break;
  806. size = bythirds(size);
  807. if (size == *len || (swap = realloc(buf, size)) == NULL) {
  808. free(buf);
  809. buf = NULL;
  810. break;
  811. }
  812. buf = swap;
  813. }
  814. fclose(in);
  815. }
  816. return buf;
  817. }
  818. int main(int argc, char **argv)
  819. {
  820. int ret, put = 0;
  821. unsigned skip = 0;
  822. char *arg, *name = NULL;
  823. unsigned char *source = NULL, *dest;
  824. size_t len = 0;
  825. unsigned long sourcelen, destlen;
  826. /* process arguments */
  827. while (arg = *++argv, --argc)
  828. if (arg[0] == '-') {
  829. if (arg[1] == 'w' && arg[2] == 0)
  830. put = 1;
  831. else if (arg[1] >= '0' && arg[1] <= '9')
  832. skip = (unsigned)atoi(arg + 1);
  833. else {
  834. fprintf(stderr, "invalid option %s\n", arg);
  835. return 3;
  836. }
  837. }
  838. else if (name != NULL) {
  839. fprintf(stderr, "only one file name allowed\n");
  840. return 3;
  841. }
  842. else
  843. name = arg;
  844. source = load(name, &len);
  845. if (source == NULL) {
  846. fprintf(stderr, "memory allocation failure\n");
  847. return 4;
  848. }
  849. if (len == 0) {
  850. fprintf(stderr, "could not read %s, or it was empty\n",
  851. name == NULL ? "<stdin>" : name);
  852. free(source);
  853. return 3;
  854. }
  855. if (skip >= len) {
  856. fprintf(stderr, "skip request of %d leaves no input\n", skip);
  857. free(source);
  858. return 3;
  859. }
  860. /* test inflate data with offset skip */
  861. len -= skip;
  862. sourcelen = (unsigned long)len;
  863. ret = puff(NIL, &destlen, source + skip, &sourcelen);
  864. if (ret)
  865. fprintf(stderr, "puff() failed with return code %d\n", ret);
  866. else {
  867. fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen);
  868. if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n",
  869. len - sourcelen);
  870. }
  871. /* if requested, inflate again and write decompressd data to stdout */
  872. if (put) {
  873. dest = malloc(destlen);
  874. if (dest == NULL) {
  875. fprintf(stderr, "memory allocation failure\n");
  876. free(source);
  877. return 4;
  878. }
  879. puff(dest, &destlen, source + skip, &sourcelen);
  880. fwrite(dest, 1, destlen, stdout);
  881. free(dest);
  882. }
  883. /* clean up */
  884. free(source);
  885. return ret;
  886. }
  887. #endif