00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <stdio.h>
00056 #include <stdlib.h>
00057 #include <fcntl.h>
00058 #include <unistd.h>
00059 #include "zlib.h"
00060
00061
00062 #define local static
00063
00064
00065 local int bail(char *why1, char *why2)
00066 {
00067 fprintf(stderr, "gzjoin error: %s%s, output incomplete\n", why1, why2);
00068 exit(1);
00069 return 0;
00070 }
00071
00072
00073
00074 #define CHUNK 32768
00075
00076
00077 typedef struct {
00078 char *name;
00079 int fd;
00080 unsigned left;
00081 unsigned char *next;
00082 unsigned char *buf;
00083 } bin;
00084
00085
00086 local void bclose(bin *in)
00087 {
00088 if (in != NULL) {
00089 if (in->fd != -1)
00090 close(in->fd);
00091 if (in->buf != NULL)
00092 free(in->buf);
00093 free(in);
00094 }
00095 }
00096
00097
00098
00099 local bin *bopen(char *name)
00100 {
00101 bin *in;
00102
00103 in = malloc(sizeof(bin));
00104 if (in == NULL)
00105 return NULL;
00106 in->buf = malloc(CHUNK);
00107 in->fd = open(name, O_RDONLY, 0);
00108 if (in->buf == NULL || in->fd == -1) {
00109 bclose(in);
00110 return NULL;
00111 }
00112 in->left = 0;
00113 in->next = in->buf;
00114 in->name = name;
00115 return in;
00116 }
00117
00118
00119
00120 local int bload(bin *in)
00121 {
00122 long len;
00123
00124 if (in == NULL)
00125 return -1;
00126 if (in->left != 0)
00127 return 0;
00128 in->next = in->buf;
00129 do {
00130 len = (long)read(in->fd, in->buf + in->left, CHUNK - in->left);
00131 if (len < 0)
00132 return -1;
00133 in->left += (unsigned)len;
00134 } while (len != 0 && in->left < CHUNK);
00135 return len == 0 ? 1 : 0;
00136 }
00137
00138
00139 #define bget(in) (in->left ? 0 : bload(in), \
00140 in->left ? (in->left--, *(in->next)++) : \
00141 bail("unexpected end of file on ", in->name))
00142
00143
00144 local unsigned long bget4(bin *in)
00145 {
00146 unsigned long val;
00147
00148 val = bget(in);
00149 val += (unsigned long)(bget(in)) << 8;
00150 val += (unsigned long)(bget(in)) << 16;
00151 val += (unsigned long)(bget(in)) << 24;
00152 return val;
00153 }
00154
00155
00156 local void bskip(bin *in, unsigned skip)
00157 {
00158
00159 if (in == NULL)
00160 return;
00161
00162
00163 if (skip <= in->left) {
00164 in->left -= skip;
00165 in->next += skip;
00166 return;
00167 }
00168
00169
00170 skip -= in->left;
00171 in->left = 0;
00172
00173
00174 if (skip > CHUNK) {
00175 unsigned left;
00176
00177 left = skip & (CHUNK - 1);
00178 if (left == 0) {
00179
00180
00181 lseek(in->fd, skip - 1, SEEK_CUR);
00182 if (read(in->fd, in->buf, 1) != 1)
00183 bail("unexpected end of file on ", in->name);
00184 return;
00185 }
00186
00187
00188 lseek(in->fd, skip - left, SEEK_CUR);
00189 skip = left;
00190 }
00191
00192
00193 bload(in);
00194 if (skip > in->left)
00195 bail("unexpected end of file on ", in->name);
00196 in->left -= skip;
00197 in->next += skip;
00198 }
00199
00200
00201
00202
00203 local void gzhead(bin *in)
00204 {
00205 int flags;
00206
00207
00208 if (bget(in) != 0x1f || bget(in) != 0x8b || bget(in) != 8)
00209 bail(in->name, " is not a valid gzip file");
00210
00211
00212 flags = bget(in);
00213 if ((flags & 0xe0) != 0)
00214 bail("unknown reserved bits set in ", in->name);
00215
00216
00217 bskip(in, 6);
00218
00219
00220 if (flags & 4) {
00221 unsigned len;
00222
00223 len = bget(in);
00224 len += (unsigned)(bget(in)) << 8;
00225 bskip(in, len);
00226 }
00227
00228
00229 if (flags & 8)
00230 while (bget(in) != 0)
00231 ;
00232
00233
00234 if (flags & 16)
00235 while (bget(in) != 0)
00236 ;
00237
00238
00239 if (flags & 2)
00240 bskip(in, 2);
00241 }
00242
00243
00244 local void put4(unsigned long val, FILE *out)
00245 {
00246 putc(val & 0xff, out);
00247 putc((val >> 8) & 0xff, out);
00248 putc((val >> 16) & 0xff, out);
00249 putc((val >> 24) & 0xff, out);
00250 }
00251
00252
00253 local void zpull(z_streamp strm, bin *in)
00254 {
00255 if (in->left == 0)
00256 bload(in);
00257 if (in->left == 0)
00258 bail("unexpected end of file on ", in->name);
00259 strm->avail_in = in->left;
00260 strm->next_in = in->next;
00261 }
00262
00263
00264 local void gzinit(unsigned long *crc, unsigned long *tot, FILE *out)
00265 {
00266 fwrite("\x1f\x8b\x08\0\0\0\0\0\0\xff", 1, 10, out);
00267 *crc = crc32(0L, Z_NULL, 0);
00268 *tot = 0;
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278 local void gzcopy(char *name, int clr, unsigned long *crc, unsigned long *tot,
00279 FILE *out)
00280 {
00281 int ret;
00282 int pos;
00283 int last;
00284 bin *in;
00285 unsigned char *start;
00286 unsigned char *junk;
00287 z_off_t len;
00288 z_stream strm;
00289
00290
00291 in = bopen(name);
00292 if (in == NULL)
00293 bail("could not open ", name);
00294 gzhead(in);
00295
00296
00297
00298 junk = malloc(CHUNK);
00299 strm.zalloc = Z_NULL;
00300 strm.zfree = Z_NULL;
00301 strm.opaque = Z_NULL;
00302 strm.avail_in = 0;
00303 strm.next_in = Z_NULL;
00304 ret = inflateInit2(&strm, -15);
00305 if (junk == NULL || ret != Z_OK)
00306 bail("out of memory", "");
00307
00308
00309 len = 0;
00310 zpull(&strm, in);
00311 start = strm.next_in;
00312 last = start[0] & 1;
00313 if (last && clr)
00314 start[0] &= ~1;
00315 strm.avail_out = 0;
00316 for (;;) {
00317
00318 if (strm.avail_in == 0 && strm.avail_out != 0) {
00319 fwrite(start, 1, strm.next_in - start, out);
00320 start = in->buf;
00321 in->left = 0;
00322 zpull(&strm, in);
00323 }
00324
00325
00326 strm.avail_out = CHUNK;
00327 strm.next_out = junk;
00328 ret = inflate(&strm, Z_BLOCK);
00329 switch (ret) {
00330 case Z_MEM_ERROR:
00331 bail("out of memory", "");
00332 case Z_DATA_ERROR:
00333 bail("invalid compressed data in ", in->name);
00334 }
00335
00336
00337 len += CHUNK - strm.avail_out;
00338
00339
00340 if (strm.data_type & 128) {
00341
00342 if (last)
00343 break;
00344
00345
00346 pos = strm.data_type & 7;
00347
00348
00349 if (pos != 0) {
00350
00351 pos = 0x100 >> pos;
00352 last = strm.next_in[-1] & pos;
00353 if (last && clr)
00354 strm.next_in[-1] &= ~pos;
00355 }
00356 else {
00357
00358 if (strm.avail_in == 0) {
00359
00360 fwrite(start, 1, strm.next_in - start, out);
00361 start = in->buf;
00362 in->left = 0;
00363 zpull(&strm, in);
00364 }
00365 last = strm.next_in[0] & 1;
00366 if (last && clr)
00367 strm.next_in[0] &= ~1;
00368 }
00369 }
00370 }
00371
00372
00373 in->left = strm.avail_in;
00374 in->next = strm.next_in;
00375
00376
00377 pos = strm.data_type & 7;
00378 fwrite(start, 1, in->next - start - 1, out);
00379 last = in->next[-1];
00380 if (pos == 0 || !clr)
00381
00382 putc(last, out);
00383 else {
00384
00385 last &= ((0x100 >> pos) - 1);
00386 if (pos & 1) {
00387
00388 putc(last, out);
00389 if (pos == 1)
00390 putc(0, out);
00391 fwrite("\0\0\xff\xff", 1, 4, out);
00392 }
00393 else {
00394
00395 switch (pos) {
00396 case 6:
00397 putc(last | 8, out);
00398 last = 0;
00399 case 4:
00400 putc(last | 0x20, out);
00401 last = 0;
00402 case 2:
00403 putc(last | 0x80, out);
00404 putc(0, out);
00405 }
00406 }
00407 }
00408
00409
00410 *crc = crc32_combine(*crc, bget4(in), len);
00411 *tot += (unsigned long)len;
00412
00413
00414 inflateEnd(&strm);
00415 free(junk);
00416 bclose(in);
00417
00418
00419 if (!clr) {
00420 put4(*crc, out);
00421 put4(*tot, out);
00422 }
00423 }
00424
00425
00426 int main(int argc, char **argv)
00427 {
00428 unsigned long crc, tot;
00429
00430
00431 argc--;
00432 argv++;
00433
00434
00435 if (argc == 0) {
00436 fputs("gzjoin usage: gzjoin f1.gz [f2.gz [f3.gz ...]] > fjoin.gz\n",
00437 stderr);
00438 return 0;
00439 }
00440
00441
00442 gzinit(&crc, &tot, stdout);
00443 while (argc--)
00444 gzcopy(*argv++, argc, &crc, &tot, stdout);
00445
00446
00447 return 0;
00448 }