1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 /*-------------------------------------------------------------*/
18 /*--- Decompression machinery                               ---*/
19 /*---                                        decompress.cpp ---*/
20 /*-------------------------------------------------------------*/
21 
22 /* ------------------------------------------------------------------
23    The original source for this example:
24    This file is part of bzip2/libbzip2, a program and library for
25    lossless, block-sorting data compression.
26 
27    bzip2/libbzip2 version 1.0.6 of 6 September 2010
28    Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
29 
30    This program, "bzip2", the associated library "libbzip2", and all
31    documentation, are copyright (C) 1996-2010 Julian R Seward.  All
32    rights reserved.
33 
34    Redistribution and use in source and binary forms, with or without
35    modification, are permitted provided that the following conditions
36    are met:
37 
38    1. Redistributions of source code must retain the above copyright
39    notice, this list of conditions and the following disclaimer.
40 
41    2. The origin of this software must not be misrepresented; you must
42    not claim that you wrote the original software.  If you use this
43    software in a product, an acknowledgment in the product
44    documentation would be appreciated but is not required.
45 
46    3. Altered source versions must be plainly marked as such, and must
47    not be misrepresented as being the original software.
48 
49    4. The name of the author may not be used to endorse or promote
50    products derived from this software without specific prior written
51    permission.
52 
53    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
54    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
55    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
57    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
59    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
61    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 
65    Julian Seward, jseward@bzip.org
66    bzip2/libbzip2 version 1.0.6 of 6 September 2010
67    ------------------------------------------------------------------ */
68 
69 
70 #include "bzlib_private.h"
71 
72 
73 /*---------------------------------------------------*/
74 static
makeMaps_d(DState * s)75 void makeMaps_d ( DState* s )
76 {
77    Int32 i;
78    s->nInUse = 0;
79    for (i = 0; i < 256; i++)
80       if (s->inUse[i]) {
81          s->seqToUnseq[s->nInUse] = i;
82          s->nInUse++;
83       }
84 }
85 
86 
87 /*---------------------------------------------------*/
88 #define RETURN(rrr)                               \
89    { retVal = rrr; goto save_state_and_return; };
90 
91 #define GET_BITS(lll,vvv,nnn)                     \
92    case lll: s->state = lll;                      \
93    while (True) {                                 \
94       if (s->bsLive >= nnn) {                     \
95          UInt32 v;                                \
96          v = (s->bsBuff >>                        \
97              (s->bsLive-nnn)) & ((1 << nnn)-1);   \
98          s->bsLive -= nnn;                        \
99          vvv = v;                                 \
100          break;                                   \
101       }                                           \
102       if (s->strm->avail_in == 0) RETURN(BZ_OK);  \
103       s->bsBuff                                   \
104          = (s->bsBuff << 8) |                     \
105            ((UInt32)                              \
106               (*((UChar*)(s->strm->next_in))));   \
107       s->bsLive += 8;                             \
108       s->strm->next_in++;                         \
109       s->strm->avail_in--;                        \
110       s->strm->total_in_lo32++;                   \
111       if (s->strm->total_in_lo32 == 0)            \
112          s->strm->total_in_hi32++;                \
113    }
114 
115 #define GET_UCHAR(lll,uuu)                        \
116    GET_BITS(lll,uuu,8)
117 
118 #define GET_BIT(lll,uuu)                          \
119    GET_BITS(lll,uuu,1)
120 
121 /*---------------------------------------------------*/
122 #define GET_MTF_VAL(label1,label2,lval)           \
123 {                                                 \
124    if (groupPos == 0) {                           \
125       groupNo++;                                  \
126       if (groupNo >= nSelectors)                  \
127          RETURN(BZ_DATA_ERROR);                   \
128       groupPos = BZ_G_SIZE;                       \
129       gSel = s->selector[groupNo];                \
130       gMinlen = s->minLens[gSel];                 \
131       gLimit = &(s->limit[gSel][0]);              \
132       gPerm = &(s->perm[gSel][0]);                \
133       gBase = &(s->base[gSel][0]);                \
134    }                                              \
135    groupPos--;                                    \
136    zn = gMinlen;                                  \
137    GET_BITS(label1, zvec, zn);                    \
138    while (1) {                                    \
139       if (zn > 20 /* the longest code */)         \
140          RETURN(BZ_DATA_ERROR);                   \
141       if (zvec <= gLimit[zn]) break;              \
142       zn++;                                       \
143       GET_BIT(label2, zj);                        \
144       zvec = (zvec << 1) | zj;                    \
145    };                                             \
146    if (zvec - gBase[zn] < 0                       \
147        || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE)  \
148       RETURN(BZ_DATA_ERROR);                      \
149    lval = gPerm[zvec - gBase[zn]];                \
150 }
151 
152 
153 /*---------------------------------------------------*/
BZ2_decompress(DState * s)154 Int32 BZ2_decompress ( DState* s )
155 {
156    UChar      uc;
157    Int32      retVal;
158    Int32      minLen, maxLen;
159    bz_stream* strm = s->strm;
160 
161    /* stuff that needs to be saved/restored */
162    Int32  i;
163    Int32  j;
164    Int32  t;
165    Int32  alphaSize;
166    Int32  nGroups;
167    Int32  nSelectors;
168    Int32  EOB;
169    Int32  groupNo;
170    Int32  groupPos;
171    Int32  nextSym;
172    Int32  nblockMAX;
173    Int32  nblock;
174    Int32  es;
175    Int32  N;
176    Int32  curr;
177    Int32  zt;
178    Int32  zn;
179    Int32  zvec;
180    Int32  zj;
181    Int32  gSel;
182    Int32  gMinlen;
183    Int32* gLimit;
184    Int32* gBase;
185    Int32* gPerm;
186 
187    if (s->state == BZ_X_MAGIC_1) {
188       /*initialise the save area*/
189       s->save_i           = 0;
190       s->save_j           = 0;
191       s->save_t           = 0;
192       s->save_alphaSize   = 0;
193       s->save_nGroups     = 0;
194       s->save_nSelectors  = 0;
195       s->save_EOB         = 0;
196       s->save_groupNo     = 0;
197       s->save_groupPos    = 0;
198       s->save_nextSym     = 0;
199       s->save_nblockMAX   = 0;
200       s->save_nblock      = 0;
201       s->save_es          = 0;
202       s->save_N           = 0;
203       s->save_curr        = 0;
204       s->save_zt          = 0;
205       s->save_zn          = 0;
206       s->save_zvec        = 0;
207       s->save_zj          = 0;
208       s->save_gSel        = 0;
209       s->save_gMinlen     = 0;
210       s->save_gLimit      = NULL;
211       s->save_gBase       = NULL;
212       s->save_gPerm       = NULL;
213    }
214 
215    /*restore from the save area*/
216    i           = s->save_i;
217    j           = s->save_j;
218    t           = s->save_t;
219    alphaSize   = s->save_alphaSize;
220    nGroups     = s->save_nGroups;
221    nSelectors  = s->save_nSelectors;
222    EOB         = s->save_EOB;
223    groupNo     = s->save_groupNo;
224    groupPos    = s->save_groupPos;
225    nextSym     = s->save_nextSym;
226    nblockMAX   = s->save_nblockMAX;
227    nblock      = s->save_nblock;
228    es          = s->save_es;
229    N           = s->save_N;
230    curr        = s->save_curr;
231    zt          = s->save_zt;
232    zn          = s->save_zn;
233    zvec        = s->save_zvec;
234    zj          = s->save_zj;
235    gSel        = s->save_gSel;
236    gMinlen     = s->save_gMinlen;
237    gLimit      = s->save_gLimit;
238    gBase       = s->save_gBase;
239    gPerm       = s->save_gPerm;
240 
241    retVal = BZ_OK;
242 
243    switch (s->state) {
244 
245       GET_UCHAR(BZ_X_MAGIC_1, uc);
246       if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC);
247 
248       GET_UCHAR(BZ_X_MAGIC_2, uc);
249       if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC);
250 
251       GET_UCHAR(BZ_X_MAGIC_3, uc)
252       if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC);
253 
254       GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8)
255       if (s->blockSize100k < (BZ_HDR_0 + 1) ||
256           s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC);
257       s->blockSize100k -= BZ_HDR_0;
258 
259       if (s->smallDecompress) {
260          s->ll16 = (UInt16*)BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
261          s->ll4  = (UChar*)BZALLOC(
262                       ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar)
263                    );
264          if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
265       } else {
266          s->tt  = (UInt32*)BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
267          if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
268       }
269 
270       GET_UCHAR(BZ_X_BLKHDR_1, uc);
271 
272       if (uc == 0x17) goto endhdr_2;
273       if (uc != 0x31) RETURN(BZ_DATA_ERROR);
274       GET_UCHAR(BZ_X_BLKHDR_2, uc);
275       if (uc != 0x41) RETURN(BZ_DATA_ERROR);
276       GET_UCHAR(BZ_X_BLKHDR_3, uc);
277       if (uc != 0x59) RETURN(BZ_DATA_ERROR);
278       GET_UCHAR(BZ_X_BLKHDR_4, uc);
279       if (uc != 0x26) RETURN(BZ_DATA_ERROR);
280       GET_UCHAR(BZ_X_BLKHDR_5, uc);
281       if (uc != 0x53) RETURN(BZ_DATA_ERROR);
282       GET_UCHAR(BZ_X_BLKHDR_6, uc);
283       if (uc != 0x59) RETURN(BZ_DATA_ERROR);
284 
285       s->currBlockNo++;
286       if (s->verbosity >= 2)
287          VPrintf1 ( "\n    [%d: huff+mtf ", s->currBlockNo );
288 
289       s->storedBlockCRC = 0;
290       GET_UCHAR(BZ_X_BCRC_1, uc);
291       s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
292       GET_UCHAR(BZ_X_BCRC_2, uc);
293       s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
294       GET_UCHAR(BZ_X_BCRC_3, uc);
295       s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
296       GET_UCHAR(BZ_X_BCRC_4, uc);
297       s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
298 
299       GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);
300 
301       s->origPtr = 0;
302       GET_UCHAR(BZ_X_ORIGPTR_1, uc);
303       s->origPtr = (s->origPtr << 8) | ((Int32)uc);
304       GET_UCHAR(BZ_X_ORIGPTR_2, uc);
305       s->origPtr = (s->origPtr << 8) | ((Int32)uc);
306       GET_UCHAR(BZ_X_ORIGPTR_3, uc);
307       s->origPtr = (s->origPtr << 8) | ((Int32)uc);
308 
309       if (s->origPtr < 0)
310          RETURN(BZ_DATA_ERROR);
311       if (s->origPtr > 10 + 100000*s->blockSize100k)
312          RETURN(BZ_DATA_ERROR);
313 
314       /*--- Receive the mapping table ---*/
315       for (i = 0; i < 16; i++) {
316          GET_BIT(BZ_X_MAPPING_1, uc);
317          if (uc == 1)
318             s->inUse16[i] = True; else
319             s->inUse16[i] = False;
320       }
321 
322       for (i = 0; i < 256; i++) s->inUse[i] = False;
323 
324       for (i = 0; i < 16; i++)
325          if (s->inUse16[i])
326             for (j = 0; j < 16; j++) {
327                GET_BIT(BZ_X_MAPPING_2, uc);
328                if (uc == 1) s->inUse[i * 16 + j] = True;
329             }
330       makeMaps_d ( s );
331       if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
332       alphaSize = s->nInUse+2;
333 
334       /*--- Now the selectors ---*/
335       GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
336       if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
337       GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
338       if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
339       for (i = 0; i < nSelectors; i++) {
340          j = 0;
341          while (True) {
342             GET_BIT(BZ_X_SELECTOR_3, uc);
343             if (uc == 0) break;
344             j++;
345             if (j >= nGroups) RETURN(BZ_DATA_ERROR);
346          }
347          s->selectorMtf[i] = j;
348       }
349 
350       /*--- Undo the MTF values for the selectors. ---*/
351       {
352          UChar pos[BZ_N_GROUPS], tmp, v;
353          for (v = 0; v < nGroups; v++) pos[v] = v;
354 
355          for (i = 0; i < nSelectors; i++) {
356             v = s->selectorMtf[i];
357             tmp = pos[v];
358             while (v > 0) { pos[v] = pos[v-1]; v--; }
359             pos[0] = tmp;
360             s->selector[i] = tmp;
361          }
362       }
363 
364       /*--- Now the coding tables ---*/
365       for (t = 0; t < nGroups; t++) {
366          GET_BITS(BZ_X_CODING_1, curr, 5);
367          for (i = 0; i < alphaSize; i++) {
368             while (True) {
369                if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR);
370                GET_BIT(BZ_X_CODING_2, uc);
371                if (uc == 0) break;
372                GET_BIT(BZ_X_CODING_3, uc);
373                if (uc == 0) curr++; else curr--;
374             }
375             s->len[t][i] = curr;
376          }
377       }
378 
379       /*--- Create the Huffman decoding tables ---*/
380       for (t = 0; t < nGroups; t++) {
381          minLen = 32;
382          maxLen = 0;
383          for (i = 0; i < alphaSize; i++) {
384             if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
385             if (s->len[t][i] < minLen) minLen = s->len[t][i];
386          }
387          BZ2_hbCreateDecodeTables (
388             &(s->limit[t][0]),
389             &(s->base[t][0]),
390             &(s->perm[t][0]),
391             &(s->len[t][0]),
392             minLen, maxLen, alphaSize
393          );
394          s->minLens[t] = minLen;
395       }
396 
397       /*--- Now the MTF values ---*/
398 
399       EOB      = s->nInUse+1;
400       nblockMAX = 100000 * s->blockSize100k;
401       groupNo  = -1;
402       groupPos = 0;
403 
404       for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
405 
406       /*-- MTF init --*/
407       {
408          Int32 ii, jj, kk;
409          kk = MTFA_SIZE-1;
410          for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
411             for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
412                s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj);
413                kk--;
414             }
415             s->mtfbase[ii] = kk + 1;
416          }
417       }
418       /*-- end MTF init --*/
419 
420       nblock = 0;
421       GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
422 
423       while (True) {
424 
425          if (nextSym == EOB) break;
426 
427          if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
428 
429             es = -1;
430             N = 1;
431             do {
432                /* Check that N doesn't get too big, so that es doesn't
433                   go negative.  The maximum value that can be
434                   RUNA/RUNB encoded is equal to the block size (post
435                   the initial RLE), viz, 900k, so bounding N at 2
436                   million should guard against overflow without
437                   rejecting any legitimate inputs. */
438                if (N >= 2*1024*1024) RETURN(BZ_DATA_ERROR);
439                if (nextSym == BZ_RUNA) es = es + (0+1) * N; else
440                if (nextSym == BZ_RUNB) es = es + (1+1) * N;
441                N = N * 2;
442                GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
443             }
444                while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
445 
446             es++;
447             uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
448             s->unzftab[uc] += es;
449 
450             if (s->smallDecompress)
451                while (es > 0) {
452                   if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
453                   s->ll16[nblock] = (UInt16)uc;
454                   nblock++;
455                   es--;
456                }
457             else
458                while (es > 0) {
459                   if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
460                   s->tt[nblock] = (UInt32)uc;
461                   nblock++;
462                   es--;
463                };
464 
465             continue;
466 
467          } else {
468 
469             if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
470 
471             /*-- uc = MTF ( nextSym-1 ) --*/
472             {
473                Int32 ii, jj, kk, pp, lno, off;
474                UInt32 nn;
475                nn = (UInt32)(nextSym - 1);
476 
477                if (nn < MTFL_SIZE) {
478                   /* avoid general-case expense */
479                   pp = s->mtfbase[0];
480                   uc = s->mtfa[pp+nn];
481                   while (nn > 3) {
482                      Int32 z = pp+nn;
483                      s->mtfa[(z)  ] = s->mtfa[(z)-1];
484                      s->mtfa[(z)-1] = s->mtfa[(z)-2];
485                      s->mtfa[(z)-2] = s->mtfa[(z)-3];
486                      s->mtfa[(z)-3] = s->mtfa[(z)-4];
487                      nn -= 4;
488                   }
489                   while (nn > 0) {
490                      s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
491                   };
492                   s->mtfa[pp] = uc;
493                } else {
494                   /* general case */
495                   lno = nn / MTFL_SIZE;
496                   off = nn % MTFL_SIZE;
497                   pp = s->mtfbase[lno] + off;
498                   uc = s->mtfa[pp];
499                   while (pp > s->mtfbase[lno]) {
500                      s->mtfa[pp] = s->mtfa[pp-1]; pp--;
501                   };
502                   s->mtfbase[lno]++;
503                   while (lno > 0) {
504                      s->mtfbase[lno]--;
505                      s->mtfa[s->mtfbase[lno]]
506                         = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
507                      lno--;
508                   }
509                   s->mtfbase[0]--;
510                   s->mtfa[s->mtfbase[0]] = uc;
511                   if (s->mtfbase[0] == 0) {
512                      kk = MTFA_SIZE-1;
513                      for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
514                         for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
515                            s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
516                            kk--;
517                         }
518                         s->mtfbase[ii] = kk + 1;
519                      }
520                   }
521                }
522             }
523             /*-- end uc = MTF ( nextSym-1 ) --*/
524 
525             s->unzftab[s->seqToUnseq[uc]]++;
526             if (s->smallDecompress)
527                s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else
528                s->tt[nblock]   = (UInt32)(s->seqToUnseq[uc]);
529             nblock++;
530 
531             GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
532             continue;
533          }
534       }
535 
536       /* Now we know what nblock is, we can do a better sanity
537          check on s->origPtr.
538       */
539       if (s->origPtr < 0 || s->origPtr >= nblock)
540          RETURN(BZ_DATA_ERROR);
541 
542       /*-- Set up cftab to facilitate generation of T^(-1) --*/
543       /* Check: unzftab entries in range. */
544       for (i = 0; i <= 255; i++) {
545          if (s->unzftab[i] < 0 || s->unzftab[i] > nblock)
546             RETURN(BZ_DATA_ERROR);
547       }
548       /* Actually generate cftab. */
549       s->cftab[0] = 0;
550       for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1];
551       for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
552       /* Check: cftab entries in range. */
553       for (i = 0; i <= 256; i++) {
554          if (s->cftab[i] < 0 || s->cftab[i] > nblock) {
555             /* s->cftab[i] can legitimately be == nblock */
556             RETURN(BZ_DATA_ERROR);
557          }
558       }
559       /* Check: cftab entries non-descending. */
560       for (i = 1; i <= 256; i++) {
561          if (s->cftab[i-1] > s->cftab[i]) {
562             RETURN(BZ_DATA_ERROR);
563          }
564       }
565 
566       s->state_out_len = 0;
567       s->state_out_ch  = 0;
568       BZ_INITIALISE_CRC ( s->calculatedBlockCRC );
569       s->state = BZ_X_OUTPUT;
570       if (s->verbosity >= 2) VPrintf0 ( "rt+rld" );
571 
572       if (s->smallDecompress) {
573 
574          /*-- Make a copy of cftab, used in generation of T --*/
575          for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
576 
577          /*-- compute the T vector --*/
578          for (i = 0; i < nblock; i++) {
579             uc = (UChar)(s->ll16[i]);
580             SET_LL(i, s->cftabCopy[uc]);
581             s->cftabCopy[uc]++;
582          }
583 
584          /*-- Compute T^(-1) by pointer reversal on T --*/
585          i = s->origPtr;
586          j = GET_LL(i);
587          do {
588             Int32 tmp = GET_LL(j);
589             SET_LL(j, i);
590             i = j;
591             j = tmp;
592          }
593             while (i != s->origPtr);
594 
595          s->tPos = s->origPtr;
596          s->nblock_used = 0;
597          if (s->blockRandomised) {
598             BZ_RAND_INIT_MASK;
599             BZ_GET_SMALL(s->k0); s->nblock_used++;
600             BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
601          } else {
602             BZ_GET_SMALL(s->k0); s->nblock_used++;
603          }
604 
605       } else {
606 
607          /*-- compute the T^(-1) vector --*/
608          for (i = 0; i < nblock; i++) {
609             uc = (UChar)(s->tt[i] & 0xff);
610             s->tt[s->cftab[uc]] |= (i << 8);
611             s->cftab[uc]++;
612          }
613 
614          s->tPos = s->tt[s->origPtr] >> 8;
615          s->nblock_used = 0;
616          if (s->blockRandomised) {
617             BZ_RAND_INIT_MASK;
618             BZ_GET_FAST(s->k0); s->nblock_used++;
619             BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
620          } else {
621             BZ_GET_FAST(s->k0); s->nblock_used++;
622          }
623 
624       }
625 
626       RETURN(BZ_OK);
627 
628 
629 
630     endhdr_2:
631 
632       GET_UCHAR(BZ_X_ENDHDR_2, uc);
633       if (uc != 0x72) RETURN(BZ_DATA_ERROR);
634       GET_UCHAR(BZ_X_ENDHDR_3, uc);
635       if (uc != 0x45) RETURN(BZ_DATA_ERROR);
636       GET_UCHAR(BZ_X_ENDHDR_4, uc);
637       if (uc != 0x38) RETURN(BZ_DATA_ERROR);
638       GET_UCHAR(BZ_X_ENDHDR_5, uc);
639       if (uc != 0x50) RETURN(BZ_DATA_ERROR);
640       GET_UCHAR(BZ_X_ENDHDR_6, uc);
641       if (uc != 0x90) RETURN(BZ_DATA_ERROR);
642 
643       s->storedCombinedCRC = 0;
644       GET_UCHAR(BZ_X_CCRC_1, uc);
645       s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
646       GET_UCHAR(BZ_X_CCRC_2, uc);
647       s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
648       GET_UCHAR(BZ_X_CCRC_3, uc);
649       s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
650       GET_UCHAR(BZ_X_CCRC_4, uc);
651       s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
652 
653       s->state = BZ_X_IDLE;
654       RETURN(BZ_STREAM_END);
655 
656       default: AssertH ( False, 4001 );
657    }
658 
659    AssertH ( False, 4002 );
660 
661    save_state_and_return:
662 
663    s->save_i           = i;
664    s->save_j           = j;
665    s->save_t           = t;
666    s->save_alphaSize   = alphaSize;
667    s->save_nGroups     = nGroups;
668    s->save_nSelectors  = nSelectors;
669    s->save_EOB         = EOB;
670    s->save_groupNo     = groupNo;
671    s->save_groupPos    = groupPos;
672    s->save_nextSym     = nextSym;
673    s->save_nblockMAX   = nblockMAX;
674    s->save_nblock      = nblock;
675    s->save_es          = es;
676    s->save_N           = N;
677    s->save_curr        = curr;
678    s->save_zt          = zt;
679    s->save_zn          = zn;
680    s->save_zvec        = zvec;
681    s->save_zj          = zj;
682    s->save_gSel        = gSel;
683    s->save_gMinlen     = gMinlen;
684    s->save_gLimit      = gLimit;
685    s->save_gBase       = gBase;
686    s->save_gPerm       = gPerm;
687 
688    return retVal;
689 }
690 
691 
692 /*-------------------------------------------------------------*/
693 /*--- end                                      decompress.c ---*/
694 /*-------------------------------------------------------------*/
695