decompress.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*-------------------------------------------------------------*/
  2. /*--- Decompression machinery ---*/
  3. /*--- decompress.c ---*/
  4. /*-------------------------------------------------------------*/
  5. /* ------------------------------------------------------------------
  6. This file is part of bzip2/libbzip2, a program and library for
  7. lossless, block-sorting data compression.
  8. bzip2/libbzip2 version 1.0.8 of 13 July 2019
  9. Copyright (C) 1996-2019 Julian Seward <jseward@acm.org>
  10. Please read the WARNING, DISCLAIMER and PATENTS sections in the
  11. README file.
  12. This program is released under the terms of the license contained
  13. in the file LICENSE.
  14. ------------------------------------------------------------------ */
  15. #include "bzlib_private.h"
  16. /*---------------------------------------------------*/
  17. static
  18. void makeMaps_d ( DState* s )
  19. {
  20. Int32 i;
  21. s->nInUse = 0;
  22. for (i = 0; i < 256; i++)
  23. if (s->inUse[i]) {
  24. s->seqToUnseq[s->nInUse] = i;
  25. s->nInUse++;
  26. }
  27. }
  28. /*---------------------------------------------------*/
  29. #define RETURN(rrr) \
  30. { retVal = rrr; goto save_state_and_return; };
  31. #define GET_BITS(lll,vvv,nnn) \
  32. case lll: s->state = lll; \
  33. while (True) { \
  34. if (s->bsLive >= nnn) { \
  35. UInt32 v; \
  36. v = (s->bsBuff >> \
  37. (s->bsLive-nnn)) & ((1 << nnn)-1); \
  38. s->bsLive -= nnn; \
  39. vvv = v; \
  40. break; \
  41. } \
  42. if (s->strm->avail_in == 0) RETURN(BZ_OK); \
  43. s->bsBuff \
  44. = (s->bsBuff << 8) | \
  45. ((UInt32) \
  46. (*((UChar*)(s->strm->next_in)))); \
  47. s->bsLive += 8; \
  48. s->strm->next_in++; \
  49. s->strm->avail_in--; \
  50. s->strm->total_in_lo32++; \
  51. if (s->strm->total_in_lo32 == 0) \
  52. s->strm->total_in_hi32++; \
  53. }
  54. #define GET_UCHAR(lll,uuu) \
  55. GET_BITS(lll,uuu,8)
  56. #define GET_BIT(lll,uuu) \
  57. GET_BITS(lll,uuu,1)
  58. /*---------------------------------------------------*/
  59. #define GET_MTF_VAL(label1,label2,lval) \
  60. { \
  61. if (groupPos == 0) { \
  62. groupNo++; \
  63. if (groupNo >= nSelectors) \
  64. RETURN(BZ_DATA_ERROR); \
  65. groupPos = BZ_G_SIZE; \
  66. gSel = s->selector[groupNo]; \
  67. gMinlen = s->minLens[gSel]; \
  68. gLimit = &(s->limit[gSel][0]); \
  69. gPerm = &(s->perm[gSel][0]); \
  70. gBase = &(s->base[gSel][0]); \
  71. } \
  72. groupPos--; \
  73. zn = gMinlen; \
  74. GET_BITS(label1, zvec, zn); \
  75. while (1) { \
  76. if (zn > 20 /* the longest code */) \
  77. RETURN(BZ_DATA_ERROR); \
  78. if (zvec <= gLimit[zn]) break; \
  79. zn++; \
  80. GET_BIT(label2, zj); \
  81. zvec = (zvec << 1) | zj; \
  82. }; \
  83. if (zvec - gBase[zn] < 0 \
  84. || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \
  85. RETURN(BZ_DATA_ERROR); \
  86. lval = gPerm[zvec - gBase[zn]]; \
  87. }
  88. /*---------------------------------------------------*/
  89. Int32 BZ2_decompress ( DState* s )
  90. {
  91. UChar uc;
  92. Int32 retVal;
  93. Int32 minLen, maxLen;
  94. bz_stream* strm = s->strm;
  95. /* stuff that needs to be saved/restored */
  96. Int32 i;
  97. Int32 j;
  98. Int32 t;
  99. Int32 alphaSize;
  100. Int32 nGroups;
  101. Int32 nSelectors;
  102. Int32 EOB;
  103. Int32 groupNo;
  104. Int32 groupPos;
  105. Int32 nextSym;
  106. Int32 nblockMAX;
  107. Int32 nblock;
  108. Int32 es;
  109. Int32 N;
  110. Int32 curr;
  111. Int32 zt;
  112. Int32 zn;
  113. Int32 zvec;
  114. Int32 zj;
  115. Int32 gSel;
  116. Int32 gMinlen;
  117. Int32* gLimit;
  118. Int32* gBase;
  119. Int32* gPerm;
  120. if (s->state == BZ_X_MAGIC_1) {
  121. /*initialise the save area*/
  122. s->save_i = 0;
  123. s->save_j = 0;
  124. s->save_t = 0;
  125. s->save_alphaSize = 0;
  126. s->save_nGroups = 0;
  127. s->save_nSelectors = 0;
  128. s->save_EOB = 0;
  129. s->save_groupNo = 0;
  130. s->save_groupPos = 0;
  131. s->save_nextSym = 0;
  132. s->save_nblockMAX = 0;
  133. s->save_nblock = 0;
  134. s->save_es = 0;
  135. s->save_N = 0;
  136. s->save_curr = 0;
  137. s->save_zt = 0;
  138. s->save_zn = 0;
  139. s->save_zvec = 0;
  140. s->save_zj = 0;
  141. s->save_gSel = 0;
  142. s->save_gMinlen = 0;
  143. s->save_gLimit = NULL;
  144. s->save_gBase = NULL;
  145. s->save_gPerm = NULL;
  146. }
  147. /*restore from the save area*/
  148. i = s->save_i;
  149. j = s->save_j;
  150. t = s->save_t;
  151. alphaSize = s->save_alphaSize;
  152. nGroups = s->save_nGroups;
  153. nSelectors = s->save_nSelectors;
  154. EOB = s->save_EOB;
  155. groupNo = s->save_groupNo;
  156. groupPos = s->save_groupPos;
  157. nextSym = s->save_nextSym;
  158. nblockMAX = s->save_nblockMAX;
  159. nblock = s->save_nblock;
  160. es = s->save_es;
  161. N = s->save_N;
  162. curr = s->save_curr;
  163. zt = s->save_zt;
  164. zn = s->save_zn;
  165. zvec = s->save_zvec;
  166. zj = s->save_zj;
  167. gSel = s->save_gSel;
  168. gMinlen = s->save_gMinlen;
  169. gLimit = s->save_gLimit;
  170. gBase = s->save_gBase;
  171. gPerm = s->save_gPerm;
  172. retVal = BZ_OK;
  173. switch (s->state) {
  174. GET_UCHAR(BZ_X_MAGIC_1, uc);
  175. if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC);
  176. GET_UCHAR(BZ_X_MAGIC_2, uc);
  177. if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC);
  178. GET_UCHAR(BZ_X_MAGIC_3, uc)
  179. if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC);
  180. GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8)
  181. if (s->blockSize100k < (BZ_HDR_0 + 1) ||
  182. s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC);
  183. s->blockSize100k -= BZ_HDR_0;
  184. if (s->smallDecompress) {
  185. s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
  186. s->ll4 = BZALLOC(
  187. ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar)
  188. );
  189. if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
  190. } else {
  191. s->tt = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
  192. if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
  193. }
  194. GET_UCHAR(BZ_X_BLKHDR_1, uc);
  195. if (uc == 0x17) goto endhdr_2;
  196. if (uc != 0x31) RETURN(BZ_DATA_ERROR);
  197. GET_UCHAR(BZ_X_BLKHDR_2, uc);
  198. if (uc != 0x41) RETURN(BZ_DATA_ERROR);
  199. GET_UCHAR(BZ_X_BLKHDR_3, uc);
  200. if (uc != 0x59) RETURN(BZ_DATA_ERROR);
  201. GET_UCHAR(BZ_X_BLKHDR_4, uc);
  202. if (uc != 0x26) RETURN(BZ_DATA_ERROR);
  203. GET_UCHAR(BZ_X_BLKHDR_5, uc);
  204. if (uc != 0x53) RETURN(BZ_DATA_ERROR);
  205. GET_UCHAR(BZ_X_BLKHDR_6, uc);
  206. if (uc != 0x59) RETURN(BZ_DATA_ERROR);
  207. s->currBlockNo++;
  208. if (s->verbosity >= 2)
  209. VPrintf1 ( "\n [%d: huff+mtf ", s->currBlockNo );
  210. s->storedBlockCRC = 0;
  211. GET_UCHAR(BZ_X_BCRC_1, uc);
  212. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  213. GET_UCHAR(BZ_X_BCRC_2, uc);
  214. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  215. GET_UCHAR(BZ_X_BCRC_3, uc);
  216. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  217. GET_UCHAR(BZ_X_BCRC_4, uc);
  218. s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
  219. GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);
  220. s->origPtr = 0;
  221. GET_UCHAR(BZ_X_ORIGPTR_1, uc);
  222. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  223. GET_UCHAR(BZ_X_ORIGPTR_2, uc);
  224. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  225. GET_UCHAR(BZ_X_ORIGPTR_3, uc);
  226. s->origPtr = (s->origPtr << 8) | ((Int32)uc);
  227. if (s->origPtr < 0)
  228. RETURN(BZ_DATA_ERROR);
  229. if (s->origPtr > 10 + 100000*s->blockSize100k)
  230. RETURN(BZ_DATA_ERROR);
  231. /*--- Receive the mapping table ---*/
  232. for (i = 0; i < 16; i++) {
  233. GET_BIT(BZ_X_MAPPING_1, uc);
  234. if (uc == 1)
  235. s->inUse16[i] = True; else
  236. s->inUse16[i] = False;
  237. }
  238. for (i = 0; i < 256; i++) s->inUse[i] = False;
  239. for (i = 0; i < 16; i++)
  240. if (s->inUse16[i])
  241. for (j = 0; j < 16; j++) {
  242. GET_BIT(BZ_X_MAPPING_2, uc);
  243. if (uc == 1) s->inUse[i * 16 + j] = True;
  244. }
  245. makeMaps_d ( s );
  246. if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
  247. alphaSize = s->nInUse+2;
  248. /*--- Now the selectors ---*/
  249. GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
  250. if (nGroups < 2 || nGroups > BZ_N_GROUPS) RETURN(BZ_DATA_ERROR);
  251. GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
  252. if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
  253. for (i = 0; i < nSelectors; i++) {
  254. j = 0;
  255. while (True) {
  256. GET_BIT(BZ_X_SELECTOR_3, uc);
  257. if (uc == 0) break;
  258. j++;
  259. if (j >= nGroups) RETURN(BZ_DATA_ERROR);
  260. }
  261. /* Having more than BZ_MAX_SELECTORS doesn't make much sense
  262. since they will never be used, but some implementations might
  263. "round up" the number of selectors, so just ignore those. */
  264. if (i < BZ_MAX_SELECTORS)
  265. s->selectorMtf[i] = j;
  266. }
  267. if (nSelectors > BZ_MAX_SELECTORS)
  268. nSelectors = BZ_MAX_SELECTORS;
  269. /*--- Undo the MTF values for the selectors. ---*/
  270. {
  271. UChar pos[BZ_N_GROUPS], tmp, v;
  272. for (v = 0; v < nGroups; v++) pos[v] = v;
  273. for (i = 0; i < nSelectors; i++) {
  274. v = s->selectorMtf[i];
  275. tmp = pos[v];
  276. while (v > 0) { pos[v] = pos[v-1]; v--; }
  277. pos[0] = tmp;
  278. s->selector[i] = tmp;
  279. }
  280. }
  281. /*--- Now the coding tables ---*/
  282. for (t = 0; t < nGroups; t++) {
  283. GET_BITS(BZ_X_CODING_1, curr, 5);
  284. for (i = 0; i < alphaSize; i++) {
  285. while (True) {
  286. if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR);
  287. GET_BIT(BZ_X_CODING_2, uc);
  288. if (uc == 0) break;
  289. GET_BIT(BZ_X_CODING_3, uc);
  290. if (uc == 0) curr++; else curr--;
  291. }
  292. s->len[t][i] = curr;
  293. }
  294. }
  295. /*--- Create the Huffman decoding tables ---*/
  296. for (t = 0; t < nGroups; t++) {
  297. minLen = 32;
  298. maxLen = 0;
  299. for (i = 0; i < alphaSize; i++) {
  300. if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  301. if (s->len[t][i] < minLen) minLen = s->len[t][i];
  302. }
  303. BZ2_hbCreateDecodeTables (
  304. &(s->limit[t][0]),
  305. &(s->base[t][0]),
  306. &(s->perm[t][0]),
  307. &(s->len[t][0]),
  308. minLen, maxLen, alphaSize
  309. );
  310. s->minLens[t] = minLen;
  311. }
  312. /*--- Now the MTF values ---*/
  313. EOB = s->nInUse+1;
  314. nblockMAX = 100000 * s->blockSize100k;
  315. groupNo = -1;
  316. groupPos = 0;
  317. for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
  318. /*-- MTF init --*/
  319. {
  320. Int32 ii, jj, kk;
  321. kk = MTFA_SIZE-1;
  322. for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
  323. for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
  324. s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj);
  325. kk--;
  326. }
  327. s->mtfbase[ii] = kk + 1;
  328. }
  329. }
  330. /*-- end MTF init --*/
  331. nblock = 0;
  332. GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
  333. while (True) {
  334. if (nextSym == EOB) break;
  335. if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
  336. es = -1;
  337. N = 1;
  338. do {
  339. /* Check that N doesn't get too big, so that es doesn't
  340. go negative. The maximum value that can be
  341. RUNA/RUNB encoded is equal to the block size (post
  342. the initial RLE), viz, 900k, so bounding N at 2
  343. million should guard against overflow without
  344. rejecting any legitimate inputs. */
  345. if (N >= 2*1024*1024) RETURN(BZ_DATA_ERROR);
  346. if (nextSym == BZ_RUNA) es = es + (0+1) * N; else
  347. if (nextSym == BZ_RUNB) es = es + (1+1) * N;
  348. N = N * 2;
  349. GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
  350. }
  351. while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
  352. es++;
  353. uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
  354. s->unzftab[uc] += es;
  355. if (s->smallDecompress)
  356. while (es > 0) {
  357. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  358. s->ll16[nblock] = (UInt16)uc;
  359. nblock++;
  360. es--;
  361. }
  362. else
  363. while (es > 0) {
  364. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  365. s->tt[nblock] = (UInt32)uc;
  366. nblock++;
  367. es--;
  368. };
  369. continue;
  370. } else {
  371. if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
  372. /*-- uc = MTF ( nextSym-1 ) --*/
  373. {
  374. Int32 ii, jj, kk, pp, lno, off;
  375. UInt32 nn;
  376. nn = (UInt32)(nextSym - 1);
  377. if (nn < MTFL_SIZE) {
  378. /* avoid general-case expense */
  379. pp = s->mtfbase[0];
  380. uc = s->mtfa[pp+nn];
  381. while (nn > 3) {
  382. Int32 z = pp+nn;
  383. s->mtfa[(z) ] = s->mtfa[(z)-1];
  384. s->mtfa[(z)-1] = s->mtfa[(z)-2];
  385. s->mtfa[(z)-2] = s->mtfa[(z)-3];
  386. s->mtfa[(z)-3] = s->mtfa[(z)-4];
  387. nn -= 4;
  388. }
  389. while (nn > 0) {
  390. s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
  391. };
  392. s->mtfa[pp] = uc;
  393. } else {
  394. /* general case */
  395. lno = nn / MTFL_SIZE;
  396. off = nn % MTFL_SIZE;
  397. pp = s->mtfbase[lno] + off;
  398. uc = s->mtfa[pp];
  399. while (pp > s->mtfbase[lno]) {
  400. s->mtfa[pp] = s->mtfa[pp-1]; pp--;
  401. };
  402. s->mtfbase[lno]++;
  403. while (lno > 0) {
  404. s->mtfbase[lno]--;
  405. s->mtfa[s->mtfbase[lno]]
  406. = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
  407. lno--;
  408. }
  409. s->mtfbase[0]--;
  410. s->mtfa[s->mtfbase[0]] = uc;
  411. if (s->mtfbase[0] == 0) {
  412. kk = MTFA_SIZE-1;
  413. for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
  414. for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
  415. s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
  416. kk--;
  417. }
  418. s->mtfbase[ii] = kk + 1;
  419. }
  420. }
  421. }
  422. }
  423. /*-- end uc = MTF ( nextSym-1 ) --*/
  424. s->unzftab[s->seqToUnseq[uc]]++;
  425. if (s->smallDecompress)
  426. s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else
  427. s->tt[nblock] = (UInt32)(s->seqToUnseq[uc]);
  428. nblock++;
  429. GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
  430. continue;
  431. }
  432. }
  433. /* Now we know what nblock is, we can do a better sanity
  434. check on s->origPtr.
  435. */
  436. if (s->origPtr < 0 || s->origPtr >= nblock)
  437. RETURN(BZ_DATA_ERROR);
  438. /*-- Set up cftab to facilitate generation of T^(-1) --*/
  439. /* Check: unzftab entries in range. */
  440. for (i = 0; i <= 255; i++) {
  441. if (s->unzftab[i] < 0 || s->unzftab[i] > nblock)
  442. RETURN(BZ_DATA_ERROR);
  443. }
  444. /* Actually generate cftab. */
  445. s->cftab[0] = 0;
  446. for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1];
  447. for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
  448. /* Check: cftab entries in range. */
  449. for (i = 0; i <= 256; i++) {
  450. if (s->cftab[i] < 0 || s->cftab[i] > nblock) {
  451. /* s->cftab[i] can legitimately be == nblock */
  452. RETURN(BZ_DATA_ERROR);
  453. }
  454. }
  455. /* Check: cftab entries non-descending. */
  456. for (i = 1; i <= 256; i++) {
  457. if (s->cftab[i-1] > s->cftab[i]) {
  458. RETURN(BZ_DATA_ERROR);
  459. }
  460. }
  461. s->state_out_len = 0;
  462. s->state_out_ch = 0;
  463. BZ_INITIALISE_CRC ( s->calculatedBlockCRC );
  464. s->state = BZ_X_OUTPUT;
  465. if (s->verbosity >= 2) VPrintf0 ( "rt+rld" );
  466. if (s->smallDecompress) {
  467. /*-- Make a copy of cftab, used in generation of T --*/
  468. for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
  469. /*-- compute the T vector --*/
  470. for (i = 0; i < nblock; i++) {
  471. uc = (UChar)(s->ll16[i]);
  472. SET_LL(i, s->cftabCopy[uc]);
  473. s->cftabCopy[uc]++;
  474. }
  475. /*-- Compute T^(-1) by pointer reversal on T --*/
  476. i = s->origPtr;
  477. j = GET_LL(i);
  478. do {
  479. Int32 tmp = GET_LL(j);
  480. SET_LL(j, i);
  481. i = j;
  482. j = tmp;
  483. }
  484. while (i != s->origPtr);
  485. s->tPos = s->origPtr;
  486. s->nblock_used = 0;
  487. if (s->blockRandomised) {
  488. BZ_RAND_INIT_MASK;
  489. BZ_GET_SMALL(s->k0); s->nblock_used++;
  490. BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
  491. } else {
  492. BZ_GET_SMALL(s->k0); s->nblock_used++;
  493. }
  494. } else {
  495. /*-- compute the T^(-1) vector --*/
  496. for (i = 0; i < nblock; i++) {
  497. uc = (UChar)(s->tt[i] & 0xff);
  498. s->tt[s->cftab[uc]] |= (i << 8);
  499. s->cftab[uc]++;
  500. }
  501. s->tPos = s->tt[s->origPtr] >> 8;
  502. s->nblock_used = 0;
  503. if (s->blockRandomised) {
  504. BZ_RAND_INIT_MASK;
  505. BZ_GET_FAST(s->k0); s->nblock_used++;
  506. BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
  507. } else {
  508. BZ_GET_FAST(s->k0); s->nblock_used++;
  509. }
  510. }
  511. RETURN(BZ_OK);
  512. endhdr_2:
  513. GET_UCHAR(BZ_X_ENDHDR_2, uc);
  514. if (uc != 0x72) RETURN(BZ_DATA_ERROR);
  515. GET_UCHAR(BZ_X_ENDHDR_3, uc);
  516. if (uc != 0x45) RETURN(BZ_DATA_ERROR);
  517. GET_UCHAR(BZ_X_ENDHDR_4, uc);
  518. if (uc != 0x38) RETURN(BZ_DATA_ERROR);
  519. GET_UCHAR(BZ_X_ENDHDR_5, uc);
  520. if (uc != 0x50) RETURN(BZ_DATA_ERROR);
  521. GET_UCHAR(BZ_X_ENDHDR_6, uc);
  522. if (uc != 0x90) RETURN(BZ_DATA_ERROR);
  523. s->storedCombinedCRC = 0;
  524. GET_UCHAR(BZ_X_CCRC_1, uc);
  525. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  526. GET_UCHAR(BZ_X_CCRC_2, uc);
  527. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  528. GET_UCHAR(BZ_X_CCRC_3, uc);
  529. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  530. GET_UCHAR(BZ_X_CCRC_4, uc);
  531. s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
  532. s->state = BZ_X_IDLE;
  533. RETURN(BZ_STREAM_END);
  534. default: AssertH ( False, 4001 );
  535. }
  536. AssertH ( False, 4002 );
  537. save_state_and_return:
  538. s->save_i = i;
  539. s->save_j = j;
  540. s->save_t = t;
  541. s->save_alphaSize = alphaSize;
  542. s->save_nGroups = nGroups;
  543. s->save_nSelectors = nSelectors;
  544. s->save_EOB = EOB;
  545. s->save_groupNo = groupNo;
  546. s->save_groupPos = groupPos;
  547. s->save_nextSym = nextSym;
  548. s->save_nblockMAX = nblockMAX;
  549. s->save_nblock = nblock;
  550. s->save_es = es;
  551. s->save_N = N;
  552. s->save_curr = curr;
  553. s->save_zt = zt;
  554. s->save_zn = zn;
  555. s->save_zvec = zvec;
  556. s->save_zj = zj;
  557. s->save_gSel = gSel;
  558. s->save_gMinlen = gMinlen;
  559. s->save_gLimit = gLimit;
  560. s->save_gBase = gBase;
  561. s->save_gPerm = gPerm;
  562. return retVal;
  563. }
  564. /*-------------------------------------------------------------*/
  565. /*--- end decompress.c ---*/
  566. /*-------------------------------------------------------------*/