tigr_inflate.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "tigr_internal.h"
  2. #include <stdlib.h>
  3. #include <setjmp.h>
  4. typedef struct {
  5. unsigned bits, count;
  6. const unsigned char *in, *inend;
  7. unsigned char *out, *outend;
  8. jmp_buf jmp;
  9. unsigned litcodes[288], distcodes[32], lencodes[19];
  10. int tlit, tdist, tlen;
  11. } State;
  12. #define FAIL() longjmp(s->jmp, 1)
  13. #define CHECK(X) \
  14. if (!(X)) \
  15. FAIL()
  16. // Built-in DEFLATE standard tables.
  17. static char order[] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
  18. static char lenBits[29 + 2] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  19. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0 };
  20. static int lenBase[29 + 2] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  21. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 };
  22. static char distBits[30 + 2] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  23. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0 };
  24. static int distBase[30 + 2] = {
  25. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129,
  26. 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
  27. };
  28. // Table to bit-reverse a byte.
  29. static const unsigned char reverseTable[256] = {
  30. #define R2(n) n, n + 128, n + 64, n + 192
  31. #define R4(n) R2(n), R2(n + 32), R2(n + 16), R2(n + 48)
  32. #define R6(n) R4(n), R4(n + 8), R4(n + 4), R4(n + 12)
  33. R6(0), R6(2), R6(1), R6(3)
  34. };
  35. static unsigned rev16(unsigned n) {
  36. return (reverseTable[n & 0xff] << 8) | reverseTable[(n >> 8) & 0xff];
  37. }
  38. static int bits(State* s, int n) {
  39. int v = s->bits & ((1 << n) - 1);
  40. s->bits >>= n;
  41. s->count -= n;
  42. while (s->count < 16) {
  43. CHECK(s->in != s->inend);
  44. s->bits |= (*s->in++) << s->count;
  45. s->count += 8;
  46. }
  47. return v;
  48. }
  49. static unsigned char* emit(State* s, int len) {
  50. s->out += len;
  51. CHECK(s->out <= s->outend);
  52. return s->out - len;
  53. }
  54. static void copy(State* s, const unsigned char* src, int len) {
  55. unsigned char* dest = emit(s, len);
  56. while (len--)
  57. *dest++ = *src++;
  58. }
  59. static int build(State* s, unsigned* tree, unsigned char* lens, unsigned int symcount) {
  60. unsigned int codes[16], first[16], counts[16] = { 0 };
  61. // Frequency count.
  62. for (unsigned int n = 0; n < symcount; n++)
  63. counts[lens[n]]++;
  64. // Distribute codes.
  65. counts[0] = codes[0] = first[0] = 0;
  66. for (unsigned int n = 1; n <= 15; n++) {
  67. codes[n] = (codes[n - 1] + counts[n - 1]) << 1;
  68. first[n] = first[n - 1] + counts[n - 1];
  69. }
  70. CHECK(first[15] + counts[15] <= symcount);
  71. // Insert keys into the tree for each symbol.
  72. for (unsigned int n = 0; n < symcount; n++) {
  73. int len = lens[n];
  74. if (len != 0) {
  75. unsigned code = codes[len]++, slot = first[len]++;
  76. tree[slot] = (code << (32 - len)) | (n << 4) | len;
  77. }
  78. }
  79. return first[15];
  80. }
  81. static int decode(State* s, unsigned tree[], int max) {
  82. // Find the next prefix code.
  83. unsigned lo = 0, hi = max, key;
  84. unsigned search = (rev16(s->bits) << 16) | 0xffff;
  85. while (lo < hi) {
  86. unsigned guess = (lo + hi) / 2;
  87. if (search < tree[guess])
  88. hi = guess;
  89. else
  90. lo = guess + 1;
  91. }
  92. // Pull out the key and check it.
  93. key = tree[lo - 1];
  94. CHECK(((search ^ key) >> (32 - (key & 0xf))) == 0);
  95. bits(s, key & 0xf);
  96. return (key >> 4) & 0xfff;
  97. }
  98. static void run(State* s, int sym) {
  99. int length = bits(s, lenBits[sym]) + lenBase[sym];
  100. int dsym = decode(s, s->distcodes, s->tdist);
  101. int offs = bits(s, distBits[dsym]) + distBase[dsym];
  102. copy(s, s->out - offs, length);
  103. }
  104. static void block(State* s) {
  105. for (;;) {
  106. int sym = decode(s, s->litcodes, s->tlit);
  107. if (sym < 256)
  108. *emit(s, 1) = (unsigned char)sym;
  109. else if (sym > 256)
  110. run(s, sym - 257);
  111. else
  112. break;
  113. }
  114. }
  115. static void stored(State* s) {
  116. // Uncompressed data block.
  117. int len;
  118. bits(s, s->count & 7);
  119. len = bits(s, 16);
  120. CHECK(((len ^ s->bits) & 0xffff) == 0xffff);
  121. CHECK(s->in + len <= s->inend);
  122. copy(s, s->in, len);
  123. s->in += len;
  124. bits(s, 16);
  125. }
  126. static void fixed(State* s) {
  127. // Fixed set of Huffman codes.
  128. int n;
  129. unsigned char lens[288 + 32];
  130. for (n = 0; n <= 143; n++)
  131. lens[n] = 8;
  132. for (n = 144; n <= 255; n++)
  133. lens[n] = 9;
  134. for (n = 256; n <= 279; n++)
  135. lens[n] = 7;
  136. for (n = 280; n <= 287; n++)
  137. lens[n] = 8;
  138. for (n = 0; n < 32; n++)
  139. lens[288 + n] = 5;
  140. // Build lit/dist trees.
  141. s->tlit = build(s, s->litcodes, lens, 288);
  142. s->tdist = build(s, s->distcodes, lens + 288, 32);
  143. }
  144. static void dynamic(State* s) {
  145. int n, i, nlit, ndist, nlen;
  146. unsigned char lenlens[19] = { 0 }, lens[288 + 32];
  147. nlit = 257 + bits(s, 5);
  148. ndist = 1 + bits(s, 5);
  149. nlen = 4 + bits(s, 4);
  150. for (n = 0; n < nlen; n++)
  151. lenlens[(int) order[n]] = (unsigned char)bits(s, 3);
  152. // Build the tree for decoding code lengths.
  153. s->tlen = build(s, s->lencodes, lenlens, 19);
  154. // Decode code lengths.
  155. for (n = 0; n < nlit + ndist;) {
  156. int sym = decode(s, s->lencodes, s->tlen);
  157. switch (sym) {
  158. case 16:
  159. for (i = 3 + bits(s, 2); i; i--, n++)
  160. lens[n] = lens[n - 1];
  161. break;
  162. case 17:
  163. for (i = 3 + bits(s, 3); i; i--, n++)
  164. lens[n] = 0;
  165. break;
  166. case 18:
  167. for (i = 11 + bits(s, 7); i; i--, n++)
  168. lens[n] = 0;
  169. break;
  170. default:
  171. lens[n++] = (unsigned char)sym;
  172. break;
  173. }
  174. }
  175. // Build lit/dist trees.
  176. s->tlit = build(s, s->litcodes, lens, nlit);
  177. s->tdist = build(s, s->distcodes, lens + nlit, ndist);
  178. }
  179. int tigrInflate(void* out, unsigned outlen, const void* in, unsigned inlen) {
  180. int last;
  181. State* s = (State*)calloc(1, sizeof(State));
  182. // We assume we can buffer 2 extra bytes from off the end of 'in'.
  183. s->in = (unsigned char*)in;
  184. s->inend = s->in + inlen + 2;
  185. s->out = (unsigned char*)out;
  186. s->outend = s->out + outlen;
  187. s->bits = 0;
  188. s->count = 0;
  189. bits(s, 0);
  190. if (setjmp(s->jmp) == 1) {
  191. free(s);
  192. return 0;
  193. }
  194. do {
  195. last = bits(s, 1);
  196. switch (bits(s, 2)) {
  197. case 0:
  198. stored(s);
  199. break;
  200. case 1:
  201. fixed(s);
  202. block(s);
  203. break;
  204. case 2:
  205. dynamic(s);
  206. block(s);
  207. break;
  208. case 3:
  209. FAIL();
  210. }
  211. } while (!last);
  212. free(s);
  213. return 1;
  214. }
  215. #undef CHECK
  216. #undef FAIL