xref: /minix3/common/dist/zlib/examples/gzjoin.c (revision 44bedb31d842b4b0444105519bcf929a69fe2dc1)
1*44bedb31SLionel Sambuc /*	$NetBSD: gzjoin.c,v 1.1.1.1 2006/01/14 20:11:09 christos Exp $	*/
2*44bedb31SLionel Sambuc 
3*44bedb31SLionel Sambuc /* gzjoin -- command to join gzip files into one gzip file
4*44bedb31SLionel Sambuc 
5*44bedb31SLionel Sambuc   Copyright (C) 2004 Mark Adler, all rights reserved
6*44bedb31SLionel Sambuc   version 1.0, 11 Dec 2004
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  11 Dec 2004     - First version
31*44bedb31SLionel Sambuc  * 1.1  12 Jun 2005     - Changed ssize_t to long for portability
32*44bedb31SLionel Sambuc  */
33*44bedb31SLionel Sambuc 
34*44bedb31SLionel Sambuc /*
35*44bedb31SLionel Sambuc    gzjoin takes one or more gzip files on the command line and writes out a
36*44bedb31SLionel Sambuc    single gzip file that will uncompress to the concatenation of the
37*44bedb31SLionel Sambuc    uncompressed data from the individual gzip files.  gzjoin does this without
38*44bedb31SLionel Sambuc    having to recompress any of the data and without having to calculate a new
39*44bedb31SLionel Sambuc    crc32 for the concatenated uncompressed data.  gzjoin does however have to
40*44bedb31SLionel Sambuc    decompress all of the input data in order to find the bits in the compressed
41*44bedb31SLionel Sambuc    data that need to be modified to concatenate the streams.
42*44bedb31SLionel Sambuc 
43*44bedb31SLionel Sambuc    gzjoin does not do an integrity check on the input gzip files other than
44*44bedb31SLionel Sambuc    checking the gzip header and decompressing the compressed data.  They are
45*44bedb31SLionel Sambuc    otherwise assumed to be complete and correct.
46*44bedb31SLionel Sambuc 
47*44bedb31SLionel Sambuc    Each joint between gzip files removes at least 18 bytes of previous trailer
48*44bedb31SLionel Sambuc    and subsequent header, and inserts an average of about three bytes to the
49*44bedb31SLionel Sambuc    compressed data in order to connect the streams.  The output gzip file
50*44bedb31SLionel Sambuc    has a minimal ten-byte gzip header with no file name or modification time.
51*44bedb31SLionel Sambuc 
52*44bedb31SLionel Sambuc    This program was written to illustrate the use of the Z_BLOCK option of
53*44bedb31SLionel Sambuc    inflate() and the crc32_combine() function.  gzjoin will not compile with
54*44bedb31SLionel Sambuc    versions of zlib earlier than 1.2.3.
55*44bedb31SLionel Sambuc  */
56*44bedb31SLionel Sambuc 
57*44bedb31SLionel Sambuc #include <stdio.h>      /* fputs(), fprintf(), fwrite(), putc() */
58*44bedb31SLionel Sambuc #include <stdlib.h>     /* exit(), malloc(), free() */
59*44bedb31SLionel Sambuc #include <fcntl.h>      /* open() */
60*44bedb31SLionel Sambuc #include <unistd.h>     /* close(), read(), lseek() */
61*44bedb31SLionel Sambuc #include "zlib.h"
62*44bedb31SLionel Sambuc     /* crc32(), crc32_combine(), inflateInit2(), inflate(), inflateEnd() */
63*44bedb31SLionel Sambuc 
64*44bedb31SLionel Sambuc #define local static
65*44bedb31SLionel Sambuc 
66*44bedb31SLionel Sambuc /* exit with an error (return a value to allow use in an expression) */
bail(char * why1,char * why2)67*44bedb31SLionel Sambuc local int bail(char *why1, char *why2)
68*44bedb31SLionel Sambuc {
69*44bedb31SLionel Sambuc     fprintf(stderr, "gzjoin error: %s%s, output incomplete\n", why1, why2);
70*44bedb31SLionel Sambuc     exit(1);
71*44bedb31SLionel Sambuc     return 0;
72*44bedb31SLionel Sambuc }
73*44bedb31SLionel Sambuc 
74*44bedb31SLionel Sambuc /* -- simple buffered file input with access to the buffer -- */
75*44bedb31SLionel Sambuc 
76*44bedb31SLionel Sambuc #define CHUNK 32768         /* must be a power of two and fit in unsigned */
77*44bedb31SLionel Sambuc 
78*44bedb31SLionel Sambuc /* bin buffered input file type */
79*44bedb31SLionel Sambuc typedef struct {
80*44bedb31SLionel Sambuc     char *name;             /* name of file for error messages */
81*44bedb31SLionel Sambuc     int fd;                 /* file descriptor */
82*44bedb31SLionel Sambuc     unsigned left;          /* bytes remaining at next */
83*44bedb31SLionel Sambuc     unsigned char *next;    /* next byte to read */
84*44bedb31SLionel Sambuc     unsigned char *buf;     /* allocated buffer of length CHUNK */
85*44bedb31SLionel Sambuc } bin;
86*44bedb31SLionel Sambuc 
87*44bedb31SLionel Sambuc /* close a buffered file and free allocated memory */
bclose(bin * in)88*44bedb31SLionel Sambuc local void bclose(bin *in)
89*44bedb31SLionel Sambuc {
90*44bedb31SLionel Sambuc     if (in != NULL) {
91*44bedb31SLionel Sambuc         if (in->fd != -1)
92*44bedb31SLionel Sambuc             close(in->fd);
93*44bedb31SLionel Sambuc         if (in->buf != NULL)
94*44bedb31SLionel Sambuc             free(in->buf);
95*44bedb31SLionel Sambuc         free(in);
96*44bedb31SLionel Sambuc     }
97*44bedb31SLionel Sambuc }
98*44bedb31SLionel Sambuc 
99*44bedb31SLionel Sambuc /* open a buffered file for input, return a pointer to type bin, or NULL on
100*44bedb31SLionel Sambuc    failure */
bopen(char * name)101*44bedb31SLionel Sambuc local bin *bopen(char *name)
102*44bedb31SLionel Sambuc {
103*44bedb31SLionel Sambuc     bin *in;
104*44bedb31SLionel Sambuc 
105*44bedb31SLionel Sambuc     in = malloc(sizeof(bin));
106*44bedb31SLionel Sambuc     if (in == NULL)
107*44bedb31SLionel Sambuc         return NULL;
108*44bedb31SLionel Sambuc     in->buf = malloc(CHUNK);
109*44bedb31SLionel Sambuc     in->fd = open(name, O_RDONLY, 0);
110*44bedb31SLionel Sambuc     if (in->buf == NULL || in->fd == -1) {
111*44bedb31SLionel Sambuc         bclose(in);
112*44bedb31SLionel Sambuc         return NULL;
113*44bedb31SLionel Sambuc     }
114*44bedb31SLionel Sambuc     in->left = 0;
115*44bedb31SLionel Sambuc     in->next = in->buf;
116*44bedb31SLionel Sambuc     in->name = name;
117*44bedb31SLionel Sambuc     return in;
118*44bedb31SLionel Sambuc }
119*44bedb31SLionel Sambuc 
120*44bedb31SLionel Sambuc /* load buffer from file, return -1 on read error, 0 or 1 on success, with
121*44bedb31SLionel Sambuc    1 indicating that end-of-file was reached */
bload(bin * in)122*44bedb31SLionel Sambuc local int bload(bin *in)
123*44bedb31SLionel Sambuc {
124*44bedb31SLionel Sambuc     long len;
125*44bedb31SLionel Sambuc 
126*44bedb31SLionel Sambuc     if (in == NULL)
127*44bedb31SLionel Sambuc         return -1;
128*44bedb31SLionel Sambuc     if (in->left != 0)
129*44bedb31SLionel Sambuc         return 0;
130*44bedb31SLionel Sambuc     in->next = in->buf;
131*44bedb31SLionel Sambuc     do {
132*44bedb31SLionel Sambuc         len = (long)read(in->fd, in->buf + in->left, CHUNK - in->left);
133*44bedb31SLionel Sambuc         if (len < 0)
134*44bedb31SLionel Sambuc             return -1;
135*44bedb31SLionel Sambuc         in->left += (unsigned)len;
136*44bedb31SLionel Sambuc     } while (len != 0 && in->left < CHUNK);
137*44bedb31SLionel Sambuc     return len == 0 ? 1 : 0;
138*44bedb31SLionel Sambuc }
139*44bedb31SLionel Sambuc 
140*44bedb31SLionel Sambuc /* get a byte from the file, bail if end of file */
141*44bedb31SLionel Sambuc #define bget(in) (in->left ? 0 : bload(in), \
142*44bedb31SLionel Sambuc                   in->left ? (in->left--, *(in->next)++) : \
143*44bedb31SLionel Sambuc                     bail("unexpected end of file on ", in->name))
144*44bedb31SLionel Sambuc 
145*44bedb31SLionel Sambuc /* get a four-byte little-endian unsigned integer from file */
bget4(bin * in)146*44bedb31SLionel Sambuc local unsigned long bget4(bin *in)
147*44bedb31SLionel Sambuc {
148*44bedb31SLionel Sambuc     unsigned long val;
149*44bedb31SLionel Sambuc 
150*44bedb31SLionel Sambuc     val = bget(in);
151*44bedb31SLionel Sambuc     val += (unsigned long)(bget(in)) << 8;
152*44bedb31SLionel Sambuc     val += (unsigned long)(bget(in)) << 16;
153*44bedb31SLionel Sambuc     val += (unsigned long)(bget(in)) << 24;
154*44bedb31SLionel Sambuc     return val;
155*44bedb31SLionel Sambuc }
156*44bedb31SLionel Sambuc 
157*44bedb31SLionel Sambuc /* skip bytes in file */
bskip(bin * in,unsigned skip)158*44bedb31SLionel Sambuc local void bskip(bin *in, unsigned skip)
159*44bedb31SLionel Sambuc {
160*44bedb31SLionel Sambuc     /* check pointer */
161*44bedb31SLionel Sambuc     if (in == NULL)
162*44bedb31SLionel Sambuc         return;
163*44bedb31SLionel Sambuc 
164*44bedb31SLionel Sambuc     /* easy case -- skip bytes in buffer */
165*44bedb31SLionel Sambuc     if (skip <= in->left) {
166*44bedb31SLionel Sambuc         in->left -= skip;
167*44bedb31SLionel Sambuc         in->next += skip;
168*44bedb31SLionel Sambuc         return;
169*44bedb31SLionel Sambuc     }
170*44bedb31SLionel Sambuc 
171*44bedb31SLionel Sambuc     /* skip what's in buffer, discard buffer contents */
172*44bedb31SLionel Sambuc     skip -= in->left;
173*44bedb31SLionel Sambuc     in->left = 0;
174*44bedb31SLionel Sambuc 
175*44bedb31SLionel Sambuc     /* seek past multiples of CHUNK bytes */
176*44bedb31SLionel Sambuc     if (skip > CHUNK) {
177*44bedb31SLionel Sambuc         unsigned left;
178*44bedb31SLionel Sambuc 
179*44bedb31SLionel Sambuc         left = skip & (CHUNK - 1);
180*44bedb31SLionel Sambuc         if (left == 0) {
181*44bedb31SLionel Sambuc             /* exact number of chunks: seek all the way minus one byte to check
182*44bedb31SLionel Sambuc                for end-of-file with a read */
183*44bedb31SLionel Sambuc             lseek(in->fd, skip - 1, SEEK_CUR);
184*44bedb31SLionel Sambuc             if (read(in->fd, in->buf, 1) != 1)
185*44bedb31SLionel Sambuc                 bail("unexpected end of file on ", in->name);
186*44bedb31SLionel Sambuc             return;
187*44bedb31SLionel Sambuc         }
188*44bedb31SLionel Sambuc 
189*44bedb31SLionel Sambuc         /* skip the integral chunks, update skip with remainder */
190*44bedb31SLionel Sambuc         lseek(in->fd, skip - left, SEEK_CUR);
191*44bedb31SLionel Sambuc         skip = left;
192*44bedb31SLionel Sambuc     }
193*44bedb31SLionel Sambuc 
194*44bedb31SLionel Sambuc     /* read more input and skip remainder */
195*44bedb31SLionel Sambuc     bload(in);
196*44bedb31SLionel Sambuc     if (skip > in->left)
197*44bedb31SLionel Sambuc         bail("unexpected end of file on ", in->name);
198*44bedb31SLionel Sambuc     in->left -= skip;
199*44bedb31SLionel Sambuc     in->next += skip;
200*44bedb31SLionel Sambuc }
201*44bedb31SLionel Sambuc 
202*44bedb31SLionel Sambuc /* -- end of buffered input functions -- */
203*44bedb31SLionel Sambuc 
204*44bedb31SLionel Sambuc /* skip the gzip header from file in */
gzhead(bin * in)205*44bedb31SLionel Sambuc local void gzhead(bin *in)
206*44bedb31SLionel Sambuc {
207*44bedb31SLionel Sambuc     int flags;
208*44bedb31SLionel Sambuc 
209*44bedb31SLionel Sambuc     /* verify gzip magic header and compression method */
210*44bedb31SLionel Sambuc     if (bget(in) != 0x1f || bget(in) != 0x8b || bget(in) != 8)
211*44bedb31SLionel Sambuc         bail(in->name, " is not a valid gzip file");
212*44bedb31SLionel Sambuc 
213*44bedb31SLionel Sambuc     /* get and verify flags */
214*44bedb31SLionel Sambuc     flags = bget(in);
215*44bedb31SLionel Sambuc     if ((flags & 0xe0) != 0)
216*44bedb31SLionel Sambuc         bail("unknown reserved bits set in ", in->name);
217*44bedb31SLionel Sambuc 
218*44bedb31SLionel Sambuc     /* skip modification time, extra flags, and os */
219*44bedb31SLionel Sambuc     bskip(in, 6);
220*44bedb31SLionel Sambuc 
221*44bedb31SLionel Sambuc     /* skip extra field if present */
222*44bedb31SLionel Sambuc     if (flags & 4) {
223*44bedb31SLionel Sambuc         unsigned len;
224*44bedb31SLionel Sambuc 
225*44bedb31SLionel Sambuc         len = bget(in);
226*44bedb31SLionel Sambuc         len += (unsigned)(bget(in)) << 8;
227*44bedb31SLionel Sambuc         bskip(in, len);
228*44bedb31SLionel Sambuc     }
229*44bedb31SLionel Sambuc 
230*44bedb31SLionel Sambuc     /* skip file name if present */
231*44bedb31SLionel Sambuc     if (flags & 8)
232*44bedb31SLionel Sambuc         while (bget(in) != 0)
233*44bedb31SLionel Sambuc             ;
234*44bedb31SLionel Sambuc 
235*44bedb31SLionel Sambuc     /* skip comment if present */
236*44bedb31SLionel Sambuc     if (flags & 16)
237*44bedb31SLionel Sambuc         while (bget(in) != 0)
238*44bedb31SLionel Sambuc             ;
239*44bedb31SLionel Sambuc 
240*44bedb31SLionel Sambuc     /* skip header crc if present */
241*44bedb31SLionel Sambuc     if (flags & 2)
242*44bedb31SLionel Sambuc         bskip(in, 2);
243*44bedb31SLionel Sambuc }
244*44bedb31SLionel Sambuc 
245*44bedb31SLionel Sambuc /* write a four-byte little-endian unsigned integer to out */
put4(unsigned long val,FILE * out)246*44bedb31SLionel Sambuc local void put4(unsigned long val, FILE *out)
247*44bedb31SLionel Sambuc {
248*44bedb31SLionel Sambuc     putc(val & 0xff, out);
249*44bedb31SLionel Sambuc     putc((val >> 8) & 0xff, out);
250*44bedb31SLionel Sambuc     putc((val >> 16) & 0xff, out);
251*44bedb31SLionel Sambuc     putc((val >> 24) & 0xff, out);
252*44bedb31SLionel Sambuc }
253*44bedb31SLionel Sambuc 
254*44bedb31SLionel Sambuc /* Load up zlib stream from buffered input, bail if end of file */
zpull(z_streamp strm,bin * in)255*44bedb31SLionel Sambuc local void zpull(z_streamp strm, bin *in)
256*44bedb31SLionel Sambuc {
257*44bedb31SLionel Sambuc     if (in->left == 0)
258*44bedb31SLionel Sambuc         bload(in);
259*44bedb31SLionel Sambuc     if (in->left == 0)
260*44bedb31SLionel Sambuc         bail("unexpected end of file on ", in->name);
261*44bedb31SLionel Sambuc     strm->avail_in = in->left;
262*44bedb31SLionel Sambuc     strm->next_in = in->next;
263*44bedb31SLionel Sambuc }
264*44bedb31SLionel Sambuc 
265*44bedb31SLionel Sambuc /* Write header for gzip file to out and initialize trailer. */
gzinit(unsigned long * crc,unsigned long * tot,FILE * out)266*44bedb31SLionel Sambuc local void gzinit(unsigned long *crc, unsigned long *tot, FILE *out)
267*44bedb31SLionel Sambuc {
268*44bedb31SLionel Sambuc     fwrite("\x1f\x8b\x08\0\0\0\0\0\0\xff", 1, 10, out);
269*44bedb31SLionel Sambuc     *crc = crc32(0L, Z_NULL, 0);
270*44bedb31SLionel Sambuc     *tot = 0;
271*44bedb31SLionel Sambuc }
272*44bedb31SLionel Sambuc 
273*44bedb31SLionel Sambuc /* Copy the compressed data from name, zeroing the last block bit of the last
274*44bedb31SLionel Sambuc    block if clr is true, and adding empty blocks as needed to get to a byte
275*44bedb31SLionel Sambuc    boundary.  If clr is false, then the last block becomes the last block of
276*44bedb31SLionel Sambuc    the output, and the gzip trailer is written.  crc and tot maintains the
277*44bedb31SLionel Sambuc    crc and length (modulo 2^32) of the output for the trailer.  The resulting
278*44bedb31SLionel Sambuc    gzip file is written to out.  gzinit() must be called before the first call
279*44bedb31SLionel Sambuc    of gzcopy() to write the gzip header and to initialize crc and tot. */
gzcopy(char * name,int clr,unsigned long * crc,unsigned long * tot,FILE * out)280*44bedb31SLionel Sambuc local void gzcopy(char *name, int clr, unsigned long *crc, unsigned long *tot,
281*44bedb31SLionel Sambuc                   FILE *out)
282*44bedb31SLionel Sambuc {
283*44bedb31SLionel Sambuc     int ret;                /* return value from zlib functions */
284*44bedb31SLionel Sambuc     int pos;                /* where the "last block" bit is in byte */
285*44bedb31SLionel Sambuc     int last;               /* true if processing the last block */
286*44bedb31SLionel Sambuc     bin *in;                /* buffered input file */
287*44bedb31SLionel Sambuc     unsigned char *start;   /* start of compressed data in buffer */
288*44bedb31SLionel Sambuc     unsigned char *junk;    /* buffer for uncompressed data -- discarded */
289*44bedb31SLionel Sambuc     z_off_t len;            /* length of uncompressed data (support > 4 GB) */
290*44bedb31SLionel Sambuc     z_stream strm;          /* zlib inflate stream */
291*44bedb31SLionel Sambuc 
292*44bedb31SLionel Sambuc     /* open gzip file and skip header */
293*44bedb31SLionel Sambuc     in = bopen(name);
294*44bedb31SLionel Sambuc     if (in == NULL)
295*44bedb31SLionel Sambuc         bail("could not open ", name);
296*44bedb31SLionel Sambuc     gzhead(in);
297*44bedb31SLionel Sambuc 
298*44bedb31SLionel Sambuc     /* allocate buffer for uncompressed data and initialize raw inflate
299*44bedb31SLionel Sambuc        stream */
300*44bedb31SLionel Sambuc     junk = malloc(CHUNK);
301*44bedb31SLionel Sambuc     strm.zalloc = Z_NULL;
302*44bedb31SLionel Sambuc     strm.zfree = Z_NULL;
303*44bedb31SLionel Sambuc     strm.opaque = Z_NULL;
304*44bedb31SLionel Sambuc     strm.avail_in = 0;
305*44bedb31SLionel Sambuc     strm.next_in = Z_NULL;
306*44bedb31SLionel Sambuc     ret = inflateInit2(&strm, -15);
307*44bedb31SLionel Sambuc     if (junk == NULL || ret != Z_OK)
308*44bedb31SLionel Sambuc         bail("out of memory", "");
309*44bedb31SLionel Sambuc 
310*44bedb31SLionel Sambuc     /* inflate and copy compressed data, clear last-block bit if requested */
311*44bedb31SLionel Sambuc     len = 0;
312*44bedb31SLionel Sambuc     zpull(&strm, in);
313*44bedb31SLionel Sambuc     start = strm.next_in;
314*44bedb31SLionel Sambuc     last = start[0] & 1;
315*44bedb31SLionel Sambuc     if (last && clr)
316*44bedb31SLionel Sambuc         start[0] &= ~1;
317*44bedb31SLionel Sambuc     strm.avail_out = 0;
318*44bedb31SLionel Sambuc     for (;;) {
319*44bedb31SLionel Sambuc         /* if input used and output done, write used input and get more */
320*44bedb31SLionel Sambuc         if (strm.avail_in == 0 && strm.avail_out != 0) {
321*44bedb31SLionel Sambuc             fwrite(start, 1, strm.next_in - start, out);
322*44bedb31SLionel Sambuc             start = in->buf;
323*44bedb31SLionel Sambuc             in->left = 0;
324*44bedb31SLionel Sambuc             zpull(&strm, in);
325*44bedb31SLionel Sambuc         }
326*44bedb31SLionel Sambuc 
327*44bedb31SLionel Sambuc         /* decompress -- return early when end-of-block reached */
328*44bedb31SLionel Sambuc         strm.avail_out = CHUNK;
329*44bedb31SLionel Sambuc         strm.next_out = junk;
330*44bedb31SLionel Sambuc         ret = inflate(&strm, Z_BLOCK);
331*44bedb31SLionel Sambuc         switch (ret) {
332*44bedb31SLionel Sambuc         case Z_MEM_ERROR:
333*44bedb31SLionel Sambuc             bail("out of memory", "");
334*44bedb31SLionel Sambuc         case Z_DATA_ERROR:
335*44bedb31SLionel Sambuc             bail("invalid compressed data in ", in->name);
336*44bedb31SLionel Sambuc         }
337*44bedb31SLionel Sambuc 
338*44bedb31SLionel Sambuc         /* update length of uncompressed data */
339*44bedb31SLionel Sambuc         len += CHUNK - strm.avail_out;
340*44bedb31SLionel Sambuc 
341*44bedb31SLionel Sambuc         /* check for block boundary (only get this when block copied out) */
342*44bedb31SLionel Sambuc         if (strm.data_type & 128) {
343*44bedb31SLionel Sambuc             /* if that was the last block, then done */
344*44bedb31SLionel Sambuc             if (last)
345*44bedb31SLionel Sambuc                 break;
346*44bedb31SLionel Sambuc 
347*44bedb31SLionel Sambuc             /* number of unused bits in last byte */
348*44bedb31SLionel Sambuc             pos = strm.data_type & 7;
349*44bedb31SLionel Sambuc 
350*44bedb31SLionel Sambuc             /* find the next last-block bit */
351*44bedb31SLionel Sambuc             if (pos != 0) {
352*44bedb31SLionel Sambuc                 /* next last-block bit is in last used byte */
353*44bedb31SLionel Sambuc                 pos = 0x100 >> pos;
354*44bedb31SLionel Sambuc                 last = strm.next_in[-1] & pos;
355*44bedb31SLionel Sambuc                 if (last && clr)
356*44bedb31SLionel Sambuc                     strm.next_in[-1] &= ~pos;
357*44bedb31SLionel Sambuc             }
358*44bedb31SLionel Sambuc             else {
359*44bedb31SLionel Sambuc                 /* next last-block bit is in next unused byte */
360*44bedb31SLionel Sambuc                 if (strm.avail_in == 0) {
361*44bedb31SLionel Sambuc                     /* don't have that byte yet -- get it */
362*44bedb31SLionel Sambuc                     fwrite(start, 1, strm.next_in - start, out);
363*44bedb31SLionel Sambuc                     start = in->buf;
364*44bedb31SLionel Sambuc                     in->left = 0;
365*44bedb31SLionel Sambuc                     zpull(&strm, in);
366*44bedb31SLionel Sambuc                 }
367*44bedb31SLionel Sambuc                 last = strm.next_in[0] & 1;
368*44bedb31SLionel Sambuc                 if (last && clr)
369*44bedb31SLionel Sambuc                     strm.next_in[0] &= ~1;
370*44bedb31SLionel Sambuc             }
371*44bedb31SLionel Sambuc         }
372*44bedb31SLionel Sambuc     }
373*44bedb31SLionel Sambuc 
374*44bedb31SLionel Sambuc     /* update buffer with unused input */
375*44bedb31SLionel Sambuc     in->left = strm.avail_in;
376*44bedb31SLionel Sambuc     in->next = strm.next_in;
377*44bedb31SLionel Sambuc 
378*44bedb31SLionel Sambuc     /* copy used input, write empty blocks to get to byte boundary */
379*44bedb31SLionel Sambuc     pos = strm.data_type & 7;
380*44bedb31SLionel Sambuc     fwrite(start, 1, in->next - start - 1, out);
381*44bedb31SLionel Sambuc     last = in->next[-1];
382*44bedb31SLionel Sambuc     if (pos == 0 || !clr)
383*44bedb31SLionel Sambuc         /* already at byte boundary, or last file: write last byte */
384*44bedb31SLionel Sambuc         putc(last, out);
385*44bedb31SLionel Sambuc     else {
386*44bedb31SLionel Sambuc         /* append empty blocks to last byte */
387*44bedb31SLionel Sambuc         last &= ((0x100 >> pos) - 1);       /* assure unused bits are zero */
388*44bedb31SLionel Sambuc         if (pos & 1) {
389*44bedb31SLionel Sambuc             /* odd -- append an empty stored block */
390*44bedb31SLionel Sambuc             putc(last, out);
391*44bedb31SLionel Sambuc             if (pos == 1)
392*44bedb31SLionel Sambuc                 putc(0, out);               /* two more bits in block header */
393*44bedb31SLionel Sambuc             fwrite("\0\0\xff\xff", 1, 4, out);
394*44bedb31SLionel Sambuc         }
395*44bedb31SLionel Sambuc         else {
396*44bedb31SLionel Sambuc             /* even -- append 1, 2, or 3 empty fixed blocks */
397*44bedb31SLionel Sambuc             switch (pos) {
398*44bedb31SLionel Sambuc             case 6:
399*44bedb31SLionel Sambuc                 putc(last | 8, out);
400*44bedb31SLionel Sambuc                 last = 0;
401*44bedb31SLionel Sambuc             case 4:
402*44bedb31SLionel Sambuc                 putc(last | 0x20, out);
403*44bedb31SLionel Sambuc                 last = 0;
404*44bedb31SLionel Sambuc             case 2:
405*44bedb31SLionel Sambuc                 putc(last | 0x80, out);
406*44bedb31SLionel Sambuc                 putc(0, out);
407*44bedb31SLionel Sambuc             }
408*44bedb31SLionel Sambuc         }
409*44bedb31SLionel Sambuc     }
410*44bedb31SLionel Sambuc 
411*44bedb31SLionel Sambuc     /* update crc and tot */
412*44bedb31SLionel Sambuc     *crc = crc32_combine(*crc, bget4(in), len);
413*44bedb31SLionel Sambuc     *tot += (unsigned long)len;
414*44bedb31SLionel Sambuc 
415*44bedb31SLionel Sambuc     /* clean up */
416*44bedb31SLionel Sambuc     inflateEnd(&strm);
417*44bedb31SLionel Sambuc     free(junk);
418*44bedb31SLionel Sambuc     bclose(in);
419*44bedb31SLionel Sambuc 
420*44bedb31SLionel Sambuc     /* write trailer if this is the last gzip file */
421*44bedb31SLionel Sambuc     if (!clr) {
422*44bedb31SLionel Sambuc         put4(*crc, out);
423*44bedb31SLionel Sambuc         put4(*tot, out);
424*44bedb31SLionel Sambuc     }
425*44bedb31SLionel Sambuc }
426*44bedb31SLionel Sambuc 
427*44bedb31SLionel Sambuc /* join the gzip files on the command line, write result to stdout */
main(int argc,char ** argv)428*44bedb31SLionel Sambuc int main(int argc, char **argv)
429*44bedb31SLionel Sambuc {
430*44bedb31SLionel Sambuc     unsigned long crc, tot;     /* running crc and total uncompressed length */
431*44bedb31SLionel Sambuc 
432*44bedb31SLionel Sambuc     /* skip command name */
433*44bedb31SLionel Sambuc     argc--;
434*44bedb31SLionel Sambuc     argv++;
435*44bedb31SLionel Sambuc 
436*44bedb31SLionel Sambuc     /* show usage if no arguments */
437*44bedb31SLionel Sambuc     if (argc == 0) {
438*44bedb31SLionel Sambuc         fputs("gzjoin usage: gzjoin f1.gz [f2.gz [f3.gz ...]] > fjoin.gz\n",
439*44bedb31SLionel Sambuc               stderr);
440*44bedb31SLionel Sambuc         return 0;
441*44bedb31SLionel Sambuc     }
442*44bedb31SLionel Sambuc 
443*44bedb31SLionel Sambuc     /* join gzip files on command line and write to stdout */
444*44bedb31SLionel Sambuc     gzinit(&crc, &tot, stdout);
445*44bedb31SLionel Sambuc     while (argc--)
446*44bedb31SLionel Sambuc         gzcopy(*argv++, argc, &crc, &tot, stdout);
447*44bedb31SLionel Sambuc 
448*44bedb31SLionel Sambuc     /* done */
449*44bedb31SLionel Sambuc     return 0;
450*44bedb31SLionel Sambuc }
451