ยปCore Development>Code coverage>Modules/zlib/example.c

Python code coverage for Modules/zlib/example.c

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