~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/inflate64_priv.h

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * This file contains code from zlib library v.1.2.3 with modifications
3
 
 * by aCaB <acab@clamav.net> to allow decompression of deflate64 stereams
4
 
 * (aka zip method 9). The implementation is heavily inspired by InfoZip
5
 
 *  and zlib's inf9back.c
6
 
 * 
7
 
 * Full copy of the original zlib license follows:
8
 
 */
9
 
 
10
 
/* zlib.h -- interface of the 'zlib' general purpose compression library
11
 
  version 1.2.3, July 18th, 2005
12
 
 
13
 
  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
14
 
 
15
 
  This software is provided 'as-is', without any express or implied
16
 
  warranty.  In no event will the authors be held liable for any damages
17
 
  arising from the use of this software.
18
 
 
19
 
  Permission is granted to anyone to use this software for any purpose,
20
 
  including commercial applications, and to alter it and redistribute it
21
 
  freely, subject to the following restrictions:
22
 
 
23
 
  1. The origin of this software must not be misrepresented; you must not
24
 
     claim that you wrote the original software. If you use this software
25
 
     in a product, an acknowledgment in the product documentation would be
26
 
     appreciated but is not required.
27
 
  2. Altered source versions must be plainly marked as such, and must not be
28
 
     misrepresented as being the original software.
29
 
  3. This notice may not be removed or altered from any source distribution.
30
 
 
31
 
  Jean-loup Gailly        Mark Adler
32
 
  jloup@gzip.org          madler@alumni.caltech.edu
33
 
 
34
 
 
35
 
  The data format used by the zlib library is described by RFCs (Request for
36
 
  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
37
 
  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
38
 
*/
39
 
 
40
 
#ifndef __INFLATE64_PRIV_H
41
 
#define __INFLATE64_PRIV_H
42
 
 
43
 
#if HAVE_CONFIG_H
44
 
#include "clamav-config.h"
45
 
#endif
46
 
 
47
 
#include "inflate64.h"
48
 
 
49
 
#include <zlib.h> /* adler/crc32 */
50
 
 
51
 
#ifndef Z_BLOCK
52
 
#define Z_BLOCK 5
53
 
#endif
54
 
 
55
 
#ifndef local
56
 
#  define local static
57
 
#endif
58
 
/* compile with -Dlocal if your debugger can't find static symbols */
59
 
 
60
 
#ifndef PKZIP_BUG_WORKAROUND
61
 
#define PKZIP_BUG_WORKAROUND
62
 
#endif
63
 
 
64
 
/* Diagnostic functions */
65
 
#ifdef INF64DEBUG
66
 
#  include <stdio.h>
67
 
#  define Assert(cond,msg) {if(!(cond)) perror(msg);}
68
 
#  define Trace(x) {if (verbose>=0) fprintf x ;}
69
 
#  define Tracev(x) {if (verbose>0) fprintf x ;}
70
 
#  define Tracevv(x) {if (verbose>1) fprintf x ;}
71
 
#  define Tracec(c,x) {if (verbose>0 && (c)) fprintf x ;}
72
 
#  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
73
 
#else
74
 
#  define Assert(cond,msg)
75
 
#  define Trace(x)
76
 
#  define Tracev(x)
77
 
#  define Tracevv(x)
78
 
#  define Tracec(c,x)
79
 
#  define Tracecv(c,x)
80
 
#endif
81
 
 
82
 
#define MAXBITS 15
83
 
#define ENOUGH 2048
84
 
#define MAXD 592
85
 
 
86
 
typedef struct {
87
 
    unsigned char op;           /* operation, extra bits, table bits */
88
 
    unsigned char bits;         /* bits in this part of the code */
89
 
    unsigned short val;         /* offset in table or code value */
90
 
} code;
91
 
 
92
 
/* Type of code to build for inftable() */
93
 
typedef enum {
94
 
    CODES,
95
 
    LENS,
96
 
    DISTS
97
 
} codetype;
98
 
 
99
 
/* Possible inflate modes between inflate() calls */
100
 
typedef enum {
101
 
    HEAD,       /* i: waiting for magic header */
102
 
    FLAGS,      /* i: waiting for method and flags (gzip) */
103
 
    TIME,       /* i: waiting for modification time (gzip) */
104
 
    OS,         /* i: waiting for extra flags and operating system (gzip) */
105
 
    EXLEN,      /* i: waiting for extra length (gzip) */
106
 
    EXTRA,      /* i: waiting for extra bytes (gzip) */
107
 
    NAME,       /* i: waiting for end of file name (gzip) */
108
 
    COMMENT,    /* i: waiting for end of comment (gzip) */
109
 
    HCRC,       /* i: waiting for header crc (gzip) */
110
 
    DICTID,     /* i: waiting for dictionary check value */
111
 
    DICT,       /* waiting for inflateSetDictionary() call */
112
 
        TYPE,       /* i: waiting for type bits, including last-flag bit */
113
 
        TYPEDO,     /* i: same, but skip check to exit inflate on new block */
114
 
        STORED,     /* i: waiting for stored size (length and complement) */
115
 
        COPY,       /* i/o: waiting for input or output to copy stored block */
116
 
        TABLE,      /* i: waiting for dynamic block table lengths */
117
 
        LENLENS,    /* i: waiting for code length code lengths */
118
 
        CODELENS,   /* i: waiting for length/lit and distance code lengths */
119
 
            LEN,        /* i: waiting for length/lit code */
120
 
            LENEXT,     /* i: waiting for length extra bits */
121
 
            DIST,       /* i: waiting for distance code */
122
 
            DISTEXT,    /* i: waiting for distance extra bits */
123
 
            MATCH,      /* o: waiting for output space to copy string */
124
 
            LIT,        /* o: waiting for output space to write literal */
125
 
    CHECK,      /* i: waiting for 32-bit check value */
126
 
    LENGTH,     /* i: waiting for 32-bit length (gzip) */
127
 
    DONE,       /* finished check, done -- remain here until reset */
128
 
    ACAB_BAD,        /* got a data error -- remain here until reset */
129
 
    MEM,        /* got an inflate() memory error -- remain here until reset */
130
 
    SYNC        /* looking for synchronization bytes to restart inflate() */
131
 
} inflate_mode;
132
 
 
133
 
/*
134
 
    State transitions between above modes -
135
 
 
136
 
    (most modes can go to the BAD or MEM mode -- not shown for clarity)
137
 
 
138
 
    Process header:
139
 
        HEAD -> (gzip) or (zlib)
140
 
        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
141
 
        NAME -> COMMENT -> HCRC -> TYPE
142
 
        (zlib) -> DICTID or TYPE
143
 
        DICTID -> DICT -> TYPE
144
 
    Read deflate blocks:
145
 
            TYPE -> STORED or TABLE or LEN or CHECK
146
 
            STORED -> COPY -> TYPE
147
 
            TABLE -> LENLENS -> CODELENS -> LEN
148
 
    Read deflate codes:
149
 
                LEN -> LENEXT or LIT or TYPE
150
 
                LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
151
 
                LIT -> LEN
152
 
    Process trailer:
153
 
        CHECK -> LENGTH -> DONE
154
 
 */
155
 
 
156
 
/* state maintained between inflate() calls.  Approximately 7K bytes. */
157
 
struct inflate_state {
158
 
    inflate_mode mode;          /* current inflate mode */
159
 
    int last;                   /* true if processing last block */
160
 
    int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip */
161
 
    int havedict;               /* true if dictionary provided */
162
 
    int flags;                  /* gzip header method and flags (0 if zlib) */
163
 
    unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
164
 
    unsigned long check;        /* protected copy of check value */
165
 
    unsigned long total;        /* protected copy of output count */
166
 
        /* sliding window */
167
 
    unsigned wbits;             /* log base 2 of requested window size */
168
 
    unsigned wsize;             /* window size or zero if not using window */
169
 
    unsigned whave;             /* valid bytes in the window */
170
 
    unsigned write;             /* window write index */
171
 
    unsigned char FAR *window;  /* allocated sliding window, if needed */
172
 
        /* bit accumulator */
173
 
    unsigned long hold;         /* input bit accumulator */
174
 
    unsigned bits;              /* number of bits in "in" */
175
 
        /* for string and stored block copying */
176
 
    unsigned length;            /* literal or length of data to copy */
177
 
    unsigned offset;            /* distance back to copy string from */
178
 
        /* for table and code decoding */
179
 
    unsigned extra;             /* extra bits needed */
180
 
        /* fixed and dynamic code tables */
181
 
    code const FAR *lencode;    /* starting table for length/literal codes */
182
 
    code const FAR *distcode;   /* starting table for distance codes */
183
 
    unsigned lenbits;           /* index bits for lencode */
184
 
    unsigned distbits;          /* index bits for distcode */
185
 
        /* dynamic table building */
186
 
    unsigned ncode;             /* number of code length code lengths */
187
 
    unsigned nlen;              /* number of length code lengths */
188
 
    unsigned ndist;             /* number of distance code lengths */
189
 
    unsigned have;              /* number of code lengths in lens[] */
190
 
    code FAR *next;             /* next available space in codes[] */
191
 
    unsigned short lens[320];   /* temporary storage for code lengths */
192
 
    unsigned short work[288];   /* work area for code table building */
193
 
    code codes[ENOUGH];         /* space for code tables */
194
 
};
195
 
 
196
 
#endif