home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / Jikes / Source / src / unzip.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-03  |  16.8 KB  |  384 lines

  1. // $Id: unzip.h,v 1.7 2001/01/05 09:13:21 mdejong Exp $
  2. #ifndef unzip_INCLUDED
  3. #define unzip_INCLUDED
  4.  
  5. #include "platform.h"
  6.  
  7. #ifdef    HAVE_JIKES_NAMESPACE
  8. namespace Jikes {    // Open namespace Jikes block
  9. #endif
  10.  
  11. //
  12. // NOTE: Jikes incorporates compression code from the Info-ZIP
  13. // group. There are no extra charges or costs due to the use of
  14. // this code, and the original compression sources are freely
  15. // available from http://www.cdrom/com/pub/infozip/ or
  16. // ftp://ftp.cdrom.com/pub/infozip/ on the Internet.
  17. // The sole use by Jikes of this compression code is contained in the
  18. // files unzip.h and unzip.cpp, which are based on Info-ZIP's inflate.c and
  19. // associated header files.
  20. //
  21.  
  22. //
  23. // You can do whatever you like with this source file, though I would
  24. // prefer that if you modify it and redistribute it that you include
  25. // comments to that effect with your name and the date.  Thank you.
  26. // The abbreviated History list below includes the work of the
  27. // following:
  28. // M. Adler, G. Roelofs, J-l. Failly, J. Bush, C. Ghisler, A. Verheijen,
  29. // P. Kienitz, C. Spieler, S. Maxwell, J. Altman
  30. // Only the first and last entries from the original inflate.c are
  31. // reproduced here.
  32. //
  33.  
  34. //
  35. // History:
  36. // vers    date          who           what
  37. // ----  ---------  --------------  ------------------------------------
  38. //  a    ~~ Feb 92  M. Adler        used full (large, one-step) lookup table
  39. //  ...
  40. //  c16  20 Apr 97  J. Altman       added memzero(v[]) in huft_build()
  41. //
  42.  
  43. #define DFUNZIP /* needed for unzip compilation*/
  44. //FIXME: need to move to platform.h
  45. //#include <stdio.h>
  46. //#include <stdlib.h>
  47. //#include <string.h>
  48.  
  49. //
  50. // inflate.c -- put in the public domain by Mark Adler
  51. // version c15c, 28 March 1997
  52. //
  53.  
  54. //
  55. // Inflate deflated (PKZIP's method 8 compressed) data.  The compression
  56. // method searches for as much of the current string of bytes (up to a
  57. // length of 258) in the previous 32K bytes.  If it doesn't find any
  58. // matches (of at least length 3), it codes the next byte.  Otherwise, it
  59. // codes the length of the matched string and its distance backwards from
  60. // the current position.  There is a single Huffman code that codes both
  61. // single bytes (called "literals") and match lengths.  A second Huffman
  62. // code codes the distance information, which follows a length code.  Each
  63. // length or distance code actually represents a base value and a number
  64. // of "extra" (sometimes zero) bits to get to add to the base value.  At
  65. // the end of each deflated block is a special end-of-block (EOB) literal/
  66. // length code.  The decoding process is basically: get a literal/length
  67. // code; if EOB then done; if a literal, emit the decoded byte; if a
  68. // length then get the distance and emit the referred-to bytes from the
  69. // sliding window of previously emitted data.
  70.  
  71. // There are (currently) three kinds of inflate blocks: stored, fixed, and
  72. // dynamic.  The compressor outputs a chunk of data at a time and decides
  73. // which method to use on a chunk-by-chunk basis.  A chunk might typically
  74. // be 32K to 64K, uncompressed.  If the chunk is uncompressible, then the
  75. // "stored" method is used.  In this case, the bytes are simply stored as
  76. // is, eight bits per byte, with none of the above coding.  The bytes are
  77. // preceded by a count, since there is no longer an EOB code.
  78.  
  79. // If the data are compressible, then either the fixed or dynamic methods
  80. // are used.  In the dynamic method, the compressed data are preceded by
  81. // an encoding of the literal/length and distance Huffman codes that are
  82. // to be used to decode this block.  The representation is itself Huffman
  83. // coded, and so is preceded by a description of that code.  These code
  84. // descriptions take up a little space, and so for small blocks, there is
  85. // a predefined set of codes, called the fixed codes.  The fixed method is
  86. // used if the block ends up smaller that way (usually for quite small
  87. // chunks); otherwise the dynamic method is used.  In the latter case, the
  88. // codes are customized to the probabilities in the current block and so
  89. // can code it much better than the pre-determined fixed codes can.
  90.  
  91. // The Huffman codes themselves are decoded using a multi-level table
  92. // lookup, in order to maximize the speed of decoding plus the speed of
  93. // building the decoding tables.  See the comments below that precede the
  94. // lbits and dbits tuning parameters.
  95.  
  96. // GRR:  return values(?)
  97. //         0  OK
  98. //         1  incomplete table
  99. //         2  bad input
  100. //         3  not enough memory
  101. //
  102.  
  103. //
  104. // If BMAX needs to be larger than 16, then h and x[] should be unsigned long.
  105. //
  106. #define BMAX 16         /* maximum bit length of any code (16 for explode) */
  107. #define N_MAX 288       /* maximum number of codes in any set */
  108.  
  109.  
  110. //
  111. // Notes beyond the 1.93a appnote.txt:
  112.  
  113. // 1. Distance pointers never point before the beginning of the output
  114. //    stream.
  115. // 2. Distance pointers can point back across blocks, up to 32k away.
  116. // 3. There is an implied maximum of 7 bits for the bit length table and
  117. //    15 bits for the actual data.
  118. // 4. If only one code exists, then it is encoded using one bit.  (Zero
  119. //    would be more efficient, but perhaps a little confusing.)  If two
  120. //    codes exist, they are coded using one bit each (0 and 1).
  121. // 5. There is no way of sending zero distance codes--a dummy must be
  122. //    sent if there are none.  (History: a pre 2.0 version of PKZIP would
  123. //    store blocks with no distance codes, but this was discovered to be
  124. //    too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
  125. //    zero distance codes, which is sent as one code of zero bits in
  126. //    length.
  127. // 6. There are up to 286 literal/length codes.  Code 256 represents the
  128. //    end-of-block.  Note however that the static length tree defines
  129. //    288 codes just to fill out the Huffman codes.  Codes 286 and 287
  130. //    cannot be used though, since there is no length base or extra bits
  131. //    defined for them.  Similarily, there are up to 30 distance codes.
  132. //    However, static trees define 32 codes (all 5 bits) to fill out the
  133. //    Huffman codes, but the last two had better not show up in the data.
  134. // 7. Unzip can check dynamic Huffman blocks for complete code sets.
  135. //    The exception is that a single code would not be complete (see #4).
  136. // 8. The five bits following the block type is really the number of
  137. //    literal codes sent minus 257.
  138. // 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  139. //    (1+6+6).  Therefore, to output three times the length, you output
  140. //    three codes (1+1+1), whereas to output four times the same length,
  141. //    you only need two codes (1+3).  Hmm.
  142. //10. In the tree reconstruction algorithm, Code = Code + Increment
  143. //    only if BitLength(i) is not zero.  (Pretty obvious.)
  144. //11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
  145. //12. Note: length code 284 can represent 227-258, but length code 285
  146. //    really is 258.  The last length deserves its own, short code
  147. //    since it gets used a lot in very redundant files.  The length
  148. //    258 is special since 258 - 3 (the min match length) is 255.
  149. //13. The literal/length and distance code bit lengths are read as a
  150. //    single stream of lengths.  It is possible (and advantageous) for
  151. //    a repeat code (16, 17, or 18) to go across the boundary between
  152. //    the two sets of lengths.
  153. //
  154.  
  155. #define PKZIP_BUG_WORKAROUND    /* PKZIP 1.93a problem--live with it */
  156.  
  157. //
  158. //  inflate.h must supply the unsigned char slide[WSIZE] array, the zvoid typedef
  159. //  (void if (void *) is accepted, else char) and the NEXTBYTE,
  160. //  FLUSH() and memzero macros.  If the window size is not 32K, it
  161. //  should also define WSIZE.  If INFMOD is defined, it can include
  162. //  compiled functions to support the NEXTBYTE and/or FLUSH() macros.
  163. //  There are defaults for NEXTBYTE and FLUSH() below for use as
  164. //  examples of what those functions need to do.  Normally, you would
  165. //  also want FLUSH() to compute a crc on the data.
  166.  
  167. //  This module uses the external functions malloc() and free() (and
  168. //  probably memset() or bzero() in the memzero() macro).  Their
  169. //  prototypes are normally found in <string.h> and <stdlib.h>.
  170. //
  171.  
  172. /* #define DEBUG */
  173.  
  174.  
  175. #ifndef WSIZE           /* default is 32K */
  176. #  define WSIZE 0x8000  /* window size--must be a power of two, and at least */
  177. #endif                  /* 32K for zip's deflate method */
  178.  
  179. #  define wsize WSIZE       /* wsize is a constant */
  180.  
  181.  
  182. #ifndef NEXTBYTE        /* default is to simply get a byte from stdin */
  183. /* default for   define NEXTBYTE is  getchar() */
  184. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  185.     #define NEXTBYTE getc(global_file)
  186. #elif defined(WIN32_FILE_SYSTEM)
  187.     #define NEXTBYTE ((u1) (*global_file++))
  188. #endif
  189. #endif
  190.  
  191. #ifndef MESSAGE   /* only used twice, for fixed strings--NOT general-purpose */
  192. #  define MESSAGE(str,len,flag)  fprintf(stderr,(char *)(str))
  193. #endif
  194.  
  195. #ifndef FLUSH           /* default is to simply write the buffer to stdout */
  196. /* default   define FLUSH(n) is fwrite(slide_buffer, 1, n, stdout)   return value not used */
  197. #define FLUSH(n) memcpy(global_bufferp, slide_buffer, n); global_bufferp += n
  198. #endif
  199. /* Warning: the fwrite above might not work on 16-bit compilers, since
  200.    0x8000 might be interpreted as -32,768 by the library function. */
  201.  
  202. #ifndef Trace
  203. #  ifdef DEBUG
  204. #    define Trace(x) fprintf x
  205. #  else
  206. #    define Trace(x)
  207. #  endif
  208. #endif
  209.  
  210.  
  211. /*---------------------------------------------------------------------------*/
  212.  
  213. // Macros for inflate() bit peeking and grabbing.
  214. // The usage is:
  215. //
  216. //      NEEDBITS(j)
  217. //      x = b & mask_bits[j];
  218. //
  219. //      DUMPBITS(j)
  220. // where NEEDBITS makes sure that b has at least j bits in it, and
  221. // DUMPBITS removes the bits from b.  The macros use the variable k
  222. // for the number of bits in b.  Normally, b and k are register
  223. // variables for speed and are initialized at the begining of a
  224. // routine that uses these macros from a global bit buffer and count.
  225. //
  226. // In order to not ask for more bits than there are in the compressed
  227. // stream, the Huffman tables are constructed to only ask for just
  228. // enough bits to make up the end-of-block code (value 256).  Then no
  229. // bytes need to be "returned" to the buffer at the end of the last
  230. // block.  See the huft_build() routine.
  231. //
  232.  
  233. #ifndef CHECK_EOF
  234. #  define CHECK_EOF   /* default as of 5.13/5.2 */
  235. #endif
  236.  
  237. #ifndef CHECK_EOF
  238. #  define NEEDBITS(n) {while(k<(n)){b|=((unsigned long)NEXTBYTE)<<k;k+=8;}}
  239. #else
  240. #  define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1; b|=((unsigned long)c)<<k;k+=8;}}
  241. #endif                      /* Piet Plomp:  change "return 1" to "break" */
  242.  
  243. #define DUMPBITS(n) {b>>=(n);k-=(n);}
  244.  
  245.  
  246. //
  247. // Huffman code lookup table entry--this entry is four bytes for machines
  248. // that have 16-bit pointers (e.g. PC's in the small or medium model).
  249. // Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
  250. // means that v is a literal, 16 < e < 32 means that v is a pointer to
  251. // the next table, which codes e - 16 bits, and lastly e == 99 indicates
  252. // an unused code.  If a code with e == 99 is looked up, this implies an
  253. // error in the data.
  254. //
  255. struct huft {
  256.     unsigned char e;                /* number of extra bits or operation */
  257.     unsigned char b;                /* number of bits in this code or subcode */
  258.     union {
  259.         unsigned short n;            /* literal, length base, or distance base */
  260.         struct huft *t;   /* pointer to next level of table */
  261.     } v;
  262. };
  263.  
  264. //
  265. // The inflate algorithm uses a sliding 32K byte window on the uncompressed
  266. // stream to find repeated byte strings.  This is implemented here as a
  267. // circular buffer.  The index is updated simply by incrementing and then
  268. // and'ing with 0x7fff (32K-1). */
  269. // It is left to other modules to supply the 32K area.  It is assumed
  270. // to be usable as if it were declared "uch slide[32768];" or as just
  271. // "uch *slide;" and then malloc'ed in the latter case.  The definition
  272. // must be in unzip.h, included above.
  273. //
  274. class Unzip
  275. {
  276. public:
  277.     static unsigned long global_bb;                         /* bit buffer */
  278.     static unsigned global_bk;                    /* bits in bit buffer */
  279.  
  280.     static unsigned global_wp;  /* current position in slide */
  281.     static unsigned global_hufts; /* huff memory usage */
  282.     static unsigned char slide_buffer[];
  283.     static struct huft *global_fixed_tl;    /* inflate static */
  284.     static struct huft *global_fixed_td;    /* inflate static */
  285.     static int global_fixed_bl,
  286.                global_fixed_bd;
  287. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  288.     static FILE *global_file; /* file pointer for zip file */
  289. #elif defined(WIN32_FILE_SYSTEM)
  290.     static char *global_file;
  291. #endif
  292.     static char *global_bufferp; /* current position in output buffer */
  293.  
  294.     /* Tables for deflate from PKZIP's appnote.txt. */
  295.     static unsigned border[];
  296.     static unsigned short cplens[];
  297.     static unsigned short cplext[]; /* Extra bits for literal codes 257..285 */
  298.     static unsigned short cpdist[]; /* Copy offsets for distance codes 0..29 */
  299.     static unsigned short cpdext[]; /* Extra bits for distance codes */
  300.  
  301.     /* moved to consts.h (included in unzip.c), resp. funzip.c */
  302.     /* And'ing with mask_bits[n] masks the lower n bits */
  303.     static unsigned short mask_bits[];
  304.  
  305.     //
  306.     // Huffman code decoding is performed using a multi-level table lookup.
  307.     // The fastest way to decode is to simply build a lookup table whose
  308.     // size is determined by the longest code.  However, the time it takes
  309.     // to build this table can also be a factor if the data being decoded
  310.     // are not very long.  The most common codes are necessarily the
  311.     // shortest codes, so those codes dominate the decoding time, and hence
  312.     // the speed.  The idea is you can have a shorter table that decodes the
  313.     // shorter, more probable codes, and then point to subsidiary tables for
  314.     // the longer codes.  The time it costs to decode the longer codes is
  315.     // then traded against the time it takes to make longer tables.
  316.     //
  317.     // This results of this trade are in the variables lbits and dbits
  318.     // below.  lbits is the number of bits the first level table for literal/
  319.     // length codes can decode in one step, and dbits is the same thing for
  320.     // the distance codes.  Subsequent tables are also less than or equal to
  321.     // those sizes.  These values may be adjusted either when all of the
  322.     // codes are shorter than that, in which case the longest code length in
  323.     // bits is used, or when the shortest code is *longer* than the requested
  324.     // table size, in which case the length of the shortest code in bits is
  325.     // used.
  326.     //
  327.     // There are two different values for the two tables, since they code a
  328.     // different number of possibilities each.  The literal/length table
  329.     // codes 286 possible values, or in a flat code, a little over eight
  330.     // bits.  The distance table codes 30 possible values, or a little less
  331.     // than five bits, flat.  The optimum values for speed end up being
  332.     // about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  333.     // The optimum values may differ though from machine to machine, and
  334.     // possibly even between compilers.  Your mileage may vary.
  335.     //
  336.  
  337.     static int lbits;           /* bits in base literal/length lookup table */
  338.     static int dbits;           /* bits in base distance lookup table */
  339.  
  340.     static int huft_build(unsigned *b,unsigned n, unsigned s, unsigned short *d, unsigned short *e, struct huft **t, int *m);
  341.     static int huft_free(struct huft *);
  342.     static int inflate_codes(struct huft *tl,struct huft * td, int  bl,int bd);
  343.     static int inflate_stored();
  344.     static int inflate_fixed();
  345.     static int inflate_dynamic();
  346.     static int inflate_block(int *);
  347.     static int inflate_free();
  348.  
  349. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  350.     static int unzip8(FILE * zipfile, char *buffer);
  351.  
  352.     static int UncompressFile0(FILE *, char *, long);
  353.     static int UncompressFile1(FILE *, char *, long);
  354.     static int UncompressFile2(FILE *, char *, long);
  355.     static int UncompressFile3(FILE *, char *, long);
  356.     static int UncompressFile4(FILE *, char *, long);
  357.     static int UncompressFile5(FILE *, char *, long);
  358.     static int UncompressFile6(FILE *, char *, long);
  359.     static int UncompressFile7(FILE *, char *, long);
  360.     static int UncompressFile8(FILE *, char *, long);
  361.     static int UncompressFile9(FILE *, char *, long);
  362. #elif defined(WIN32_FILE_SYSTEM)
  363.     static int unzip8(char *zipfile, char *buffer);
  364.  
  365.     static int UncompressFile0(char *, char *, long);
  366.     static int UncompressFile1(char *, char *, long);
  367.     static int UncompressFile2(char *, char *, long);
  368.     static int UncompressFile3(char *, char *, long);
  369.     static int UncompressFile4(char *, char *, long);
  370.     static int UncompressFile5(char *, char *, long);
  371.     static int UncompressFile6(char *, char *, long);
  372.     static int UncompressFile7(char *, char *, long);
  373.     static int UncompressFile8(char *, char *, long);
  374.     static int UncompressFile9(char *, char *, long);
  375. #endif
  376. }; // end class unzip
  377.  
  378. #ifdef    HAVE_JIKES_NAMESPACE
  379. }            // Close namespace Jikes block
  380. #endif
  381.  
  382. #endif /* unzip_INCLUDED */
  383.  
  384.