comparison zlib/examples/zlib_how.html @ 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
2 "http://www.w3.org/TR/REC-html40/loose.dtd"> 2 "http://www.w3.org/TR/REC-html40/loose.dtd">
3 <html> 3 <html>
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>
11 We often get questions about how the <tt>deflate()</tt> and <tt>inflate()</tt> functions should be used. 11 We often get questions about how the <tt>deflate()</tt> and <tt>inflate()</tt> functions should be used.
12 Users wonder when they should provide more input, when they should use more output, 12 Users wonder when they should provide more input, when they should use more output,
19 <p> 19 <p>
20 Without further adieu, here is the program <a href="zpipe.c"><tt>zpipe.c</tt></a>: 20 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
34 <tt>stdio.h</tt> we use <tt>fopen()</tt>, <tt>fread()</tt>, <tt>fwrite()</tt>, 37 <tt>stdio.h</tt> we use <tt>fopen()</tt>, <tt>fread()</tt>, <tt>fwrite()</tt>,
35 <tt>feof()</tt>, <tt>ferror()</tt>, and <tt>fclose()</tt> for file i/o, and 38 <tt>feof()</tt>, <tt>ferror()</tt>, and <tt>fclose()</tt> for file i/o, and
45 #include &lt;stdio.h&gt; 48 #include &lt;stdio.h&gt;
46 #include &lt;string.h&gt; 49 #include &lt;string.h&gt;
47 #include &lt;assert.h&gt; 50 #include &lt;assert.h&gt;
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 &lt;fcntl.h&gt;
62 # include &lt;io.h&gt;
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
53 on the order of 128K or 256K bytes should be used. 71 on the order of 128K or 256K bytes should be used.
54 <pre><b> 72 <pre><b>
78 <tt>deflate()</tt>. 96 <tt>deflate()</tt>.
79 <pre><b> 97 <pre><b>
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>.
88 The <tt>zalloc</tt>, <tt>zfree</tt>, and <tt>opaque</tt> fields in the <tt>strm</tt> 106 The <tt>zalloc</tt>, <tt>zfree</tt>, and <tt>opaque</tt> fields in the <tt>strm</tt>
89 structure must be initialized before calling <tt>deflateInit()</tt>. Here they are 107 structure must be initialized before calling <tt>deflateInit()</tt>. Here they are
311 can tell from the <em>zlib</em> stream itself when the stream is complete. 329 can tell from the <em>zlib</em> stream itself when the stream is complete.
312 <pre><b> 330 <pre><b>
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>
321 and <tt>next_in</tt> must be initialized before calling <tt>inflateInit()</tt>. This 339 and <tt>next_in</tt> must be initialized before calling <tt>inflateInit()</tt>. This
322 is because the application has the option to provide the start of the zlib stream in 340 is because the application has the option to provide the start of the zlib stream in
492 /* compress or decompress from stdin to stdout */ 510 /* compress or decompress from stdin to stdout */
493 int main(int argc, char **argv) 511 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);
500 if (ret != Z_OK) 522 if (ret != Z_OK)
501 zerr(ret); 523 zerr(ret);
516 return 1; 538 return 1;
517 } 539 }
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>