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