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

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