Deleted Added
full compact
infback.c (180208) infback.c (205471)
1/* infback.c -- inflate using a call-back interface
1/* infback.c -- inflate using a call-back interface
2 * Copyright (C) 1995-2005 Mark Adler
2 * Copyright (C) 1995-2009 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 This code is largely copied from inflate.c. Normally either infback.o or
8 inflate.o would be linked into an application--not both. The interface
9 with inffast.c is retained so that optimized assembler-coded versions of
10 inflate_fast() can be used with either inflate.c or infback.c.

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

50 sizeof(struct inflate_state));
51 if (state == Z_NULL) return Z_MEM_ERROR;
52 Tracev((stderr, "inflate: allocated\n"));
53 strm->state = (struct internal_state FAR *)state;
54 state->dmax = 32768U;
55 state->wbits = windowBits;
56 state->wsize = 1U << windowBits;
57 state->window = window;
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 This code is largely copied from inflate.c. Normally either infback.o or
8 inflate.o would be linked into an application--not both. The interface
9 with inffast.c is retained so that optimized assembler-coded versions of
10 inflate_fast() can be used with either inflate.c or infback.c.

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

50 sizeof(struct inflate_state));
51 if (state == Z_NULL) return Z_MEM_ERROR;
52 Tracev((stderr, "inflate: allocated\n"));
53 strm->state = (struct internal_state FAR *)state;
54 state->dmax = 32768U;
55 state->wbits = windowBits;
56 state->wsize = 1U << windowBits;
57 state->window = window;
58 state->write = 0;
58 state->wnext = 0;
59 state->whave = 0;
60 return Z_OK;
61}
62
63/*
64 Return state with length and distance decoding tables and index sizes set to
65 fixed code decoding. Normally this returns fixed tables from inffixed.h.
66 If BUILDFIXED is defined, then instead this routine builds the tables the

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

248 struct inflate_state FAR *state;
249 unsigned char FAR *next; /* next input */
250 unsigned char FAR *put; /* next output */
251 unsigned have, left; /* available input and output */
252 unsigned long hold; /* bit buffer */
253 unsigned bits; /* bits in bit buffer */
254 unsigned copy; /* number of stored or match bytes to copy */
255 unsigned char FAR *from; /* where to copy match bytes from */
59 state->whave = 0;
60 return Z_OK;
61}
62
63/*
64 Return state with length and distance decoding tables and index sizes set to
65 fixed code decoding. Normally this returns fixed tables from inffixed.h.
66 If BUILDFIXED is defined, then instead this routine builds the tables the

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

248 struct inflate_state FAR *state;
249 unsigned char FAR *next; /* next input */
250 unsigned char FAR *put; /* next output */
251 unsigned have, left; /* available input and output */
252 unsigned long hold; /* bit buffer */
253 unsigned bits; /* bits in bit buffer */
254 unsigned copy; /* number of stored or match bytes to copy */
255 unsigned char FAR *from; /* where to copy match bytes from */
256 code this; /* current decoding table entry */
256 code here; /* current decoding table entry */
257 code last; /* parent table entry */
258 unsigned len; /* length to copy for repeats, bits to drop */
259 int ret; /* return code */
260 static const unsigned short order[19] = /* permutation of code lengths */
261 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
262
263 /* Check that the strm exists and that the state was initialized */
264 if (strm == Z_NULL || strm->state == Z_NULL)

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

384 break;
385 }
386 Tracev((stderr, "inflate: code lengths ok\n"));
387
388 /* get length and distance code code lengths */
389 state->have = 0;
390 while (state->have < state->nlen + state->ndist) {
391 for (;;) {
257 code last; /* parent table entry */
258 unsigned len; /* length to copy for repeats, bits to drop */
259 int ret; /* return code */
260 static const unsigned short order[19] = /* permutation of code lengths */
261 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
262
263 /* Check that the strm exists and that the state was initialized */
264 if (strm == Z_NULL || strm->state == Z_NULL)

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

384 break;
385 }
386 Tracev((stderr, "inflate: code lengths ok\n"));
387
388 /* get length and distance code code lengths */
389 state->have = 0;
390 while (state->have < state->nlen + state->ndist) {
391 for (;;) {
392 this = state->lencode[BITS(state->lenbits)];
393 if ((unsigned)(this.bits) <= bits) break;
392 here = state->lencode[BITS(state->lenbits)];
393 if ((unsigned)(here.bits) <= bits) break;
394 PULLBYTE();
395 }
394 PULLBYTE();
395 }
396 if (this.val < 16) {
397 NEEDBITS(this.bits);
398 DROPBITS(this.bits);
399 state->lens[state->have++] = this.val;
396 if (here.val < 16) {
397 NEEDBITS(here.bits);
398 DROPBITS(here.bits);
399 state->lens[state->have++] = here.val;
400 }
401 else {
400 }
401 else {
402 if (this.val == 16) {
403 NEEDBITS(this.bits + 2);
404 DROPBITS(this.bits);
402 if (here.val == 16) {
403 NEEDBITS(here.bits + 2);
404 DROPBITS(here.bits);
405 if (state->have == 0) {
406 strm->msg = (char *)"invalid bit length repeat";
407 state->mode = BAD;
408 break;
409 }
410 len = (unsigned)(state->lens[state->have - 1]);
411 copy = 3 + BITS(2);
412 DROPBITS(2);
413 }
405 if (state->have == 0) {
406 strm->msg = (char *)"invalid bit length repeat";
407 state->mode = BAD;
408 break;
409 }
410 len = (unsigned)(state->lens[state->have - 1]);
411 copy = 3 + BITS(2);
412 DROPBITS(2);
413 }
414 else if (this.val == 17) {
415 NEEDBITS(this.bits + 3);
416 DROPBITS(this.bits);
414 else if (here.val == 17) {
415 NEEDBITS(here.bits + 3);
416 DROPBITS(here.bits);
417 len = 0;
418 copy = 3 + BITS(3);
419 DROPBITS(3);
420 }
421 else {
417 len = 0;
418 copy = 3 + BITS(3);
419 DROPBITS(3);
420 }
421 else {
422 NEEDBITS(this.bits + 7);
423 DROPBITS(this.bits);
422 NEEDBITS(here.bits + 7);
423 DROPBITS(here.bits);
424 len = 0;
425 copy = 11 + BITS(7);
426 DROPBITS(7);
427 }
428 if (state->have + copy > state->nlen + state->ndist) {
429 strm->msg = (char *)"invalid bit length repeat";
430 state->mode = BAD;
431 break;
432 }
433 while (copy--)
434 state->lens[state->have++] = (unsigned short)len;
435 }
436 }
437
438 /* handle error breaks in while */
439 if (state->mode == BAD) break;
440
424 len = 0;
425 copy = 11 + BITS(7);
426 DROPBITS(7);
427 }
428 if (state->have + copy > state->nlen + state->ndist) {
429 strm->msg = (char *)"invalid bit length repeat";
430 state->mode = BAD;
431 break;
432 }
433 while (copy--)
434 state->lens[state->have++] = (unsigned short)len;
435 }
436 }
437
438 /* handle error breaks in while */
439 if (state->mode == BAD) break;
440
441 /* build code tables */
441 /* check for end-of-block code (better have one) */
442 if (state->lens[256] == 0) {
443 strm->msg = (char *)"invalid code -- missing end-of-block";
444 state->mode = BAD;
445 break;
446 }
447
448 /* build code tables -- note: do not change the lenbits or distbits
449 values here (9 and 6) without reading the comments in inftrees.h
450 concerning the ENOUGH constants, which depend on those values */
442 state->next = state->codes;
443 state->lencode = (code const FAR *)(state->next);
444 state->lenbits = 9;
445 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
446 &(state->lenbits), state->work);
447 if (ret) {
448 strm->msg = (char *)"invalid literal/lengths set";
449 state->mode = BAD;

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

469 state->whave = state->wsize - left;
470 inflate_fast(strm, state->wsize);
471 LOAD();
472 break;
473 }
474
475 /* get a literal, length, or end-of-block code */
476 for (;;) {
451 state->next = state->codes;
452 state->lencode = (code const FAR *)(state->next);
453 state->lenbits = 9;
454 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
455 &(state->lenbits), state->work);
456 if (ret) {
457 strm->msg = (char *)"invalid literal/lengths set";
458 state->mode = BAD;

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

478 state->whave = state->wsize - left;
479 inflate_fast(strm, state->wsize);
480 LOAD();
481 break;
482 }
483
484 /* get a literal, length, or end-of-block code */
485 for (;;) {
477 this = state->lencode[BITS(state->lenbits)];
478 if ((unsigned)(this.bits) <= bits) break;
486 here = state->lencode[BITS(state->lenbits)];
487 if ((unsigned)(here.bits) <= bits) break;
479 PULLBYTE();
480 }
488 PULLBYTE();
489 }
481 if (this.op && (this.op & 0xf0) == 0) {
482 last = this;
490 if (here.op && (here.op & 0xf0) == 0) {
491 last = here;
483 for (;;) {
492 for (;;) {
484 this = state->lencode[last.val +
493 here = state->lencode[last.val +
485 (BITS(last.bits + last.op) >> last.bits)];
494 (BITS(last.bits + last.op) >> last.bits)];
486 if ((unsigned)(last.bits + this.bits) <= bits) break;
495 if ((unsigned)(last.bits + here.bits) <= bits) break;
487 PULLBYTE();
488 }
489 DROPBITS(last.bits);
490 }
496 PULLBYTE();
497 }
498 DROPBITS(last.bits);
499 }
491 DROPBITS(this.bits);
492 state->length = (unsigned)this.val;
500 DROPBITS(here.bits);
501 state->length = (unsigned)here.val;
493
494 /* process literal */
502
503 /* process literal */
495 if (this.op == 0) {
496 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
504 if (here.op == 0) {
505 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
497 "inflate: literal '%c'\n" :
506 "inflate: literal '%c'\n" :
498 "inflate: literal 0x%02x\n", this.val));
507 "inflate: literal 0x%02x\n", here.val));
499 ROOM();
500 *put++ = (unsigned char)(state->length);
501 left--;
502 state->mode = LEN;
503 break;
504 }
505
506 /* process end of block */
508 ROOM();
509 *put++ = (unsigned char)(state->length);
510 left--;
511 state->mode = LEN;
512 break;
513 }
514
515 /* process end of block */
507 if (this.op & 32) {
516 if (here.op & 32) {
508 Tracevv((stderr, "inflate: end of block\n"));
509 state->mode = TYPE;
510 break;
511 }
512
513 /* invalid code */
517 Tracevv((stderr, "inflate: end of block\n"));
518 state->mode = TYPE;
519 break;
520 }
521
522 /* invalid code */
514 if (this.op & 64) {
523 if (here.op & 64) {
515 strm->msg = (char *)"invalid literal/length code";
516 state->mode = BAD;
517 break;
518 }
519
520 /* length code -- get extra bits, if any */
524 strm->msg = (char *)"invalid literal/length code";
525 state->mode = BAD;
526 break;
527 }
528
529 /* length code -- get extra bits, if any */
521 state->extra = (unsigned)(this.op) & 15;
530 state->extra = (unsigned)(here.op) & 15;
522 if (state->extra != 0) {
523 NEEDBITS(state->extra);
524 state->length += BITS(state->extra);
525 DROPBITS(state->extra);
526 }
527 Tracevv((stderr, "inflate: length %u\n", state->length));
528
529 /* get distance code */
530 for (;;) {
531 if (state->extra != 0) {
532 NEEDBITS(state->extra);
533 state->length += BITS(state->extra);
534 DROPBITS(state->extra);
535 }
536 Tracevv((stderr, "inflate: length %u\n", state->length));
537
538 /* get distance code */
539 for (;;) {
531 this = state->distcode[BITS(state->distbits)];
532 if ((unsigned)(this.bits) <= bits) break;
540 here = state->distcode[BITS(state->distbits)];
541 if ((unsigned)(here.bits) <= bits) break;
533 PULLBYTE();
534 }
542 PULLBYTE();
543 }
535 if ((this.op & 0xf0) == 0) {
536 last = this;
544 if ((here.op & 0xf0) == 0) {
545 last = here;
537 for (;;) {
546 for (;;) {
538 this = state->distcode[last.val +
547 here = state->distcode[last.val +
539 (BITS(last.bits + last.op) >> last.bits)];
548 (BITS(last.bits + last.op) >> last.bits)];
540 if ((unsigned)(last.bits + this.bits) <= bits) break;
549 if ((unsigned)(last.bits + here.bits) <= bits) break;
541 PULLBYTE();
542 }
543 DROPBITS(last.bits);
544 }
550 PULLBYTE();
551 }
552 DROPBITS(last.bits);
553 }
545 DROPBITS(this.bits);
546 if (this.op & 64) {
554 DROPBITS(here.bits);
555 if (here.op & 64) {
547 strm->msg = (char *)"invalid distance code";
548 state->mode = BAD;
549 break;
550 }
556 strm->msg = (char *)"invalid distance code";
557 state->mode = BAD;
558 break;
559 }
551 state->offset = (unsigned)this.val;
560 state->offset = (unsigned)here.val;
552
553 /* get distance extra bits, if any */
561
562 /* get distance extra bits, if any */
554 state->extra = (unsigned)(this.op) & 15;
563 state->extra = (unsigned)(here.op) & 15;
555 if (state->extra != 0) {
556 NEEDBITS(state->extra);
557 state->offset += BITS(state->extra);
558 DROPBITS(state->extra);
559 }
560 if (state->offset > state->wsize - (state->whave < state->wsize ?
561 left : 0)) {
562 strm->msg = (char *)"invalid distance too far back";

--- 61 unchanged lines hidden ---
564 if (state->extra != 0) {
565 NEEDBITS(state->extra);
566 state->offset += BITS(state->extra);
567 DROPBITS(state->extra);
568 }
569 if (state->offset > state->wsize - (state->whave < state->wsize ?
570 left : 0)) {
571 strm->msg = (char *)"invalid distance too far back";

--- 61 unchanged lines hidden ---