inflate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <flate.h>
  12. enum {
  13. HistorySize= 32*1024,
  14. BufSize= 4*1024,
  15. MaxHuffBits= 17, /* maximum bits in a encoded code */
  16. Nlitlen= 288, /* number of litlen codes */
  17. Noff= 32, /* number of offset codes */
  18. Nclen= 19, /* number of codelen codes */
  19. LenShift= 10, /* code = len<<LenShift|code */
  20. LitlenBits= 7, /* number of bits in litlen decode table */
  21. OffBits= 6, /* number of bits in offset decode table */
  22. ClenBits= 6, /* number of bits in code len decode table */
  23. MaxFlatBits= LitlenBits,
  24. MaxLeaf= Nlitlen
  25. };
  26. typedef struct Input Input;
  27. typedef struct History History;
  28. typedef struct Huff Huff;
  29. struct Input
  30. {
  31. int error; /* first error encountered, or FlateOk */
  32. void *wr;
  33. int (*w)(void*, void*, int);
  34. void *getr;
  35. int (*get)(void*);
  36. uint32_t sreg;
  37. int nbits;
  38. };
  39. struct History
  40. {
  41. uint8_t his[HistorySize];
  42. uint8_t *cp; /* current pointer in history */
  43. int full; /* his has been filled up at least once */
  44. };
  45. struct Huff
  46. {
  47. int maxbits; /* max bits for any code */
  48. int minbits; /* min bits to get before looking in flat */
  49. int flatmask; /* bits used in "flat" fast decoding table */
  50. uint32_t flat[1<<MaxFlatBits];
  51. uint32_t maxcode[MaxHuffBits];
  52. uint32_t last[MaxHuffBits];
  53. uint32_t decode[MaxLeaf];
  54. };
  55. /* litlen code words 257-285 extra bits */
  56. static int litlenextra[Nlitlen-257] =
  57. {
  58. /* 257 */ 0, 0, 0,
  59. /* 260 */ 0, 0, 0, 0, 0, 1, 1, 1, 1, 2,
  60. /* 270 */ 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
  61. /* 280 */ 4, 5, 5, 5, 5, 0, 0, 0
  62. };
  63. static int litlenbase[Nlitlen-257];
  64. /* offset code word extra bits */
  65. static int offextra[Noff] =
  66. {
  67. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
  68. 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
  69. 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
  70. 0, 0,
  71. };
  72. static int offbase[Noff];
  73. /* order code lengths */
  74. static int clenorder[Nclen] =
  75. {
  76. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  77. };
  78. /* for static huffman tables */
  79. static Huff litlentab;
  80. static Huff offtab;
  81. static uint8_t revtab[256];
  82. static int uncblock(Input *in, History*);
  83. static int fixedblock(Input *in, History*);
  84. static int dynamicblock(Input *in, History*);
  85. static int sregfill(Input *in, int n);
  86. static int sregunget(Input *in);
  87. static int decode(Input*, History*, Huff*, Huff*);
  88. static int hufftab(Huff*, char*, int, int);
  89. static int hdecsym(Input *in, Huff *h, int b);
  90. int
  91. inflateinit(void)
  92. {
  93. char *len;
  94. int i, j, base;
  95. /* byte reverse table */
  96. for(i=0; i<256; i++)
  97. for(j=0; j<8; j++)
  98. if(i & (1<<j))
  99. revtab[i] |= 0x80 >> j;
  100. for(i=257,base=3; i<Nlitlen; i++) {
  101. litlenbase[i-257] = base;
  102. base += 1<<litlenextra[i-257];
  103. }
  104. /* strange table entry in spec... */
  105. litlenbase[285-257]--;
  106. for(i=0,base=1; i<Noff; i++) {
  107. offbase[i] = base;
  108. base += 1<<offextra[i];
  109. }
  110. len = malloc(MaxLeaf);
  111. if(len == nil)
  112. return FlateNoMem;
  113. /* static Litlen bit lengths */
  114. for(i=0; i<144; i++)
  115. len[i] = 8;
  116. for(i=144; i<256; i++)
  117. len[i] = 9;
  118. for(i=256; i<280; i++)
  119. len[i] = 7;
  120. for(i=280; i<Nlitlen; i++)
  121. len[i] = 8;
  122. if(!hufftab(&litlentab, len, Nlitlen, MaxFlatBits))
  123. return FlateInternal;
  124. /* static Offset bit lengths */
  125. for(i=0; i<Noff; i++)
  126. len[i] = 5;
  127. if(!hufftab(&offtab, len, Noff, MaxFlatBits))
  128. return FlateInternal;
  129. free(len);
  130. return FlateOk;
  131. }
  132. int
  133. inflate(void *wr, int (*w)(void*, void*, int), void *getr, int (*get)(void*))
  134. {
  135. History *his;
  136. Input in;
  137. int final, type;
  138. his = malloc(sizeof(History));
  139. if(his == nil)
  140. return FlateNoMem;
  141. his->cp = his->his;
  142. his->full = 0;
  143. in.getr = getr;
  144. in.get = get;
  145. in.wr = wr;
  146. in.w = w;
  147. in.nbits = 0;
  148. in.sreg = 0;
  149. in.error = FlateOk;
  150. do {
  151. if(!sregfill(&in, 3))
  152. goto bad;
  153. final = in.sreg & 0x1;
  154. type = (in.sreg>>1) & 0x3;
  155. in.sreg >>= 3;
  156. in.nbits -= 3;
  157. switch(type) {
  158. default:
  159. in.error = FlateCorrupted;
  160. goto bad;
  161. case 0:
  162. /* uncompressed */
  163. if(!uncblock(&in, his))
  164. goto bad;
  165. break;
  166. case 1:
  167. /* fixed huffman */
  168. if(!fixedblock(&in, his))
  169. goto bad;
  170. break;
  171. case 2:
  172. /* dynamic huffman */
  173. if(!dynamicblock(&in, his))
  174. goto bad;
  175. break;
  176. }
  177. } while(!final);
  178. if(his->cp != his->his && (*w)(wr, his->his, his->cp - his->his) != his->cp - his->his) {
  179. in.error = FlateOutputFail;
  180. goto bad;
  181. }
  182. if(!sregunget(&in))
  183. goto bad;
  184. free(his);
  185. if(in.error != FlateOk)
  186. return FlateInternal;
  187. return FlateOk;
  188. bad:
  189. free(his);
  190. if(in.error == FlateOk)
  191. return FlateInternal;
  192. return in.error;
  193. }
  194. static int
  195. uncblock(Input *in, History *his)
  196. {
  197. int len, nlen, c;
  198. uint8_t *hs, *hp, *he;
  199. if(!sregunget(in))
  200. return 0;
  201. len = (*in->get)(in->getr);
  202. len |= (*in->get)(in->getr)<<8;
  203. nlen = (*in->get)(in->getr);
  204. nlen |= (*in->get)(in->getr)<<8;
  205. if(len != (~nlen&0xffff)) {
  206. in->error = FlateCorrupted;
  207. return 0;
  208. }
  209. hp = his->cp;
  210. hs = his->his;
  211. he = hs + HistorySize;
  212. while(len > 0) {
  213. c = (*in->get)(in->getr);
  214. if(c < 0)
  215. return 0;
  216. *hp++ = c;
  217. if(hp == he) {
  218. his->full = 1;
  219. if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
  220. in->error = FlateOutputFail;
  221. return 0;
  222. }
  223. hp = hs;
  224. }
  225. len--;
  226. }
  227. his->cp = hp;
  228. return 1;
  229. }
  230. static int
  231. fixedblock(Input *in, History *his)
  232. {
  233. return decode(in, his, &litlentab, &offtab);
  234. }
  235. static int
  236. dynamicblock(Input *in, History *his)
  237. {
  238. Huff *lentab, *offtab;
  239. char *len;
  240. int i, j, n, c, nlit, ndist, nclen, res, nb;
  241. if(!sregfill(in, 14))
  242. return 0;
  243. nlit = (in->sreg&0x1f) + 257;
  244. ndist = ((in->sreg>>5) & 0x1f) + 1;
  245. nclen = ((in->sreg>>10) & 0xf) + 4;
  246. in->sreg >>= 14;
  247. in->nbits -= 14;
  248. if(nlit > Nlitlen || ndist > Noff || nlit < 257) {
  249. in->error = FlateCorrupted;
  250. return 0;
  251. }
  252. /* huff table header */
  253. len = malloc(Nlitlen+Noff);
  254. lentab = malloc(sizeof(Huff));
  255. offtab = malloc(sizeof(Huff));
  256. if(len == nil || lentab == nil || offtab == nil){
  257. in->error = FlateNoMem;
  258. goto bad;
  259. }
  260. for(i=0; i < Nclen; i++)
  261. len[i] = 0;
  262. for(i=0; i<nclen; i++) {
  263. if(!sregfill(in, 3))
  264. goto bad;
  265. len[clenorder[i]] = in->sreg & 0x7;
  266. in->sreg >>= 3;
  267. in->nbits -= 3;
  268. }
  269. if(!hufftab(lentab, len, Nclen, ClenBits)){
  270. in->error = FlateCorrupted;
  271. goto bad;
  272. }
  273. n = nlit+ndist;
  274. for(i=0; i<n;) {
  275. nb = lentab->minbits;
  276. for(;;){
  277. if(in->nbits<nb && !sregfill(in, nb))
  278. goto bad;
  279. c = lentab->flat[in->sreg & lentab->flatmask];
  280. nb = c & 0xff;
  281. if(nb > in->nbits){
  282. if(nb != 0xff)
  283. continue;
  284. c = hdecsym(in, lentab, c);
  285. if(c < 0)
  286. goto bad;
  287. }else{
  288. c >>= 8;
  289. in->sreg >>= nb;
  290. in->nbits -= nb;
  291. }
  292. break;
  293. }
  294. if(c < 16) {
  295. j = 1;
  296. } else if(c == 16) {
  297. if(in->nbits<2 && !sregfill(in, 2))
  298. goto bad;
  299. j = (in->sreg&0x3)+3;
  300. in->sreg >>= 2;
  301. in->nbits -= 2;
  302. if(i == 0) {
  303. in->error = FlateCorrupted;
  304. goto bad;
  305. }
  306. c = len[i-1];
  307. } else if(c == 17) {
  308. if(in->nbits<3 && !sregfill(in, 3))
  309. goto bad;
  310. j = (in->sreg&0x7)+3;
  311. in->sreg >>= 3;
  312. in->nbits -= 3;
  313. c = 0;
  314. } else if(c == 18) {
  315. if(in->nbits<7 && !sregfill(in, 7))
  316. goto bad;
  317. j = (in->sreg&0x7f)+11;
  318. in->sreg >>= 7;
  319. in->nbits -= 7;
  320. c = 0;
  321. } else {
  322. in->error = FlateCorrupted;
  323. goto bad;
  324. }
  325. if(i+j > n) {
  326. in->error = FlateCorrupted;
  327. goto bad;
  328. }
  329. while(j) {
  330. len[i] = c;
  331. i++;
  332. j--;
  333. }
  334. }
  335. if(!hufftab(lentab, len, nlit, LitlenBits)
  336. || !hufftab(offtab, &len[nlit], ndist, OffBits)){
  337. in->error = FlateCorrupted;
  338. goto bad;
  339. }
  340. res = decode(in, his, lentab, offtab);
  341. free(len);
  342. free(lentab);
  343. free(offtab);
  344. return res;
  345. bad:
  346. free(len);
  347. free(lentab);
  348. free(offtab);
  349. return 0;
  350. }
  351. static int
  352. decode(Input *in, History *his, Huff *litlentab, Huff *offtab)
  353. {
  354. int len, off;
  355. uint8_t *hs, *hp, *hq, *he;
  356. int c;
  357. int nb;
  358. hs = his->his;
  359. he = hs + HistorySize;
  360. hp = his->cp;
  361. for(;;) {
  362. nb = litlentab->minbits;
  363. for(;;){
  364. if(in->nbits<nb && !sregfill(in, nb))
  365. return 0;
  366. c = litlentab->flat[in->sreg & litlentab->flatmask];
  367. nb = c & 0xff;
  368. if(nb > in->nbits){
  369. if(nb != 0xff)
  370. continue;
  371. c = hdecsym(in, litlentab, c);
  372. if(c < 0)
  373. return 0;
  374. }else{
  375. c >>= 8;
  376. in->sreg >>= nb;
  377. in->nbits -= nb;
  378. }
  379. break;
  380. }
  381. if(c < 256) {
  382. /* literal */
  383. *hp++ = c;
  384. if(hp == he) {
  385. his->full = 1;
  386. if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
  387. in->error = FlateOutputFail;
  388. return 0;
  389. }
  390. hp = hs;
  391. }
  392. continue;
  393. }
  394. if(c == 256)
  395. break;
  396. if(c > 285) {
  397. in->error = FlateCorrupted;
  398. return 0;
  399. }
  400. c -= 257;
  401. nb = litlenextra[c];
  402. if(in->nbits < nb && !sregfill(in, nb))
  403. return 0;
  404. len = litlenbase[c] + (in->sreg & ((1<<nb)-1));
  405. in->sreg >>= nb;
  406. in->nbits -= nb;
  407. /* get offset */
  408. nb = offtab->minbits;
  409. for(;;){
  410. if(in->nbits<nb && !sregfill(in, nb))
  411. return 0;
  412. c = offtab->flat[in->sreg & offtab->flatmask];
  413. nb = c & 0xff;
  414. if(nb > in->nbits){
  415. if(nb != 0xff)
  416. continue;
  417. c = hdecsym(in, offtab, c);
  418. if(c < 0)
  419. return 0;
  420. }else{
  421. c >>= 8;
  422. in->sreg >>= nb;
  423. in->nbits -= nb;
  424. }
  425. break;
  426. }
  427. if(c > 29) {
  428. in->error = FlateCorrupted;
  429. return 0;
  430. }
  431. nb = offextra[c];
  432. if(in->nbits < nb && !sregfill(in, nb))
  433. return 0;
  434. off = offbase[c] + (in->sreg & ((1<<nb)-1));
  435. in->sreg >>= nb;
  436. in->nbits -= nb;
  437. hq = hp - off;
  438. if(hq < hs) {
  439. if(!his->full) {
  440. in->error = FlateCorrupted;
  441. return 0;
  442. }
  443. hq += HistorySize;
  444. }
  445. /* slow but correct */
  446. while(len) {
  447. *hp = *hq;
  448. hq++;
  449. hp++;
  450. if(hq >= he)
  451. hq = hs;
  452. if(hp == he) {
  453. his->full = 1;
  454. if((*in->w)(in->wr, hs, HistorySize) != HistorySize) {
  455. in->error = FlateOutputFail;
  456. return 0;
  457. }
  458. hp = hs;
  459. }
  460. len--;
  461. }
  462. }
  463. his->cp = hp;
  464. return 1;
  465. }
  466. static int
  467. revcode(int c, int b)
  468. {
  469. /* shift encode up so it starts on bit 15 then reverse */
  470. c <<= (16-b);
  471. c = revtab[c>>8] | (revtab[c&0xff]<<8);
  472. return c;
  473. }
  474. /*
  475. * construct the huffman decoding arrays and a fast lookup table.
  476. * the fast lookup is a table indexed by the next flatbits bits,
  477. * which returns the symbol matched and the number of bits consumed,
  478. * or the minimum number of bits needed and 0xff if more than flatbits
  479. * bits are needed.
  480. *
  481. * flatbits can be longer than the smallest huffman code,
  482. * because shorter codes are assigned smaller lexical prefixes.
  483. * this means assuming zeros for the next few bits will give a
  484. * conservative answer, in the sense that it will either give the
  485. * correct answer, or return the minimum number of bits which
  486. * are needed for an answer.
  487. */
  488. static int
  489. hufftab(Huff *h, char *hb, int maxleaf, int flatbits)
  490. {
  491. uint32_t bitcount[MaxHuffBits];
  492. uint32_t c, fc, ec, mincode, code, nc[MaxHuffBits];
  493. int i, b, minbits, maxbits;
  494. for(i = 0; i < MaxHuffBits; i++)
  495. bitcount[i] = 0;
  496. maxbits = -1;
  497. minbits = MaxHuffBits + 1;
  498. for(i=0; i < maxleaf; i++){
  499. b = hb[i];
  500. if(b){
  501. bitcount[b]++;
  502. if(b < minbits)
  503. minbits = b;
  504. if(b > maxbits)
  505. maxbits = b;
  506. }
  507. }
  508. h->maxbits = maxbits;
  509. if(maxbits <= 0){
  510. h->maxbits = 0;
  511. h->minbits = 0;
  512. h->flatmask = 0;
  513. return 1;
  514. }
  515. code = 0;
  516. c = 0;
  517. for(b = 0; b <= maxbits; b++){
  518. h->last[b] = c;
  519. c += bitcount[b];
  520. mincode = code << 1;
  521. nc[b] = mincode;
  522. code = mincode + bitcount[b];
  523. if(code > (1 << b))
  524. return 0;
  525. h->maxcode[b] = code - 1;
  526. h->last[b] += code - 1;
  527. }
  528. if(flatbits > maxbits)
  529. flatbits = maxbits;
  530. h->flatmask = (1 << flatbits) - 1;
  531. if(minbits > flatbits)
  532. minbits = flatbits;
  533. h->minbits = minbits;
  534. b = 1 << flatbits;
  535. for(i = 0; i < b; i++)
  536. h->flat[i] = ~0;
  537. /*
  538. * initialize the flat table to include the minimum possible
  539. * bit length for each code prefix
  540. */
  541. for(b = maxbits; b > flatbits; b--){
  542. code = h->maxcode[b];
  543. if(code == -1)
  544. break;
  545. mincode = code + 1 - bitcount[b];
  546. mincode >>= b - flatbits;
  547. code >>= b - flatbits;
  548. for(; mincode <= code; mincode++)
  549. h->flat[revcode(mincode, flatbits)] = (b << 8) | 0xff;
  550. }
  551. for(i = 0; i < maxleaf; i++){
  552. b = hb[i];
  553. if(b <= 0)
  554. continue;
  555. c = nc[b]++;
  556. if(b <= flatbits){
  557. code = (i << 8) | b;
  558. ec = (c + 1) << (flatbits - b);
  559. if(ec > (1<<flatbits))
  560. return 0; /* this is actually an internal error */
  561. for(fc = c << (flatbits - b); fc < ec; fc++)
  562. h->flat[revcode(fc, flatbits)] = code;
  563. }
  564. if(b > minbits){
  565. c = h->last[b] - c;
  566. if(c >= maxleaf)
  567. return 0;
  568. h->decode[c] = i;
  569. }
  570. }
  571. return 1;
  572. }
  573. static int
  574. hdecsym(Input *in, Huff *h, int nb)
  575. {
  576. int32_t c;
  577. if((nb & 0xff) == 0xff)
  578. nb = nb >> 8;
  579. else
  580. nb = nb & 0xff;
  581. for(; nb <= h->maxbits; nb++){
  582. if(in->nbits<nb && !sregfill(in, nb))
  583. return -1;
  584. c = revtab[in->sreg&0xff]<<8;
  585. c |= revtab[(in->sreg>>8)&0xff];
  586. c >>= (16-nb);
  587. if(c <= h->maxcode[nb]){
  588. in->sreg >>= nb;
  589. in->nbits -= nb;
  590. return h->decode[h->last[nb] - c];
  591. }
  592. }
  593. in->error = FlateCorrupted;
  594. return -1;
  595. }
  596. static int
  597. sregfill(Input *in, int n)
  598. {
  599. int c;
  600. while(n > in->nbits) {
  601. c = (*in->get)(in->getr);
  602. if(c < 0){
  603. in->error = FlateInputFail;
  604. return 0;
  605. }
  606. in->sreg |= c<<in->nbits;
  607. in->nbits += 8;
  608. }
  609. return 1;
  610. }
  611. static int
  612. sregunget(Input *in)
  613. {
  614. if(in->nbits >= 8) {
  615. in->error = FlateInternal;
  616. return 0;
  617. }
  618. /* throw other bits on the floor */
  619. in->nbits = 0;
  620. in->sreg = 0;
  621. return 1;
  622. }