89 #define CHUNK (1U << LGCHUNK) 95 fprintf(stderr,
"gzappend error: %s%s\n", msg1, msg2);
127 unsigned char *
start, *last, *to, *from;
131 if (rot >= len) rot %=
len;
132 if (rot == 0)
return;
135 last = list + (len - 1);
140 memcpy(list, list + 1, len - 1);
146 if (rot == len - 1) {
148 memmove(list + 1, list, len - 1);
154 cycles =
gcd(len, rot);
156 start = from = list + cycles;
161 if (from > last) from -=
len;
162 if (from == start)
break;
184 len =
read(in->fd, in->buf, 1 << in->size);
185 if (len == -1)
bye(
"error reading ", in->name);
186 in->left = (unsigned)len;
194 if (
readin(in) == 0)
bye(
"unexpected end of ", in->name);
198 #define read1(in) (in->left == 0 ? readmore(in) : 0, \ 199 in->left--, *(in->next)++) 208 bypass = n & ~((1U << in->size) - 1);
211 bye(
"seeking ", in->name);
216 bye(
"unexpected end of ", in->name);
228 val += (unsigned)
read1(in) << 8;
240 if (
read1(in) != 31 ||
read1(in) != 139)
bye(in->name,
" not a gzip file");
241 if (
read1(in) != 8)
bye(
"unknown compression method in", in->name);
243 if (flags & 0xe0)
bye(
"unknown header flags set in", in->name);
247 n += (unsigned)(
read1(in)) << 8;
250 if (flags & 8)
while (
read1(in) != 0) ;
251 if (flags & 16)
while (
read1(in) != 0) ;
252 if (flags & 2)
skip(in, 2);
261 int ret, lastbit,
left, full;
263 unsigned long crc, tot;
264 unsigned char *window;
270 gz.fd =
open(name, O_RDWR, 0);
271 if (gz.fd == -1)
bye(
"cannot open ", name);
273 if (gz.buf ==
NULL)
bye(
"out of memory",
"");
282 if (window ==
NULL)
bye(
"out of memory",
"");
287 if (ret !=
Z_OK)
bye(
"out of memory",
" or library mismatch");
314 bye(
"invalid compressed data--format violated in", name);
343 if (crc !=
read4(&gz))
344 bye(
"invalid compressed data--crc mismatch in ", name);
346 if ((tot & 0xffffffffUL) !=
read4(&gz))
347 bye(
"invalid compressed data--length mismatch in", name);
350 if (gz.left ||
readin(&gz))
352 "gzappend warning: junk at end of gzip file overwritten\n");
356 if (
read(gz.fd, gz.buf, 1) != 1)
bye(
"reading after seek on ", name);
357 *gz.buf = (
unsigned char)(*gz.buf ^ (1 << ((8 - lastbit) & 7)));
359 if (
write(gz.fd, gz.buf, 1) != 1)
bye(
"writing after seek to ", name);
369 if (ret !=
Z_OK)
bye(
"out of memory",
"");
375 if (
read(gz.fd, gz.buf, 1) != 1)
bye(
"reading after seek on ", name);
392 unsigned char *
in, *
out;
397 fd =
open(name, O_RDONLY, 0);
399 fprintf(stderr,
"gzappend warning: %s not found, skipping ...\n",
406 if (in ==
NULL || out ==
NULL)
bye(
"out of memory",
"");
414 "gzappend warning: error reading %s, skipping rest ...\n",
430 if (len == -1)
bye(
"writing gzip file",
"");
431 left -= (unsigned)len;
439 out[0] = (
unsigned char)(strm->
adler);
440 out[1] = (
unsigned char)(strm->
adler >> 8);
441 out[2] = (
unsigned char)(strm->
adler >> 16);
442 out[3] = (
unsigned char)(strm->
adler >> 24);
443 out[4] = (
unsigned char)(strm->
total_in);
444 out[5] = (
unsigned char)(strm->
total_in >> 8);
445 out[6] = (
unsigned char)(strm->
total_in >> 16);
446 out[7] = (
unsigned char)(strm->
total_in >> 24);
449 ret =
write(gd, out + 8 - len, len);
450 if (ret == -1)
bye(
"writing gzip file",
"");
459 if (fd > 0)
close(fd);
466 int main(
int argc,
char **argv)
477 "gzappend 1.2 (11 Oct 2012) Copyright (C) 2003, 2012 Mark Adler\n" 480 "usage: gzappend [-level] file.gz [ addthis [ andthis ... ]]\n");
486 if (argv[0][0] ==
'-') {
487 if (argv[0][1] <
'0' || argv[0][1] >
'9' || argv[0][2] != 0)
488 bye(
"invalid compression level",
"");
489 level = argv[0][1] -
'0';
490 if (*++argv ==
NULL)
bye(
"no gzip file name after options",
"");
494 gd =
gzscan(*argv++, &strm, level);
502 }
while (*++argv !=
NULL);
local void gzheader(file *in)
local int readin(file *in)
GLboolean GLboolean GLboolean GLboolean a
int write(int fd, const char *buf, int nbytes)
int read(int fd, char *buf, int nbytes)
unsigned long read4(file *in)
local void gztack(char *name, int gd, z_stream *strm, int last)
#define Z_DEFAULT_COMPRESSION
local void rotate(unsigned char *list, unsigned len, unsigned rot)
int ZEXPORT deflateEnd(z_streamp strm)
int ZEXPORT deflatePrime(z_streamp strm, int bits, int value)
GLboolean GLboolean GLboolean b
#define inflateInit2(strm, windowBits)
GLenum GLuint GLenum GLsizei const GLchar * buf
local int gzscan(char *name, z_stream *strm, int level)
off_t lseek(int fd, off_t offset, int whence)
local int readmore(file *in)
local int out(void *out_desc, unsigned char *buf, unsigned len)
int ZEXPORT inflate(z_streamp strm, int flush)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
#define Z_DEFAULT_STRATEGY
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
int ZEXPORT deflate(z_streamp strm, int flush)
local void skip(file *in, unsigned n)
local unsigned gcd(unsigned a, unsigned b)
GLuint const GLchar * name
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
int open(const char *name, int flags, int mode)
int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
local void bye(char *msg1, char *msg2)
int main(int argc, char **argv)
int ZEXPORT inflateEnd(z_streamp strm)