diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:25:27 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:25:27 -0700 |
commit | b1c19ca6d82c98a8be6cd9cad7a9c5fa5e8e634e (patch) | |
tree | f0eeb8f52d07819f417411da5104c9d743dd46de /examples | |
parent | abf180a067223611620dd97dd5681df7c7fa7c9b (diff) | |
download | zlib-b1c19ca6d82c98a8be6cd9cad7a9c5fa5e8e634e.tar.gz zlib-b1c19ca6d82c98a8be6cd9cad7a9c5fa5e8e634e.tar.bz2 zlib-b1c19ca6d82c98a8be6cd9cad7a9c5fa5e8e634e.zip |
zlib 1.2.3.1v1.2.3.1
Diffstat (limited to 'examples')
-rw-r--r-- | examples/gzlog.c | 2 | ||||
-rw-r--r-- | examples/zlib_how.html | 36 | ||||
-rw-r--r-- | examples/zpipe.c | 24 | ||||
-rw-r--r-- | examples/zran.c | 2 |
4 files changed, 50 insertions, 14 deletions
diff --git a/examples/gzlog.c b/examples/gzlog.c index f71f817..b6acdef 100644 --- a/examples/gzlog.c +++ b/examples/gzlog.c | |||
@@ -241,7 +241,7 @@ int gzlog_write(void *obj, char *data, size_t len) | |||
241 | some = len; | 241 | some = len; |
242 | if (write(log->fd, data, some) != some) | 242 | if (write(log->fd, data, some) != some) |
243 | return 1; | 243 | return 1; |
244 | log->crc = crc32(log->crc, data, some); | 244 | log->crc = crc32(log->crc, (unsigned char *)data, some); |
245 | log->len += some; | 245 | log->len += some; |
246 | len -= some; | 246 | len -= some; |
247 | data += some; | 247 | data += some; |
diff --git a/examples/zlib_how.html b/examples/zlib_how.html index 40998db..444ff1c 100644 --- a/examples/zlib_how.html +++ b/examples/zlib_how.html | |||
@@ -4,7 +4,7 @@ | |||
4 | <head> | 4 | <head> |
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
6 | <title>zlib Usage Example</title> | 6 | <title>zlib Usage Example</title> |
7 | <!-- Copyright (c) 2004 Mark Adler. --> | 7 | <!-- Copyright (c) 2004, 2005 Mark Adler. --> |
8 | </head> | 8 | </head> |
9 | <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#00A000"> | 9 | <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#00A000"> |
10 | <h2 align="center"> zlib Usage Example </h2> | 10 | <h2 align="center"> zlib Usage Example </h2> |
@@ -21,13 +21,16 @@ Without further adieu, here is the program <a href="zpipe.c"><tt>zpipe.c</tt></a | |||
21 | <pre><b> | 21 | <pre><b> |
22 | /* zpipe.c: example of proper use of zlib's inflate() and deflate() | 22 | /* zpipe.c: example of proper use of zlib's inflate() and deflate() |
23 | Not copyrighted -- provided to the public domain | 23 | Not copyrighted -- provided to the public domain |
24 | Version 1.2 9 November 2004 Mark Adler */ | 24 | Version 1.4 11 December 2005 Mark Adler */ |
25 | 25 | ||
26 | /* Version history: | 26 | /* Version history: |
27 | 1.0 30 Oct 2004 First version | 27 | 1.0 30 Oct 2004 First version |
28 | 1.1 8 Nov 2004 Add void casting for unused return values | 28 | 1.1 8 Nov 2004 Add void casting for unused return values |
29 | Use switch statement for inflate() return values | 29 | Use switch statement for inflate() return values |
30 | 1.2 9 Nov 2004 Add assertions to document zlib guarantees | 30 | 1.2 9 Nov 2004 Add assertions to document zlib guarantees |
31 | 1.3 6 Apr 2005 Remove incorrect assertion in inf() | ||
32 | 1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions | ||
33 | Avoid some compiler warnings for input and output buffers | ||
31 | */ | 34 | */ |
32 | </b></pre><!-- --> | 35 | </b></pre><!-- --> |
33 | We now include the header files for the required definitions. From | 36 | We now include the header files for the required definitions. From |
@@ -47,6 +50,21 @@ functions <tt>inflateInit()</tt>, <tt>inflate()</tt>, and | |||
47 | #include <assert.h> | 50 | #include <assert.h> |
48 | #include "zlib.h" | 51 | #include "zlib.h" |
49 | </b></pre><!-- --> | 52 | </b></pre><!-- --> |
53 | This is an ugly hack required to avoid corruption of the input and output data on | ||
54 | Windows/MS-DOS systems. Without this, those systems would assume that the input and output | ||
55 | files are text, and try to convert the end-of-line characters from one standard to | ||
56 | another. That would corrupt binary data, and in particular would render the compressed data unusable. | ||
57 | This sets the input and output to binary which suppresses the end-of-line conversions. | ||
58 | <tt>SET_BINARY_MODE()</tt> will be used later on <tt>stdin</tt> and <tt>stdout</tt>, at the beginning of <tt>main()</tt>. | ||
59 | <pre><b> | ||
60 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) | ||
61 | # include <fcntl.h> | ||
62 | # include <io.h> | ||
63 | # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) | ||
64 | #else | ||
65 | # define SET_BINARY_MODE(file) | ||
66 | #endif | ||
67 | </b></pre><!-- --> | ||
50 | <tt>CHUNK</tt> is simply the buffer size for feeding data to and pulling data | 68 | <tt>CHUNK</tt> is simply the buffer size for feeding data to and pulling data |
51 | from the <em>zlib</em> routines. Larger buffer sizes would be more efficient, | 69 | from the <em>zlib</em> routines. Larger buffer sizes would be more efficient, |
52 | especially for <tt>inflate()</tt>. If the memory is available, buffers sizes | 70 | especially for <tt>inflate()</tt>. If the memory is available, buffers sizes |
@@ -80,8 +98,8 @@ is used to pass information to and from the <em>zlib</em> routines, and to maint | |||
80 | int ret, flush; | 98 | int ret, flush; |
81 | unsigned have; | 99 | unsigned have; |
82 | z_stream strm; | 100 | z_stream strm; |
83 | char in[CHUNK]; | 101 | unsigned char in[CHUNK]; |
84 | char out[CHUNK]; | 102 | unsigned char out[CHUNK]; |
85 | </b></pre><!-- --> | 103 | </b></pre><!-- --> |
86 | The first thing we do is to initialize the <em>zlib</em> state for compression using | 104 | The first thing we do is to initialize the <em>zlib</em> state for compression using |
87 | <tt>deflateInit()</tt>. This must be done before the first use of <tt>deflate()</tt>. | 105 | <tt>deflateInit()</tt>. This must be done before the first use of <tt>deflate()</tt>. |
@@ -313,8 +331,8 @@ can tell from the <em>zlib</em> stream itself when the stream is complete. | |||
313 | int ret; | 331 | int ret; |
314 | unsigned have; | 332 | unsigned have; |
315 | z_stream strm; | 333 | z_stream strm; |
316 | char in[CHUNK]; | 334 | unsigned char in[CHUNK]; |
317 | char out[CHUNK]; | 335 | unsigned char out[CHUNK]; |
318 | </b></pre><!-- --> | 336 | </b></pre><!-- --> |
319 | The initialization of the state is the same, except that there is no compression level, | 337 | The initialization of the state is the same, except that there is no compression level, |
320 | of course, and two more elements of the structure are initialized. <tt>avail_in</tt> | 338 | of course, and two more elements of the structure are initialized. <tt>avail_in</tt> |
@@ -494,6 +512,10 @@ int main(int argc, char **argv) | |||
494 | { | 512 | { |
495 | int ret; | 513 | int ret; |
496 | 514 | ||
515 | /* avoid end-of-line conversions */ | ||
516 | SET_BINARY_MODE(stdin); | ||
517 | SET_BINARY_MODE(stdout); | ||
518 | |||
497 | /* do compression if no arguments */ | 519 | /* do compression if no arguments */ |
498 | if (argc == 1) { | 520 | if (argc == 1) { |
499 | ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); | 521 | ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); |
@@ -518,6 +540,6 @@ int main(int argc, char **argv) | |||
518 | } | 540 | } |
519 | </b></pre> | 541 | </b></pre> |
520 | <hr> | 542 | <hr> |
521 | <i>Copyright (c) 2004 by Mark Adler<br>Last modified 13 November 2004</i> | 543 | <i>Copyright (c) 2004, 2005 by Mark Adler<br>Last modified 11 December 2005</i> |
522 | </body> | 544 | </body> |
523 | </html> | 545 | </html> |
diff --git a/examples/zpipe.c b/examples/zpipe.c index 26abb56..83535d1 100644 --- a/examples/zpipe.c +++ b/examples/zpipe.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* zpipe.c: example of proper use of zlib's inflate() and deflate() | 1 | /* zpipe.c: example of proper use of zlib's inflate() and deflate() |
2 | Not copyrighted -- provided to the public domain | 2 | Not copyrighted -- provided to the public domain |
3 | Version 1.2 9 November 2004 Mark Adler */ | 3 | Version 1.4 11 December 2005 Mark Adler */ |
4 | 4 | ||
5 | /* Version history: | 5 | /* Version history: |
6 | 1.0 30 Oct 2004 First version | 6 | 1.0 30 Oct 2004 First version |
@@ -8,6 +8,8 @@ | |||
8 | Use switch statement for inflate() return values | 8 | Use switch statement for inflate() return values |
9 | 1.2 9 Nov 2004 Add assertions to document zlib guarantees | 9 | 1.2 9 Nov 2004 Add assertions to document zlib guarantees |
10 | 1.3 6 Apr 2005 Remove incorrect assertion in inf() | 10 | 1.3 6 Apr 2005 Remove incorrect assertion in inf() |
11 | 1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions | ||
12 | Avoid some compiler warnings for input and output buffers | ||
11 | */ | 13 | */ |
12 | 14 | ||
13 | #include <stdio.h> | 15 | #include <stdio.h> |
@@ -15,6 +17,14 @@ | |||
15 | #include <assert.h> | 17 | #include <assert.h> |
16 | #include "zlib.h" | 18 | #include "zlib.h" |
17 | 19 | ||
20 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) | ||
21 | # include <fcntl.h> | ||
22 | # include <io.h> | ||
23 | # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) | ||
24 | #else | ||
25 | # define SET_BINARY_MODE(file) | ||
26 | #endif | ||
27 | |||
18 | #define CHUNK 16384 | 28 | #define CHUNK 16384 |
19 | 29 | ||
20 | /* Compress from file source to file dest until EOF on source. | 30 | /* Compress from file source to file dest until EOF on source. |
@@ -28,8 +38,8 @@ int def(FILE *source, FILE *dest, int level) | |||
28 | int ret, flush; | 38 | int ret, flush; |
29 | unsigned have; | 39 | unsigned have; |
30 | z_stream strm; | 40 | z_stream strm; |
31 | char in[CHUNK]; | 41 | unsigned char in[CHUNK]; |
32 | char out[CHUNK]; | 42 | unsigned char out[CHUNK]; |
33 | 43 | ||
34 | /* allocate deflate state */ | 44 | /* allocate deflate state */ |
35 | strm.zalloc = Z_NULL; | 45 | strm.zalloc = Z_NULL; |
@@ -84,8 +94,8 @@ int inf(FILE *source, FILE *dest) | |||
84 | int ret; | 94 | int ret; |
85 | unsigned have; | 95 | unsigned have; |
86 | z_stream strm; | 96 | z_stream strm; |
87 | char in[CHUNK]; | 97 | unsigned char in[CHUNK]; |
88 | char out[CHUNK]; | 98 | unsigned char out[CHUNK]; |
89 | 99 | ||
90 | /* allocate inflate state */ | 100 | /* allocate inflate state */ |
91 | strm.zalloc = Z_NULL; | 101 | strm.zalloc = Z_NULL; |
@@ -167,6 +177,10 @@ int main(int argc, char **argv) | |||
167 | { | 177 | { |
168 | int ret; | 178 | int ret; |
169 | 179 | ||
180 | /* avoid end-of-line conversions */ | ||
181 | SET_BINARY_MODE(stdin); | ||
182 | SET_BINARY_MODE(stdout); | ||
183 | |||
170 | /* do compression if no arguments */ | 184 | /* do compression if no arguments */ |
171 | if (argc == 1) { | 185 | if (argc == 1) { |
172 | ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); | 186 | ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); |
diff --git a/examples/zran.c b/examples/zran.c index 8c7717e..617a130 100644 --- a/examples/zran.c +++ b/examples/zran.c | |||
@@ -351,7 +351,7 @@ int main(int argc, char **argv) | |||
351 | int len; | 351 | int len; |
352 | off_t offset; | 352 | off_t offset; |
353 | FILE *in; | 353 | FILE *in; |
354 | struct access *index; | 354 | struct access *index = NULL; |
355 | unsigned char buf[CHUNK]; | 355 | unsigned char buf[CHUNK]; |
356 | 356 | ||
357 | /* open input file */ | 357 | /* open input file */ |