Chris@43
|
1 /* gzappend -- command to append to a gzip file
|
Chris@43
|
2
|
Chris@43
|
3 Copyright (C) 2003, 2012 Mark Adler, all rights reserved
|
Chris@43
|
4 version 1.2, 11 Oct 2012
|
Chris@43
|
5
|
Chris@43
|
6 This software is provided 'as-is', without any express or implied
|
Chris@43
|
7 warranty. In no event will the author be held liable for any damages
|
Chris@43
|
8 arising from the use of this software.
|
Chris@43
|
9
|
Chris@43
|
10 Permission is granted to anyone to use this software for any purpose,
|
Chris@43
|
11 including commercial applications, and to alter it and redistribute it
|
Chris@43
|
12 freely, subject to the following restrictions:
|
Chris@43
|
13
|
Chris@43
|
14 1. The origin of this software must not be misrepresented; you must not
|
Chris@43
|
15 claim that you wrote the original software. If you use this software
|
Chris@43
|
16 in a product, an acknowledgment in the product documentation would be
|
Chris@43
|
17 appreciated but is not required.
|
Chris@43
|
18 2. Altered source versions must be plainly marked as such, and must not be
|
Chris@43
|
19 misrepresented as being the original software.
|
Chris@43
|
20 3. This notice may not be removed or altered from any source distribution.
|
Chris@43
|
21
|
Chris@43
|
22 Mark Adler madler@alumni.caltech.edu
|
Chris@43
|
23 */
|
Chris@43
|
24
|
Chris@43
|
25 /*
|
Chris@43
|
26 * Change history:
|
Chris@43
|
27 *
|
Chris@43
|
28 * 1.0 19 Oct 2003 - First version
|
Chris@43
|
29 * 1.1 4 Nov 2003 - Expand and clarify some comments and notes
|
Chris@43
|
30 * - Add version and copyright to help
|
Chris@43
|
31 * - Send help to stdout instead of stderr
|
Chris@43
|
32 * - Add some preemptive typecasts
|
Chris@43
|
33 * - Add L to constants in lseek() calls
|
Chris@43
|
34 * - Remove some debugging information in error messages
|
Chris@43
|
35 * - Use new data_type definition for zlib 1.2.1
|
Chris@43
|
36 * - Simplfy and unify file operations
|
Chris@43
|
37 * - Finish off gzip file in gztack()
|
Chris@43
|
38 * - Use deflatePrime() instead of adding empty blocks
|
Chris@43
|
39 * - Keep gzip file clean on appended file read errors
|
Chris@43
|
40 * - Use in-place rotate instead of auxiliary buffer
|
Chris@43
|
41 * (Why you ask? Because it was fun to write!)
|
Chris@43
|
42 * 1.2 11 Oct 2012 - Fix for proper z_const usage
|
Chris@43
|
43 * - Check for input buffer malloc failure
|
Chris@43
|
44 */
|
Chris@43
|
45
|
Chris@43
|
46 /*
|
Chris@43
|
47 gzappend takes a gzip file and appends to it, compressing files from the
|
Chris@43
|
48 command line or data from stdin. The gzip file is written to directly, to
|
Chris@43
|
49 avoid copying that file, in case it's large. Note that this results in the
|
Chris@43
|
50 unfriendly behavior that if gzappend fails, the gzip file is corrupted.
|
Chris@43
|
51
|
Chris@43
|
52 This program was written to illustrate the use of the new Z_BLOCK option of
|
Chris@43
|
53 zlib 1.2.x's inflate() function. This option returns from inflate() at each
|
Chris@43
|
54 block boundary to facilitate locating and modifying the last block bit at
|
Chris@43
|
55 the start of the final deflate block. Also whether using Z_BLOCK or not,
|
Chris@43
|
56 another required feature of zlib 1.2.x is that inflate() now provides the
|
Chris@43
|
57 number of unusued bits in the last input byte used. gzappend will not work
|
Chris@43
|
58 with versions of zlib earlier than 1.2.1.
|
Chris@43
|
59
|
Chris@43
|
60 gzappend first decompresses the gzip file internally, discarding all but
|
Chris@43
|
61 the last 32K of uncompressed data, and noting the location of the last block
|
Chris@43
|
62 bit and the number of unused bits in the last byte of the compressed data.
|
Chris@43
|
63 The gzip trailer containing the CRC-32 and length of the uncompressed data
|
Chris@43
|
64 is verified. This trailer will be later overwritten.
|
Chris@43
|
65
|
Chris@43
|
66 Then the last block bit is cleared by seeking back in the file and rewriting
|
Chris@43
|
67 the byte that contains it. Seeking forward, the last byte of the compressed
|
Chris@43
|
68 data is saved along with the number of unused bits to initialize deflate.
|
Chris@43
|
69
|
Chris@43
|
70 A deflate process is initialized, using the last 32K of the uncompressed
|
Chris@43
|
71 data from the gzip file to initialize the dictionary. If the total
|
Chris@43
|
72 uncompressed data was less than 32K, then all of it is used to initialize
|
Chris@43
|
73 the dictionary. The deflate output bit buffer is also initialized with the
|
Chris@43
|
74 last bits from the original deflate stream. From here on, the data to
|
Chris@43
|
75 append is simply compressed using deflate, and written to the gzip file.
|
Chris@43
|
76 When that is complete, the new CRC-32 and uncompressed length are written
|
Chris@43
|
77 as the trailer of the gzip file.
|
Chris@43
|
78 */
|
Chris@43
|
79
|
Chris@43
|
80 #include <stdio.h>
|
Chris@43
|
81 #include <stdlib.h>
|
Chris@43
|
82 #include <string.h>
|
Chris@43
|
83 #include <fcntl.h>
|
Chris@43
|
84 #include <unistd.h>
|
Chris@43
|
85 #include "zlib.h"
|
Chris@43
|
86
|
Chris@43
|
87 #define local static
|
Chris@43
|
88 #define LGCHUNK 14
|
Chris@43
|
89 #define CHUNK (1U << LGCHUNK)
|
Chris@43
|
90 #define DSIZE 32768U
|
Chris@43
|
91
|
Chris@43
|
92 /* print an error message and terminate with extreme prejudice */
|
Chris@43
|
93 local void bye(char *msg1, char *msg2)
|
Chris@43
|
94 {
|
Chris@43
|
95 fprintf(stderr, "gzappend error: %s%s\n", msg1, msg2);
|
Chris@43
|
96 exit(1);
|
Chris@43
|
97 }
|
Chris@43
|
98
|
Chris@43
|
99 /* return the greatest common divisor of a and b using Euclid's algorithm,
|
Chris@43
|
100 modified to be fast when one argument much greater than the other, and
|
Chris@43
|
101 coded to avoid unnecessary swapping */
|
Chris@43
|
102 local unsigned gcd(unsigned a, unsigned b)
|
Chris@43
|
103 {
|
Chris@43
|
104 unsigned c;
|
Chris@43
|
105
|
Chris@43
|
106 while (a && b)
|
Chris@43
|
107 if (a > b) {
|
Chris@43
|
108 c = b;
|
Chris@43
|
109 while (a - c >= c)
|
Chris@43
|
110 c <<= 1;
|
Chris@43
|
111 a -= c;
|
Chris@43
|
112 }
|
Chris@43
|
113 else {
|
Chris@43
|
114 c = a;
|
Chris@43
|
115 while (b - c >= c)
|
Chris@43
|
116 c <<= 1;
|
Chris@43
|
117 b -= c;
|
Chris@43
|
118 }
|
Chris@43
|
119 return a + b;
|
Chris@43
|
120 }
|
Chris@43
|
121
|
Chris@43
|
122 /* rotate list[0..len-1] left by rot positions, in place */
|
Chris@43
|
123 local void rotate(unsigned char *list, unsigned len, unsigned rot)
|
Chris@43
|
124 {
|
Chris@43
|
125 unsigned char tmp;
|
Chris@43
|
126 unsigned cycles;
|
Chris@43
|
127 unsigned char *start, *last, *to, *from;
|
Chris@43
|
128
|
Chris@43
|
129 /* normalize rot and handle degenerate cases */
|
Chris@43
|
130 if (len < 2) return;
|
Chris@43
|
131 if (rot >= len) rot %= len;
|
Chris@43
|
132 if (rot == 0) return;
|
Chris@43
|
133
|
Chris@43
|
134 /* pointer to last entry in list */
|
Chris@43
|
135 last = list + (len - 1);
|
Chris@43
|
136
|
Chris@43
|
137 /* do simple left shift by one */
|
Chris@43
|
138 if (rot == 1) {
|
Chris@43
|
139 tmp = *list;
|
Chris@43
|
140 memcpy(list, list + 1, len - 1);
|
Chris@43
|
141 *last = tmp;
|
Chris@43
|
142 return;
|
Chris@43
|
143 }
|
Chris@43
|
144
|
Chris@43
|
145 /* do simple right shift by one */
|
Chris@43
|
146 if (rot == len - 1) {
|
Chris@43
|
147 tmp = *last;
|
Chris@43
|
148 memmove(list + 1, list, len - 1);
|
Chris@43
|
149 *list = tmp;
|
Chris@43
|
150 return;
|
Chris@43
|
151 }
|
Chris@43
|
152
|
Chris@43
|
153 /* otherwise do rotate as a set of cycles in place */
|
Chris@43
|
154 cycles = gcd(len, rot); /* number of cycles */
|
Chris@43
|
155 do {
|
Chris@43
|
156 start = from = list + cycles; /* start index is arbitrary */
|
Chris@43
|
157 tmp = *from; /* save entry to be overwritten */
|
Chris@43
|
158 for (;;) {
|
Chris@43
|
159 to = from; /* next step in cycle */
|
Chris@43
|
160 from += rot; /* go right rot positions */
|
Chris@43
|
161 if (from > last) from -= len; /* (pointer better not wrap) */
|
Chris@43
|
162 if (from == start) break; /* all but one shifted */
|
Chris@43
|
163 *to = *from; /* shift left */
|
Chris@43
|
164 }
|
Chris@43
|
165 *to = tmp; /* complete the circle */
|
Chris@43
|
166 } while (--cycles);
|
Chris@43
|
167 }
|
Chris@43
|
168
|
Chris@43
|
169 /* structure for gzip file read operations */
|
Chris@43
|
170 typedef struct {
|
Chris@43
|
171 int fd; /* file descriptor */
|
Chris@43
|
172 int size; /* 1 << size is bytes in buf */
|
Chris@43
|
173 unsigned left; /* bytes available at next */
|
Chris@43
|
174 unsigned char *buf; /* buffer */
|
Chris@43
|
175 z_const unsigned char *next; /* next byte in buffer */
|
Chris@43
|
176 char *name; /* file name for error messages */
|
Chris@43
|
177 } file;
|
Chris@43
|
178
|
Chris@43
|
179 /* reload buffer */
|
Chris@43
|
180 local int readin(file *in)
|
Chris@43
|
181 {
|
Chris@43
|
182 int len;
|
Chris@43
|
183
|
Chris@43
|
184 len = read(in->fd, in->buf, 1 << in->size);
|
Chris@43
|
185 if (len == -1) bye("error reading ", in->name);
|
Chris@43
|
186 in->left = (unsigned)len;
|
Chris@43
|
187 in->next = in->buf;
|
Chris@43
|
188 return len;
|
Chris@43
|
189 }
|
Chris@43
|
190
|
Chris@43
|
191 /* read from file in, exit if end-of-file */
|
Chris@43
|
192 local int readmore(file *in)
|
Chris@43
|
193 {
|
Chris@43
|
194 if (readin(in) == 0) bye("unexpected end of ", in->name);
|
Chris@43
|
195 return 0;
|
Chris@43
|
196 }
|
Chris@43
|
197
|
Chris@43
|
198 #define read1(in) (in->left == 0 ? readmore(in) : 0, \
|
Chris@43
|
199 in->left--, *(in->next)++)
|
Chris@43
|
200
|
Chris@43
|
201 /* skip over n bytes of in */
|
Chris@43
|
202 local void skip(file *in, unsigned n)
|
Chris@43
|
203 {
|
Chris@43
|
204 unsigned bypass;
|
Chris@43
|
205
|
Chris@43
|
206 if (n > in->left) {
|
Chris@43
|
207 n -= in->left;
|
Chris@43
|
208 bypass = n & ~((1U << in->size) - 1);
|
Chris@43
|
209 if (bypass) {
|
Chris@43
|
210 if (lseek(in->fd, (off_t)bypass, SEEK_CUR) == -1)
|
Chris@43
|
211 bye("seeking ", in->name);
|
Chris@43
|
212 n -= bypass;
|
Chris@43
|
213 }
|
Chris@43
|
214 readmore(in);
|
Chris@43
|
215 if (n > in->left)
|
Chris@43
|
216 bye("unexpected end of ", in->name);
|
Chris@43
|
217 }
|
Chris@43
|
218 in->left -= n;
|
Chris@43
|
219 in->next += n;
|
Chris@43
|
220 }
|
Chris@43
|
221
|
Chris@43
|
222 /* read a four-byte unsigned integer, little-endian, from in */
|
Chris@43
|
223 unsigned long read4(file *in)
|
Chris@43
|
224 {
|
Chris@43
|
225 unsigned long val;
|
Chris@43
|
226
|
Chris@43
|
227 val = read1(in);
|
Chris@43
|
228 val += (unsigned)read1(in) << 8;
|
Chris@43
|
229 val += (unsigned long)read1(in) << 16;
|
Chris@43
|
230 val += (unsigned long)read1(in) << 24;
|
Chris@43
|
231 return val;
|
Chris@43
|
232 }
|
Chris@43
|
233
|
Chris@43
|
234 /* skip over gzip header */
|
Chris@43
|
235 local void gzheader(file *in)
|
Chris@43
|
236 {
|
Chris@43
|
237 int flags;
|
Chris@43
|
238 unsigned n;
|
Chris@43
|
239
|
Chris@43
|
240 if (read1(in) != 31 || read1(in) != 139) bye(in->name, " not a gzip file");
|
Chris@43
|
241 if (read1(in) != 8) bye("unknown compression method in", in->name);
|
Chris@43
|
242 flags = read1(in);
|
Chris@43
|
243 if (flags & 0xe0) bye("unknown header flags set in", in->name);
|
Chris@43
|
244 skip(in, 6);
|
Chris@43
|
245 if (flags & 4) {
|
Chris@43
|
246 n = read1(in);
|
Chris@43
|
247 n += (unsigned)(read1(in)) << 8;
|
Chris@43
|
248 skip(in, n);
|
Chris@43
|
249 }
|
Chris@43
|
250 if (flags & 8) while (read1(in) != 0) ;
|
Chris@43
|
251 if (flags & 16) while (read1(in) != 0) ;
|
Chris@43
|
252 if (flags & 2) skip(in, 2);
|
Chris@43
|
253 }
|
Chris@43
|
254
|
Chris@43
|
255 /* decompress gzip file "name", return strm with a deflate stream ready to
|
Chris@43
|
256 continue compression of the data in the gzip file, and return a file
|
Chris@43
|
257 descriptor pointing to where to write the compressed data -- the deflate
|
Chris@43
|
258 stream is initialized to compress using level "level" */
|
Chris@43
|
259 local int gzscan(char *name, z_stream *strm, int level)
|
Chris@43
|
260 {
|
Chris@43
|
261 int ret, lastbit, left, full;
|
Chris@43
|
262 unsigned have;
|
Chris@43
|
263 unsigned long crc, tot;
|
Chris@43
|
264 unsigned char *window;
|
Chris@43
|
265 off_t lastoff, end;
|
Chris@43
|
266 file gz;
|
Chris@43
|
267
|
Chris@43
|
268 /* open gzip file */
|
Chris@43
|
269 gz.name = name;
|
Chris@43
|
270 gz.fd = open(name, O_RDWR, 0);
|
Chris@43
|
271 if (gz.fd == -1) bye("cannot open ", name);
|
Chris@43
|
272 gz.buf = malloc(CHUNK);
|
Chris@43
|
273 if (gz.buf == NULL) bye("out of memory", "");
|
Chris@43
|
274 gz.size = LGCHUNK;
|
Chris@43
|
275 gz.left = 0;
|
Chris@43
|
276
|
Chris@43
|
277 /* skip gzip header */
|
Chris@43
|
278 gzheader(&gz);
|
Chris@43
|
279
|
Chris@43
|
280 /* prepare to decompress */
|
Chris@43
|
281 window = malloc(DSIZE);
|
Chris@43
|
282 if (window == NULL) bye("out of memory", "");
|
Chris@43
|
283 strm->zalloc = Z_NULL;
|
Chris@43
|
284 strm->zfree = Z_NULL;
|
Chris@43
|
285 strm->opaque = Z_NULL;
|
Chris@43
|
286 ret = inflateInit2(strm, -15);
|
Chris@43
|
287 if (ret != Z_OK) bye("out of memory", " or library mismatch");
|
Chris@43
|
288
|
Chris@43
|
289 /* decompress the deflate stream, saving append information */
|
Chris@43
|
290 lastbit = 0;
|
Chris@43
|
291 lastoff = lseek(gz.fd, 0L, SEEK_CUR) - gz.left;
|
Chris@43
|
292 left = 0;
|
Chris@43
|
293 strm->avail_in = gz.left;
|
Chris@43
|
294 strm->next_in = gz.next;
|
Chris@43
|
295 crc = crc32(0L, Z_NULL, 0);
|
Chris@43
|
296 have = full = 0;
|
Chris@43
|
297 do {
|
Chris@43
|
298 /* if needed, get more input */
|
Chris@43
|
299 if (strm->avail_in == 0) {
|
Chris@43
|
300 readmore(&gz);
|
Chris@43
|
301 strm->avail_in = gz.left;
|
Chris@43
|
302 strm->next_in = gz.next;
|
Chris@43
|
303 }
|
Chris@43
|
304
|
Chris@43
|
305 /* set up output to next available section of sliding window */
|
Chris@43
|
306 strm->avail_out = DSIZE - have;
|
Chris@43
|
307 strm->next_out = window + have;
|
Chris@43
|
308
|
Chris@43
|
309 /* inflate and check for errors */
|
Chris@43
|
310 ret = inflate(strm, Z_BLOCK);
|
Chris@43
|
311 if (ret == Z_STREAM_ERROR) bye("internal stream error!", "");
|
Chris@43
|
312 if (ret == Z_MEM_ERROR) bye("out of memory", "");
|
Chris@43
|
313 if (ret == Z_DATA_ERROR)
|
Chris@43
|
314 bye("invalid compressed data--format violated in", name);
|
Chris@43
|
315
|
Chris@43
|
316 /* update crc and sliding window pointer */
|
Chris@43
|
317 crc = crc32(crc, window + have, DSIZE - have - strm->avail_out);
|
Chris@43
|
318 if (strm->avail_out)
|
Chris@43
|
319 have = DSIZE - strm->avail_out;
|
Chris@43
|
320 else {
|
Chris@43
|
321 have = 0;
|
Chris@43
|
322 full = 1;
|
Chris@43
|
323 }
|
Chris@43
|
324
|
Chris@43
|
325 /* process end of block */
|
Chris@43
|
326 if (strm->data_type & 128) {
|
Chris@43
|
327 if (strm->data_type & 64)
|
Chris@43
|
328 left = strm->data_type & 0x1f;
|
Chris@43
|
329 else {
|
Chris@43
|
330 lastbit = strm->data_type & 0x1f;
|
Chris@43
|
331 lastoff = lseek(gz.fd, 0L, SEEK_CUR) - strm->avail_in;
|
Chris@43
|
332 }
|
Chris@43
|
333 }
|
Chris@43
|
334 } while (ret != Z_STREAM_END);
|
Chris@43
|
335 inflateEnd(strm);
|
Chris@43
|
336 gz.left = strm->avail_in;
|
Chris@43
|
337 gz.next = strm->next_in;
|
Chris@43
|
338
|
Chris@43
|
339 /* save the location of the end of the compressed data */
|
Chris@43
|
340 end = lseek(gz.fd, 0L, SEEK_CUR) - gz.left;
|
Chris@43
|
341
|
Chris@43
|
342 /* check gzip trailer and save total for deflate */
|
Chris@43
|
343 if (crc != read4(&gz))
|
Chris@43
|
344 bye("invalid compressed data--crc mismatch in ", name);
|
Chris@43
|
345 tot = strm->total_out;
|
Chris@43
|
346 if ((tot & 0xffffffffUL) != read4(&gz))
|
Chris@43
|
347 bye("invalid compressed data--length mismatch in", name);
|
Chris@43
|
348
|
Chris@43
|
349 /* if not at end of file, warn */
|
Chris@43
|
350 if (gz.left || readin(&gz))
|
Chris@43
|
351 fprintf(stderr,
|
Chris@43
|
352 "gzappend warning: junk at end of gzip file overwritten\n");
|
Chris@43
|
353
|
Chris@43
|
354 /* clear last block bit */
|
Chris@43
|
355 lseek(gz.fd, lastoff - (lastbit != 0), SEEK_SET);
|
Chris@43
|
356 if (read(gz.fd, gz.buf, 1) != 1) bye("reading after seek on ", name);
|
Chris@43
|
357 *gz.buf = (unsigned char)(*gz.buf ^ (1 << ((8 - lastbit) & 7)));
|
Chris@43
|
358 lseek(gz.fd, -1L, SEEK_CUR);
|
Chris@43
|
359 if (write(gz.fd, gz.buf, 1) != 1) bye("writing after seek to ", name);
|
Chris@43
|
360
|
Chris@43
|
361 /* if window wrapped, build dictionary from window by rotating */
|
Chris@43
|
362 if (full) {
|
Chris@43
|
363 rotate(window, DSIZE, have);
|
Chris@43
|
364 have = DSIZE;
|
Chris@43
|
365 }
|
Chris@43
|
366
|
Chris@43
|
367 /* set up deflate stream with window, crc, total_in, and leftover bits */
|
Chris@43
|
368 ret = deflateInit2(strm, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
|
Chris@43
|
369 if (ret != Z_OK) bye("out of memory", "");
|
Chris@43
|
370 deflateSetDictionary(strm, window, have);
|
Chris@43
|
371 strm->adler = crc;
|
Chris@43
|
372 strm->total_in = tot;
|
Chris@43
|
373 if (left) {
|
Chris@43
|
374 lseek(gz.fd, --end, SEEK_SET);
|
Chris@43
|
375 if (read(gz.fd, gz.buf, 1) != 1) bye("reading after seek on ", name);
|
Chris@43
|
376 deflatePrime(strm, 8 - left, *gz.buf);
|
Chris@43
|
377 }
|
Chris@43
|
378 lseek(gz.fd, end, SEEK_SET);
|
Chris@43
|
379
|
Chris@43
|
380 /* clean up and return */
|
Chris@43
|
381 free(window);
|
Chris@43
|
382 free(gz.buf);
|
Chris@43
|
383 return gz.fd;
|
Chris@43
|
384 }
|
Chris@43
|
385
|
Chris@43
|
386 /* append file "name" to gzip file gd using deflate stream strm -- if last
|
Chris@43
|
387 is true, then finish off the deflate stream at the end */
|
Chris@43
|
388 local void gztack(char *name, int gd, z_stream *strm, int last)
|
Chris@43
|
389 {
|
Chris@43
|
390 int fd, len, ret;
|
Chris@43
|
391 unsigned left;
|
Chris@43
|
392 unsigned char *in, *out;
|
Chris@43
|
393
|
Chris@43
|
394 /* open file to compress and append */
|
Chris@43
|
395 fd = 0;
|
Chris@43
|
396 if (name != NULL) {
|
Chris@43
|
397 fd = open(name, O_RDONLY, 0);
|
Chris@43
|
398 if (fd == -1)
|
Chris@43
|
399 fprintf(stderr, "gzappend warning: %s not found, skipping ...\n",
|
Chris@43
|
400 name);
|
Chris@43
|
401 }
|
Chris@43
|
402
|
Chris@43
|
403 /* allocate buffers */
|
Chris@43
|
404 in = malloc(CHUNK);
|
Chris@43
|
405 out = malloc(CHUNK);
|
Chris@43
|
406 if (in == NULL || out == NULL) bye("out of memory", "");
|
Chris@43
|
407
|
Chris@43
|
408 /* compress input file and append to gzip file */
|
Chris@43
|
409 do {
|
Chris@43
|
410 /* get more input */
|
Chris@43
|
411 len = read(fd, in, CHUNK);
|
Chris@43
|
412 if (len == -1) {
|
Chris@43
|
413 fprintf(stderr,
|
Chris@43
|
414 "gzappend warning: error reading %s, skipping rest ...\n",
|
Chris@43
|
415 name);
|
Chris@43
|
416 len = 0;
|
Chris@43
|
417 }
|
Chris@43
|
418 strm->avail_in = (unsigned)len;
|
Chris@43
|
419 strm->next_in = in;
|
Chris@43
|
420 if (len) strm->adler = crc32(strm->adler, in, (unsigned)len);
|
Chris@43
|
421
|
Chris@43
|
422 /* compress and write all available output */
|
Chris@43
|
423 do {
|
Chris@43
|
424 strm->avail_out = CHUNK;
|
Chris@43
|
425 strm->next_out = out;
|
Chris@43
|
426 ret = deflate(strm, last && len == 0 ? Z_FINISH : Z_NO_FLUSH);
|
Chris@43
|
427 left = CHUNK - strm->avail_out;
|
Chris@43
|
428 while (left) {
|
Chris@43
|
429 len = write(gd, out + CHUNK - strm->avail_out - left, left);
|
Chris@43
|
430 if (len == -1) bye("writing gzip file", "");
|
Chris@43
|
431 left -= (unsigned)len;
|
Chris@43
|
432 }
|
Chris@43
|
433 } while (strm->avail_out == 0 && ret != Z_STREAM_END);
|
Chris@43
|
434 } while (len != 0);
|
Chris@43
|
435
|
Chris@43
|
436 /* write trailer after last entry */
|
Chris@43
|
437 if (last) {
|
Chris@43
|
438 deflateEnd(strm);
|
Chris@43
|
439 out[0] = (unsigned char)(strm->adler);
|
Chris@43
|
440 out[1] = (unsigned char)(strm->adler >> 8);
|
Chris@43
|
441 out[2] = (unsigned char)(strm->adler >> 16);
|
Chris@43
|
442 out[3] = (unsigned char)(strm->adler >> 24);
|
Chris@43
|
443 out[4] = (unsigned char)(strm->total_in);
|
Chris@43
|
444 out[5] = (unsigned char)(strm->total_in >> 8);
|
Chris@43
|
445 out[6] = (unsigned char)(strm->total_in >> 16);
|
Chris@43
|
446 out[7] = (unsigned char)(strm->total_in >> 24);
|
Chris@43
|
447 len = 8;
|
Chris@43
|
448 do {
|
Chris@43
|
449 ret = write(gd, out + 8 - len, len);
|
Chris@43
|
450 if (ret == -1) bye("writing gzip file", "");
|
Chris@43
|
451 len -= ret;
|
Chris@43
|
452 } while (len);
|
Chris@43
|
453 close(gd);
|
Chris@43
|
454 }
|
Chris@43
|
455
|
Chris@43
|
456 /* clean up and return */
|
Chris@43
|
457 free(out);
|
Chris@43
|
458 free(in);
|
Chris@43
|
459 if (fd > 0) close(fd);
|
Chris@43
|
460 }
|
Chris@43
|
461
|
Chris@43
|
462 /* process the compression level option if present, scan the gzip file, and
|
Chris@43
|
463 append the specified files, or append the data from stdin if no other file
|
Chris@43
|
464 names are provided on the command line -- the gzip file must be writable
|
Chris@43
|
465 and seekable */
|
Chris@43
|
466 int main(int argc, char **argv)
|
Chris@43
|
467 {
|
Chris@43
|
468 int gd, level;
|
Chris@43
|
469 z_stream strm;
|
Chris@43
|
470
|
Chris@43
|
471 /* ignore command name */
|
Chris@43
|
472 argc--; argv++;
|
Chris@43
|
473
|
Chris@43
|
474 /* provide usage if no arguments */
|
Chris@43
|
475 if (*argv == NULL) {
|
Chris@43
|
476 printf(
|
Chris@43
|
477 "gzappend 1.2 (11 Oct 2012) Copyright (C) 2003, 2012 Mark Adler\n"
|
Chris@43
|
478 );
|
Chris@43
|
479 printf(
|
Chris@43
|
480 "usage: gzappend [-level] file.gz [ addthis [ andthis ... ]]\n");
|
Chris@43
|
481 return 0;
|
Chris@43
|
482 }
|
Chris@43
|
483
|
Chris@43
|
484 /* set compression level */
|
Chris@43
|
485 level = Z_DEFAULT_COMPRESSION;
|
Chris@43
|
486 if (argv[0][0] == '-') {
|
Chris@43
|
487 if (argv[0][1] < '0' || argv[0][1] > '9' || argv[0][2] != 0)
|
Chris@43
|
488 bye("invalid compression level", "");
|
Chris@43
|
489 level = argv[0][1] - '0';
|
Chris@43
|
490 if (*++argv == NULL) bye("no gzip file name after options", "");
|
Chris@43
|
491 }
|
Chris@43
|
492
|
Chris@43
|
493 /* prepare to append to gzip file */
|
Chris@43
|
494 gd = gzscan(*argv++, &strm, level);
|
Chris@43
|
495
|
Chris@43
|
496 /* append files on command line, or from stdin if none */
|
Chris@43
|
497 if (*argv == NULL)
|
Chris@43
|
498 gztack(NULL, gd, &strm, 1);
|
Chris@43
|
499 else
|
Chris@43
|
500 do {
|
Chris@43
|
501 gztack(*argv, gd, &strm, argv[1] == NULL);
|
Chris@43
|
502 } while (*++argv != NULL);
|
Chris@43
|
503 return 0;
|
Chris@43
|
504 }
|