home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 January / VPR0101A.BIN / OLS / TAR32053 / tar32053.exe / SRC / UNZIP.C < prev    next >
C/C++ Source or Header  |  1999-05-23  |  5KB  |  154 lines

  1. #ifndef __DEFCONF_H
  2. #include "defconf.h"
  3. #endif
  4. /*
  5.    This file was hacked for kmtar for WIN32
  6.                                     at 1996-05-06.
  7.                                     by tantan SGL00213@niftyserve.or.jp 
  8. */
  9.  
  10. /* unzip.c -- decompress files in gzip or pkzip format.
  11.  * Copyright (C) 1992-1993 Jean-loup Gailly
  12.  * This is free software; you can redistribute it and/or modify it under the
  13.  * terms of the GNU General Public License, see the file COPYING.
  14.  *
  15.  * The code in this file is derived from the file funzip.c written
  16.  * and put in the public domain by Mark Adler.
  17.  */
  18.  
  19. /*
  20.    This version can extract files in gzip or pkzip format.
  21.    For the latter, only the first entry is extracted, and it has to be
  22.    either deflated or stored.
  23.  */
  24.  
  25. #ifdef RCSID
  26. static char rcsid[] = "$Id: unzip.c,v 0.13 1993/06/10 13:29:00 jloup Exp $";
  27. #endif
  28.  
  29. #include "tailor.h"
  30. #include "gzip.h"
  31. #include "misc.h"
  32. #include "main.h"    /* for Exitcode */
  33. #include "tar32.h"    /* for ERROR constance */
  34.  
  35. /* PKZIP header definitions */
  36. #define LOCSIG 0x04034b50L      /* four-byte lead-in (lsb first) */
  37. #define LOCFLG 6                /* offset of bit flag */
  38. #define  CRPFLG 1               /*  bit for encrypted entry */
  39. #define  EXTFLG 8               /*  bit for extended local header */
  40. #define LOCHOW 8                /* offset of compression method */
  41. #define LOCTIM 10               /* file mod time (for decryption) */
  42. #define LOCCRC 14               /* offset of crc */
  43. #define LOCSIZ 18               /* offset of compressed size */
  44. #define LOCLEN 22               /* offset of uncompressed length */
  45. #define LOCFIL 26               /* offset of file name field length */
  46. #define LOCEXT 28               /* offset of extra field length */
  47. #define LOCHDR 30               /* size of local header, including sig */
  48. #define EXTHDR 16               /* size of extended local header, inc sig */
  49.  
  50.  
  51. /* Globals */
  52.  
  53. int decrypt;        /* flag to turn on decryption */
  54. char *key;          /* not used--needed to link crypt.c */
  55. int pkzip = 0;      /* set for a pkzip file */
  56. int ext_header = 0; /* set if extended local header */
  57.  
  58.  
  59. /* ===========================================================================
  60.  * Unzip in to out.  This routine works on both gzip and pkzip files.
  61.  *
  62.  * IN assertions: the buffer inbuf contains already the beginning of
  63.  *   the compressed data, from offsets inptr to insize-1 included.
  64.  *   The magic header has already been checked. The output buffer is cleared.
  65.  */
  66. static int entry_flag=0;   /* count entry number */
  67. int unzip(void)
  68. {
  69.     ulg orig_crc = 0;       /* original crc */
  70.     ulg orig_len = 0;       /* original uncompressed length */
  71.     int n,res,e;
  72.     uch buf[EXTHDR];        /* extended local header */
  73.     uch outbuf[4];
  74.     extern void inflate_init(void);
  75.     extern int _inflate(int *e);
  76.     extern void inflate_end(void);
  77.  
  78. #if    0
  79.     if (pkzip && !ext_header) {  /* crc and length at the end otherwise */
  80.     orig_crc = LG(inbuf + LOCCRC);
  81.     orig_len = LG(inbuf + LOCLEN);
  82.     }
  83. #endif
  84.  
  85.     if (method == -1 && entry_flag == -1)
  86.         return EOF;
  87.  
  88.     /* Decompress */
  89.     if (method != DEFLATED) 
  90.     fatal("unzip","internal error, invalid method");
  91.  
  92.      if (entry_flag == 0){
  93.         updcrc(NULL, 0);           /* initialize crc */
  94.          inflate_init();
  95.          entry_flag = 1;
  96.     }
  97.     
  98.     if (entry_flag == -1)
  99.         inflate_end();
  100.     else{
  101.         res = _inflate(&e);
  102.         if (res == 3)
  103.             fatal("unzip","out of memory");
  104.         else if (res != 0) 
  105.             fatal("unzip","invalid compressed data--format violated");
  106.  
  107.         if (!e)
  108.             return OK;
  109.            entry_flag = -1;
  110.            return OK;
  111.             
  112.     }
  113.  
  114.  
  115.     /* Get the crc and original length */
  116.     if (!pkzip) {
  117.         /* crc32  (see algorithm.doc)
  118.      * uncompressed input size modulo 2^32
  119.          */
  120.         int ret;
  121.  
  122.         for (n = 0; n < 8; n++) {
  123.             ret = get_byte();
  124.             buf[n] = (uch)ret;
  125.             /* buf[n] = (uch)get_byte();*/ /* may cause an error if EOF */
  126.         }
  127.         if(ret == EOF){
  128.             Exitcode = ERROR_CANNOT_READ;
  129.             return EOF;
  130.         }
  131.  
  132.         orig_crc = LG(buf);
  133.         orig_len = LG(buf+4);
  134.         
  135.     }
  136.  
  137.     /* Validate decompression */
  138.     if (orig_crc != updcrc(outbuf, 0)) {
  139.     fatal("unzip","invalid compressed data--crc error");
  140.     }
  141.     if (orig_len != (ulg)bytes_out) {
  142.     fatal("unzip","invalid compressed data--length error");
  143.     }
  144.  
  145.     ext_header = pkzip = 0; /* for next file */
  146.     return OK;
  147. }
  148.  
  149. void init_unzip(void)
  150. {
  151.     clear_bufs();
  152.     entry_flag=0;   /* count entry number */
  153. }
  154.