Archive-Unzip-Burst

 view release on metacpan or  search on metacpan

unzip-6.0/funzip.c  view on Meta::CPAN

/*
  Copyright (c) 1990-2009 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 2009-Jan-02 or later
  (the contents of which are also included in unzip.h) for terms of use.
  If, for some reason, all these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/* funzip.c -- by Mark Adler */

#define VERSION "3.95 of 20 January 2009"


/* Copyright history:
   - Starting with UnZip 5.41 of 16-April-2000, this source file
     is covered by the Info-Zip LICENSE cited above.
   - Prior versions of this source file, found in UnZip source packages
     up to UnZip 5.40, were put in the public domain.
     The original copyright note by Mark Adler was:
         "You can do whatever you like with this source file,
         though I would prefer that if you modify it and
         redistribute it that you include comments to that effect
         with your name and the date.  Thank you."

   History:
   vers     date          who           what
   ----   ---------  --------------  ------------------------------------
   1.0    13 Aug 92  M. Adler        really simple unzip filter.
   1.1    13 Aug 92  M. Adler        cleaned up somewhat, give help if
                                     stdin not redirected, warn if more
                                     zip file entries after the first.
   1.2    15 Aug 92  M. Adler        added check of lengths for stored
                                     entries, added more help.
   1.3    16 Aug 92  M. Adler        removed redundant #define's, added
                                     decryption.
   1.4    27 Aug 92  G. Roelofs      added exit(0).
   1.5     1 Sep 92  K. U. Rommel    changed read/write modes for OS/2.
   1.6     6 Sep 92  G. Roelofs      modified to use dummy crypt.c and
                                     crypt.h instead of -DCRYPT.
   1.7    23 Sep 92  G. Roelofs      changed to use DOS_OS2; included
                                     crypt.c under MS-DOS.
   1.8     9 Oct 92  M. Adler        improved inflation error msgs.
   1.9    17 Oct 92  G. Roelofs      changed ULONG/UWORD/byte to ulg/ush/uch;
                                     renamed inflate_entry() to inflate();
                                     adapted to use new, in-place zdecode.
   2.0    22 Oct 92  M. Adler        allow filename argument, prompt for
                                     passwords and don't echo, still allow
                                     command-line password entry, but as an
                                     option.
   2.1    23 Oct 92  J-l. Gailly     fixed crypt/store bug,
                     G. Roelofs      removed crypt.c under MS-DOS, fixed
                                     decryption check to compare single byte.
   2.2    28 Oct 92  G. Roelofs      removed declaration of key.
   2.3    14 Dec 92  M. Adler        replaced fseek (fails on stdin for SCO
                                     Unix V.3.2.4).  added quietflg for
                                     inflate.c.
   3.0    11 May 93  M. Adler        added gzip support
   3.1     9 Jul 93  K. U. Rommel    fixed OS/2 pipe bug (PIPE_ERROR)
   3.2     4 Sep 93  G. Roelofs      moved crc_32_tab[] to tables.h; used FOPx
                                     from unzip.h; nuked OUTB macro and outbuf;
                                     replaced flush(); inlined FlushOutput();
                                     renamed decrypt to encrypted
   3.3    29 Sep 93  G. Roelofs      replaced ReadByte() with NEXTBYTE macro;
                                     revised (restored?) flush(); added FUNZIP
   3.4    21 Oct 93  G. Roelofs      renamed quietflg to qflag; changed outcnt,
                     H. Gessau       second updcrc() arg and flush() arg to ulg;
                                     added inflate_free(); added "g =" to null
                                     getc(in) to avoid compiler warnings
   3.5    31 Oct 93  H. Gessau       changed DOS_OS2 to DOS_NT_OS2
   3.6     6 Dec 93  H. Gessau       added "near" to mask_bits[]
   3.7     9 Dec 93  G. Roelofs      added extent typecasts to fwrite() checks
   3.8    28 Jan 94  GRR/JlG         initialized g variable in main() for gcc
   3.81   22 Feb 94  M. Hanning-Lee  corrected usage message
   3.82   27 Feb 94  G. Roelofs      added some typecasts to avoid warnings
   3.83   22 Jul 94  G. Roelofs      changed fprintf to macro for DLLs
    -      2 Aug 94  -               public release with UnZip 5.11
    -     28 Aug 94  -               public release with UnZip 5.12
   3.84    1 Oct 94  K. U. Rommel    changes for Metaware High C
   3.85   29 Oct 94  G. Roelofs      changed fprintf macro to Info
   3.86    7 May 95  K. Davis        RISCOS patches;
                     P. Kienitz      Amiga patches
   3.87   12 Aug 95  G. Roelofs      inflate_free(), DESTROYGLOBALS fixes
   3.88    4 Sep 95  C. Spieler      reordered macro to work around MSC 5.1 bug
   3.89   22 Nov 95  PK/CS           ifdef'd out updcrc() for ASM_CRC
   3.9    17 Dec 95  G. Roelofs      modified for USE_ZLIB (new fillinbuf())
    -     30 Apr 96  -               public release with UnZip 5.2
   3.91   17 Aug 96  G. Roelofs      main() -> return int (Peter Seebach)
   3.92   13 Apr 97  G. Roelofs      minor cosmetic fixes to messages
    -     22 Apr 97  -               public release with UnZip 5.3
    -     31 May 97  -               public release with UnZip 5.31
   3.93   20 Sep 97  G. Roelofs      minor cosmetic fixes to messages
    -      3 Nov 97  -               public release with UnZip 5.32
    -     28 Nov 98  -               public release with UnZip 5.4
    -     16 Apr 00  -               public release with UnZip 5.41
    -     14 Jan 01  -               public release with UnZip 5.42
   3.94   20 Feb 01  C. Spieler      added support for Deflate64(tm)
          23 Mar 02  C. Spieler      changed mask_bits[] type to "unsigned"
 */


/*

   All funzip does is take a zipfile from stdin and decompress the
   first entry to stdout.  The entry has to be either deflated or
   stored.  If the entry is encrypted, then the decryption password
   must be supplied on the command line as the first argument.

unzip-6.0/funzip.c  view on Meta::CPAN

#else
  if ((out = fdopen(1, FOPW)) == (FILE *)NULL)
    err(2, "cannot write to stdout");
#endif

  /* read local header, check validity, and skip name and extra fields */
  n = getc(G.in);  n |= getc(G.in) << 8;
  if (n == ZIPMAG)
  {
    if (fread((char *)h, 1, LOCHDR, G.in) != LOCHDR || SH(h) != LOCREM)
      err(3, "invalid zipfile");
    switch (method = SH(h + LOCHOW)) {
      case STORED:
      case DEFLATED:
#ifdef USE_DEFLATE64
      case ENHDEFLATED:
#endif
        break;
      default:
        err(3, "first entry not deflated or stored--cannot unpack");
        break;
    }
    for (n = SH(h + LOCFIL); n--; ) g = getc(G.in);
    for (n = SH(h + LOCEXT); n--; ) g = getc(G.in);
    g = 0;
    encrypted = h[LOCFLG] & CRPFLG;
  }
  else if (n == GZPMAG)
  {
    if (fread((char *)h, 1, GZPHDR, G.in) != GZPHDR)
      err(3, "invalid gzip file");
    if ((method = h[GZPHOW]) != DEFLATED && method != ENHDEFLATED)
      err(3, "gzip file not deflated");
    if (h[GZPFLG] & GZPMUL)
      err(3, "cannot handle multi-part gzip files");
    if (h[GZPFLG] & GZPISX)
    {
      n = getc(G.in);  n |= getc(G.in) << 8;
      while (n--) g = getc(G.in);
    }
    if (h[GZPFLG] & GZPISF)
      while ((g = getc(G.in)) != 0 && g != EOF) ;
    if (h[GZPFLG] & GZPISC)
      while ((g = getc(G.in)) != 0 && g != EOF) ;
    g = 1;
    encrypted = h[GZPFLG] & GZPISE;
  }
  else
    err(3, "input not a zip or gzip file");

  /* if entry encrypted, decrypt and validate encryption header */
  if (encrypted)
#if CRYPT
    {
      ush i, e;

      if (p == (char *)NULL) {
        if ((p = (char *)malloc(IZ_PWLEN+1)) == (char *)NULL)
          err(1, "out of memory");
        else if ((p = getp("Enter password: ", p, IZ_PWLEN+1)) == (char *)NULL)
          err(1, "no tty to prompt for password");
      }
      /* initialize crc_32_tab pointer for decryption */
      CRC_32_TAB = get_crc_table();
      /* prepare the decryption keys for extraction and check the password */
      init_keys(p);
      for (i = 0; i < RAND_HEAD_LEN; i++)
        e = NEXTBYTE;
      if (e != (ush)(h[LOCFLG] & EXTFLG ? h[LOCTIM + 1] : h[LOCCRC + 3]))
        err(3, "incorrect password for first entry");
    }
#else /* !CRYPT */
    err(3, "cannot decrypt entry (need to recompile with full crypt.c)");
#endif /* ?CRYPT */

  /* prepare output buffer and crc */
  G.outptr = slide;
  G.outcnt = 0L;
  outsiz = 0L;
  G.crc32val = CRCVAL_INITIAL;

  /* decompress */
  if (g || h[LOCHOW])
  {                             /* deflated entry */
    int r;

#ifdef USE_ZLIB
    /* need to allocate and prepare input buffer */
    if ((G.inbuf = (uch *)malloc(INBUFSIZ)) == (uch *)NULL)
       err(1, "out of memory");
#endif /* USE_ZLIB */
    if ((r = UZinflate(__G__ (method == ENHDEFLATED))) != 0) {
      if (r == 3)
        err(1, "out of memory");
      else
        err(4, "invalid compressed data--format violated");
    }
    inflate_free(__G);
  }
  else
  {                             /* stored entry */
    register ulg n;

    n = LG(h + LOCLEN);
#if CRYPT
    if (n != LG(h + LOCSIZ) - (encrypted ? RAND_HEAD_LEN : 0)) {
#else
    if (n != LG(h + LOCSIZ)) {
#endif
      Info(slide, 1, ((char *)slide, "len %ld, siz %ld\n", n, LG(h + LOCSIZ)));
      err(4, "invalid compressed data--length mismatch");
    }
    while (n--) {
      ush c = getc(G.in);
#if CRYPT
      if (encrypted)
        zdecode(c);
#endif
      *G.outptr++ = (uch)c;
#if (defined(USE_DEFLATE64) && defined(__16BIT__))
      if (++G.outcnt == (WSIZE>>1))     /* do FlushOutput() */



( run in 1.157 second using v1.01-cache-2.11-cpan-6aa56a78535 )