Free memory in error paths.
[oweals/opkg-lede.git] / libbb / unzip.c
index fb2429bf058a560eb724099272782854871da215..058aa07535539719180b3c3b28b1394f4116a25f 100644 (file)
  * See the file algorithm.doc for the compression algorithms and file formats.
  */
 
-#if 0
-static char *license_msg[] = {
-       "   Copyright (C) 1992-1993 Jean-loup Gailly",
-       "   This program is free software; you can redistribute it and/or modify",
-       "   it under the terms of the GNU General Public License as published by",
-       "   the Free Software Foundation; either version 2, or (at your option)",
-       "   any later version.",
-       "",
-       "   This program is distributed in the hope that it will be useful,",
-       "   but WITHOUT ANY WARRANTY; without even the implied warranty of",
-       "   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the",
-       "   GNU General Public License for more details.",
-       "",
-       "   You should have received a copy of the GNU General Public License",
-       "   along with this program; if not, write to the Free Software",
-       "   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
-       0
-};
-#endif
-
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <signal.h>
@@ -133,7 +113,7 @@ static void make_crc_table()
 
        /* initial shift register value */
        crc = 0xffffffffL;      
-       crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));
+       crc_table = (unsigned long *) xmalloc(256 * sizeof(unsigned long));
 
        /* Make exclusive-or pattern from polynomial (0xedb88320) */
        for (i = 0; i < sizeof(p)/sizeof(int); i++)
@@ -216,16 +196,16 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
        unsigned f;             /* i repeats in table every f entries */
        int g;                  /* maximum code length */
        int h;                  /* table level */
-       register unsigned i;    /* counter, current code */
-       register unsigned j;    /* counter */
-       register int k;         /* number of bits in current code */
+       unsigned i;     /* counter, current code */
+       unsigned j;     /* counter */
+       int k;          /* number of bits in current code */
        int l;                  /* bits per table (returned in m) */
-       register unsigned *p;           /* pointer into c[], b[], or v[] */
-       register huft_t *q;     /* points to current table */
+       unsigned *p;            /* pointer into c[], b[], or v[] */
+       huft_t *q;      /* points to current table */
        huft_t r;               /* table entry for structure assignment */
        huft_t *u[BMAX];        /* table stack */
        unsigned v[N_MAX];      /* values in order of bit length */
-       register int w;         /* bits before this table == (l * h) */
+       int w;          /* bits before this table == (l * h) */
        unsigned x[BMAX + 1];   /* bit offsets, then code stack */
        unsigned *xp;           /* pointer into x */
        int y;                  /* number of dummy codes added */
@@ -383,13 +363,13 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
  */
 static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
 {
-       register unsigned long e;               /* table entry flag/number of extra bits */
+       unsigned long e;                /* table entry flag/number of extra bits */
        unsigned long n, d;                             /* length and index for copy */
        unsigned long w;                                /* current window position */
        huft_t *t;                              /* pointer to table entry */
        unsigned ml, md;                        /* masks for bl and bd bits */
-       register unsigned long b;                               /* bit buffer */
-       register unsigned k;            /* number of bits in bit buffer */
+       unsigned long b;                                /* bit buffer */
+       unsigned k;             /* number of bits in bit buffer */
 
        /* make local copies of globals */
        b = bb;                                 /* initialize bit buffer */
@@ -510,8 +490,8 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
 static int inflate_block(int *e)
 {
        unsigned t;                     /* block type */
-       register unsigned long b;                       /* bit buffer */
-       register unsigned k;            /* number of bits in bit buffer */
+       unsigned long b;                        /* bit buffer */
+       unsigned k;             /* number of bits in bit buffer */
        static unsigned short cplens[] = {              /* Copy lengths for literal codes 257..285 */
                3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
                35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
@@ -564,8 +544,8 @@ static int inflate_block(int *e)
                {
                        unsigned long n;                        /* number of bytes in block */
                        unsigned long w;                        /* current window position */
-                       register unsigned long b_stored;                        /* bit buffer */
-                       register unsigned long k_stored;                /* number of bits in bit buffer */
+                       unsigned long b_stored;                 /* bit buffer */
+                       unsigned long k_stored;         /* number of bits in bit buffer */
 
                        /* make local copies of globals */
                        b_stored = bb;                          /* initialize bit buffer */
@@ -659,8 +639,11 @@ static int inflate_block(int *e)
                        }
 
                        /* decompress until an end-of-block code */
-                       if (inflate_codes(tl, td, bl, bd))
+                       if (inflate_codes(tl, td, bl, bd)) {
+                               huft_free(tl);
+                               huft_free(td);
                                return 1;
+                       }
 
                        /* free the decoding tables, return */
                        huft_free(tl);
@@ -690,8 +673,8 @@ static int inflate_block(int *e)
                        unsigned nd;                            /* number of distance codes */
 
                        unsigned ll[286 + 30];          /* literal/length and distance code lengths */
-                       register unsigned long b_dynamic;       /* bit buffer */
-                       register unsigned k_dynamic;            /* number of bits in bit buffer */
+                       unsigned long b_dynamic;        /* bit buffer */
+                       unsigned k_dynamic;             /* number of bits in bit buffer */
 
                        /* make local bit buffer */
                        b_dynamic = bb;
@@ -836,8 +819,11 @@ static int inflate_block(int *e)
                        }
 
                        /* decompress until an end-of-block code */
-                       if (inflate_codes(tl, td, bl, bd))
+                       if (inflate_codes(tl, td, bl, bd)) {
+                               huft_free(tl);
+                               huft_free(td);
                                return 1;
+                       }
 
                        /* free the decoding tables, return */
                        huft_free(tl);
@@ -1025,19 +1011,3 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
 
        return exit_code;
 }
-
-/*
- * This needs access to global variables wondow and crc_table, so its not in its own file.
- */
-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);
-}