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