summaryrefslogtreecommitdiffstats
path: root/cpukit
diff options
context:
space:
mode:
authorRalf Corsepius <ralf.corsepius@rtems.org>2011-03-18 10:11:00 +0000
committerRalf Corsepius <ralf.corsepius@rtems.org>2011-03-18 10:11:00 +0000
commitf1c8de990fd26287fa0134d252410893ecb2cf4b (patch)
treeefbe2c6827f69e1dfd0d46ca6451192df6fd489b /cpukit
parentImport from zlib-1.2.4 (diff)
downloadrtems-f1c8de990fd26287fa0134d252410893ecb2cf4b.tar.bz2
Import from zlib-1.2.4
Diffstat (limited to 'cpukit')
-rw-r--r--cpukit/zlib/examples/zlib_how.html36
-rw-r--r--cpukit/zlib/examples/zpipe.c24
2 files changed, 48 insertions, 12 deletions
diff --git a/cpukit/zlib/examples/zlib_how.html b/cpukit/zlib/examples/zlib_how.html
index 40998dbf08..444ff1c9a3 100644
--- a/cpukit/zlib/examples/zlib_how.html
+++ b/cpukit/zlib/examples/zlib_how.html
@@ -4,7 +4,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>zlib Usage Example</title>
-<!-- Copyright (c) 2004 Mark Adler. -->
+<!-- Copyright (c) 2004, 2005 Mark Adler. -->
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#00A000">
<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
<pre><b>
/* 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
*/
</b></pre><!-- -->
We now include the header files for the required definitions. From
@@ -47,6 +50,21 @@ functions <tt>inflateInit()</tt>, <tt>inflate()</tt>, and
#include &lt;assert.h&gt;
#include "zlib.h"
</b></pre><!-- -->
+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.
+<tt>SET_BINARY_MODE()</tt> will be used later on <tt>stdin</tt> and <tt>stdout</tt>, at the beginning of <tt>main()</tt>.
+<pre><b>
+#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
+# include &lt;fcntl.h&gt;
+# include &lt;io.h&gt;
+# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
+#else
+# define SET_BINARY_MODE(file)
+#endif
+</b></pre><!-- -->
<tt>CHUNK</tt> is simply the buffer size for feeding data to and pulling data
from the <em>zlib</em> routines. Larger buffer sizes would be more efficient,
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
int ret, flush;
unsigned have;
z_stream strm;
- char in[CHUNK];
- char out[CHUNK];
+ unsigned char in[CHUNK];
+ unsigned char out[CHUNK];
</b></pre><!-- -->
The first thing we do is to initialize the <em>zlib</em> state for compression using
<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.
int ret;
unsigned have;
z_stream strm;
- char in[CHUNK];
- char out[CHUNK];
+ unsigned char in[CHUNK];
+ unsigned char out[CHUNK];
</b></pre><!-- -->
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. <tt>avail_in</tt>
@@ -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)
}
</b></pre>
<hr>
-<i>Copyright (c) 2004 by Mark Adler<br>Last modified 13 November 2004</i>
+<i>Copyright (c) 2004, 2005 by Mark Adler<br>Last modified 11 December 2005</i>
</body>
</html>
diff --git a/cpukit/zlib/examples/zpipe.c b/cpukit/zlib/examples/zpipe.c
index 26abb56a9c..83535d1693 100644
--- a/cpukit/zlib/examples/zpipe.c
+++ b/cpukit/zlib/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 <stdio.h>
@@ -15,6 +17,14 @@
#include <assert.h>
#include "zlib.h"
+#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
+
#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);