]> 4ch.mooo.com Git - 16.git/blob - src/lib/dl/ext/zlib/example.c
cleaned up the repo from debugging watcom2 ^^
[16.git] / src / lib / dl / ext / zlib / example.c
1 /* example.c -- usage example of the zlib compression library
2  * Copyright (C) 1995-2006 Jean-loup Gailly.
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5
6 /* @(#) $Id$ */
7
8 #include "zlib.h"
9 #include <stdio.h>
10
11 #ifdef STDC
12 #  include <string.h>
13 #  include <stdlib.h>
14 #endif
15
16 #if TARGET_MSDOS == 16 && (defined(__SMALL__) || defined(__MEDIUM__))
17 # include <dos.h>
18 # include <i86.h>
19 #endif
20
21 #if defined(VMS) || defined(RISCOS)
22 #  define TESTFILE "foo-gz"
23 #else
24 #  define TESTFILE "foo.gz"
25 #endif
26
27 #define CHECK_ERR(err, msg) { \
28     if (err != Z_OK) { \
29         fprintf(stderr, "%s error: %d\n", msg, err); \
30         exit(1); \
31     } \
32 }
33
34 const char hello[] = "hello, hello!";
35 /* "hello world" would be more standard, but the repeated "hello"
36  * stresses the compression code better, sorry...
37  */
38
39 const char dictionary[] = "hello";
40 uLong dictId; /* Adler32 value of the dictionary */
41
42 void test_compress      OF((Byte *compr, uLong comprLen,
43                             Byte *uncompr, uLong uncomprLen));
44 void test_gzio          OF((const char *fname,
45                             Byte *uncompr, uLong uncomprLen));
46 void test_deflate       OF((Byte *compr, uLong comprLen));
47 void test_inflate       OF((Byte *compr, uLong comprLen,
48                             Byte *uncompr, uLong uncomprLen));
49 void test_large_deflate OF((Byte *compr, uLong comprLen,
50                             Byte *uncompr, uLong uncomprLen));
51 void test_large_inflate OF((Byte *compr, uLong comprLen,
52                             Byte *uncompr, uLong uncomprLen));
53 void test_flush         OF((Byte *compr, uLong *comprLen));
54 void test_sync          OF((Byte *compr, uLong comprLen,
55                             Byte *uncompr, uLong uncomprLen));
56 void test_dict_deflate  OF((Byte *compr, uLong comprLen));
57 void test_dict_inflate  OF((Byte *compr, uLong comprLen,
58                             Byte *uncompr, uLong uncomprLen));
59 int  main               OF((int argc, char *argv[]));
60
61 /* ===========================================================================
62  * Test compress() and uncompress()
63  */
64 void test_compress(compr, comprLen, uncompr, uncomprLen)
65     Byte *compr, *uncompr;
66     uLong comprLen, uncomprLen;
67 {
68     int err;
69     uLong len = (uLong)strlen(hello)+1;
70
71     err = compress(compr, &comprLen, (const Bytef*)hello, len);
72     CHECK_ERR(err, "compress");
73
74     strcpy((char*)uncompr, "garbage");
75
76     err = uncompress(uncompr, &uncomprLen, compr, comprLen);
77     CHECK_ERR(err, "uncompress");
78
79     if (strcmp((char*)uncompr, hello)) {
80         fprintf(stderr, "bad uncompress\n");
81         exit(1);
82     } else {
83         printf("uncompress(): %s\n", (char *)uncompr);
84     }
85 }
86
87 /* ===========================================================================
88  * Test read/write of .gz files
89  */
90 void test_gzio(fname, uncompr, uncomprLen)
91     const char *fname; /* compressed file name */
92     Byte *uncompr;
93     uLong uncomprLen;
94 {
95 #ifdef NO_GZCOMPRESS
96     fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
97 #else
98     int err;
99     int len = (int)strlen(hello)+1;
100     gzFile file;
101     z_off_t pos;
102
103     file = gzopen(fname, "wb");
104     if (file == NULL) {
105         fprintf(stderr, "gzopen error\n");
106         exit(1);
107     }
108     gzputc(file, 'h');
109     if (gzputs(file, "ello") != 4) {
110         fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
111         exit(1);
112     }
113     if (gzprintf(file, ", %s!", "hello") != 8) {
114         fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
115         exit(1);
116     }
117     gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
118     gzclose(file);
119
120     file = gzopen(fname, "rb");
121     if (file == NULL) {
122         fprintf(stderr, "gzopen error\n");
123         exit(1);
124     }
125     strcpy((char*)uncompr, "garbage");
126
127     if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
128         fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
129         exit(1);
130     }
131     if (strcmp((char*)uncompr, hello)) {
132         fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
133         exit(1);
134     } else {
135         printf("gzread(): %s\n", (char*)uncompr);
136     }
137
138     pos = gzseek(file, -8L, SEEK_CUR);
139     if (pos != 6 || gztell(file) != pos) {
140         fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
141                 (long)pos, (long)gztell(file));
142         exit(1);
143     }
144
145     if (gzgetc(file) != ' ') {
146         fprintf(stderr, "gzgetc error\n");
147         exit(1);
148     }
149
150     if (gzungetc(' ', file) != ' ') {
151         fprintf(stderr, "gzungetc error\n");
152         exit(1);
153     }
154
155     gzgets(file, (char*)uncompr, (int)uncomprLen);
156     if (strlen((char*)uncompr) != 7) { /* " hello!" */
157         fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
158         exit(1);
159     }
160     if (strcmp((char*)uncompr, hello + 6)) {
161         fprintf(stderr, "bad gzgets after gzseek\n");
162         exit(1);
163     } else {
164         printf("gzgets() after gzseek: %s\n", (char*)uncompr);
165     }
166
167     gzclose(file);
168 #endif
169 }
170
171 /* ===========================================================================
172  * Test deflate() with small buffers
173  */
174 void test_deflate(compr, comprLen)
175     Byte *compr;
176     uLong comprLen;
177 {
178     z_stream c_stream; /* compression stream */
179     int err;
180     uLong len = (uLong)strlen(hello)+1;
181
182     c_stream.zalloc = (alloc_func)0;
183     c_stream.zfree = (free_func)0;
184     c_stream.opaque = (voidpf)0;
185
186     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
187     CHECK_ERR(err, "deflateInit");
188
189     c_stream.next_in  = (Bytef*)hello;
190     c_stream.next_out = compr;
191
192     while (c_stream.total_in != len && c_stream.total_out < comprLen) {
193         c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
194         err = deflate(&c_stream, Z_NO_FLUSH);
195         CHECK_ERR(err, "deflate");
196     }
197     /* Finish the stream, still forcing small buffers: */
198     for (;;) {
199         c_stream.avail_out = 1;
200         err = deflate(&c_stream, Z_FINISH);
201         if (err == Z_STREAM_END) break;
202         CHECK_ERR(err, "deflate");
203     }
204
205     err = deflateEnd(&c_stream);
206     CHECK_ERR(err, "deflateEnd");
207 }
208
209 /* ===========================================================================
210  * Test inflate() with small buffers
211  */
212 void test_inflate(compr, comprLen, uncompr, uncomprLen)
213     Byte *compr, *uncompr;
214     uLong comprLen, uncomprLen;
215 {
216     int err;
217     z_stream d_stream; /* decompression stream */
218
219     strcpy((char*)uncompr, "garbage");
220
221     d_stream.zalloc = (alloc_func)0;
222     d_stream.zfree = (free_func)0;
223     d_stream.opaque = (voidpf)0;
224
225     d_stream.next_in  = compr;
226     d_stream.avail_in = 0;
227     d_stream.next_out = uncompr;
228
229     err = inflateInit(&d_stream);
230     CHECK_ERR(err, "inflateInit");
231
232     while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
233         d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
234         err = inflate(&d_stream, Z_NO_FLUSH);
235         if (err == Z_STREAM_END) break;
236         CHECK_ERR(err, "inflate");
237     }
238
239     err = inflateEnd(&d_stream);
240     CHECK_ERR(err, "inflateEnd");
241
242     if (strcmp((char*)uncompr, hello)) {
243         fprintf(stderr, "bad inflate\n");
244         exit(1);
245     } else {
246         printf("inflate(): %s\n", (char *)uncompr);
247     }
248 }
249
250 /* ===========================================================================
251  * Test deflate() with large buffers and dynamic change of compression level
252  */
253 void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
254     Byte *compr, *uncompr;
255     uLong comprLen, uncomprLen;
256 {
257     z_stream c_stream; /* compression stream */
258     int err;
259
260     c_stream.zalloc = (alloc_func)0;
261     c_stream.zfree = (free_func)0;
262     c_stream.opaque = (voidpf)0;
263
264     err = deflateInit(&c_stream, Z_BEST_SPEED);
265     CHECK_ERR(err, "deflateInit");
266
267     c_stream.next_out = compr;
268     c_stream.avail_out = (uInt)comprLen;
269
270     /* At this point, uncompr is still mostly zeroes, so it should compress
271      * very well:
272      */
273     c_stream.next_in = uncompr;
274     c_stream.avail_in = (uInt)uncomprLen;
275     err = deflate(&c_stream, Z_NO_FLUSH);
276     CHECK_ERR(err, "deflate");
277     if (c_stream.avail_in != 0) {
278         fprintf(stderr, "deflate not greedy\n");
279         exit(1);
280     }
281
282     /* Feed in already compressed data and switch to no compression: */
283     deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
284     c_stream.next_in = compr;
285     c_stream.avail_in = (uInt)comprLen/2;
286     err = deflate(&c_stream, Z_NO_FLUSH);
287     CHECK_ERR(err, "deflate");
288
289     /* Switch back to compressing mode: */
290     deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
291     c_stream.next_in = uncompr;
292     c_stream.avail_in = (uInt)uncomprLen;
293     err = deflate(&c_stream, Z_NO_FLUSH);
294     CHECK_ERR(err, "deflate");
295
296     err = deflate(&c_stream, Z_FINISH);
297     if (err != Z_STREAM_END) {
298         fprintf(stderr, "deflate should report Z_STREAM_END\n");
299         exit(1);
300     }
301     err = deflateEnd(&c_stream);
302     CHECK_ERR(err, "deflateEnd");
303 }
304
305 /* ===========================================================================
306  * Test inflate() with large buffers
307  */
308 void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
309     Byte *compr, *uncompr;
310     uLong comprLen, uncomprLen;
311 {
312     int err;
313     z_stream d_stream; /* decompression stream */
314
315     strcpy((char*)uncompr, "garbage");
316
317     d_stream.zalloc = (alloc_func)0;
318     d_stream.zfree = (free_func)0;
319     d_stream.opaque = (voidpf)0;
320
321     d_stream.next_in  = compr;
322     d_stream.avail_in = (uInt)comprLen;
323
324     err = inflateInit(&d_stream);
325     CHECK_ERR(err, "inflateInit");
326
327     for (;;) {
328         d_stream.next_out = uncompr;            /* discard the output */
329         d_stream.avail_out = (uInt)uncomprLen;
330         err = inflate(&d_stream, Z_NO_FLUSH);
331         if (err == Z_STREAM_END) break;
332         CHECK_ERR(err, "large inflate");
333     }
334
335     err = inflateEnd(&d_stream);
336     CHECK_ERR(err, "inflateEnd");
337
338     if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
339         fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
340         exit(1);
341     } else {
342         printf("large_inflate(): OK\n");
343     }
344 }
345
346 /* ===========================================================================
347  * Test deflate() with full flush
348  */
349 void test_flush(compr, comprLen)
350     Byte *compr;
351     uLong *comprLen;
352 {
353     z_stream c_stream; /* compression stream */
354     int err;
355     uInt len = (uInt)strlen(hello)+1;
356
357     c_stream.zalloc = (alloc_func)0;
358     c_stream.zfree = (free_func)0;
359     c_stream.opaque = (voidpf)0;
360
361     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
362     CHECK_ERR(err, "deflateInit");
363
364     c_stream.next_in  = (Bytef*)hello;
365     c_stream.next_out = compr;
366     c_stream.avail_in = 3;
367     c_stream.avail_out = (uInt)*comprLen;
368     err = deflate(&c_stream, Z_FULL_FLUSH);
369     CHECK_ERR(err, "deflate");
370
371     compr[3]++; /* force an error in first compressed block */
372     c_stream.avail_in = len - 3;
373
374     err = deflate(&c_stream, Z_FINISH);
375     if (err != Z_STREAM_END) {
376         CHECK_ERR(err, "deflate");
377     }
378     err = deflateEnd(&c_stream);
379     CHECK_ERR(err, "deflateEnd");
380
381     *comprLen = c_stream.total_out;
382 }
383
384 /* ===========================================================================
385  * Test inflateSync()
386  */
387 void test_sync(compr, comprLen, uncompr, uncomprLen)
388     Byte *compr, *uncompr;
389     uLong comprLen, uncomprLen;
390 {
391     int err;
392     z_stream d_stream; /* decompression stream */
393
394     strcpy((char*)uncompr, "garbage");
395
396     d_stream.zalloc = (alloc_func)0;
397     d_stream.zfree = (free_func)0;
398     d_stream.opaque = (voidpf)0;
399
400     d_stream.next_in  = compr;
401     d_stream.avail_in = 2; /* just read the zlib header */
402
403     err = inflateInit(&d_stream);
404     CHECK_ERR(err, "inflateInit");
405
406     d_stream.next_out = uncompr;
407     d_stream.avail_out = (uInt)uncomprLen;
408
409     inflate(&d_stream, Z_NO_FLUSH);
410     CHECK_ERR(err, "inflate");
411
412     d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
413     err = inflateSync(&d_stream);           /* but skip the damaged part */
414     CHECK_ERR(err, "inflateSync");
415
416     err = inflate(&d_stream, Z_FINISH);
417     if (err != Z_DATA_ERROR) {
418         fprintf(stderr, "inflate should report DATA_ERROR\n");
419         /* Because of incorrect adler32 */
420         exit(1);
421     }
422     err = inflateEnd(&d_stream);
423     CHECK_ERR(err, "inflateEnd");
424
425     printf("after inflateSync(): hel%s\n", (char *)uncompr);
426 }
427
428 /* ===========================================================================
429  * Test deflate() with preset dictionary
430  */
431 void test_dict_deflate(compr, comprLen)
432     Byte *compr;
433     uLong comprLen;
434 {
435     z_stream c_stream; /* compression stream */
436     int err;
437
438     c_stream.zalloc = (alloc_func)0;
439     c_stream.zfree = (free_func)0;
440     c_stream.opaque = (voidpf)0;
441
442     err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
443     CHECK_ERR(err, "deflateInit");
444
445     err = deflateSetDictionary(&c_stream,
446                                (const Bytef*)dictionary, sizeof(dictionary));
447     CHECK_ERR(err, "deflateSetDictionary");
448
449     dictId = c_stream.adler;
450     c_stream.next_out = compr;
451     c_stream.avail_out = (uInt)comprLen;
452
453     c_stream.next_in = (Bytef*)hello;
454     c_stream.avail_in = (uInt)strlen(hello)+1;
455
456     err = deflate(&c_stream, Z_FINISH);
457     if (err != Z_STREAM_END) {
458         fprintf(stderr, "deflate should report Z_STREAM_END\n");
459         exit(1);
460     }
461     err = deflateEnd(&c_stream);
462     CHECK_ERR(err, "deflateEnd");
463 }
464
465 /* ===========================================================================
466  * Test inflate() with a preset dictionary
467  */
468 void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
469     Byte *compr, *uncompr;
470     uLong comprLen, uncomprLen;
471 {
472     int err;
473     z_stream d_stream; /* decompression stream */
474
475     strcpy((char*)uncompr, "garbage");
476
477     d_stream.zalloc = (alloc_func)0;
478     d_stream.zfree = (free_func)0;
479     d_stream.opaque = (voidpf)0;
480
481     d_stream.next_in  = compr;
482     d_stream.avail_in = (uInt)comprLen;
483
484     err = inflateInit(&d_stream);
485     CHECK_ERR(err, "inflateInit");
486
487     d_stream.next_out = uncompr;
488     d_stream.avail_out = (uInt)uncomprLen;
489
490     for (;;) {
491         err = inflate(&d_stream, Z_NO_FLUSH);
492         if (err == Z_STREAM_END) break;
493         if (err == Z_NEED_DICT) {
494             if (d_stream.adler != dictId) {
495                 fprintf(stderr, "unexpected dictionary");
496                 exit(1);
497             }
498             err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
499                                        sizeof(dictionary));
500         }
501         CHECK_ERR(err, "inflate with dict");
502     }
503
504     err = inflateEnd(&d_stream);
505     CHECK_ERR(err, "inflateEnd");
506
507     if (strcmp((char*)uncompr, hello)) {
508         fprintf(stderr, "bad inflate with dict\n");
509         exit(1);
510     } else {
511         printf("inflate with dictionary: %s\n", (char *)uncompr);
512     }
513 }
514
515 /* ===========================================================================
516  * Usage:  example [output.gz  [input.gz]]
517  */
518
519 int main(argc, argv)
520     int argc;
521     char *argv[];
522 {
523     Byte *compr, *uncompr;
524     uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
525     uLong uncomprLen = comprLen;
526     static const char* myVersion = ZLIB_VERSION;
527
528     if (zlibVersion()[0] != myVersion[0]) {
529         fprintf(stderr, "incompatible zlib version\n");
530         exit(1);
531
532     } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
533         fprintf(stderr, "warning: different zlib version\n");
534     }
535
536     printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
537             ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
538
539     compr    = (Byte*)calloc((uInt)comprLen, 1);
540     uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
541     /* compr and uncompr are cleared to avoid reading uninitialized
542      * data and to ensure that uncompr compresses well.
543      */
544     if (compr == Z_NULL || uncompr == Z_NULL) {
545         printf("out of memory\n");
546         exit(1);
547     }
548
549     test_compress(compr, comprLen, uncompr, uncomprLen);
550
551     test_gzio((argc > 1 ? argv[1] : TESTFILE),
552               uncompr, uncomprLen);
553
554     test_deflate(compr, comprLen);
555     test_inflate(compr, comprLen, uncompr, uncomprLen);
556
557     test_large_deflate(compr, comprLen, uncompr, uncomprLen);
558     test_large_inflate(compr, comprLen, uncompr, uncomprLen);
559
560     test_flush(compr, &comprLen);
561     test_sync(compr, comprLen, uncompr, uncomprLen);
562     comprLen = uncomprLen;
563
564     test_dict_deflate(compr, comprLen);
565     test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
566
567     free(compr);
568     free(uncompr);
569
570     return 0;
571 }