1
1
/* inflate.c -- zlib decompression
2
* Copyright (C) 1995-2009 Mark Adler
2
* Copyright (C) 1995-2012 Mark Adler
3
3
* For conditions of distribution and use, see copyright notice in zlib.h
45
45
* - Rearrange window copies in inflate_fast() for speed and simplification
46
46
* - Unroll last copy for window match in inflate_fast()
47
47
* - Use local copies of window variables in inflate_fast() for speed
48
* - Pull out common write == 0 case for speed in inflate_fast()
48
* - Pull out common wnext == 0 case for speed in inflate_fast()
49
49
* - Make op and len in inflate_fast() unsigned for consistency
50
50
* - Add FAR to lcode and dcode declarations in inflate_fast()
51
51
* - Simplified bad distance check in inflate_fast()
100
100
local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
103
int ZEXPORT inflateReset(strm)
103
int ZEXPORT inflateResetKeep(strm)
106
106
struct inflate_state FAR *state;
109
109
state = (struct inflate_state FAR *)strm->state;
110
110
strm->total_in = strm->total_out = state->total = 0;
111
111
strm->msg = Z_NULL;
112
strm->adler = 1; /* to support ill-conceived Java test suite */
112
if (state->wrap) /* to support ill-conceived Java test suite */
113
strm->adler = state->wrap & 1;
113
114
state->mode = HEAD;
115
116
state->havedict = 0;
116
117
state->dmax = 32768U;
117
118
state->head = Z_NULL;
123
121
state->lencode = state->distcode = state->next = state->codes;
128
int ZEXPORT inflateReset(strm)
131
struct inflate_state FAR *state;
133
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
134
state = (struct inflate_state FAR *)strm->state;
138
return inflateResetKeep(strm);
130
141
int ZEXPORT inflateReset2(strm, windowBits)
154
165
/* set number of window bits, free window if different */
155
if (windowBits < 8 || windowBits > 15)
166
if (windowBits && (windowBits < 8 || windowBits > 15))
156
167
return Z_STREAM_ERROR;
157
if (state->wbits != windowBits && state->window != Z_NULL) {
168
if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
158
169
ZFREE(strm, state->window);
159
170
state->window = Z_NULL;
180
191
if (strm == Z_NULL) return Z_STREAM_ERROR;
181
192
strm->msg = Z_NULL; /* in case we return an error */
182
193
if (strm->zalloc == (alloc_func)0) {
195
return Z_STREAM_ERROR;
183
197
strm->zalloc = zcalloc;
184
198
strm->opaque = (voidpf)0;
186
if (strm->zfree == (free_func)0) strm->zfree = zcfree;
201
if (strm->zfree == (free_func)0)
203
return Z_STREAM_ERROR;
205
strm->zfree = zcfree;
187
207
state = (struct inflate_state FAR *)
188
208
ZALLOC(strm, 1, sizeof(struct inflate_state));
189
209
if (state == Z_NULL) return Z_MEM_ERROR;
323
343
if ((low % 7) == 0) printf("\n ");
324
printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
325
state.lencode[low].val);
344
printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
345
state.lencode[low].bits, state.lencode[low].val);
326
346
if (++low == size) break;
383
403
copy = out - strm->avail_out;
384
404
if (copy >= state->wsize) {
385
405
zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
387
407
state->whave = state->wsize;
390
dist = state->wsize - state->write;
410
dist = state->wsize - state->wnext;
391
411
if (dist > copy) dist = copy;
392
zmemcpy(state->window + state->write, strm->next_out - copy, dist);
412
zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
395
415
zmemcpy(state->window, strm->next_out - copy, copy);
397
417
state->whave = state->wsize;
400
state->write += dist;
401
if (state->write == state->wsize) state->write = 0;
420
state->wnext += dist;
421
if (state->wnext == state->wsize) state->wnext = 0;
402
422
if (state->whave < state->wsize) state->whave += dist;
499
519
bits -= bits & 7; \
502
/* Reverse the bytes in a 32-bit value */
504
((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
505
(((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
508
523
inflate() uses a state machine to process as much input data and generate as
509
524
much output data as possible before returning. The state machine is
1131
if (copy > state->write) {
1132
copy -= state->write;
1133
/* %% problem here if copy > state->wsize -- avoid? */
1134
/* %% or can (state->window + state->wsize) - copy */
1135
/* %% but really should detect and reject this case */
1147
if (copy > state->wnext) {
1148
copy -= state->wnext;
1136
1149
from = state->window + (state->wsize - copy);
1139
from = state->window + (state->write - copy);
1152
from = state->window + (state->wnext - copy);
1140
1153
if (copy > state->length) copy = state->length;
1142
1155
else { /* copy from output */
1218
if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
1231
if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1232
(state->mode < CHECK || flush != Z_FINISH)))
1219
1233
if (updatewindow(strm, out)) {
1220
1234
state->mode = MEM;
1221
1235
return Z_MEM_ERROR;
1264
1281
if (state->wrap != 0 && state->mode != DICT)
1265
1282
return Z_STREAM_ERROR;
1267
/* check for correct dictionary id */
1284
/* check for correct dictionary identifier */
1268
1285
if (state->mode == DICT) {
1269
id = adler32(0L, Z_NULL, 0);
1270
id = adler32(id, dictionary, dictLength);
1271
if (id != state->check)
1286
dictid = adler32(0L, Z_NULL, 0);
1287
dictid = adler32(dictid, dictionary, dictLength);
1288
if (dictid != state->check)
1272
1289
return Z_DATA_ERROR;
1275
/* copy dictionary to window */
1276
if (updatewindow(strm, strm->avail_out)) {
1292
/* copy dictionary to window using updatewindow(), which will amend the
1293
existing dictionary if appropriate */
1294
next = strm->next_out;
1295
avail = strm->avail_out;
1296
strm->next_out = (Bytef *)dictionary + dictLength;
1297
strm->avail_out = 0;
1298
ret = updatewindow(strm, dictLength);
1299
strm->avail_out = avail;
1300
strm->next_out = next;
1277
1302
state->mode = MEM;
1278
1303
return Z_MEM_ERROR;
1280
if (dictLength > state->wsize) {
1281
zmemcpy(state->window, dictionary + dictLength - state->wsize,
1283
state->whave = state->wsize;
1286
zmemcpy(state->window + state->wsize - dictLength, dictionary,
1288
state->whave = dictLength;
1290
1305
state->havedict = 1;
1291
1306
Tracev((stderr, "inflate: dictionary set\n"));
1436
1451
/* copy state */
1437
zmemcpy(dest, source, sizeof(z_stream));
1438
zmemcpy(copy, state, sizeof(struct inflate_state));
1452
zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1453
zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1439
1454
if (state->lencode >= state->codes &&
1440
1455
state->lencode <= state->codes + ENOUGH - 1) {
1441
1456
copy->lencode = copy->codes + (state->lencode - state->codes);
1460
1475
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1461
1476
state = (struct inflate_state FAR *)strm->state;
1462
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1463
1477
state->sane = !subvert;
1478
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1466
1481
state->sane = 1;