89 #define CHUNK (1U << LGCHUNK) 95 fprintf(stderr,
"gzappend error: %s%s\n", msg1, msg2);
123 local void rotate(
unsigned char *list,
unsigned len,
unsigned rot)
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;
185 if (len == -1)
bye(
"error reading ", in->
name);
186 in->
left = (unsigned)len;
198 #define read1(in) (in->left == 0 ? readmore(in) : 0, \ 199 in->left--, *(in->next)++) 208 bypass = n & ~((1U << in->
size) - 1);
210 if (lseek(in->
fd, (off_t)bypass,
SEEK_CUR) == -1)
216 bye(
"unexpected end of ", in->
name);
228 val += (unsigned)
read1(in) << 8;
229 val += (
unsigned long)
read1(in) << 16;
230 val += (
unsigned long)
read1(in) << 24;
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);
352 "gzappend warning: junk at end of gzip file overwritten\n");
355 lseek(gz.
fd, lastoff - (lastbit != 0),
SEEK_SET);
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);
498 gztack(NULL, gd, &strm, 1);
501 gztack(*argv, gd, &strm, argv[1] == NULL);
502 }
while (*++argv != NULL);
int deflateSetDictionary(z_streamp strm, Bytef *dictionary, uInt dictLength)
AdjacencyIterator end(const Adjacency &adj, const Vector< D > &vct, size_t pixel_index)
Returns an iterator to position after the end of elements.
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
static void gzheader(file *in)
int deflateEnd(z_streamp strm)
unsigned long read4(file *in)
static unsigned gcd(unsigned a, unsigned b)
static void rotate(unsigned char *list, unsigned len, unsigned rot)
static void bye(char *msg1, char *msg2)
static void skip(file *in, unsigned n)
unsigned long crc32(unsigned long crc, unsigned char *buf, uInt len)
#define inflateInit2(strm, windowBits)
int write(ozstream &zs, const T *x, Items items)
int deflatePrime(z_streamp strm, int bits, int value)
static int out(void *out_desc, unsigned char *buf, unsigned len)
static int gzscan(char *name, z_stream *strm, int level)
int read(izstream &zs, T *x, Items items)
z_const unsigned char * next
static unsigned in(void *in_desc, z_const unsigned char **buf)
int inflateEnd(z_streamp strm)
int inflate(z_streamp strm, int flush)
#define Z_DEFAULT_STRATEGY
static int readin(file *in)
static int readmore(file *in)
int main(int argc, char **argv)
int deflate(z_streamp strm, int flush)
#define Z_DEFAULT_COMPRESSION
static void gztack(char *name, int gd, z_stream *strm, int last)