1
1
/* zpipe.c: example of proper use of zlib's inflate() and deflate()
2
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
5
/* Version history:
6
6
1.0 30 Oct 2004 First version
7
7
1.1 8 Nov 2004 Add void casting for unused return values
8
8
Use switch statement for inflate() return values
9
9
1.2 9 Nov 2004 Add assertions to document zlib guarantees
10
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
15
#include <stdio.h>
14
16
#include <string.h>
15
17
#include <assert.h>
16
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
28
#define CHUNK 16384
19
29
20
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
38
int ret , flush ;
29
39
unsigned have ;
30
40
z_stream strm ;
31
- char in [CHUNK ];
32
- char out [CHUNK ];
41
+ unsigned char in [CHUNK ];
42
+ unsigned char out [CHUNK ];
33
43
34
44
/* allocate deflate state */
35
45
strm .zalloc = Z_NULL ;
@@ -84,8 +94,8 @@ int inf(FILE *source, FILE *dest)
84
94
int ret ;
85
95
unsigned have ;
86
96
z_stream strm ;
87
- char in [CHUNK ];
88
- char out [CHUNK ];
97
+ unsigned char in [CHUNK ];
98
+ unsigned char out [CHUNK ];
89
99
90
100
/* allocate inflate state */
91
101
strm .zalloc = Z_NULL ;
@@ -167,6 +177,10 @@ int main(int argc, char **argv)
167
177
{
168
178
int ret ;
169
179
180
+ /* avoid end-of-line conversions */
181
+ SET_BINARY_MODE (stdin );
182
+ SET_BINARY_MODE (stdout );
183
+
170
184
/* do compression if no arguments */
171
185
if (argc == 1 ) {
172
186
ret = def (stdin , stdout , Z_DEFAULT_COMPRESSION );
0 commit comments