jdk/src/share/native/java/util/zip/zlib-1.1.3/infblock.c
changeset 3713 3f49733cf145
parent 3637 84b603d2b088
parent 3712 8851d55adef0
child 3714 6a4eb8f53f91
child 4197 f6266efbfc05
equal deleted inserted replaced
3637:84b603d2b088 3713:3f49733cf145
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.  Sun designates this
       
     7  * particular file as subject to the "Classpath" exception as provided
       
     8  * by Sun in the LICENSE file that accompanied this code.
       
     9  *
       
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13  * version 2 for more details (a copy is included in the LICENSE file that
       
    14  * accompanied this code).
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License version
       
    17  * 2 along with this work; if not, write to the Free Software Foundation,
       
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19  *
       
    20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    21  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    22  * have any questions.
       
    23  *
       
    24  * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
       
    25  */
       
    26 
       
    27 /*
       
    28  * This file is available under and governed by the GNU General Public
       
    29  * License version 2 only, as published by the Free Software Foundation.
       
    30  * However, the following notice accompanied the original version of this
       
    31  * file and, per its terms, should not be removed:
       
    32  *
       
    33  * infblock.c -- interpret and process block types to last block
       
    34  * Copyright (C) 1995-1998 Mark Adler
       
    35  * For conditions of distribution and use, see copyright notice in zlib.h
       
    36  */
       
    37 
       
    38 #include "zutil.h"
       
    39 #include "infblock.h"
       
    40 #include "inftrees.h"
       
    41 #include "infcodes.h"
       
    42 #include "infutil.h"
       
    43 
       
    44 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
       
    45 
       
    46 /* simplify the use of the inflate_huft type with some defines */
       
    47 #define exop word.what.Exop
       
    48 #define bits word.what.Bits
       
    49 
       
    50 /* Table for deflate from PKZIP's appnote.txt. */
       
    51 local const uInt border[] = { /* Order of the bit length code lengths */
       
    52         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
       
    53 
       
    54 /*
       
    55    Notes beyond the 1.93a appnote.txt:
       
    56 
       
    57    1. Distance pointers never point before the beginning of the output
       
    58       stream.
       
    59    2. Distance pointers can point back across blocks, up to 32k away.
       
    60    3. There is an implied maximum of 7 bits for the bit length table and
       
    61       15 bits for the actual data.
       
    62    4. If only one code exists, then it is encoded using one bit.  (Zero
       
    63       would be more efficient, but perhaps a little confusing.)  If two
       
    64       codes exist, they are coded using one bit each (0 and 1).
       
    65    5. There is no way of sending zero distance codes--a dummy must be
       
    66       sent if there are none.  (History: a pre 2.0 version of PKZIP would
       
    67       store blocks with no distance codes, but this was discovered to be
       
    68       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
       
    69       zero distance codes, which is sent as one code of zero bits in
       
    70       length.
       
    71    6. There are up to 286 literal/length codes.  Code 256 represents the
       
    72       end-of-block.  Note however that the static length tree defines
       
    73       288 codes just to fill out the Huffman codes.  Codes 286 and 287
       
    74       cannot be used though, since there is no length base or extra bits
       
    75       defined for them.  Similarily, there are up to 30 distance codes.
       
    76       However, static trees define 32 codes (all 5 bits) to fill out the
       
    77       Huffman codes, but the last two had better not show up in the data.
       
    78    7. Unzip can check dynamic Huffman blocks for complete code sets.
       
    79       The exception is that a single code would not be complete (see #4).
       
    80    8. The five bits following the block type is really the number of
       
    81       literal codes sent minus 257.
       
    82    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
       
    83       (1+6+6).  Therefore, to output three times the length, you output
       
    84       three codes (1+1+1), whereas to output four times the same length,
       
    85       you only need two codes (1+3).  Hmm.
       
    86   10. In the tree reconstruction algorithm, Code = Code + Increment
       
    87       only if BitLength(i) is not zero.  (Pretty obvious.)
       
    88   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
       
    89   12. Note: length code 284 can represent 227-258, but length code 285
       
    90       really is 258.  The last length deserves its own, short code
       
    91       since it gets used a lot in very redundant files.  The length
       
    92       258 is special since 258 - 3 (the min match length) is 255.
       
    93   13. The literal/length and distance code bit lengths are read as a
       
    94       single stream of lengths.  It is possible (and advantageous) for
       
    95       a repeat code (16, 17, or 18) to go across the boundary between
       
    96       the two sets of lengths.
       
    97  */
       
    98 
       
    99 
       
   100 void inflate_blocks_reset(s, z, c)
       
   101 inflate_blocks_statef *s;
       
   102 z_streamp z;
       
   103 uLongf *c;
       
   104 {
       
   105   if (c != Z_NULL)
       
   106     *c = s->check;
       
   107   if (s->mode == BTREE || s->mode == DTREE)
       
   108     ZFREE(z, s->sub.trees.blens);
       
   109   if (s->mode == CODES)
       
   110     inflate_codes_free(s->sub.decode.codes, z);
       
   111   s->mode = TYPE;
       
   112   s->bitk = 0;
       
   113   s->bitb = 0;
       
   114   s->read = s->write = s->window;
       
   115   if (s->checkfn != Z_NULL)
       
   116     z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
       
   117   Tracev((stderr, "inflate:   blocks reset\n"));
       
   118 }
       
   119 
       
   120 
       
   121 inflate_blocks_statef *inflate_blocks_new(z, c, w)
       
   122 z_streamp z;
       
   123 check_func c;
       
   124 uInt w;
       
   125 {
       
   126   inflate_blocks_statef *s;
       
   127 
       
   128   if ((s = (inflate_blocks_statef *)ZALLOC
       
   129        (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
       
   130     return s;
       
   131   if ((s->hufts =
       
   132        (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
       
   133   {
       
   134     ZFREE(z, s);
       
   135     return Z_NULL;
       
   136   }
       
   137   if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
       
   138   {
       
   139     ZFREE(z, s->hufts);
       
   140     ZFREE(z, s);
       
   141     return Z_NULL;
       
   142   }
       
   143   s->end = s->window + w;
       
   144   s->checkfn = c;
       
   145   s->mode = TYPE;
       
   146   Tracev((stderr, "inflate:   blocks allocated\n"));
       
   147   inflate_blocks_reset(s, z, Z_NULL);
       
   148   return s;
       
   149 }
       
   150 
       
   151 
       
   152 int inflate_blocks(s, z, r)
       
   153 inflate_blocks_statef *s;
       
   154 z_streamp z;
       
   155 int r;
       
   156 {
       
   157   uInt t;               /* temporary storage */
       
   158   uLong b;              /* bit buffer */
       
   159   uInt k;               /* bits in bit buffer */
       
   160   Bytef *p;             /* input data pointer */
       
   161   uInt n;               /* bytes available there */
       
   162   Bytef *q;             /* output window write pointer */
       
   163   uInt m;               /* bytes to end of window or read pointer */
       
   164 
       
   165   /* copy input/output information to locals (UPDATE macro restores) */
       
   166   LOAD
       
   167 
       
   168   /* process input based on current state */
       
   169   while (1) switch (s->mode)
       
   170   {
       
   171     case TYPE:
       
   172       NEEDBITS(3)
       
   173       t = (uInt)b & 7;
       
   174       s->last = t & 1;
       
   175       switch (t >> 1)
       
   176       {
       
   177         case 0:                         /* stored */
       
   178           Tracev((stderr, "inflate:     stored block%s\n",
       
   179                  s->last ? " (last)" : ""));
       
   180           DUMPBITS(3)
       
   181           t = k & 7;                    /* go to byte boundary */
       
   182           DUMPBITS(t)
       
   183           s->mode = LENS;               /* get length of stored block */
       
   184           break;
       
   185         case 1:                         /* fixed */
       
   186           Tracev((stderr, "inflate:     fixed codes block%s\n",
       
   187                  s->last ? " (last)" : ""));
       
   188           {
       
   189             uInt bl, bd;
       
   190             inflate_huft *tl, *td;
       
   191 
       
   192             inflate_trees_fixed(&bl, &bd, &tl, &td, z);
       
   193             s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
       
   194             if (s->sub.decode.codes == Z_NULL)
       
   195             {
       
   196               r = Z_MEM_ERROR;
       
   197               LEAVE
       
   198             }
       
   199           }
       
   200           DUMPBITS(3)
       
   201           s->mode = CODES;
       
   202           break;
       
   203         case 2:                         /* dynamic */
       
   204           Tracev((stderr, "inflate:     dynamic codes block%s\n",
       
   205                  s->last ? " (last)" : ""));
       
   206           DUMPBITS(3)
       
   207           s->mode = TABLE;
       
   208           break;
       
   209         case 3:                         /* illegal */
       
   210           DUMPBITS(3)
       
   211           s->mode = BAD;
       
   212           z->msg = (char*)"invalid block type";
       
   213           r = Z_DATA_ERROR;
       
   214           LEAVE
       
   215       }
       
   216       break;
       
   217     case LENS:
       
   218       NEEDBITS(32)
       
   219       if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
       
   220       {
       
   221         s->mode = BAD;
       
   222         z->msg = (char*)"invalid stored block lengths";
       
   223         r = Z_DATA_ERROR;
       
   224         LEAVE
       
   225       }
       
   226       s->sub.left = (uInt)b & 0xffff;
       
   227       b = k = 0;                      /* dump bits */
       
   228       Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
       
   229       s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
       
   230       break;
       
   231     case STORED:
       
   232       if (n == 0)
       
   233         LEAVE
       
   234       NEEDOUT
       
   235       t = s->sub.left;
       
   236       if (t > n) t = n;
       
   237       if (t > m) t = m;
       
   238       zmemcpy(q, p, t);
       
   239       p += t;  n -= t;
       
   240       q += t;  m -= t;
       
   241       if ((s->sub.left -= t) != 0)
       
   242         break;
       
   243       Tracev((stderr, "inflate:       stored end, %lu total out\n",
       
   244               z->total_out + (q >= s->read ? q - s->read :
       
   245               (s->end - s->read) + (q - s->window))));
       
   246       s->mode = s->last ? DRY : TYPE;
       
   247       break;
       
   248     case TABLE:
       
   249       NEEDBITS(14)
       
   250       s->sub.trees.table = t = (uInt)b & 0x3fff;
       
   251 #ifndef PKZIP_BUG_WORKAROUND
       
   252       if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
       
   253       {
       
   254         s->mode = BAD;
       
   255         z->msg = (char*)"too many length or distance symbols";
       
   256         r = Z_DATA_ERROR;
       
   257         LEAVE
       
   258       }
       
   259 #endif
       
   260       t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
       
   261       if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
       
   262       {
       
   263         r = Z_MEM_ERROR;
       
   264         LEAVE
       
   265       }
       
   266       DUMPBITS(14)
       
   267       s->sub.trees.index = 0;
       
   268       Tracev((stderr, "inflate:       table sizes ok\n"));
       
   269       s->mode = BTREE;
       
   270     case BTREE:
       
   271       while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
       
   272       {
       
   273         NEEDBITS(3)
       
   274         s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
       
   275         DUMPBITS(3)
       
   276       }
       
   277       while (s->sub.trees.index < 19)
       
   278         s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
       
   279       s->sub.trees.bb = 7;
       
   280       t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
       
   281                              &s->sub.trees.tb, s->hufts, z);
       
   282       if (t != Z_OK)
       
   283       {
       
   284 
       
   285         r = t;
       
   286         if (r == Z_DATA_ERROR)
       
   287         {
       
   288           ZFREE(z, s->sub.trees.blens);
       
   289           s->mode = BAD;
       
   290         }
       
   291         LEAVE
       
   292       }
       
   293       s->sub.trees.index = 0;
       
   294       Tracev((stderr, "inflate:       bits tree ok\n"));
       
   295       s->mode = DTREE;
       
   296     case DTREE:
       
   297       while (t = s->sub.trees.table,
       
   298              s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
       
   299       {
       
   300         inflate_huft *h;
       
   301         uInt i, j, c;
       
   302 
       
   303         t = s->sub.trees.bb;
       
   304         NEEDBITS(t)
       
   305         h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
       
   306         t = h->bits;
       
   307         c = h->base;
       
   308         if (c < 16)
       
   309         {
       
   310           DUMPBITS(t)
       
   311           s->sub.trees.blens[s->sub.trees.index++] = c;
       
   312         }
       
   313         else /* c == 16..18 */
       
   314         {
       
   315           i = c == 18 ? 7 : c - 14;
       
   316           j = c == 18 ? 11 : 3;
       
   317           NEEDBITS(t + i)
       
   318           DUMPBITS(t)
       
   319           j += (uInt)b & inflate_mask[i];
       
   320           DUMPBITS(i)
       
   321           i = s->sub.trees.index;
       
   322           t = s->sub.trees.table;
       
   323           if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
       
   324               (c == 16 && i < 1))
       
   325           {
       
   326             ZFREE(z, s->sub.trees.blens);
       
   327             s->mode = BAD;
       
   328             z->msg = (char*)"invalid bit length repeat";
       
   329             r = Z_DATA_ERROR;
       
   330             LEAVE
       
   331           }
       
   332           c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
       
   333           do {
       
   334             s->sub.trees.blens[i++] = c;
       
   335           } while (--j);
       
   336           s->sub.trees.index = i;
       
   337         }
       
   338       }
       
   339       s->sub.trees.tb = Z_NULL;
       
   340       {
       
   341         uInt bl, bd;
       
   342         inflate_huft *tl, *td;
       
   343         inflate_codes_statef *c;
       
   344 
       
   345         bl = 9;         /* must be <= 9 for lookahead assumptions */
       
   346         bd = 6;         /* must be <= 9 for lookahead assumptions */
       
   347         t = s->sub.trees.table;
       
   348         t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
       
   349                                   s->sub.trees.blens, &bl, &bd, &tl, &td,
       
   350                                   s->hufts, z);
       
   351 
       
   352         if (t != Z_OK)
       
   353         {
       
   354           if (t == (uInt)Z_DATA_ERROR)
       
   355           {
       
   356               ZFREE(z, s->sub.trees.blens);
       
   357               s->mode = BAD;
       
   358           }
       
   359           r = t;
       
   360           LEAVE
       
   361         }
       
   362         Tracev((stderr, "inflate:       trees ok\n"));
       
   363         if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
       
   364         {
       
   365           r = Z_MEM_ERROR;
       
   366           LEAVE
       
   367         }
       
   368         s->sub.decode.codes = c;
       
   369       }
       
   370       ZFREE(z, s->sub.trees.blens);
       
   371       s->mode = CODES;
       
   372     case CODES:
       
   373       UPDATE
       
   374       if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
       
   375         return inflate_flush(s, z, r);
       
   376       r = Z_OK;
       
   377       inflate_codes_free(s->sub.decode.codes, z);
       
   378       LOAD
       
   379       Tracev((stderr, "inflate:       codes end, %lu total out\n",
       
   380               z->total_out + (q >= s->read ? q - s->read :
       
   381               (s->end - s->read) + (q - s->window))));
       
   382       if (!s->last)
       
   383       {
       
   384         s->mode = TYPE;
       
   385         break;
       
   386       }
       
   387       s->mode = DRY;
       
   388     case DRY:
       
   389       FLUSH
       
   390       if (s->read != s->write)
       
   391         LEAVE
       
   392       s->mode = DONE;
       
   393     case DONE:
       
   394       r = Z_STREAM_END;
       
   395       LEAVE
       
   396     case BAD:
       
   397       r = Z_DATA_ERROR;
       
   398       LEAVE
       
   399     default:
       
   400       r = Z_STREAM_ERROR;
       
   401       LEAVE
       
   402   }
       
   403 }
       
   404 
       
   405 
       
   406 int inflate_blocks_free(s, z)
       
   407 inflate_blocks_statef *s;
       
   408 z_streamp z;
       
   409 {
       
   410   inflate_blocks_reset(s, z, Z_NULL);
       
   411   ZFREE(z, s->window);
       
   412   ZFREE(z, s->hufts);
       
   413   ZFREE(z, s);
       
   414   Tracev((stderr, "inflate:   blocks freed\n"));
       
   415   return Z_OK;
       
   416 }
       
   417 
       
   418 
       
   419 void inflate_set_dictionary(s, d, n)
       
   420 inflate_blocks_statef *s;
       
   421 const Bytef *d;
       
   422 uInt  n;
       
   423 {
       
   424   zmemcpy(s->window, d, n);
       
   425   s->read = s->write = s->window + n;
       
   426 }
       
   427 
       
   428 
       
   429 /* Returns true if inflate is currently at the end of a block generated
       
   430  * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
       
   431  * IN assertion: s != Z_NULL
       
   432  */
       
   433 int inflate_blocks_sync_point(s)
       
   434 inflate_blocks_statef *s;
       
   435 {
       
   436   return s->mode == LENS;
       
   437 }