Mercurial > hg > CbC > CbC_gcc
comparison zlib/examples/gun.c @ 111:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | ae3a4bfb450b |
children |
comparison
equal
deleted
inserted
replaced
68:561a7518be6b | 111:04ced10e8804 |
---|---|
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, 2012 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.7 12 August 2012 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() |
8 1.1 21 Feb 2005 Decompress concatenated gzip streams | 8 1.1 21 Feb 2005 Decompress concatenated gzip streams |
9 Remove use of "this" variable (C++ keyword) | 9 Remove use of "this" variable (C++ keyword) |
13 Add -h option for command version and usage | 13 Add -h option for command version and usage |
14 Add a bunch of comments | 14 Add a bunch of comments |
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 | |
21 1.7 12 Aug 2012 Update for z_const usage in zlib 1.2.8 | |
18 */ | 22 */ |
19 | 23 |
20 /* | 24 /* |
21 gun [ -t ] [ name ... ] | 25 gun [ -t ] [ name ... ] |
22 | 26 |
40 compression. These files are automatically detected by virtue of their | 44 compression. These files are automatically detected by virtue of their |
41 magic header bytes. Since the end of Unix compress stream is marked by the | 45 magic header bytes. Since the end of Unix compress stream is marked by the |
42 end-of-file, they cannot be concantenated. If a Unix compress stream is | 46 end-of-file, they cannot be concantenated. If a Unix compress stream is |
43 encountered in an input file, it is the last stream in that file. | 47 encountered in an input file, it is the last stream in that file. |
44 | 48 |
45 Like gunzip and uncompress, the file attributes of the orignal compressed | 49 Like gunzip and uncompress, the file attributes of the original compressed |
46 file are maintained in the final uncompressed file, to the extent that the | 50 file are maintained in the final uncompressed file, to the extent that the |
47 user permissions allow it. | 51 user permissions allow it. |
48 | 52 |
49 On my Mac OS X PowerPC G4, gun is almost twice as fast as gunzip (version | 53 On my Mac OS X PowerPC G4, gun is almost twice as fast as gunzip (version |
50 1.2.4) is on the same file, when gun is linked with zlib 1.2.2. Also the | 54 1.2.4) is on the same file, when gun is linked with zlib 1.2.2. Also the |
80 }; | 84 }; |
81 | 85 |
82 /* Load input buffer, assumed to be empty, and return bytes loaded and a | 86 /* Load input buffer, assumed to be empty, and return bytes loaded and a |
83 pointer to them. read() is called until the buffer is full, or until it | 87 pointer to them. read() is called until the buffer is full, or until it |
84 returns end-of-file or error. Return 0 on error. */ | 88 returns end-of-file or error. Return 0 on error. */ |
85 local unsigned in(void *in_desc, unsigned char **buf) | 89 local unsigned in(void *in_desc, z_const unsigned char **buf) |
86 { | 90 { |
87 int ret; | 91 int ret; |
88 unsigned len; | 92 unsigned len; |
89 unsigned char *next; | 93 unsigned char *next; |
90 struct ind *me = (struct ind *)in_desc; | 94 struct ind *me = (struct ind *)in_desc; |
191 | 195 |
192 lunpipe() will return Z_OK on success, Z_BUF_ERROR for an unexpected end of | 196 lunpipe() will return Z_OK on success, Z_BUF_ERROR for an unexpected end of |
193 file, read error, or write error (a write error indicated by strm->next_in | 197 file, read error, or write error (a write error indicated by strm->next_in |
194 not equal to Z_NULL), or Z_DATA_ERROR for invalid input. | 198 not equal to Z_NULL), or Z_DATA_ERROR for invalid input. |
195 */ | 199 */ |
196 local int lunpipe(unsigned have, unsigned char *next, struct ind *indp, | 200 local int lunpipe(unsigned have, z_const unsigned char *next, struct ind *indp, |
197 int outfile, z_stream *strm) | 201 int outfile, z_stream *strm) |
198 { | 202 { |
199 int last; /* last byte read by NEXT(), or -1 if EOF */ | 203 int last; /* last byte read by NEXT(), or -1 if EOF */ |
200 int chunk; /* bytes left in current chunk */ | 204 unsigned chunk; /* bytes left in current chunk */ |
201 int left; /* bits left in rem */ | 205 int left; /* bits left in rem */ |
202 unsigned rem; /* unused bits from input */ | 206 unsigned rem; /* unused bits from input */ |
203 int bits; /* current bits per code */ | 207 int bits; /* current bits per code */ |
204 unsigned code; /* code, table traversal index */ | 208 unsigned code; /* code, table traversal index */ |
205 unsigned mask; /* mask for current bits codes */ | 209 unsigned mask; /* mask for current bits codes */ |
206 int max; /* maximum bits per code for this stream */ | 210 int max; /* maximum bits per code for this stream */ |
207 int flags; /* compress flags, then block compress flag */ | 211 unsigned flags; /* compress flags, then block compress flag */ |
208 unsigned end; /* last valid entry in prefix/suffix tables */ | 212 unsigned end; /* last valid entry in prefix/suffix tables */ |
209 unsigned temp; /* current code */ | 213 unsigned temp; /* current code */ |
210 unsigned prev; /* previous code */ | 214 unsigned prev; /* previous code */ |
211 unsigned final; /* last character written for previous code */ | 215 unsigned final; /* last character written for previous code */ |
212 unsigned stack; /* next position for reversed string */ | 216 unsigned stack; /* next position for reversed string */ |
213 unsigned outcnt; /* bytes in output buffer */ | 217 unsigned outcnt; /* bytes in output buffer */ |
214 struct outd outd; /* output structure */ | 218 struct outd outd; /* output structure */ |
219 unsigned char *p; | |
215 | 220 |
216 /* set up output */ | 221 /* set up output */ |
217 outd.outfile = outfile; | 222 outd.outfile = outfile; |
218 outd.check = 0; | 223 outd.check = 0; |
219 | 224 |
320 match[stack++] = (unsigned char)final; | 325 match[stack++] = (unsigned char)final; |
321 code = prev; | 326 code = prev; |
322 } | 327 } |
323 | 328 |
324 /* walk through linked list to generate output in reverse order */ | 329 /* walk through linked list to generate output in reverse order */ |
330 p = match + stack; | |
325 while (code >= 256) { | 331 while (code >= 256) { |
326 match[stack++] = suffix[code]; | 332 *p++ = suffix[code]; |
327 code = prefix[code]; | 333 code = prefix[code]; |
328 } | 334 } |
335 stack = p - match; | |
329 match[stack++] = (unsigned char)code; | 336 match[stack++] = (unsigned char)code; |
330 final = code; | 337 final = code; |
331 | 338 |
332 /* link new table entry */ | 339 /* link new table entry */ |
333 if (end < mask) { | 340 if (end < mask) { |
347 strm->next_in = outbuf; /* signal write error */ | 354 strm->next_in = outbuf; /* signal write error */ |
348 return Z_BUF_ERROR; | 355 return Z_BUF_ERROR; |
349 } | 356 } |
350 outcnt = 0; | 357 outcnt = 0; |
351 } | 358 } |
359 p = match + stack; | |
352 do { | 360 do { |
353 outbuf[outcnt++] = match[--stack]; | 361 outbuf[outcnt++] = *--p; |
354 } while (stack); | 362 } while (p > match); |
363 stack = 0; | |
355 | 364 |
356 /* loop for next code with final and prev as the last match, rem and | 365 /* 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 | 366 left provide the first 0..7 bits of the next code, end is the last |
358 valid table entry */ | 367 valid table entry */ |
359 } | 368 } |
373 */ | 382 */ |
374 local int gunpipe(z_stream *strm, int infile, int outfile) | 383 local int gunpipe(z_stream *strm, int infile, int outfile) |
375 { | 384 { |
376 int ret, first, last; | 385 int ret, first, last; |
377 unsigned have, flags, len; | 386 unsigned have, flags, len; |
378 unsigned char *next; | 387 z_const unsigned char *next = NULL; |
379 struct ind ind, *indp; | 388 struct ind ind, *indp; |
380 struct outd outd; | 389 struct outd outd; |
381 | 390 |
382 /* setup input buffer */ | 391 /* setup input buffer */ |
383 ind.infile = infile; | 392 ind.infile = infile; |
469 have = strm->avail_in; | 478 have = strm->avail_in; |
470 strm->next_in = Z_NULL; /* so Z_BUF_ERROR means EOF */ | 479 strm->next_in = Z_NULL; /* so Z_BUF_ERROR means EOF */ |
471 | 480 |
472 /* check trailer */ | 481 /* check trailer */ |
473 ret = Z_BUF_ERROR; | 482 ret = Z_BUF_ERROR; |
474 if (NEXT() != (outd.crc & 0xff) || | 483 if (NEXT() != (int)(outd.crc & 0xff) || |
475 NEXT() != ((outd.crc >> 8) & 0xff) || | 484 NEXT() != (int)((outd.crc >> 8) & 0xff) || |
476 NEXT() != ((outd.crc >> 16) & 0xff) || | 485 NEXT() != (int)((outd.crc >> 16) & 0xff) || |
477 NEXT() != ((outd.crc >> 24) & 0xff)) { | 486 NEXT() != (int)((outd.crc >> 24) & 0xff)) { |
478 /* crc error */ | 487 /* crc error */ |
479 if (last != -1) { | 488 if (last != -1) { |
480 strm->msg = (char *)"incorrect data check"; | 489 strm->msg = (char *)"incorrect data check"; |
481 ret = Z_DATA_ERROR; | 490 ret = Z_DATA_ERROR; |
482 } | 491 } |
483 break; | 492 break; |
484 } | 493 } |
485 if (NEXT() != (outd.total & 0xff) || | 494 if (NEXT() != (int)(outd.total & 0xff) || |
486 NEXT() != ((outd.total >> 8) & 0xff) || | 495 NEXT() != (int)((outd.total >> 8) & 0xff) || |
487 NEXT() != ((outd.total >> 16) & 0xff) || | 496 NEXT() != (int)((outd.total >> 16) & 0xff) || |
488 NEXT() != ((outd.total >> 24) & 0xff)) { | 497 NEXT() != (int)((outd.total >> 24) & 0xff)) { |
489 /* length error */ | 498 /* length error */ |
490 if (last != -1) { | 499 if (last != -1) { |
491 strm->msg = (char *)"incorrect length check"; | 500 strm->msg = (char *)"incorrect length check"; |
492 ret = Z_DATA_ERROR; | 501 ret = Z_DATA_ERROR; |
493 } | 502 } |
640 /* decompress each file to the same name with the suffix removed */ | 649 /* decompress each file to the same name with the suffix removed */ |
641 argc--; | 650 argc--; |
642 argv++; | 651 argv++; |
643 test = 0; | 652 test = 0; |
644 if (argc && strcmp(*argv, "-h") == 0) { | 653 if (argc && strcmp(*argv, "-h") == 0) { |
645 fprintf(stderr, "gun 1.3 (12 Jun 2005)\n"); | 654 fprintf(stderr, "gun 1.6 (17 Jan 2010)\n"); |
646 fprintf(stderr, "Copyright (c) 2005 Mark Adler\n"); | 655 fprintf(stderr, "Copyright (C) 2003-2010 Mark Adler\n"); |
647 fprintf(stderr, "usage: gun [-t] [file1.gz [file2.Z ...]]\n"); | 656 fprintf(stderr, "usage: gun [-t] [file1.gz [file2.Z ...]]\n"); |
648 return 0; | 657 return 0; |
649 } | 658 } |
650 if (argc && strcmp(*argv, "-t") == 0) { | 659 if (argc && strcmp(*argv, "-t") == 0) { |
651 test = 1; | 660 test = 1; |