summaryrefslogtreecommitdiff
path: root/examples/gun.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/gun.c')
-rw-r--r--examples/gun.c44
1 files changed, 26 insertions, 18 deletions
diff --git a/examples/gun.c b/examples/gun.c
index bfec590..72b0882 100644
--- a/examples/gun.c
+++ b/examples/gun.c
@@ -1,7 +1,7 @@
1/* gun.c -- simple gunzip to give an example of the use of inflateBack() 1/* gun.c -- simple gunzip to give an example of the use of inflateBack()
2 * Copyright (C) 2003, 2005 Mark Adler 2 * Copyright (C) 2003, 2005, 2008, 2010 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 Version 1.3 12 June 2005 Mark Adler */ 4 Version 1.6 17 January 2010 Mark Adler */
5 5
6/* Version history: 6/* Version history:
7 1.0 16 Feb 2003 First version for testing of inflateBack() 7 1.0 16 Feb 2003 First version for testing of inflateBack()
@@ -15,6 +15,9 @@
15 1.2 20 Mar 2005 Add Unix compress (LZW) decompression 15 1.2 20 Mar 2005 Add Unix compress (LZW) decompression
16 Copy file attributes from input file to output file 16 Copy file attributes from input file to output file
17 1.3 12 Jun 2005 Add casts for error messages [Oberhumer] 17 1.3 12 Jun 2005 Add casts for error messages [Oberhumer]
18 1.4 8 Dec 2006 LZW decompression speed improvements
19 1.5 9 Feb 2008 Avoid warning in latest version of gcc
20 1.6 17 Jan 2010 Avoid signed/unsigned comparison warnings
18 */ 21 */
19 22
20/* 23/*
@@ -197,14 +200,14 @@ local int lunpipe(unsigned have, unsigned char *next, struct ind *indp,
197 int outfile, z_stream *strm) 200 int outfile, z_stream *strm)
198{ 201{
199 int last; /* last byte read by NEXT(), or -1 if EOF */ 202 int last; /* last byte read by NEXT(), or -1 if EOF */
200 int chunk; /* bytes left in current chunk */ 203 unsigned chunk; /* bytes left in current chunk */
201 int left; /* bits left in rem */ 204 int left; /* bits left in rem */
202 unsigned rem; /* unused bits from input */ 205 unsigned rem; /* unused bits from input */
203 int bits; /* current bits per code */ 206 int bits; /* current bits per code */
204 unsigned code; /* code, table traversal index */ 207 unsigned code; /* code, table traversal index */
205 unsigned mask; /* mask for current bits codes */ 208 unsigned mask; /* mask for current bits codes */
206 int max; /* maximum bits per code for this stream */ 209 int max; /* maximum bits per code for this stream */
207 int flags; /* compress flags, then block compress flag */ 210 unsigned flags; /* compress flags, then block compress flag */
208 unsigned end; /* last valid entry in prefix/suffix tables */ 211 unsigned end; /* last valid entry in prefix/suffix tables */
209 unsigned temp; /* current code */ 212 unsigned temp; /* current code */
210 unsigned prev; /* previous code */ 213 unsigned prev; /* previous code */
@@ -212,6 +215,7 @@ local int lunpipe(unsigned have, unsigned char *next, struct ind *indp,
212 unsigned stack; /* next position for reversed string */ 215 unsigned stack; /* next position for reversed string */
213 unsigned outcnt; /* bytes in output buffer */ 216 unsigned outcnt; /* bytes in output buffer */
214 struct outd outd; /* output structure */ 217 struct outd outd; /* output structure */
218 unsigned char *p;
215 219
216 /* set up output */ 220 /* set up output */
217 outd.outfile = outfile; 221 outd.outfile = outfile;
@@ -322,10 +326,12 @@ local int lunpipe(unsigned have, unsigned char *next, struct ind *indp,
322 } 326 }
323 327
324 /* walk through linked list to generate output in reverse order */ 328 /* walk through linked list to generate output in reverse order */
329 p = match + stack;
325 while (code >= 256) { 330 while (code >= 256) {
326 match[stack++] = suffix[code]; 331 *p++ = suffix[code];
327 code = prefix[code]; 332 code = prefix[code];
328 } 333 }
334 stack = p - match;
329 match[stack++] = (unsigned char)code; 335 match[stack++] = (unsigned char)code;
330 final = code; 336 final = code;
331 337
@@ -349,9 +355,11 @@ local int lunpipe(unsigned have, unsigned char *next, struct ind *indp,
349 } 355 }
350 outcnt = 0; 356 outcnt = 0;
351 } 357 }
358 p = match + stack;
352 do { 359 do {
353 outbuf[outcnt++] = match[--stack]; 360 outbuf[outcnt++] = *--p;
354 } while (stack); 361 } while (p > match);
362 stack = 0;
355 363
356 /* loop for next code with final and prev as the last match, rem and 364 /* loop for next code with final and prev as the last match, rem and
357 left provide the first 0..7 bits of the next code, end is the last 365 left provide the first 0..7 bits of the next code, end is the last
@@ -375,7 +383,7 @@ local int gunpipe(z_stream *strm, int infile, int outfile)
375{ 383{
376 int ret, first, last; 384 int ret, first, last;
377 unsigned have, flags, len; 385 unsigned have, flags, len;
378 unsigned char *next; 386 unsigned char *next = NULL;
379 struct ind ind, *indp; 387 struct ind ind, *indp;
380 struct outd outd; 388 struct outd outd;
381 389
@@ -471,10 +479,10 @@ local int gunpipe(z_stream *strm, int infile, int outfile)
471 479
472 /* check trailer */ 480 /* check trailer */
473 ret = Z_BUF_ERROR; 481 ret = Z_BUF_ERROR;
474 if (NEXT() != (outd.crc & 0xff) || 482 if (NEXT() != (int)(outd.crc & 0xff) ||
475 NEXT() != ((outd.crc >> 8) & 0xff) || 483 NEXT() != (int)((outd.crc >> 8) & 0xff) ||
476 NEXT() != ((outd.crc >> 16) & 0xff) || 484 NEXT() != (int)((outd.crc >> 16) & 0xff) ||
477 NEXT() != ((outd.crc >> 24) & 0xff)) { 485 NEXT() != (int)((outd.crc >> 24) & 0xff)) {
478 /* crc error */ 486 /* crc error */
479 if (last != -1) { 487 if (last != -1) {
480 strm->msg = (char *)"incorrect data check"; 488 strm->msg = (char *)"incorrect data check";
@@ -482,10 +490,10 @@ local int gunpipe(z_stream *strm, int infile, int outfile)
482 } 490 }
483 break; 491 break;
484 } 492 }
485 if (NEXT() != (outd.total & 0xff) || 493 if (NEXT() != (int)(outd.total & 0xff) ||
486 NEXT() != ((outd.total >> 8) & 0xff) || 494 NEXT() != (int)((outd.total >> 8) & 0xff) ||
487 NEXT() != ((outd.total >> 16) & 0xff) || 495 NEXT() != (int)((outd.total >> 16) & 0xff) ||
488 NEXT() != ((outd.total >> 24) & 0xff)) { 496 NEXT() != (int)((outd.total >> 24) & 0xff)) {
489 /* length error */ 497 /* length error */
490 if (last != -1) { 498 if (last != -1) {
491 strm->msg = (char *)"incorrect length check"; 499 strm->msg = (char *)"incorrect length check";
@@ -642,8 +650,8 @@ int main(int argc, char **argv)
642 argv++; 650 argv++;
643 test = 0; 651 test = 0;
644 if (argc && strcmp(*argv, "-h") == 0) { 652 if (argc && strcmp(*argv, "-h") == 0) {
645 fprintf(stderr, "gun 1.3 (12 Jun 2005)\n"); 653 fprintf(stderr, "gun 1.6 (17 Jan 2010)\n");
646 fprintf(stderr, "Copyright (c) 2005 Mark Adler\n"); 654 fprintf(stderr, "Copyright (C) 2003-2010 Mark Adler\n");
647 fprintf(stderr, "usage: gun [-t] [file1.gz [file2.Z ...]]\n"); 655 fprintf(stderr, "usage: gun [-t] [file1.gz [file2.Z ...]]\n");
648 return 0; 656 return 0;
649 } 657 }