1 /* 2 * MSZIP decompression header (taken from cabinet.h of cabinet dll) 3 * 4 * Copyright 2002 Greg Turner 5 * Copyright 2005 Gerold Jens Wucherpfennig 6 * Copyright 2010 Christian Costa 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 21 */ 22 23 #include "fdi.h" 24 25 typedef unsigned char cab_UBYTE; /* 8 bits */ 26 typedef UINT16 cab_UWORD; /* 16 bits */ 27 typedef UINT32 cab_ULONG; /* 32 bits */ 28 typedef INT32 cab_LONG; /* 32 bits */ 29 30 typedef struct { 31 unsigned int FDI_Intmagic; 32 PFNALLOC pfnalloc; 33 PFNFREE pfnfree; 34 PFNOPEN pfnopen; 35 PFNREAD pfnread; 36 PFNWRITE pfnwrite; 37 PFNCLOSE pfnclose; 38 PFNSEEK pfnseek; 39 PERF perf; 40 } FDI_Int, *PFDI_Int; 41 42 /* cast an HFDI into a PFDI_Int */ 43 #define PFDI_INT(hfdi) ((PFDI_Int)(hfdi)) 44 45 #define PFDI_ALLOC(hfdi, size) ((*PFDI_INT(hfdi)->pfnalloc) (size)) 46 #define PFDI_FREE(hfdi, ptr) ((*PFDI_INT(hfdi)->pfnfree) (ptr)) 47 48 /* MSZIP stuff */ 49 #define ZIPWSIZE 0x8000 /* window size */ 50 #define ZIPLBITS 9 /* bits in base literal/length lookup table */ 51 #define ZIPDBITS 6 /* bits in base distance lookup table */ 52 #define ZIPBMAX 16 /* maximum bit length of any code */ 53 #define ZIPN_MAX 288 /* maximum number of codes in any set */ 54 55 struct Ziphuft { 56 cab_UBYTE e; /* number of extra bits or operation */ 57 cab_UBYTE b; /* number of bits in this code or subcode */ 58 union { 59 cab_UWORD n; /* literal, length base, or distance base */ 60 struct Ziphuft *t; /* pointer to next level of table */ 61 } v; 62 }; 63 64 struct ZIPstate { 65 cab_ULONG window_posn; /* current offset within the window */ 66 cab_ULONG bb; /* bit buffer */ 67 cab_ULONG bk; /* bits in bit buffer */ 68 cab_ULONG ll[288+32]; /* literal/length and distance code lengths */ 69 cab_ULONG c[ZIPBMAX+1]; /* bit length count table */ 70 cab_LONG lx[ZIPBMAX+1]; /* memory for l[-1..ZIPBMAX-1] */ 71 struct Ziphuft *u[ZIPBMAX]; /* table stack */ 72 cab_ULONG v[ZIPN_MAX]; /* values in order of bit length */ 73 cab_ULONG x[ZIPBMAX+1]; /* bit offsets, then code stack */ 74 cab_UBYTE *inpos; 75 }; 76 77 #define CAB(x) (decomp_state->x) 78 #define ZIP(x) (decomp_state->methods.zip.x) 79 #define DECR_OK (0) 80 #define DECR_DATAFORMAT (1) 81 #define DECR_ILLEGALDATA (2) 82 #define DECR_NOMEMORY (3) 83 #define DECR_CHECKSUM (4) 84 #define DECR_INPUT (5) 85 #define DECR_OUTPUT (6) 86 #define DECR_USERABORT (7) 87 88 #define ZIPNEEDBITS(n) {while(k<(n)){cab_LONG c=*(ZIP(inpos)++);\ 89 b|=((cab_ULONG)c)<<k;k+=8;}} 90 #define ZIPDUMPBITS(n) {b>>=(n);k-=(n);} 91 92 /* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed 93 * blocks have zero growth. MSZIP guarantees that it won't grow above 94 * uncompressed size by more than 12 bytes. LZX guarantees it won't grow 95 * more than 6144 bytes. 96 */ 97 #define CAB_BLOCKMAX (32768) 98 #define CAB_INPUTMAX (CAB_BLOCKMAX+6144) 99 100 typedef struct fdi_cds_fwd { 101 void *hfdi; /* the hfdi we are using */ 102 cab_UBYTE inbuf[CAB_INPUTMAX+2]; /* +2 for lzx bitbuffer overflows! */ 103 cab_UBYTE outbuf[CAB_BLOCKMAX]; 104 union { 105 struct ZIPstate zip; 106 } methods; 107 } fdi_decomp_state; 108 109 /* Tables for deflate from PKZIP's appnote.txt. */ 110 111 #define THOSE_ZIP_CONSTS \ 112 static const cab_UBYTE Zipborder[] = /* Order of the bit length code lengths */ \ 113 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; \ 114 static const cab_UWORD Zipcplens[] = /* Copy lengths for literal codes 257..285 */ \ 115 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, \ 116 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; \ 117 static const cab_UWORD Zipcplext[] = /* Extra bits for literal codes 257..285 */ \ 118 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, \ 119 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ \ 120 static const cab_UWORD Zipcpdist[] = /* Copy offsets for distance codes 0..29 */ \ 121 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, \ 122 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; \ 123 static const cab_UWORD Zipcpdext[] = /* Extra bits for distance codes */ \ 124 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, \ 125 10, 11, 11, 12, 12, 13, 13}; \ 126 /* And'ing with Zipmask[n] masks the lower n bits */ \ 127 static const cab_UWORD Zipmask[17] = { \ 128 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, \ 129 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff \ 130 } 131