unbzip.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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 <bio.h>
  12. #include "bzfs.h"
  13. /*
  14. * THIS FILE IS NOT IDENTICAL TO THE ORIGINAL
  15. * FROM THE BZIP2 DISTRIBUTION.
  16. *
  17. * It has been modified, mainly to break the library
  18. * into smaller pieces.
  19. *
  20. * Russ Cox
  21. * rsc@plan9.bell-labs.com
  22. * July 2000
  23. */
  24. /*---------------------------------------------*/
  25. /*--
  26. Place a 1 beside your platform, and 0 elsewhere.
  27. Attempts to autosniff this even if you don't.
  28. --*/
  29. /*--
  30. Plan 9 from Bell Labs
  31. --*/
  32. #define BZ_PLAN9 1
  33. #define BZ_UNIX 0
  34. #define exit(x) exits((x) ? "whoops" : nil)
  35. #define size_t ulong
  36. #ifdef __GNUC__
  37. # define NORETURN __attribute__ ((noreturn))
  38. #else
  39. # define NORETURN /**/
  40. #endif
  41. /*--
  42. Some more stuff for all platforms :-)
  43. This might have to get moved into the platform-specific
  44. header files if we encounter a machine with different sizes.
  45. --*/
  46. typedef char Char;
  47. typedef unsigned char Bool;
  48. typedef unsigned char UChar;
  49. typedef int Int32;
  50. typedef unsigned int UInt32;
  51. typedef short Int16;
  52. typedef unsigned short UInt16;
  53. #define True ((Bool)1)
  54. #define False ((Bool)0)
  55. /*--
  56. IntNative is your platform's `native' int size.
  57. Only here to avoid probs with 64-bit platforms.
  58. --*/
  59. typedef int IntNative;
  60. #include "bzfs.h"
  61. #include "bzlib.h"
  62. #include "bzlib_private.h"
  63. static int
  64. bunzip(int ofd, char *ofile, Biobuf *bin)
  65. {
  66. int e, n, done, onemore;
  67. char buf[8192];
  68. char obuf[8192];
  69. Biobuf bout;
  70. bz_stream strm;
  71. USED(ofile);
  72. memset(&strm, 0, sizeof strm);
  73. BZ2_bzDecompressInit(&strm, 0, 0);
  74. strm.next_in = buf;
  75. strm.avail_in = 0;
  76. strm.next_out = obuf;
  77. strm.avail_out = sizeof obuf;
  78. done = 0;
  79. Binit(&bout, ofd, OWRITE);
  80. /*
  81. * onemore is a crummy hack to go 'round the loop
  82. * once after we finish, to flush the output buffer.
  83. */
  84. onemore = 1;
  85. SET(e);
  86. do {
  87. if(!done && strm.avail_in < sizeof buf) {
  88. if(strm.avail_in)
  89. memmove(buf, strm.next_in, strm.avail_in);
  90. n = Bread(bin, buf+strm.avail_in, sizeof(buf)-strm.avail_in);
  91. if(n <= 0)
  92. done = 1;
  93. else
  94. strm.avail_in += n;
  95. strm.next_in = buf;
  96. }
  97. if(strm.avail_out < sizeof obuf) {
  98. Bwrite(&bout, obuf, sizeof(obuf)-strm.avail_out);
  99. strm.next_out = obuf;
  100. strm.avail_out = sizeof obuf;
  101. }
  102. if(onemore == 0)
  103. break;
  104. } while((e=BZ2_bzDecompress(&strm)) == BZ_OK || onemore--);
  105. if(e != BZ_STREAM_END) {
  106. fprint(2, "bunzip2: decompress failed\n");
  107. return 0;
  108. }
  109. if(BZ2_bzDecompressEnd(&strm) != BZ_OK) {
  110. fprint(2, "bunzip2: decompress end failed (can't happen)\n");
  111. return 0;
  112. }
  113. Bterm(&bout);
  114. return 1;
  115. }
  116. void
  117. _unbzip(int in, int out)
  118. {
  119. Biobuf bin;
  120. Binit(&bin, in, OREAD);
  121. if(bunzip(out, nil, &bin) != 1) {
  122. fprint(2, "bunzip2 failed\n");
  123. _exits("bunzip2");
  124. }
  125. }
  126. int
  127. unbzip(int in)
  128. {
  129. int rv, out, p[2];
  130. if(pipe(p) < 0)
  131. sysfatal("pipe: %r");
  132. rv = p[0];
  133. out = p[1];
  134. switch(rfork(RFPROC|RFFDG|RFNOTEG|RFMEM)){
  135. case -1:
  136. sysfatal("fork: %r");
  137. case 0:
  138. close(rv);
  139. break;
  140. default:
  141. close(in);
  142. close(out);
  143. return rv;
  144. }
  145. _unbzip(in, out);
  146. _exits(0);
  147. return -1; /* not reached */
  148. }
  149. int bz_config_ok ( void )
  150. {
  151. if (sizeof(int) != 4) return 0;
  152. if (sizeof(short) != 2) return 0;
  153. if (sizeof(char) != 1) return 0;
  154. return 1;
  155. }
  156. void* default_bzalloc(void *o, int items, int size)
  157. {
  158. USED(o);
  159. return sbrk(items*size);
  160. }
  161. void default_bzfree(void*, void*)
  162. {
  163. }
  164. void
  165. bz_internal_error(int)
  166. {
  167. abort();
  168. }
  169. /*-------------------------------------------------------------*/
  170. /*--- Decompression machinery ---*/
  171. /*--- decompress.c ---*/
  172. /*-------------------------------------------------------------*/
  173. /*--
  174. This file is a part of bzip2 and/or libbzip2, a program and
  175. library for lossless, block-sorting data compression.
  176. Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
  177. Redistribution and use in source and binary forms, with or without
  178. modification, are permitted provided that the following conditions
  179. are met:
  180. 1. Redistributions of source code must retain the above copyright
  181. notice, this list of conditions and the following disclaimer.
  182. 2. The origin of this software must not be misrepresented; you must
  183. not claim that you wrote the original software. If you use this
  184. software in a product, an acknowledgment in the product
  185. documentation would be appreciated but is not required.
  186. 3. Altered source versions must be plainly marked as such, and must
  187. not be misrepresented as being the original software.
  188. 4. The name of the author may not be used to endorse or promote
  189. products derived from this software without specific prior written
  190. permission.
  191. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  192. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  193. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  194. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  195. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  196. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  197. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  198. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  199. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  200. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  201. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  202. Julian Seward, Cambridge, UK.
  203. jseward@acm.org
  204. bzip2/libbzip2 version 1.0 of 21 March 2000
  205. This program is based on (at least) the work of:
  206. Mike Burrows
  207. David Wheeler
  208. Peter Fenwick
  209. Alistair Moffat
  210. Radford Neal
  211. Ian H. Witten
  212. Robert Sedgewick
  213. Jon L. Bentley
  214. For more information on these sources, see the manual.
  215. --*/
  216. /*---------------------------------------------------*/
  217. static
  218. void makeMaps_d ( DState* s )
  219. {
  220. Int32 i;
  221. s->nInUse = 0;
  222. for (i = 0; i < 256; i++)
  223. if (s->inUse[i]) {
  224. s->seqToUnseq[s->nInUse] = i;
  225. s->nInUse++;
  226. }
  227. }
  228. /*---------------------------------------------------*/
  229. #define RETURN(rrr) \
  230. { retVal = rrr; goto save_state_and_return; };
  231. #define GET_BITS(lll,vvv,nnn) \
  232. case lll: \
  233. { int x; if((retVal = getbits(s, lll, &x, nnn)) != 99) \
  234. goto save_state_and_return; vvv=x; }\
  235. int
  236. getbits(DState *s, int lll, int *vvv, int nnn)
  237. {
  238. s->state = lll;
  239. for(;;) {
  240. if (s->bsLive >= nnn) {
  241. UInt32 v;
  242. v = (s->bsBuff >>
  243. (s->bsLive-nnn)) & ((1 << nnn)-1);
  244. s->bsLive -= nnn;
  245. *vvv = v;
  246. return 99;
  247. }
  248. if (s->strm->avail_in == 0) return BZ_OK;
  249. s->bsBuff
  250. = (s->bsBuff << 8) |
  251. ((UInt32)
  252. (*((UChar*)(s->strm->next_in))));
  253. s->bsLive += 8;
  254. s->strm->next_in++;
  255. s->strm->avail_in--;
  256. s->strm->total_in_lo32++;
  257. if (s->strm->total_in_lo32 == 0)
  258. s->strm->total_in_hi32++;
  259. }
  260. }
  261. #define GET_UCHAR(lll,uuu) \
  262. GET_BITS(lll,uuu,8)
  263. #define GET_BIT(lll,uuu) \
  264. GET_BITS(lll,uuu,1)
  265. /*---------------------------------------------------*/
  266. #define GET_MTF_VAL(label1,label2,lval) \
  267. { \
  268. if (groupPos == 0) { \
  269. groupNo++; \
  270. if (groupNo >= nSelectors) \
  271. RETURN(BZ_DATA_ERROR); \
  272. groupPos = BZ_G_SIZE; \
  273. gSel = s->selector[groupNo]; \
  274. gMinlen = s->minLens[gSel]; \
  275. gLimit = &(s->limit[gSel][0]); \
  276. gPerm = &(s->perm[gSel][0]); \
  277. gBase = &(s->base[gSel][0]); \
  278. } \
  279. groupPos--; \
  280. zn = gMinlen; \
  281. GET_BITS(label1, zvec, zn); \
  282. while (1) { \
  283. if (zn > 20 /* the longest code */) \
  284. RETURN(BZ_DATA_ERROR); \
  285. if (zvec <= gLimit[zn]) break; \
  286. zn++; \
  287. GET_BIT(label2, zj); \
  288. zvec = (zvec << 1) | zj; \
  289. }; \
  290. if (zvec - gBase[zn] < 0 \
  291. || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \
  292. RETURN(BZ_DATA_ERROR); \
  293. lval = gPerm[zvec - gBase[zn]]; \
  294. }
  295. /*---------------------------------------------------*/
  296. Int32 BZ2_decompress ( DState* s )
  297. {
  298. UChar uc;
  299. Int32 retVal;
  300. Int32 minLen, maxLen;
  301. bz_stream* strm = s->strm;
  302. /* stuff that needs to be saved/restored */
  303. Int32 i;
  304. Int32 j;
  305. Int32 t;
  306. Int32 alphaSize;
  307. Int32 nGroups;
  308. Int32 nSelectors;
  309. Int32 EOB;
  310. Int32 groupNo;
  311. Int32 groupPos;
  312. Int32 nextSym;
  313. Int32 nblockMAX;
  314. Int32 nblock;
  315. Int32 es;
  316. Int32 N;
  317. Int32 curr;
  318. Int32 zt;
  319. Int32 zn;
  320. Int32 zvec;
  321. Int32 zj;
  322. Int32 gSel;
  323. Int32 gMinlen;
  324. Int32* gLimit;
  325. Int32* gBase;
  326. Int32* gPerm;
  327. if (s->state == BZ_X_MAGIC_1) {
  328. /*initialise the save area*/
  329. s->save_i = 0;
  330. s->save_j = 0;
  331. s->save_t = 0;
  332. s->save_alphaSize = 0;
  333. s->save_nGroups = 0;
  334. s->save_nSelectors = 0;
  335. s->save_EOB = 0;
  336. s->save_groupNo = 0;
  337. s->save_groupPos = 0;
  338. s->save_nextSym = 0;
  339. s->save_nblockMAX = 0;
  340. s->save_nblock = 0;
  341. s->save_es = 0;
  342. s->save_N = 0;
  343. s->save_curr = 0;
  344. s->save_zt = 0;
  345. s->save_zn = 0;
  346. s->save_zvec = 0;
  347. s->save_zj = 0;
  348. s->save_gSel = 0;
  349. s->save_gMinlen = 0;
  350. s->save_gLimit = NULL;
  351. s->save_gBase = NULL;
  352. s->save_gPerm = NULL;
  353. }
  354. /*restore from the save area*/
  355. i = s->save_i;
  356. j = s->save_j;
  357. t = s->save_t;
  358. alphaSize = s->save_alphaSize;
  359. nGroups = s->save_nGroups;
  360. nSelectors = s->save_nSelectors;
  361. EOB = s->save_EOB;
  362. groupNo = s->save_groupNo;
  363. groupPos = s->save_groupPos;
  364. nextSym = s->save_nextSym;
  365. nblockMAX = s->save_nblockMAX;
  366. nblock = s->save_nblock;
  367. es = s->save_es;
  368. N = s->save_N;
  369. curr = s->save_curr;
  370. zt = s->save_zt;
  371. zn = s->save_zn;
  372. zvec = s->save_zvec;
  373. zj = s->save_zj;
  374. gSel = s->save_gSel;
  375. gMinlen = s->save_gMinlen;
  376. gLimit = s->save_gLimit;
  377. gBase = s->save_gBase;
  378. gPerm = s->save_gPerm;
  379. retVal = BZ_OK;
  380. switch (s->state) {
  381. GET_UCHAR(BZ_X_MAGIC_1, uc);
  382. if (uc != 'B') RETURN(BZ_DATA_ERROR_MAGIC);
  383. GET_UCHAR(BZ_X_MAGIC_2, uc);
  384. if (uc != 'Z') RETURN(BZ_DATA_ERROR_MAGIC);
  385. GET_UCHAR(BZ_X_MAGIC_3, uc)
  386. if (uc != 'h') RETURN(BZ_DATA_ERROR_MAGIC);
  387. GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8)
  388. if (s->blockSize100k < '1' ||
  389. s->blockSize100k > '9') RETURN(BZ_DATA_ERROR_MAGIC);
  390. s->blockSize100k -= '0';
  391. if (0 && s->smallDecompress) {
  392. s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
  393. s->ll4 = BZALLOC(
  394. ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar)
  395. );
  396. if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
  397. } else {
  398. s->tt = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
  399. if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
  400. }
  401. GET_UCHAR(BZ_X_BLKHDR_1, uc);
  402. if (uc == 0x17) goto endhdr_2;
  403. if (uc != 0x31) RETURN(BZ_DATA_ERROR);
  404. GET_UCHAR(BZ_X_BLKHDR_2, uc);
  405. if (uc != 0x41) RETURN(BZ_DATA_ERROR);
  406. GET_UCHAR(BZ_X_BLKHDR_3, uc);
  407. if (uc != 0x59) RETURN(BZ_DATA_ERROR);
  408. GET_UCHAR(BZ_X_BLKHDR_4, uc);
  409. if (uc != 0x26) RETURN(BZ_DATA_ERROR);
  410. GET_UCHAR(BZ_X_BLKHDR_5, uc);
  411. if (uc != 0x53) RETURN(BZ_DATA_ERROR);
  412. GET_UCHAR(BZ_X_BLKHDR_6, uc);
  413. if (uc != 0x59) RETURN(BZ_DATA_ERROR);
  414. s->currBlockNo++;
  415. // if (s->verbosity >= 2)
  416. // VPrintf1 ( "\n [%d: huff+mtf ", s->currBlockNo );
  417. s->storedBlockCRC = 0;
  418. GET_UCHAR(BZ_X_BCRC_1, uc);
  419. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  420. GET_UCHAR(BZ_X_BCRC_2, uc);
  421. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  422. GET_UCHAR(BZ_X_BCRC_3, uc);
  423. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  424. GET_UCHAR(BZ_X_BCRC_4, uc);
  425. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  426. GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);
  427. s->origPtr = 0;
  428. GET_UCHAR(BZ_X_ORIGPTR_1, uc);
  429. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  430. GET_UCHAR(BZ_X_ORIGPTR_2, uc);
  431. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  432. GET_UCHAR(BZ_X_ORIGPTR_3, uc);
  433. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  434. if (s->origPtr < 0)
  435. RETURN(BZ_DATA_ERROR);
  436. if (s->origPtr > 10 + 100000*s->blockSize100k)
  437. RETURN(BZ_DATA_ERROR);
  438. /*--- Receive the mapping table ---*/
  439. for (i = 0; i < 16; i++) {
  440. GET_BIT(BZ_X_MAPPING_1, uc);
  441. if (uc == 1)
  442. s->inUse16[i] = True; else
  443. s->inUse16[i] = False;
  444. }
  445. for (i = 0; i < 256; i++) s->inUse[i] = False;
  446. for (i = 0; i < 16; i++)
  447. if (s->inUse16[i])
  448. for (j = 0; j < 16; j++) {
  449. GET_BIT(BZ_X_MAPPING_2, uc);
  450. if (uc == 1) s->inUse[i * 16 + j] = True;
  451. }
  452. makeMaps_d ( s );
  453. if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
  454. alphaSize = s->nInUse+2;
  455. /*--- Now the selectors ---*/
  456. GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
  457. if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
  458. GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
  459. if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
  460. for (i = 0; i < nSelectors; i++) {
  461. j = 0;
  462. while (True) {
  463. GET_BIT(BZ_X_SELECTOR_3, uc);
  464. if (uc == 0) break;
  465. j++;
  466. if (j >= nGroups) RETURN(BZ_DATA_ERROR);
  467. }
  468. s->selectorMtf[i] = j;
  469. }
  470. /*--- Undo the MTF values for the selectors. ---*/
  471. {
  472. UChar pos[BZ_N_GROUPS], tmp, v;
  473. for (v = 0; v < nGroups; v++) pos[v] = v;
  474. for (i = 0; i < nSelectors; i++) {
  475. v = s->selectorMtf[i];
  476. tmp = pos[v];
  477. while (v > 0) { pos[v] = pos[v-1]; v--; }
  478. pos[0] = tmp;
  479. s->selector[i] = tmp;
  480. }
  481. }
  482. /*--- Now the coding tables ---*/
  483. for (t = 0; t < nGroups; t++) {
  484. GET_BITS(BZ_X_CODING_1, curr, 5);
  485. for (i = 0; i < alphaSize; i++) {
  486. while (True) {
  487. if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR);
  488. GET_BIT(BZ_X_CODING_2, uc);
  489. if (uc == 0) break;
  490. GET_BIT(BZ_X_CODING_3, uc);
  491. if (uc == 0) curr++; else curr--;
  492. }
  493. s->len[t][i] = curr;
  494. }
  495. }
  496. /*--- Create the Huffman decoding tables ---*/
  497. for (t = 0; t < nGroups; t++) {
  498. minLen = 32;
  499. maxLen = 0;
  500. for (i = 0; i < alphaSize; i++) {
  501. if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  502. if (s->len[t][i] < minLen) minLen = s->len[t][i];
  503. }
  504. BZ2_hbCreateDecodeTables (
  505. &(s->limit[t][0]),
  506. &(s->base[t][0]),
  507. &(s->perm[t][0]),
  508. &(s->len[t][0]),
  509. minLen, maxLen, alphaSize
  510. );
  511. s->minLens[t] = minLen;
  512. }
  513. /*--- Now the MTF values ---*/
  514. EOB = s->nInUse+1;
  515. nblockMAX = 100000 * s->blockSize100k;
  516. groupNo = -1;
  517. groupPos = 0;
  518. for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
  519. /*-- MTF init --*/
  520. {
  521. Int32 ii, jj, kk;
  522. kk = MTFA_SIZE-1;
  523. for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
  524. for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
  525. s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj);
  526. kk--;
  527. }
  528. s->mtfbase[ii] = kk + 1;
  529. }
  530. }
  531. /*-- end MTF init --*/
  532. nblock = 0;
  533. GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
  534. while (True) {
  535. if (nextSym == EOB) break;
  536. if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
  537. es = -1;
  538. N = 1;
  539. do {
  540. if (nextSym == BZ_RUNA) es = es + (0+1) * N; else
  541. if (nextSym == BZ_RUNB) es = es + (1+1) * N;
  542. N = N * 2;
  543. GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
  544. }
  545. while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
  546. es++;
  547. uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
  548. s->unzftab[uc] += es;
  549. if (0 && s->smallDecompress)
  550. while (es > 0) {
  551. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  552. s->ll16[nblock] = (UInt16)uc;
  553. nblock++;
  554. es--;
  555. }
  556. else
  557. while (es > 0) {
  558. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  559. s->tt[nblock] = (UInt32)uc;
  560. nblock++;
  561. es--;
  562. };
  563. continue;
  564. } else {
  565. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  566. /*-- uc = MTF ( nextSym-1 ) --*/
  567. {
  568. Int32 ii, jj, kk, pp, lno, off;
  569. UInt32 nn;
  570. nn = (UInt32)(nextSym - 1);
  571. if (nn < MTFL_SIZE) {
  572. /* avoid general-case expense */
  573. pp = s->mtfbase[0];
  574. uc = s->mtfa[pp+nn];
  575. while (nn > 3) {
  576. Int32 z = pp+nn;
  577. s->mtfa[(z) ] = s->mtfa[(z)-1];
  578. s->mtfa[(z)-1] = s->mtfa[(z)-2];
  579. s->mtfa[(z)-2] = s->mtfa[(z)-3];
  580. s->mtfa[(z)-3] = s->mtfa[(z)-4];
  581. nn -= 4;
  582. }
  583. while (nn > 0) {
  584. s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
  585. };
  586. s->mtfa[pp] = uc;
  587. } else {
  588. /* general case */
  589. lno = nn / MTFL_SIZE;
  590. off = nn % MTFL_SIZE;
  591. pp = s->mtfbase[lno] + off;
  592. uc = s->mtfa[pp];
  593. while (pp > s->mtfbase[lno]) {
  594. s->mtfa[pp] = s->mtfa[pp-1]; pp--;
  595. };
  596. s->mtfbase[lno]++;
  597. while (lno > 0) {
  598. s->mtfbase[lno]--;
  599. s->mtfa[s->mtfbase[lno]]
  600. = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
  601. lno--;
  602. }
  603. s->mtfbase[0]--;
  604. s->mtfa[s->mtfbase[0]] = uc;
  605. if (s->mtfbase[0] == 0) {
  606. kk = MTFA_SIZE-1;
  607. for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
  608. for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
  609. s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
  610. kk--;
  611. }
  612. s->mtfbase[ii] = kk + 1;
  613. }
  614. }
  615. }
  616. }
  617. /*-- end uc = MTF ( nextSym-1 ) --*/
  618. s->unzftab[s->seqToUnseq[uc]]++;
  619. if (0 && s->smallDecompress)
  620. s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else
  621. s->tt[nblock] = (UInt32)(s->seqToUnseq[uc]);
  622. nblock++;
  623. GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
  624. continue;
  625. }
  626. }
  627. /* Now we know what nblock is, we can do a better sanity
  628. check on s->origPtr.
  629. */
  630. if (s->origPtr < 0 || s->origPtr >= nblock)
  631. RETURN(BZ_DATA_ERROR);
  632. s->state_out_len = 0;
  633. s->state_out_ch = 0;
  634. BZ_INITIALISE_CRC ( s->calculatedBlockCRC );
  635. s->state = BZ_X_OUTPUT;
  636. // if (s->verbosity >= 2) VPrintf0 ( "rt+rld" );
  637. /*-- Set up cftab to facilitate generation of T^(-1) --*/
  638. s->cftab[0] = 0;
  639. for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1];
  640. for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
  641. if (0 && s->smallDecompress) {
  642. /*-- Make a copy of cftab, used in generation of T --*/
  643. for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
  644. /*-- compute the T vector --*/
  645. for (i = 0; i < nblock; i++) {
  646. uc = (UChar)(s->ll16[i]);
  647. SET_LL(i, s->cftabCopy[uc]);
  648. s->cftabCopy[uc]++;
  649. }
  650. /*-- Compute T^(-1) by pointer reversal on T --*/
  651. i = s->origPtr;
  652. j = GET_LL(i);
  653. do {
  654. Int32 tmp = GET_LL(j);
  655. SET_LL(j, i);
  656. i = j;
  657. j = tmp;
  658. }
  659. while (i != s->origPtr);
  660. s->tPos = s->origPtr;
  661. s->nblock_used = 0;
  662. if (s->blockRandomised) {
  663. BZ_RAND_INIT_MASK;
  664. BZ_GET_SMALL(s->k0); s->nblock_used++;
  665. BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
  666. } else {
  667. BZ_GET_SMALL(s->k0); s->nblock_used++;
  668. }
  669. } else {
  670. /*-- compute the T^(-1) vector --*/
  671. for (i = 0; i < nblock; i++) {
  672. uc = (UChar)(s->tt[i] & 0xff);
  673. s->tt[s->cftab[uc]] |= (i << 8);
  674. s->cftab[uc]++;
  675. }
  676. s->tPos = s->tt[s->origPtr] >> 8;
  677. s->nblock_used = 0;
  678. if (s->blockRandomised) {
  679. BZ_RAND_INIT_MASK;
  680. BZ_GET_FAST(s->k0); s->nblock_used++;
  681. BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
  682. } else {
  683. BZ_GET_FAST(s->k0); s->nblock_used++;
  684. }
  685. }
  686. RETURN(BZ_OK);
  687. endhdr_2:
  688. GET_UCHAR(BZ_X_ENDHDR_2, uc);
  689. if (uc != 0x72) RETURN(BZ_DATA_ERROR);
  690. GET_UCHAR(BZ_X_ENDHDR_3, uc);
  691. if (uc != 0x45) RETURN(BZ_DATA_ERROR);
  692. GET_UCHAR(BZ_X_ENDHDR_4, uc);
  693. if (uc != 0x38) RETURN(BZ_DATA_ERROR);
  694. GET_UCHAR(BZ_X_ENDHDR_5, uc);
  695. if (uc != 0x50) RETURN(BZ_DATA_ERROR);
  696. GET_UCHAR(BZ_X_ENDHDR_6, uc);
  697. if (uc != 0x90) RETURN(BZ_DATA_ERROR);
  698. s->storedCombinedCRC = 0;
  699. GET_UCHAR(BZ_X_CCRC_1, uc);
  700. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  701. GET_UCHAR(BZ_X_CCRC_2, uc);
  702. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  703. GET_UCHAR(BZ_X_CCRC_3, uc);
  704. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  705. GET_UCHAR(BZ_X_CCRC_4, uc);
  706. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  707. s->state = BZ_X_IDLE;
  708. RETURN(BZ_STREAM_END);
  709. default: AssertH ( False, 4001 );
  710. }
  711. AssertH ( False, 4002 );
  712. save_state_and_return:
  713. s->save_i = i;
  714. s->save_j = j;
  715. s->save_t = t;
  716. s->save_alphaSize = alphaSize;
  717. s->save_nGroups = nGroups;
  718. s->save_nSelectors = nSelectors;
  719. s->save_EOB = EOB;
  720. s->save_groupNo = groupNo;
  721. s->save_groupPos = groupPos;
  722. s->save_nextSym = nextSym;
  723. s->save_nblockMAX = nblockMAX;
  724. s->save_nblock = nblock;
  725. s->save_es = es;
  726. s->save_N = N;
  727. s->save_curr = curr;
  728. s->save_zt = zt;
  729. s->save_zn = zn;
  730. s->save_zvec = zvec;
  731. s->save_zj = zj;
  732. s->save_gSel = gSel;
  733. s->save_gMinlen = gMinlen;
  734. s->save_gLimit = gLimit;
  735. s->save_gBase = gBase;
  736. s->save_gPerm = gPerm;
  737. return retVal;
  738. }
  739. /*-------------------------------------------------------------*/
  740. /*--- end decompress.c ---*/
  741. /*-------------------------------------------------------------*/