home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / utils / djtar / zmethod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-16  |  6.9 KB  |  212 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* zmethod -- detect compressed tar files for on-the-fly decompression.
  3.  *
  4.  * Derived from GNU Zip program, Copyright (C) 1992-1993 Jean-loup Gailly
  5.  * The unzip code was written and put in the public domain by Mark Adler.
  6.  * Portions of the lzw code are derived from the public domain 'compress'
  7.  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  8.  * Ken Turkowski, Dave Mack and Peter Jannesen.
  9.  *
  10.  * Adaptation to DJTAR program: Eli Zaretskii, November 1995
  11.  */
  12.  
  13. /* This software is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY.  No author or distributor accepts
  15.    responsibility to anyone for the consequences of using it or for
  16.    whether it serves any particular purpose or works at all, unless he
  17.    says so in writing.  Refer to the GNU General Public License
  18.    for full details.
  19.  
  20.    Everyone is granted permission to copy, modify and redistribute
  21.    this software, but only under the conditions described in the GNU
  22.    General Public License.   A copy of this license is supposed to
  23.    have been given to you along with this software so you can know your
  24.    rights and responsibilities.  It should be in a file named COPYING.
  25.    Among other things, the copyright notice and this notice must be
  26.    preserved on all copies.  */
  27.  
  28.  
  29. #include "tailor.h"
  30. #include "zread.h"
  31. #include "lzw.h"
  32.  
  33.         /* global buffers */
  34.  
  35. uch inbuf[INBUFSIZ + INBUF_EXTRA];
  36. uch outbuf[OUTBUFSIZ+OUTBUF_EXTRA];
  37. ush d_buf[DIST_BUFSIZE];
  38. uch window[2L*WSIZE];
  39. ush tab_prefix[1L<<BITS];
  40.  
  41.  
  42.         /* local variables */
  43.  
  44. int test = 0;         /* test .gz file integrity */
  45. char *progname;       /* program name */
  46. int maxbits = BITS;   /* max bits per code for LZW */
  47. int method = DEFLATED;/* compression method */
  48. int level = 6;        /* compression level */
  49. int exit_code = OK;   /* program exit code */
  50. char *ifname;         /* input file name */
  51. void *ifd;                 /* input file descriptor */
  52. unsigned insize;           /* valid bytes in inbuf */
  53. unsigned inptr;            /* index of next byte to be processed in inbuf */
  54. unsigned outcnt;           /* bytes in output buffer */
  55. int (*decompressor)(void *) = unzip; /* function to call */
  56. long header_bytes, bytes_out;
  57.  
  58. /* ========================================================================
  59.  * Check the magic number of the input file and update ofname if an
  60.  * original name was given and to_stdout is not set.
  61.  * Return the compression method, -1 for error, -2 for warning.
  62.  * Set inptr to the offset of the next byte to be processed.
  63.  * Updates time_stamp if there is one and --no-time is not used.
  64.  * This function may be called repeatedly for an input file consisting
  65.  * of several contiguous gzip'ed members.
  66.  * IN assertions: there is at least one remaining compressed member.
  67.  *   If the member is a zip file, it must be the only one.
  68.  */
  69.  
  70. int last_member;
  71.  
  72. int
  73. get_method(void *in)
  74. {
  75.     uch flags;     /* compression flags */
  76.     char magic[2]; /* magic header */
  77.     ulg stamp;     /* time stamp */
  78.  
  79.     ifd = in;
  80.     magic[0] = (char)get_byte();
  81.     magic[1] = (char)get_byte();
  82.  
  83.     method = -1;                 /* unknown yet */
  84.     part_nb++;                   /* number of parts in gzip file */
  85.     header_bytes = 0;
  86.     last_member = RECORD_IO;
  87.     /* assume multiple members in gzip file except for record oriented I/O */
  88.  
  89.     if (memcmp(magic, GZIP_MAGIC, 2) == 0
  90.         || memcmp(magic, OLD_GZIP_MAGIC, 2) == 0) {
  91.  
  92.     method = (int)get_byte();
  93.     if (method != DEFLATED) {
  94.         fprintf(log_out,
  95.        "%s: %s: unknown method %d -- get newer version of zread\n",
  96.             progname, ifname, method);
  97.         exit_code = ERROR;
  98.         return -1;
  99.     }
  100.     decompressor = unzip;
  101.     flags  = (uch)get_byte();
  102.  
  103.     if ((flags & ENCRYPTED) != 0) {
  104.         fprintf(log_out,
  105.             "%s: %s is encrypted -- get newer version of zread\n",
  106.             progname, ifname);
  107.         exit_code = ERROR;
  108.         return -1;
  109.     }
  110.     if ((flags & CONTINUATION) != 0) {
  111.         fprintf(log_out,
  112.        "%s: %s is a a multi-part gzip file -- get newer version of zread\n",
  113.             progname, ifname);
  114.         exit_code = ERROR;
  115.     }
  116.     if ((flags & RESERVED) != 0) {
  117.         fprintf(log_out,
  118.             "%s: %s has flags 0x%x -- get newer version of zread\n",
  119.             progname, ifname, flags);
  120.         exit_code = ERROR;
  121.     }
  122.     stamp  = (ulg)get_byte();
  123.     stamp |= ((ulg)get_byte()) << 8;
  124.     stamp |= ((ulg)get_byte()) << 16;
  125.     stamp |= ((ulg)get_byte()) << 24;
  126.  
  127.     (void)get_byte();  /* Ignore extra flags for the moment */
  128.     (void)get_byte();  /* Ignore OS type for the moment */
  129.  
  130.     if ((flags & CONTINUATION) != 0) {
  131.         unsigned part = (unsigned)get_byte();
  132.         part |= ((unsigned)get_byte())<<8;
  133.         if (v_switch) {
  134.         fprintf(log_out,"%s: %s: part number %u\n",
  135.             progname, ifname, part);
  136.         }
  137.     }
  138.     if ((flags & EXTRA_FIELD) != 0) {
  139.         unsigned len = (unsigned)get_byte();
  140.         len |= ((unsigned)get_byte())<<8;
  141.         if (v_switch) {
  142.         fprintf(log_out,"%s: %s: extra field of %u bytes ignored\n",
  143.             progname, ifname, len);
  144.         }
  145.         while (len--) (void)get_byte();
  146.     }
  147.  
  148.     /* Get original file name if it was truncated */
  149.     if ((flags & ORIG_NAME) != 0) {
  150.         char orig_name[MAX_PATH_LEN], *p = orig_name;
  151.         for (;;) {
  152.         *p = (char)get_byte();
  153.         if (*p++ == '\0') break;
  154.         if (p >= orig_name + sizeof(orig_name)) {
  155.             error("corrupted input -- file name too large");
  156.         }
  157.         }
  158.     } /* ORIG_NAME */
  159.  
  160.     /* Discard file comment if any */
  161.     if ((flags & COMMENT) != 0) {
  162.         while (get_char() != 0) /* null */ ;
  163.     }
  164.     if (part_nb == 1) {
  165.         header_bytes = inptr + 2*sizeof(long); /* include crc and size */
  166.     }
  167.  
  168.     } else if (memcmp(magic, PKZIP_MAGIC, 2) == 0 && inptr == 2
  169.         && memcmp((char*)inbuf, PKZIP_MAGIC, 4) == 0) {
  170.     /* To simplify the code, we support a zip file when alone only.
  171.          * We are thus guaranteed that the entire local header fits in inbuf.
  172.          */
  173.         inptr = 0;
  174.     decompressor = unzip;
  175.     if (check_zipfile() != OK) return -1;
  176.     /* check_zipfile may get ofname from the local header */
  177.     last_member = 1;
  178.  
  179.     } else if (memcmp(magic, PACK_MAGIC, 2) == 0) {
  180.     decompressor = unpack;
  181.     method = PACKED;
  182.  
  183.     } else if (memcmp(magic, LZW_MAGIC, 2) == 0) {
  184.     decompressor = unlzw;
  185.     method = COMPRESSED;
  186.     last_member = 1;
  187.  
  188.     } else if (memcmp(magic, LZH_MAGIC, 2) == 0) {
  189.     decompressor = unlzh;
  190.     method = LZHED;
  191.     last_member = 1;
  192.  
  193.     } else if (part_nb == 1) { /* pass input unchanged by default */
  194.     method = STORED;
  195.     decompressor = copy;
  196.         inptr = 0;
  197.     last_member = 1;
  198.     }
  199.     if (method >= 0) return method;
  200.  
  201.     if (part_nb == 1 && z_switch) {
  202.     fprintf(log_out, "\n%s: %s: not in gzip format\n", progname, ifname);
  203.     exit_code = ERROR;
  204.     return -1;
  205.     } else {
  206.     WARN((log_out, "\n%s: %s: decompression OK, trailing garbage ignored\n",
  207.           progname, ifname));
  208.     return -2;
  209.     }
  210. }
  211.  
  212.