GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/swissknife_zpipe.cc
Date: 2026-10-04 02:40:33
Exec Total Coverage
Lines: 0 91 0.0%
Branches: 0 61 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Slightly adapted zpipe.c for the use within the CernVM File System
5 */
6
7 /* zpipe.c: example of proper use of zlib's inflate() and deflate()
8 Not copyrighted -- provided to the public domain
9 Version 1.4 11 December 2005 Mark Adler */
10
11 /* Version history:
12 1.0 30 Oct 2004 First version
13 1.1 8 Nov 2004 Add void casting for unused return values
14 Use switch statement for inflate() return values
15 1.2 9 Nov 2004 Add assertions to document zlib guarantees
16 1.3 6 Apr 2005 Remove incorrect assertion in inf()
17 1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions
18 Avoid some compiler warnings for input and output buffers
19 */
20
21 #include "swissknife_zpipe.h"
22
23 #include <stdio.h>
24
25 #include <cassert>
26 #include <cstring>
27
28 #include "duplex_zlib.h" // IWYU pragma: keep
29
30 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
31 #include <fcntl.h>
32 #include <io.h>
33 #define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
34 #else
35 #define SET_BINARY_MODE(file)
36 #endif
37
38 #define CHUNK 16384
39
40 /* Compress from file source to file dest until EOF on source.
41 def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
42 allocated for processing, Z_STREAM_ERROR if an invalid compression
43 level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
44 version of the library linked do not match, or Z_ERRNO if there is
45 an error reading or writing the files. */
46 ✗ int def(FILE *source, FILE *dest, int level) {
47 int ret, flush;
48 unsigned have;
49 z_stream strm;
50 unsigned char in[CHUNK];
51 unsigned char out[CHUNK];
52
53 /* allocate deflate state */
54 ✗ strm.zalloc = Z_NULL;
55 ✗ strm.zfree = Z_NULL;
56 ✗ strm.opaque = Z_NULL;
57 ✗ ret = deflateInit(&strm, level);
58 ✗ if (ret != Z_OK)
59 ✗ return ret;
60
61 /* compress until end of file */
62 do {
63 ✗ strm.avail_in = fread(in, 1, CHUNK, source);
64 ✗ if (ferror(source)) {
65 ✗ (void)deflateEnd(&strm);
66 ✗ return Z_ERRNO;
67 }
68 ✗ flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
69 ✗ strm.next_in = in;
70
71 /* run deflate() on input until output buffer not full, finish
72 compression if all of source has been read in */
73 do {
74 ✗ strm.avail_out = CHUNK;
75 ✗ strm.next_out = out;
76 ✗ ret = deflate(&strm, flush); /* no bad return value */
77 ✗ assert(ret != Z_STREAM_ERROR); /* state not clobbered */
78 ✗ have = CHUNK - strm.avail_out;
79 ✗ if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
80 ✗ (void)deflateEnd(&strm);
81 ✗ return Z_ERRNO;
82 }
83 ✗ } while (strm.avail_out == 0);
84 ✗ assert(strm.avail_in == 0); /* all input will be used */
85
86 /* done when last data in file processed */
87 ✗ } while (flush != Z_FINISH);
88 ✗ assert(ret == Z_STREAM_END); /* stream will be complete */
89
90 /* clean up and return */
91 ✗ (void)deflateEnd(&strm);
92 ✗ return Z_OK;
93 }
94
95 /* Decompress from file source to file dest until stream ends or EOF.
96 inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
97 allocated for processing, Z_DATA_ERROR if the deflate data is
98 invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
99 the version of the library linked do not match, or Z_ERRNO if there
100 is an error reading or writing the files. */
101 ✗ int inf(FILE *source, FILE *dest) {
102 int ret;
103 unsigned have;
104 z_stream strm;
105 unsigned char in[CHUNK];
106 unsigned char out[CHUNK];
107
108 /* allocate inflate state */
109 ✗ strm.zalloc = Z_NULL;
110 ✗ strm.zfree = Z_NULL;
111 ✗ strm.opaque = Z_NULL;
112 ✗ strm.avail_in = 0;
113 ✗ strm.next_in = Z_NULL;
114 ✗ ret = inflateInit(&strm);
115 ✗ if (ret != Z_OK)
116 ✗ return ret;
117
118 /* decompress until deflate stream ends or end of file */
119 do {
120 ✗ strm.avail_in = fread(in, 1, CHUNK, source);
121 ✗ if (ferror(source)) {
122 ✗ (void)inflateEnd(&strm);
123 ✗ return Z_ERRNO;
124 }
125 ✗ if (strm.avail_in == 0)
126 ✗ break;
127 ✗ strm.next_in = in;
128
129 /* run inflate() on input until output buffer not full */
130 do {
131 ✗ strm.avail_out = CHUNK;
132 ✗ strm.next_out = out;
133 ✗ ret = inflate(&strm, Z_NO_FLUSH);
134 ✗ assert(ret != Z_STREAM_ERROR); /* state not clobbered */
135 ✗ switch (ret) {
136 ✗ case Z_NEED_DICT:
137 ✗ ret = Z_DATA_ERROR; /* and fall through */
138 ✗ case Z_DATA_ERROR:
139 case Z_MEM_ERROR:
140 ✗ (void)inflateEnd(&strm);
141 ✗ return ret;
142 }
143 ✗ have = CHUNK - strm.avail_out;
144 ✗ if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
145 ✗ (void)inflateEnd(&strm);
146 ✗ return Z_ERRNO;
147 }
148 ✗ } while (strm.avail_out == 0);
149
150 /* done when inflate() says it's done */
151 ✗ } while (ret != Z_STREAM_END);
152
153 /* clean up and return */
154 ✗ (void)inflateEnd(&strm);
155 ✗ return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
156 }
157
158 /* report a zlib or i/o error */
159 ✗ void zerr(int ret) {
160 ✗ fputs("zpipe: ", stderr);
161 ✗ switch (ret) {
162 ✗ case Z_ERRNO:
163 ✗ if (ferror(stdin))
164 ✗ fputs("error reading stdin\n", stderr);
165 ✗ if (ferror(stdout))
166 ✗ fputs("error writing stdout\n", stderr);
167 ✗ break;
168 ✗ case Z_STREAM_ERROR:
169 ✗ fputs("invalid compression level\n", stderr);
170 ✗ break;
171 ✗ case Z_DATA_ERROR:
172 ✗ fputs("invalid or incomplete deflate data\n", stderr);
173 ✗ break;
174 ✗ case Z_MEM_ERROR:
175 ✗ fputs("out of memory\n", stderr);
176 ✗ break;
177 ✗ case Z_VERSION_ERROR:
178 ✗ fputs("zlib version mismatch!\n", stderr);
179 }
180 }
181
182 /* compress or decompress from stdin to stdout */
183 ✗ int swissknife::CommandZpipe::Main(const swissknife::ArgumentList &args) {
184 int ret;
185
186 /* avoid end-of-line conversions */
187 SET_BINARY_MODE(stdin);
188 SET_BINARY_MODE(stdout);
189
190 /* do compression if no arguments */
191 ✗ if (args.find('d') == args.end()) {
192 ✗ ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
193 ✗ if (ret != Z_OK)
194 ✗ zerr(ret);
195 ✗ return ret;
196 } else {
197 /* do decompression if -d specified */
198 ✗ ret = inf(stdin, stdout);
199 ✗ if (ret != Z_OK)
200 ✗ zerr(ret);
201 ✗ return ret;
202 }
203 }
204