FreeBSD/Linux Kernel Cross Reference
sys/lib/libz/inflate.c
1 /* $NetBSD: inflate.c,v 1.5 2003/03/25 22:48:44 mycroft Exp $ */
2
3 /* inflate.c -- zlib interface to inflate modules
4 * Copyright (C) 1995-2002 Mark Adler
5 * For conditions of distribution and use, see copyright notice in zlib.h
6 */
7
8 #include "zutil.h"
9 #include "infblock.h"
10
11 struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
12
13 typedef enum {
14 BLOCKS, /* decompressing blocks */
15 DONE, /* finished check, done */
16 BAD} /* got an error--stay here */
17 inflate_mode;
18
19 /* inflate private state */
20 struct internal_state {
21
22 /* mode */
23 inflate_mode mode; /* current inflate mode */
24
25 /* mode independent information */
26 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
27 inflate_blocks_statef
28 *blocks; /* current inflate_blocks state */
29
30 };
31
32
33 int ZEXPORT inflateReset(z)
34 z_streamp z;
35 {
36 if (z == Z_NULL || z->state == Z_NULL)
37 return Z_STREAM_ERROR;
38 z->total_in = z->total_out = 0;
39 z->msg = Z_NULL;
40 z->state->mode = BLOCKS;
41 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
42 Tracev((stderr, "inflate: reset\n"));
43 return Z_OK;
44 }
45
46
47 int ZEXPORT inflateEnd(z)
48 z_streamp z;
49 {
50 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
51 return Z_STREAM_ERROR;
52 if (z->state->blocks != Z_NULL)
53 inflate_blocks_free(z->state->blocks, z);
54 ZFREE(z, z->state);
55 z->state = Z_NULL;
56 Tracev((stderr, "inflate: end\n"));
57 return Z_OK;
58 }
59
60
61 int ZEXPORT inflateInit2_(z, w, version, stream_size)
62 z_streamp z;
63 int w;
64 const char *version;
65 int stream_size;
66 {
67 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
68 stream_size != sizeof(z_stream))
69 return Z_VERSION_ERROR;
70
71 /* initialize state */
72 if (z == Z_NULL)
73 return Z_STREAM_ERROR;
74 z->msg = Z_NULL;
75 if (z->zalloc == Z_NULL)
76 {
77 z->zalloc = zcalloc;
78 z->opaque = (voidpf)0;
79 }
80 if (z->zfree == Z_NULL) z->zfree = zcfree;
81 if ((z->state = (struct internal_state FAR *)
82 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
83 return Z_MEM_ERROR;
84 z->state->blocks = Z_NULL;
85
86 /* handle undocumented nowrap option (no zlib header or check) */
87 if (w < 0)
88 w = - w;
89 else
90 return Z_STREAM_ERROR;
91
92 /* set window size */
93 if (w < 8 || w > 15)
94 {
95 inflateEnd(z);
96 return Z_STREAM_ERROR;
97 }
98 z->state->wbits = (uInt)w;
99
100 /* create inflate_blocks state */
101 if ((z->state->blocks =
102 inflate_blocks_new(z, Z_NULL, (uInt)1 << w))
103 == Z_NULL)
104 {
105 inflateEnd(z);
106 return Z_MEM_ERROR;
107 }
108 Tracev((stderr, "inflate: allocated\n"));
109
110 /* reset state */
111 inflateReset(z);
112 return Z_OK;
113 }
114
115
116 int ZEXPORT inflateInit_(z, version, stream_size)
117 z_streamp z;
118 const char *version;
119 int stream_size;
120 {
121 return inflateInit2_(z, -DEF_WBITS, version, stream_size);
122 }
123
124
125 #define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
126 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
127
128 int ZEXPORT inflate(z, f)
129 z_streamp z;
130 int f;
131 {
132 int r;
133
134 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
135 return Z_STREAM_ERROR;
136 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
137 r = Z_BUF_ERROR;
138 while (1) switch (z->state->mode)
139 {
140 case BLOCKS:
141 r = inflate_blocks(z->state->blocks, z, r);
142 if (r == Z_DATA_ERROR)
143 {
144 z->state->mode = BAD;
145 break;
146 }
147 if (r == Z_OK)
148 r = f;
149 if (r != Z_STREAM_END)
150 return r;
151 r = f;
152 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
153 z->state->mode = DONE;
154 case DONE:
155 return Z_STREAM_END;
156 case BAD:
157 return Z_DATA_ERROR;
158 default:
159 return Z_STREAM_ERROR;
160 }
161 #ifdef NEED_DUMMY_RETURN
162 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
163 #endif
164 }
Cache object: e1f6543b3f6fa6edf68cbdd2df495564
|