cannam@128: /* gzjoin -- command to join gzip files into one gzip file cannam@128: cannam@128: Copyright (C) 2004, 2005, 2012 Mark Adler, all rights reserved cannam@128: version 1.2, 14 Aug 2012 cannam@128: cannam@128: This software is provided 'as-is', without any express or implied cannam@128: warranty. In no event will the author be held liable for any damages cannam@128: arising from the use of this software. cannam@128: cannam@128: Permission is granted to anyone to use this software for any purpose, cannam@128: including commercial applications, and to alter it and redistribute it cannam@128: freely, subject to the following restrictions: cannam@128: cannam@128: 1. The origin of this software must not be misrepresented; you must not cannam@128: claim that you wrote the original software. If you use this software cannam@128: in a product, an acknowledgment in the product documentation would be cannam@128: appreciated but is not required. cannam@128: 2. Altered source versions must be plainly marked as such, and must not be cannam@128: misrepresented as being the original software. cannam@128: 3. This notice may not be removed or altered from any source distribution. cannam@128: cannam@128: Mark Adler madler@alumni.caltech.edu cannam@128: */ cannam@128: cannam@128: /* cannam@128: * Change history: cannam@128: * cannam@128: * 1.0 11 Dec 2004 - First version cannam@128: * 1.1 12 Jun 2005 - Changed ssize_t to long for portability cannam@128: * 1.2 14 Aug 2012 - Clean up for z_const usage cannam@128: */ cannam@128: cannam@128: /* cannam@128: gzjoin takes one or more gzip files on the command line and writes out a cannam@128: single gzip file that will uncompress to the concatenation of the cannam@128: uncompressed data from the individual gzip files. gzjoin does this without cannam@128: having to recompress any of the data and without having to calculate a new cannam@128: crc32 for the concatenated uncompressed data. gzjoin does however have to cannam@128: decompress all of the input data in order to find the bits in the compressed cannam@128: data that need to be modified to concatenate the streams. cannam@128: cannam@128: gzjoin does not do an integrity check on the input gzip files other than cannam@128: checking the gzip header and decompressing the compressed data. They are cannam@128: otherwise assumed to be complete and correct. cannam@128: cannam@128: Each joint between gzip files removes at least 18 bytes of previous trailer cannam@128: and subsequent header, and inserts an average of about three bytes to the cannam@128: compressed data in order to connect the streams. The output gzip file cannam@128: has a minimal ten-byte gzip header with no file name or modification time. cannam@128: cannam@128: This program was written to illustrate the use of the Z_BLOCK option of cannam@128: inflate() and the crc32_combine() function. gzjoin will not compile with cannam@128: versions of zlib earlier than 1.2.3. cannam@128: */ cannam@128: cannam@128: #include /* fputs(), fprintf(), fwrite(), putc() */ cannam@128: #include /* exit(), malloc(), free() */ cannam@128: #include /* open() */ cannam@128: #include /* close(), read(), lseek() */ cannam@128: #include "zlib.h" cannam@128: /* crc32(), crc32_combine(), inflateInit2(), inflate(), inflateEnd() */ cannam@128: cannam@128: #define local static cannam@128: cannam@128: /* exit with an error (return a value to allow use in an expression) */ cannam@128: local int bail(char *why1, char *why2) cannam@128: { cannam@128: fprintf(stderr, "gzjoin error: %s%s, output incomplete\n", why1, why2); cannam@128: exit(1); cannam@128: return 0; cannam@128: } cannam@128: cannam@128: /* -- simple buffered file input with access to the buffer -- */ cannam@128: cannam@128: #define CHUNK 32768 /* must be a power of two and fit in unsigned */ cannam@128: cannam@128: /* bin buffered input file type */ cannam@128: typedef struct { cannam@128: char *name; /* name of file for error messages */ cannam@128: int fd; /* file descriptor */ cannam@128: unsigned left; /* bytes remaining at next */ cannam@128: unsigned char *next; /* next byte to read */ cannam@128: unsigned char *buf; /* allocated buffer of length CHUNK */ cannam@128: } bin; cannam@128: cannam@128: /* close a buffered file and free allocated memory */ cannam@128: local void bclose(bin *in) cannam@128: { cannam@128: if (in != NULL) { cannam@128: if (in->fd != -1) cannam@128: close(in->fd); cannam@128: if (in->buf != NULL) cannam@128: free(in->buf); cannam@128: free(in); cannam@128: } cannam@128: } cannam@128: cannam@128: /* open a buffered file for input, return a pointer to type bin, or NULL on cannam@128: failure */ cannam@128: local bin *bopen(char *name) cannam@128: { cannam@128: bin *in; cannam@128: cannam@128: in = malloc(sizeof(bin)); cannam@128: if (in == NULL) cannam@128: return NULL; cannam@128: in->buf = malloc(CHUNK); cannam@128: in->fd = open(name, O_RDONLY, 0); cannam@128: if (in->buf == NULL || in->fd == -1) { cannam@128: bclose(in); cannam@128: return NULL; cannam@128: } cannam@128: in->left = 0; cannam@128: in->next = in->buf; cannam@128: in->name = name; cannam@128: return in; cannam@128: } cannam@128: cannam@128: /* load buffer from file, return -1 on read error, 0 or 1 on success, with cannam@128: 1 indicating that end-of-file was reached */ cannam@128: local int bload(bin *in) cannam@128: { cannam@128: long len; cannam@128: cannam@128: if (in == NULL) cannam@128: return -1; cannam@128: if (in->left != 0) cannam@128: return 0; cannam@128: in->next = in->buf; cannam@128: do { cannam@128: len = (long)read(in->fd, in->buf + in->left, CHUNK - in->left); cannam@128: if (len < 0) cannam@128: return -1; cannam@128: in->left += (unsigned)len; cannam@128: } while (len != 0 && in->left < CHUNK); cannam@128: return len == 0 ? 1 : 0; cannam@128: } cannam@128: cannam@128: /* get a byte from the file, bail if end of file */ cannam@128: #define bget(in) (in->left ? 0 : bload(in), \ cannam@128: in->left ? (in->left--, *(in->next)++) : \ cannam@128: bail("unexpected end of file on ", in->name)) cannam@128: cannam@128: /* get a four-byte little-endian unsigned integer from file */ cannam@128: local unsigned long bget4(bin *in) cannam@128: { cannam@128: unsigned long val; cannam@128: cannam@128: val = bget(in); cannam@128: val += (unsigned long)(bget(in)) << 8; cannam@128: val += (unsigned long)(bget(in)) << 16; cannam@128: val += (unsigned long)(bget(in)) << 24; cannam@128: return val; cannam@128: } cannam@128: cannam@128: /* skip bytes in file */ cannam@128: local void bskip(bin *in, unsigned skip) cannam@128: { cannam@128: /* check pointer */ cannam@128: if (in == NULL) cannam@128: return; cannam@128: cannam@128: /* easy case -- skip bytes in buffer */ cannam@128: if (skip <= in->left) { cannam@128: in->left -= skip; cannam@128: in->next += skip; cannam@128: return; cannam@128: } cannam@128: cannam@128: /* skip what's in buffer, discard buffer contents */ cannam@128: skip -= in->left; cannam@128: in->left = 0; cannam@128: cannam@128: /* seek past multiples of CHUNK bytes */ cannam@128: if (skip > CHUNK) { cannam@128: unsigned left; cannam@128: cannam@128: left = skip & (CHUNK - 1); cannam@128: if (left == 0) { cannam@128: /* exact number of chunks: seek all the way minus one byte to check cannam@128: for end-of-file with a read */ cannam@128: lseek(in->fd, skip - 1, SEEK_CUR); cannam@128: if (read(in->fd, in->buf, 1) != 1) cannam@128: bail("unexpected end of file on ", in->name); cannam@128: return; cannam@128: } cannam@128: cannam@128: /* skip the integral chunks, update skip with remainder */ cannam@128: lseek(in->fd, skip - left, SEEK_CUR); cannam@128: skip = left; cannam@128: } cannam@128: cannam@128: /* read more input and skip remainder */ cannam@128: bload(in); cannam@128: if (skip > in->left) cannam@128: bail("unexpected end of file on ", in->name); cannam@128: in->left -= skip; cannam@128: in->next += skip; cannam@128: } cannam@128: cannam@128: /* -- end of buffered input functions -- */ cannam@128: cannam@128: /* skip the gzip header from file in */ cannam@128: local void gzhead(bin *in) cannam@128: { cannam@128: int flags; cannam@128: cannam@128: /* verify gzip magic header and compression method */ cannam@128: if (bget(in) != 0x1f || bget(in) != 0x8b || bget(in) != 8) cannam@128: bail(in->name, " is not a valid gzip file"); cannam@128: cannam@128: /* get and verify flags */ cannam@128: flags = bget(in); cannam@128: if ((flags & 0xe0) != 0) cannam@128: bail("unknown reserved bits set in ", in->name); cannam@128: cannam@128: /* skip modification time, extra flags, and os */ cannam@128: bskip(in, 6); cannam@128: cannam@128: /* skip extra field if present */ cannam@128: if (flags & 4) { cannam@128: unsigned len; cannam@128: cannam@128: len = bget(in); cannam@128: len += (unsigned)(bget(in)) << 8; cannam@128: bskip(in, len); cannam@128: } cannam@128: cannam@128: /* skip file name if present */ cannam@128: if (flags & 8) cannam@128: while (bget(in) != 0) cannam@128: ; cannam@128: cannam@128: /* skip comment if present */ cannam@128: if (flags & 16) cannam@128: while (bget(in) != 0) cannam@128: ; cannam@128: cannam@128: /* skip header crc if present */ cannam@128: if (flags & 2) cannam@128: bskip(in, 2); cannam@128: } cannam@128: cannam@128: /* write a four-byte little-endian unsigned integer to out */ cannam@128: local void put4(unsigned long val, FILE *out) cannam@128: { cannam@128: putc(val & 0xff, out); cannam@128: putc((val >> 8) & 0xff, out); cannam@128: putc((val >> 16) & 0xff, out); cannam@128: putc((val >> 24) & 0xff, out); cannam@128: } cannam@128: cannam@128: /* Load up zlib stream from buffered input, bail if end of file */ cannam@128: local void zpull(z_streamp strm, bin *in) cannam@128: { cannam@128: if (in->left == 0) cannam@128: bload(in); cannam@128: if (in->left == 0) cannam@128: bail("unexpected end of file on ", in->name); cannam@128: strm->avail_in = in->left; cannam@128: strm->next_in = in->next; cannam@128: } cannam@128: cannam@128: /* Write header for gzip file to out and initialize trailer. */ cannam@128: local void gzinit(unsigned long *crc, unsigned long *tot, FILE *out) cannam@128: { cannam@128: fwrite("\x1f\x8b\x08\0\0\0\0\0\0\xff", 1, 10, out); cannam@128: *crc = crc32(0L, Z_NULL, 0); cannam@128: *tot = 0; cannam@128: } cannam@128: cannam@128: /* Copy the compressed data from name, zeroing the last block bit of the last cannam@128: block if clr is true, and adding empty blocks as needed to get to a byte cannam@128: boundary. If clr is false, then the last block becomes the last block of cannam@128: the output, and the gzip trailer is written. crc and tot maintains the cannam@128: crc and length (modulo 2^32) of the output for the trailer. The resulting cannam@128: gzip file is written to out. gzinit() must be called before the first call cannam@128: of gzcopy() to write the gzip header and to initialize crc and tot. */ cannam@128: local void gzcopy(char *name, int clr, unsigned long *crc, unsigned long *tot, cannam@128: FILE *out) cannam@128: { cannam@128: int ret; /* return value from zlib functions */ cannam@128: int pos; /* where the "last block" bit is in byte */ cannam@128: int last; /* true if processing the last block */ cannam@128: bin *in; /* buffered input file */ cannam@128: unsigned char *start; /* start of compressed data in buffer */ cannam@128: unsigned char *junk; /* buffer for uncompressed data -- discarded */ cannam@128: z_off_t len; /* length of uncompressed data (support > 4 GB) */ cannam@128: z_stream strm; /* zlib inflate stream */ cannam@128: cannam@128: /* open gzip file and skip header */ cannam@128: in = bopen(name); cannam@128: if (in == NULL) cannam@128: bail("could not open ", name); cannam@128: gzhead(in); cannam@128: cannam@128: /* allocate buffer for uncompressed data and initialize raw inflate cannam@128: stream */ cannam@128: junk = malloc(CHUNK); cannam@128: strm.zalloc = Z_NULL; cannam@128: strm.zfree = Z_NULL; cannam@128: strm.opaque = Z_NULL; cannam@128: strm.avail_in = 0; cannam@128: strm.next_in = Z_NULL; cannam@128: ret = inflateInit2(&strm, -15); cannam@128: if (junk == NULL || ret != Z_OK) cannam@128: bail("out of memory", ""); cannam@128: cannam@128: /* inflate and copy compressed data, clear last-block bit if requested */ cannam@128: len = 0; cannam@128: zpull(&strm, in); cannam@128: start = in->next; cannam@128: last = start[0] & 1; cannam@128: if (last && clr) cannam@128: start[0] &= ~1; cannam@128: strm.avail_out = 0; cannam@128: for (;;) { cannam@128: /* if input used and output done, write used input and get more */ cannam@128: if (strm.avail_in == 0 && strm.avail_out != 0) { cannam@128: fwrite(start, 1, strm.next_in - start, out); cannam@128: start = in->buf; cannam@128: in->left = 0; cannam@128: zpull(&strm, in); cannam@128: } cannam@128: cannam@128: /* decompress -- return early when end-of-block reached */ cannam@128: strm.avail_out = CHUNK; cannam@128: strm.next_out = junk; cannam@128: ret = inflate(&strm, Z_BLOCK); cannam@128: switch (ret) { cannam@128: case Z_MEM_ERROR: cannam@128: bail("out of memory", ""); cannam@128: case Z_DATA_ERROR: cannam@128: bail("invalid compressed data in ", in->name); cannam@128: } cannam@128: cannam@128: /* update length of uncompressed data */ cannam@128: len += CHUNK - strm.avail_out; cannam@128: cannam@128: /* check for block boundary (only get this when block copied out) */ cannam@128: if (strm.data_type & 128) { cannam@128: /* if that was the last block, then done */ cannam@128: if (last) cannam@128: break; cannam@128: cannam@128: /* number of unused bits in last byte */ cannam@128: pos = strm.data_type & 7; cannam@128: cannam@128: /* find the next last-block bit */ cannam@128: if (pos != 0) { cannam@128: /* next last-block bit is in last used byte */ cannam@128: pos = 0x100 >> pos; cannam@128: last = strm.next_in[-1] & pos; cannam@128: if (last && clr) cannam@128: in->buf[strm.next_in - in->buf - 1] &= ~pos; cannam@128: } cannam@128: else { cannam@128: /* next last-block bit is in next unused byte */ cannam@128: if (strm.avail_in == 0) { cannam@128: /* don't have that byte yet -- get it */ cannam@128: fwrite(start, 1, strm.next_in - start, out); cannam@128: start = in->buf; cannam@128: in->left = 0; cannam@128: zpull(&strm, in); cannam@128: } cannam@128: last = strm.next_in[0] & 1; cannam@128: if (last && clr) cannam@128: in->buf[strm.next_in - in->buf] &= ~1; cannam@128: } cannam@128: } cannam@128: } cannam@128: cannam@128: /* update buffer with unused input */ cannam@128: in->left = strm.avail_in; cannam@128: in->next = in->buf + (strm.next_in - in->buf); cannam@128: cannam@128: /* copy used input, write empty blocks to get to byte boundary */ cannam@128: pos = strm.data_type & 7; cannam@128: fwrite(start, 1, in->next - start - 1, out); cannam@128: last = in->next[-1]; cannam@128: if (pos == 0 || !clr) cannam@128: /* already at byte boundary, or last file: write last byte */ cannam@128: putc(last, out); cannam@128: else { cannam@128: /* append empty blocks to last byte */ cannam@128: last &= ((0x100 >> pos) - 1); /* assure unused bits are zero */ cannam@128: if (pos & 1) { cannam@128: /* odd -- append an empty stored block */ cannam@128: putc(last, out); cannam@128: if (pos == 1) cannam@128: putc(0, out); /* two more bits in block header */ cannam@128: fwrite("\0\0\xff\xff", 1, 4, out); cannam@128: } cannam@128: else { cannam@128: /* even -- append 1, 2, or 3 empty fixed blocks */ cannam@128: switch (pos) { cannam@128: case 6: cannam@128: putc(last | 8, out); cannam@128: last = 0; cannam@128: case 4: cannam@128: putc(last | 0x20, out); cannam@128: last = 0; cannam@128: case 2: cannam@128: putc(last | 0x80, out); cannam@128: putc(0, out); cannam@128: } cannam@128: } cannam@128: } cannam@128: cannam@128: /* update crc and tot */ cannam@128: *crc = crc32_combine(*crc, bget4(in), len); cannam@128: *tot += (unsigned long)len; cannam@128: cannam@128: /* clean up */ cannam@128: inflateEnd(&strm); cannam@128: free(junk); cannam@128: bclose(in); cannam@128: cannam@128: /* write trailer if this is the last gzip file */ cannam@128: if (!clr) { cannam@128: put4(*crc, out); cannam@128: put4(*tot, out); cannam@128: } cannam@128: } cannam@128: cannam@128: /* join the gzip files on the command line, write result to stdout */ cannam@128: int main(int argc, char **argv) cannam@128: { cannam@128: unsigned long crc, tot; /* running crc and total uncompressed length */ cannam@128: cannam@128: /* skip command name */ cannam@128: argc--; cannam@128: argv++; cannam@128: cannam@128: /* show usage if no arguments */ cannam@128: if (argc == 0) { cannam@128: fputs("gzjoin usage: gzjoin f1.gz [f2.gz [f3.gz ...]] > fjoin.gz\n", cannam@128: stderr); cannam@128: return 0; cannam@128: } cannam@128: cannam@128: /* join gzip files on command line and write to stdout */ cannam@128: gzinit(&crc, &tot, stdout); cannam@128: while (argc--) cannam@128: gzcopy(*argv++, argc, &crc, &tot, stdout); cannam@128: cannam@128: /* done */ cannam@128: return 0; cannam@128: }