From b1c19ca6d82c98a8be6cd9cad7a9c5fa5e8e634e Mon Sep 17 00:00:00 2001 From: Mark Adler Date: Fri, 9 Sep 2011 23:25:27 -0700 Subject: zlib 1.2.3.1 --- examples/gzlog.c | 2 +- examples/zlib_how.html | 36 +++++++++++++++++++++++++++++------- examples/zpipe.c | 24 +++++++++++++++++++----- examples/zran.c | 2 +- 4 files changed, 50 insertions(+), 14 deletions(-) (limited to 'examples') 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) some = len; if (write(log->fd, data, some) != some) return 1; - log->crc = crc32(log->crc, data, some); + log->crc = crc32(log->crc, (unsigned char *)data, some); log->len += some; len -= some; 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 @@ zlib Usage Example - +

zlib Usage Example

@@ -21,13 +21,16 @@ Without further adieu, here is the program zpipe.c /* zpipe.c: example of proper use of zlib's inflate() and deflate() Not copyrighted -- provided to the public domain - Version 1.2 9 November 2004 Mark Adler */ + Version 1.4 11 December 2005 Mark Adler */ /* Version history: 1.0 30 Oct 2004 First version 1.1 8 Nov 2004 Add void casting for unused return values Use switch statement for inflate() return values 1.2 9 Nov 2004 Add assertions to document zlib guarantees + 1.3 6 Apr 2005 Remove incorrect assertion in inf() + 1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions + Avoid some compiler warnings for input and output buffers */ We now include the header files for the required definitions. From @@ -47,6 +50,21 @@ functions inflateInit(), inflate(), and #include <assert.h> #include "zlib.h" +This is an ugly hack required to avoid corruption of the input and output data on +Windows/MS-DOS systems. Without this, those systems would assume that the input and output +files are text, and try to convert the end-of-line characters from one standard to +another. That would corrupt binary data, and in particular would render the compressed data unusable. +This sets the input and output to binary which suppresses the end-of-line conversions. +SET_BINARY_MODE() will be used later on stdin and stdout, at the beginning of main(). +

+#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
+#  include <fcntl.h>
+#  include <io.h>
+#  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
+#else
+#  define SET_BINARY_MODE(file)
+#endif
+
CHUNK is simply the buffer size for feeding data to and pulling data from the zlib routines. Larger buffer sizes would be more efficient, especially for inflate(). If the memory is available, buffers sizes @@ -80,8 +98,8 @@ is used to pass information to and from the zlib routines, and to maint int ret, flush; unsigned have; z_stream strm; - char in[CHUNK]; - char out[CHUNK]; + unsigned char in[CHUNK]; + unsigned char out[CHUNK]; The first thing we do is to initialize the zlib state for compression using deflateInit(). This must be done before the first use of deflate(). @@ -313,8 +331,8 @@ can tell from the zlib stream itself when the stream is complete. int ret; unsigned have; z_stream strm; - char in[CHUNK]; - char out[CHUNK]; + unsigned char in[CHUNK]; + unsigned char out[CHUNK]; The initialization of the state is the same, except that there is no compression level, of course, and two more elements of the structure are initialized. avail_in @@ -494,6 +512,10 @@ int main(int argc, char **argv) { int ret; + /* avoid end-of-line conversions */ + SET_BINARY_MODE(stdin); + SET_BINARY_MODE(stdout); + /* do compression if no arguments */ if (argc == 1) { ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); @@ -518,6 +540,6 @@ int main(int argc, char **argv) }
-Copyright (c) 2004 by Mark Adler
Last modified 13 November 2004
+Copyright (c) 2004, 2005 by Mark Adler
Last modified 11 December 2005
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 @@ /* zpipe.c: example of proper use of zlib's inflate() and deflate() Not copyrighted -- provided to the public domain - Version 1.2 9 November 2004 Mark Adler */ + Version 1.4 11 December 2005 Mark Adler */ /* Version history: 1.0 30 Oct 2004 First version @@ -8,6 +8,8 @@ Use switch statement for inflate() return values 1.2 9 Nov 2004 Add assertions to document zlib guarantees 1.3 6 Apr 2005 Remove incorrect assertion in inf() + 1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions + Avoid some compiler warnings for input and output buffers */ #include @@ -15,6 +17,14 @@ #include #include "zlib.h" +#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) +# include +# include +# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) +#else +# define SET_BINARY_MODE(file) +#endif + #define CHUNK 16384 /* Compress from file source to file dest until EOF on source. @@ -28,8 +38,8 @@ int def(FILE *source, FILE *dest, int level) int ret, flush; unsigned have; z_stream strm; - char in[CHUNK]; - char out[CHUNK]; + unsigned char in[CHUNK]; + unsigned char out[CHUNK]; /* allocate deflate state */ strm.zalloc = Z_NULL; @@ -84,8 +94,8 @@ int inf(FILE *source, FILE *dest) int ret; unsigned have; z_stream strm; - char in[CHUNK]; - char out[CHUNK]; + unsigned char in[CHUNK]; + unsigned char out[CHUNK]; /* allocate inflate state */ strm.zalloc = Z_NULL; @@ -167,6 +177,10 @@ int main(int argc, char **argv) { int ret; + /* avoid end-of-line conversions */ + SET_BINARY_MODE(stdin); + SET_BINARY_MODE(stdout); + /* do compression if no arguments */ if (argc == 1) { 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) int len; off_t offset; FILE *in; - struct access *index; + struct access *index = NULL; unsigned char buf[CHUNK]; /* open input file */ -- cgit v1.2.1