- }
-
- /* free decoding table for trees */
- huft_free(tl);
-
- /* restore the global bit buffer */
- bb = b_dynamic;
- bk = k_dynamic;
-
- /* build the decoding tables for literal/length and distance codes */
- bl = lbits;
- if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
- if (i == 1) {
- error_msg("Incomplete literal tree");
- huft_free(tl);
- }
- return i; /* incomplete code set */
- }
- bd = dbits;
- if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
- if (i == 1) {
- error_msg("incomplete distance tree");
- huft_free(td);
- }
- huft_free(tl);
- return i; /* incomplete code set */
- }
-
- /* decompress until an end-of-block code */
- if (inflate_codes(tl, td, bl, bd))
- return 1;
-
- /* free the decoding tables, return */
- huft_free(tl);
- huft_free(td);
- return 0;
- }
- default:
- /* bad block type */
- return 2;
- }
-}
-
-/*
- * decompress an inflated entry
- *
- * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
- */
-static int inflate()
-{
- int e; /* last block flag */
- int r; /* result code */
- unsigned h = 0; /* maximum struct huft's malloc'ed */
-
- /* initialize window, bit buffer */
- outcnt = 0;
- bk = 0;
- bb = 0;
-
- /* decompress until the last block */
- do {
- hufts = 0;
- if ((r = inflate_block(&e)) != 0) {
- return r;
- }
- if (hufts > h) {
- h = hufts;
- }
- } while (!e);
-
- /* flush out window */
- flush_window();
-
- /* return success */
- return 0;
-}
-
-/* ===========================================================================
- * Unzip in to out. This routine works on both gzip and pkzip files.
- *
- * IN assertions: the buffer inbuf contains already the beginning of
- * the compressed data, from offsets inptr to insize-1 included.
- * The magic header has already been checked. The output buffer is cleared.
- * in, out: input and output file descriptors
- */
-extern int unzip(FILE *l_in_file, FILE *l_out_file)
-{
- const int extra_field = 0x04; /* bit 2 set: extra field present */
- const int orig_name = 0x08; /* bit 3 set: original file name present */
- const int comment = 0x10; /* bit 4 set: file comment present */
- unsigned char buf[8]; /* extended local header */
- unsigned char flags; /* compression flags */
- char magic[2]; /* magic header */
- int method;
- typedef void (*sig_type) (int);
- int exit_code=0; /* program exit code */
-
- in_file = l_in_file;
- out_file = l_out_file;
-
-/* if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
- (void) signal(SIGINT, (sig_type) abort_gzip);
- }
-#ifdef SIGTERM
- if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
- (void) signal(SIGTERM, (sig_type) abort_gzip);
- }
-#endif
-#ifdef SIGHUP
- if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
- (void) signal(SIGHUP, (sig_type) abort_gzip);
- }
-#endif
-*/
- /* Allocate all global buffers (for DYN_ALLOC option) */
- window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char)));
- outcnt = 0;
- bytes_out = 0L;
-
- /* set the buffer size */
- setvbuf(in_file, NULL, _IOFBF, 0x8000);
-
- magic[0] = fgetc(in_file);
- magic[1] = fgetc(in_file);
-
- /* Magic header for gzip files, 1F 8B = \037\213 */
- if (memcmp(magic, "\037\213", 2) != 0) {
- error_msg("Invalid gzip magic");
- return EXIT_FAILURE;
- }
-
- method = (int) fgetc(in_file);
- if (method != 8) {
- error_msg("unknown method %d -- get newer version of gzip", method);
- exit_code = 1;
- return -1;
- }
-
- flags = (unsigned char) fgetc(in_file);
-
- /* Ignore time stamp(4), extra flags(1), OS type(1) */
- fseek(in_file, 6, SEEK_CUR);
-
- if ((flags & extra_field) != 0) {
- fseek(in_file, (size_t) fgetc(in_file) + ((size_t)fgetc(in_file) << 8), SEEK_CUR);
- }
-
- /* Discard original name if any */
- if ((flags & orig_name) != 0) {
- while (fgetc(in_file) != 0); /* null */
- }
-
- /* Discard file comment if any */
- if ((flags & comment) != 0) {
- while (fgetc(in_file) != 0); /* null */
- }
-
- if (method < 0) {
- printf("it failed\n");
- return(exit_code); /* error message already emitted */
- }
-
- make_crc_table();
-
- /* Decompress */
- if (method == 8) {
-
- int res = inflate();
-
- if (res == 3) {
- error_msg(memory_exhausted);
- } else if (res != 0) {
- error_msg("invalid compressed data--format violated");
- }
-
- } else {
- error_msg("internal error, invalid method");
- }
-
- /* Get the crc and original length
- * crc32 (see algorithm.doc)
- * uncompressed input size modulo 2^32
- */
- fread(buf, 1, 8, in_file);
-
- /* Validate decompression - crc */
- if (((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) {
- error_msg("invalid compressed data--crc error");
- }
- /* Validate decompression - size */
- if (((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) {
- error_msg("invalid compressed data--length error");
- }
-
- free(window);
- free(crc_table);
-
- return 0;
-}
-
-extern int gz_open(FILE *compressed_file, int *pid)
-{
- int unzip_pipe[2];
-
-// signal(SIGCHLD, abort_gzip);
- if (pipe(unzip_pipe)!=0) {
- error_msg("pipe error");
- return(EXIT_FAILURE);
- }
- if ((*pid = fork()) == -1) {
- error_msg("fork failured");
- return(EXIT_FAILURE);
- }
- if (*pid==0) {
- /* child process */
- close(unzip_pipe[0]);
- unzip(compressed_file, fdopen(unzip_pipe[1], "w"));
- printf("finished unzipping\n");
- fflush(NULL);
- fclose(compressed_file);
- close(unzip_pipe[1]);
- exit(EXIT_SUCCESS);
- }
-
- close(unzip_pipe[1]);
- return(unzip_pipe[0]);
-}
-
-extern void gz_close(int gunzip_pid)
-{
- if (kill(gunzip_pid, SIGTERM) == -1) {
- error_msg_and_die("*** Couldnt kill old gunzip process *** aborting");
- }
-
- if (waitpid(gunzip_pid, NULL, 0) == -1) {
- printf("Couldnt wait ?");
- }
- free(window);
- free(crc_table);
-}
-
-extern int gunzip_main(int argc, char **argv)
-{
- struct stat stat_buf;
-
- char *if_name = NULL;
- char *of_name = NULL;
- char *delete_file_name = NULL;
-
- const int gunzip_to_stdout = 1;
- const int gunzip_from_stdin = 2;
- const int gunzip_force = 4;
- const int gunzip_test = 8;
-
- int flags = 0;
- int opt = 0;
- int delete_old_file = FALSE;
-
-#ifdef BB_ZCAT
- /* if called as zcat */
- if (strcmp(applet_name, "zcat") == 0) {
- if (argc != 2) {
- show_usage();
- }
- flags |= gunzip_force;
- flags |= gunzip_to_stdout;
- } else
-#endif
- {
- /* workout flags as regular gunzip */
- /* set default flags */
- if (argc == 1) {
- flags |= (gunzip_from_stdin | gunzip_to_stdout);
- }
-
- /* Parse any options */
- while ((opt = getopt(argc, argv, "ctfh")) != -1) {
- switch (opt) {
- case 'c':
- flags |= gunzip_to_stdout;
- break;
- case 'f':
- flags |= gunzip_force;
- break;
- case 't':
- flags |= gunzip_test;
- break;
- case 'h':
- default:
- show_usage(); /* exit's inside usage */
- }