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