root/zBoot/unzip.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. check_zipfile
  2. unzip

   1 /* unzip.c -- decompress files in gzip or pkzip format.
   2  * Copyright (C) 1992-1993 Jean-loup Gailly
   3  * This is free software; you can redistribute it and/or modify it under the
   4  * terms of the GNU General Public License, see the file COPYING.
   5  *
   6  * The code in this file is derived from the file funzip.c written
   7  * and put in the public domain by Mark Adler.
   8  */
   9 
  10 /*
  11    This version can extract files in gzip or pkzip format.
  12    For the latter, only the first entry is extracted, and it has to be
  13    either deflated or stored.
  14  */
  15 
  16 #ifndef lint
  17 static char rcsid[] = "$Id: unzip.c,v 0.9 1993/02/10 16:07:22 jloup Exp $";
  18 #endif
  19 
  20 /* #include "tailor.h" */
  21 #include "gzip.h"
  22 #include "crypt.h"
  23 
  24 #include <stdio.h>
  25 
  26 /* PKZIP header definitions */
  27 #define LOCSIG 0x04034b50L      /* four-byte lead-in (lsb first) */
  28 #define LOCFLG 6                /* offset of bit flag */
  29 #define  CRPFLG 1               /*  bit for encrypted entry */
  30 #define  EXTFLG 8               /*  bit for extended local header */
  31 #define LOCHOW 8                /* offset of compression method */
  32 #define LOCTIM 10               /* file mod time (for decryption) */
  33 #define LOCCRC 14               /* offset of crc */
  34 #define LOCSIZ 18               /* offset of compressed size */
  35 #define LOCLEN 22               /* offset of uncompressed length */
  36 #define LOCFIL 26               /* offset of file name field length */
  37 #define LOCEXT 28               /* offset of extra field length */
  38 #define LOCHDR 30               /* size of local header, including sig */
  39 #define EXTHDR 16               /* size of extended local header, inc sig */
  40 
  41 
  42 /* Globals */
  43 
  44 int decrypt;      /* flag to turn on decryption */
  45 char *key;        /* not used--needed to link crypt.c */
  46 int pkzip = 0;    /* set for a pkzip file */
  47 int extended = 0; /* set if extended local header */
  48 
  49 /* ===========================================================================
  50  * Check zip file and advance inptr to the start of the compressed data.
  51  * Get ofname from the local header if necessary.
  52  */
  53 int check_zipfile(in)
     /* [previous][next][first][last][top][bottom][index][help] */
  54     int in;   /* input file descriptors */
  55 {
  56     uch *h = inbuf + inptr; /* first local header */
  57 
  58     /* ifd = in; */
  59 
  60     /* Check validity of local header, and skip name and extra fields */
  61     inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
  62 
  63     if (inptr > insize || LG(h) != LOCSIG) {
  64         error("input not a zip file or empty");
  65     }
  66     method = h[LOCHOW];
  67     if (method != STORED && method != DEFLATED) {
  68         error("first entry not deflated or stored--can't extract");
  69     }
  70 
  71     /* If entry encrypted, decrypt and validate encryption header */
  72     if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
  73         error("encrypted file, not yet supported.\n");
  74         exit_code = ERROR;
  75         return -1;
  76     }
  77 
  78     /* Save flags for unzip() */
  79     extended = (h[LOCFLG] & EXTFLG) != 0;
  80     pkzip = 1;
  81 
  82     /* Get ofname and time stamp from local header (to be done) */
  83     return 0;
  84 }
  85 
  86 /* ===========================================================================
  87  * Unzip in to out.  This routine works on both gzip and pkzip files.
  88  *
  89  * IN assertions: the buffer inbuf contains already the beginning of
  90  *   the compressed data, from offsets inptr to insize-1 included.
  91  *   The magic header has already been checked. The output buffer is cleared.
  92  */
  93 void unzip(in, out)
     /* [previous][next][first][last][top][bottom][index][help] */
  94     int in, out;   /* input and output file descriptors */
  95 {
  96     ulg orig_crc = 0;       /* original crc */
  97     ulg orig_len = 0;       /* original uncompressed length */
  98     int n;
  99     uch buf[EXTHDR];        /* extended local header */
 100 
 101     /* ifd = in;
 102     ofd = out; */
 103 
 104     updcrc(NULL, 0);           /* initialize crc */
 105 
 106     if (pkzip && !extended) {  /* crc and length at the end otherwise */
 107         orig_crc = LG(inbuf + LOCCRC);
 108         orig_len = LG(inbuf + LOCLEN);
 109     }
 110 
 111     /* Decompress */
 112     if (method == DEFLATED)  {
 113 
 114         int res = inflate();
 115 
 116         if (res == 3) {
 117             error("out of memory");
 118         } else if (res != 0) {
 119             error("invalid compressed data--format violated");
 120         }
 121 
 122     } else if (pkzip && method == STORED) {
 123 
 124         register ulg n = LG(inbuf + LOCLEN);
 125 
 126         if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
 127 
 128             error("invalid compressed data--length mismatch");
 129         }
 130         while (n--) {
 131             uch c = (uch)get_byte();
 132 #ifdef CRYPT
 133             if (decrypt) zdecode(c);
 134 #endif
 135             if (!test) put_char(c);
 136         }
 137     } else {
 138         error("internal error, invalid method");
 139     }
 140 
 141     /* Get the crc and original length */
 142     if (!pkzip) {
 143         /* crc32  (see algorithm.doc)
 144          * uncompressed input size modulo 2^32
 145          */
 146         for (n = 0; n < 8; n++) {
 147             buf[n] = (uch)get_byte(); /* may cause an error if EOF */
 148         }
 149         orig_crc = LG(buf);
 150         orig_len = LG(buf+4);
 151 
 152     } else if (extended) {  /* If extended header, check it */
 153         /* signature - 4bytes: 0x50 0x4b 0x07 0x08
 154          * CRC-32 value
 155          * compressed size 4-bytes
 156          * uncompressed size 4-bytes
 157          */
 158         for (n = 0; n < EXTHDR; n++) {
 159             buf[n] = (uch)get_byte(); /* may cause an error if EOF */
 160         }
 161         orig_crc = LG(buf+4);
 162         orig_len = LG(buf+12);
 163     }
 164 
 165     /* Validate decompression */
 166     if (orig_crc != updcrc(outbuf, 0)) {
 167         error("invalid compressed data--crc error");
 168     }
 169     if (orig_len != bytes_out) {
 170         error("invalid compressed data--length error");
 171     }
 172 
 173     /* Check if there are more entries in a pkzip file */
 174     if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
 175             error("zip file has more than one entry");
 176     }
 177     extended = pkzip = 0; /* for next file */
 178 }

/* [previous][next][first][last][top][bottom][index][help] */