Deleted Added
full compact
inflate.c (237691) inflate.c (254069)
1/* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2012 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 * Change history:
8 *

--- 79 unchanged lines hidden (view full) ---

88#ifdef MAKEFIXED
89# ifndef BUILDFIXED
90# define BUILDFIXED
91# endif
92#endif
93
94/* function prototypes */
95local void fixedtables OF((struct inflate_state FAR *state));
1/* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2012 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 * Change history:
8 *

--- 79 unchanged lines hidden (view full) ---

88#ifdef MAKEFIXED
89# ifndef BUILDFIXED
90# define BUILDFIXED
91# endif
92#endif
93
94/* function prototypes */
95local void fixedtables OF((struct inflate_state FAR *state));
96local int updatewindow OF((z_streamp strm, unsigned out));
96local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
97 unsigned copy));
97#ifdef BUILDFIXED
98 void makefixed OF((void));
99#endif
98#ifdef BUILDFIXED
99 void makefixed OF((void));
100#endif
100local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
101local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
101 unsigned len));
102
103int ZEXPORT inflateResetKeep(strm)
104z_streamp strm;
105{
106 struct inflate_state FAR *state;
107
108 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;

--- 261 unchanged lines hidden (view full) ---

370 is loaded.
371
372 Providing output buffers larger than 32K to inflate() should provide a speed
373 advantage, since only the last 32K of output is copied to the sliding window
374 upon return from inflate(), and since all distances after the first 32K of
375 output will fall in the output data, making match copies simpler and faster.
376 The advantage may be dependent on the size of the processor's data caches.
377 */
102 unsigned len));
103
104int ZEXPORT inflateResetKeep(strm)
105z_streamp strm;
106{
107 struct inflate_state FAR *state;
108
109 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;

--- 261 unchanged lines hidden (view full) ---

371 is loaded.
372
373 Providing output buffers larger than 32K to inflate() should provide a speed
374 advantage, since only the last 32K of output is copied to the sliding window
375 upon return from inflate(), and since all distances after the first 32K of
376 output will fall in the output data, making match copies simpler and faster.
377 The advantage may be dependent on the size of the processor's data caches.
378 */
378local int updatewindow(strm, out)
379local int updatewindow(strm, end, copy)
379z_streamp strm;
380z_streamp strm;
380unsigned out;
381const Bytef *end;
382unsigned copy;
381{
382 struct inflate_state FAR *state;
383{
384 struct inflate_state FAR *state;
383 unsigned copy, dist;
385 unsigned dist;
384
385 state = (struct inflate_state FAR *)strm->state;
386
387 /* if it hasn't been done already, allocate space for the window */
388 if (state->window == Z_NULL) {
389 state->window = (unsigned char FAR *)
390 ZALLOC(strm, 1U << state->wbits,
391 sizeof(unsigned char));
392 if (state->window == Z_NULL) return 1;
393 }
394
395 /* if window not in use yet, initialize */
396 if (state->wsize == 0) {
397 state->wsize = 1U << state->wbits;
398 state->wnext = 0;
399 state->whave = 0;
400 }
401
402 /* copy state->wsize or less output bytes into the circular window */
386
387 state = (struct inflate_state FAR *)strm->state;
388
389 /* if it hasn't been done already, allocate space for the window */
390 if (state->window == Z_NULL) {
391 state->window = (unsigned char FAR *)
392 ZALLOC(strm, 1U << state->wbits,
393 sizeof(unsigned char));
394 if (state->window == Z_NULL) return 1;
395 }
396
397 /* if window not in use yet, initialize */
398 if (state->wsize == 0) {
399 state->wsize = 1U << state->wbits;
400 state->wnext = 0;
401 state->whave = 0;
402 }
403
404 /* copy state->wsize or less output bytes into the circular window */
403 copy = out - strm->avail_out;
404 if (copy >= state->wsize) {
405 if (copy >= state->wsize) {
405 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
406 zmemcpy(state->window, end - state->wsize, state->wsize);
406 state->wnext = 0;
407 state->whave = state->wsize;
408 }
409 else {
410 dist = state->wsize - state->wnext;
411 if (dist > copy) dist = copy;
407 state->wnext = 0;
408 state->whave = state->wsize;
409 }
410 else {
411 dist = state->wsize - state->wnext;
412 if (dist > copy) dist = copy;
412 zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
413 zmemcpy(state->window + state->wnext, end - copy, dist);
413 copy -= dist;
414 if (copy) {
414 copy -= dist;
415 if (copy) {
415 zmemcpy(state->window, strm->next_out - copy, copy);
416 zmemcpy(state->window, end - copy, copy);
416 state->wnext = copy;
417 state->whave = state->wsize;
418 }
419 else {
420 state->wnext += dist;
421 if (state->wnext == state->wsize) state->wnext = 0;
422 if (state->whave < state->wsize) state->whave += dist;
423 }

--- 177 unchanged lines hidden (view full) ---

601 will return Z_BUF_ERROR if it has not reached the end of the stream.
602 */
603
604int ZEXPORT inflate(strm, flush)
605z_streamp strm;
606int flush;
607{
608 struct inflate_state FAR *state;
417 state->wnext = copy;
418 state->whave = state->wsize;
419 }
420 else {
421 state->wnext += dist;
422 if (state->wnext == state->wsize) state->wnext = 0;
423 if (state->whave < state->wsize) state->whave += dist;
424 }

--- 177 unchanged lines hidden (view full) ---

602 will return Z_BUF_ERROR if it has not reached the end of the stream.
603 */
604
605int ZEXPORT inflate(strm, flush)
606z_streamp strm;
607int flush;
608{
609 struct inflate_state FAR *state;
609 unsigned char FAR *next; /* next input */
610 z_const unsigned char FAR *next; /* next input */
610 unsigned char FAR *put; /* next output */
611 unsigned have, left; /* available input and output */
612 unsigned long hold; /* bit buffer */
613 unsigned bits; /* bits in bit buffer */
614 unsigned in, out; /* save starting available input and output */
615 unsigned copy; /* number of stored or match bytes to copy */
616 unsigned char FAR *from; /* where to copy match bytes from */
617 code here; /* current decoding table entry */

--- 297 unchanged lines hidden (view full) ---

915 while (state->have < state->ncode) {
916 NEEDBITS(3);
917 state->lens[order[state->have++]] = (unsigned short)BITS(3);
918 DROPBITS(3);
919 }
920 while (state->have < 19)
921 state->lens[order[state->have++]] = 0;
922 state->next = state->codes;
611 unsigned char FAR *put; /* next output */
612 unsigned have, left; /* available input and output */
613 unsigned long hold; /* bit buffer */
614 unsigned bits; /* bits in bit buffer */
615 unsigned in, out; /* save starting available input and output */
616 unsigned copy; /* number of stored or match bytes to copy */
617 unsigned char FAR *from; /* where to copy match bytes from */
618 code here; /* current decoding table entry */

--- 297 unchanged lines hidden (view full) ---

916 while (state->have < state->ncode) {
917 NEEDBITS(3);
918 state->lens[order[state->have++]] = (unsigned short)BITS(3);
919 DROPBITS(3);
920 }
921 while (state->have < 19)
922 state->lens[order[state->have++]] = 0;
923 state->next = state->codes;
923 state->lencode = (code const FAR *)(state->next);
924 state->lencode = (const code FAR *)(state->next);
924 state->lenbits = 7;
925 ret = inflate_table(CODES, state->lens, 19, &(state->next),
926 &(state->lenbits), state->work);
927 if (ret) {
928 strm->msg = (char *)"invalid code lengths set";
929 state->mode = BAD;
930 break;
931 }

--- 57 unchanged lines hidden (view full) ---

989 state->mode = BAD;
990 break;
991 }
992
993 /* build code tables -- note: do not change the lenbits or distbits
994 values here (9 and 6) without reading the comments in inftrees.h
995 concerning the ENOUGH constants, which depend on those values */
996 state->next = state->codes;
925 state->lenbits = 7;
926 ret = inflate_table(CODES, state->lens, 19, &(state->next),
927 &(state->lenbits), state->work);
928 if (ret) {
929 strm->msg = (char *)"invalid code lengths set";
930 state->mode = BAD;
931 break;
932 }

--- 57 unchanged lines hidden (view full) ---

990 state->mode = BAD;
991 break;
992 }
993
994 /* build code tables -- note: do not change the lenbits or distbits
995 values here (9 and 6) without reading the comments in inftrees.h
996 concerning the ENOUGH constants, which depend on those values */
997 state->next = state->codes;
997 state->lencode = (code const FAR *)(state->next);
998 state->lencode = (const code FAR *)(state->next);
998 state->lenbits = 9;
999 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1000 &(state->lenbits), state->work);
1001 if (ret) {
1002 strm->msg = (char *)"invalid literal/lengths set";
1003 state->mode = BAD;
1004 break;
1005 }
999 state->lenbits = 9;
1000 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1001 &(state->lenbits), state->work);
1002 if (ret) {
1003 strm->msg = (char *)"invalid literal/lengths set";
1004 state->mode = BAD;
1005 break;
1006 }
1006 state->distcode = (code const FAR *)(state->next);
1007 state->distcode = (const code FAR *)(state->next);
1007 state->distbits = 6;
1008 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1009 &(state->next), &(state->distbits), state->work);
1010 if (ret) {
1011 strm->msg = (char *)"invalid distances set";
1012 state->mode = BAD;
1013 break;
1014 }

--- 210 unchanged lines hidden (view full) ---

1225 If there was no progress during the inflate() call, return a buffer
1226 error. Call updatewindow() to create and/or update the window state.
1227 Note: a memory error from inflate() is non-recoverable.
1228 */
1229 inf_leave:
1230 RESTORE();
1231 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1232 (state->mode < CHECK || flush != Z_FINISH)))
1008 state->distbits = 6;
1009 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1010 &(state->next), &(state->distbits), state->work);
1011 if (ret) {
1012 strm->msg = (char *)"invalid distances set";
1013 state->mode = BAD;
1014 break;
1015 }

--- 210 unchanged lines hidden (view full) ---

1226 If there was no progress during the inflate() call, return a buffer
1227 error. Call updatewindow() to create and/or update the window state.
1228 Note: a memory error from inflate() is non-recoverable.
1229 */
1230 inf_leave:
1231 RESTORE();
1232 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1233 (state->mode < CHECK || flush != Z_FINISH)))
1233 if (updatewindow(strm, out)) {
1234 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1234 state->mode = MEM;
1235 return Z_MEM_ERROR;
1236 }
1237 in -= strm->avail_in;
1238 out -= strm->avail_out;
1239 strm->total_in += in;
1240 strm->total_out += out;
1241 state->total += out;

--- 17 unchanged lines hidden (view full) ---

1259 state = (struct inflate_state FAR *)strm->state;
1260 if (state->window != Z_NULL) ZFREE(strm, state->window);
1261 ZFREE(strm, strm->state);
1262 strm->state = Z_NULL;
1263 Tracev((stderr, "inflate: end\n"));
1264 return Z_OK;
1265}
1266
1235 state->mode = MEM;
1236 return Z_MEM_ERROR;
1237 }
1238 in -= strm->avail_in;
1239 out -= strm->avail_out;
1240 strm->total_in += in;
1241 strm->total_out += out;
1242 state->total += out;

--- 17 unchanged lines hidden (view full) ---

1260 state = (struct inflate_state FAR *)strm->state;
1261 if (state->window != Z_NULL) ZFREE(strm, state->window);
1262 ZFREE(strm, strm->state);
1263 strm->state = Z_NULL;
1264 Tracev((stderr, "inflate: end\n"));
1265 return Z_OK;
1266}
1267
1268int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
1269z_streamp strm;
1270Bytef *dictionary;
1271uInt *dictLength;
1272{
1273 struct inflate_state FAR *state;
1274
1275 /* check state */
1276 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1277 state = (struct inflate_state FAR *)strm->state;
1278
1279 /* copy dictionary */
1280 if (state->whave && dictionary != Z_NULL) {
1281 zmemcpy(dictionary, state->window + state->wnext,
1282 state->whave - state->wnext);
1283 zmemcpy(dictionary + state->whave - state->wnext,
1284 state->window, state->wnext);
1285 }
1286 if (dictLength != Z_NULL)
1287 *dictLength = state->whave;
1288 return Z_OK;
1289}
1290
1267int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1268z_streamp strm;
1269const Bytef *dictionary;
1270uInt dictLength;
1271{
1272 struct inflate_state FAR *state;
1273 unsigned long dictid;
1291int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1292z_streamp strm;
1293const Bytef *dictionary;
1294uInt dictLength;
1295{
1296 struct inflate_state FAR *state;
1297 unsigned long dictid;
1274 unsigned char *next;
1275 unsigned avail;
1276 int ret;
1277
1278 /* check state */
1279 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1280 state = (struct inflate_state FAR *)strm->state;
1281 if (state->wrap != 0 && state->mode != DICT)
1282 return Z_STREAM_ERROR;
1283
1284 /* check for correct dictionary identifier */
1285 if (state->mode == DICT) {
1286 dictid = adler32(0L, Z_NULL, 0);
1287 dictid = adler32(dictid, dictionary, dictLength);
1288 if (dictid != state->check)
1289 return Z_DATA_ERROR;
1290 }
1291
1292 /* copy dictionary to window using updatewindow(), which will amend the
1293 existing dictionary if appropriate */
1298 int ret;
1299
1300 /* check state */
1301 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1302 state = (struct inflate_state FAR *)strm->state;
1303 if (state->wrap != 0 && state->mode != DICT)
1304 return Z_STREAM_ERROR;
1305
1306 /* check for correct dictionary identifier */
1307 if (state->mode == DICT) {
1308 dictid = adler32(0L, Z_NULL, 0);
1309 dictid = adler32(dictid, dictionary, dictLength);
1310 if (dictid != state->check)
1311 return Z_DATA_ERROR;
1312 }
1313
1314 /* copy dictionary to window using updatewindow(), which will amend the
1315 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;
1316 ret = updatewindow(strm, dictionary + dictLength, dictLength);
1301 if (ret) {
1302 state->mode = MEM;
1303 return Z_MEM_ERROR;
1304 }
1305 state->havedict = 1;
1306 Tracev((stderr, "inflate: dictionary set\n"));
1307 return Z_OK;
1308}

--- 23 unchanged lines hidden (view full) ---

1332 return value is how many bytes were read including the last byte of the
1333 pattern. If *have is less than four, then the pattern has not been found
1334 yet and the return value is len. In the latter case, syncsearch() can be
1335 called again with more data and the *have state. *have is initialized to
1336 zero for the first call.
1337 */
1338local unsigned syncsearch(have, buf, len)
1339unsigned FAR *have;
1317 if (ret) {
1318 state->mode = MEM;
1319 return Z_MEM_ERROR;
1320 }
1321 state->havedict = 1;
1322 Tracev((stderr, "inflate: dictionary set\n"));
1323 return Z_OK;
1324}

--- 23 unchanged lines hidden (view full) ---

1348 return value is how many bytes were read including the last byte of the
1349 pattern. If *have is less than four, then the pattern has not been found
1350 yet and the return value is len. In the latter case, syncsearch() can be
1351 called again with more data and the *have state. *have is initialized to
1352 zero for the first call.
1353 */
1354local unsigned syncsearch(have, buf, len)
1355unsigned FAR *have;
1340unsigned char FAR *buf;
1356const unsigned char FAR *buf;
1341unsigned len;
1342{
1343 unsigned got;
1344 unsigned next;
1345
1346 got = *have;
1347 next = 0;
1348 while (next < len && got < 4) {

--- 148 unchanged lines hidden ---
1357unsigned len;
1358{
1359 unsigned got;
1360 unsigned next;
1361
1362 got = *have;
1363 next = 0;
1364 while (next < len && got < 4) {

--- 148 unchanged lines hidden ---