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

  1. #ifndef __DEFCONF_H
  2. #include "defconf.h"
  3. #endif
  4. /*
  5.  *  Gzip.c for kmtar Power Up Kit v3.x
  6.  *  This file made from orignal gzip.c
  7.  *   
  8.             at first 1993-08-08 
  9.                last  1995-03-04
  10.             tantan     SGL00213@niftyserve.or.jp
  11.  */
  12. #include    <stdio.h>
  13. #include    <io.h>
  14. #ifdef    __TURBOC__
  15. #include    <string.h>
  16. #endif
  17. #include    "gzip.h"
  18. #include    "lzw.h"
  19. #include    "tailor.h"
  20. #include    "archio.h"
  21. #include    "misc.h"
  22. #include    "compress.h"
  23. #ifdef    WIN32
  24.   #include    <conio.h>
  25. #endif
  26.  
  27.  
  28.         /* local variables */
  29.  
  30.  
  31. int force = 0;        /* */
  32. int no_name = 0;      /* don't save or restore the original file name */
  33. int recursive = 0;    /* recurse through directories (-r) */
  34. int list = 0;         /* list the file contents (-l) */
  35. int verbose = 0;      /* be verbose (-v) */
  36. int quiet = 0;        /* be very quiet (-q) */
  37. int do_lzw = 0;       /* generate output compatible with old compress (-Z) */
  38. int test = 0;         /* test .gz file integrity */
  39. int foreground;       /* set if program run in foreground */
  40. char *progname;       /* program name */
  41. int maxbits = BITS;   /* max bits per code for LZW */
  42. int method = DEFLATED;/* compression method */
  43. int level = 6;        /* compression level */
  44. int exit_code = OK;   /* program exit code */
  45. int save_orig_name=0;   /* set if original name must be saved */
  46. int last_member;      /* set for .zip and .Z files */
  47. int part_nb;          /* number of parts in .gz file */
  48. long gzip_time_stamp;       /* original time stamp (modification time) */
  49.  
  50. char gzip_filename[MAX_PATH_LEN];
  51. int gzip_ostype;
  52.  
  53. long ifile_size;      /* input file size, -1 for devices (debug only) */
  54.  
  55. char *env;            /* contents of GZIP env variable */
  56. char **args = NULL;   /* argv pointer if GZIP env variable defined */
  57. char z_suffix[MAX_SUFFIX+1]; /* default suffix (can be set with --suffix) */
  58. int  z_len;           /* strlen(z_suffix) */
  59.  
  60. long bytes_in;             /* number of input bytes */
  61. long bytes_out;            /* number of output bytes */
  62. long total_in = 0;         /* input bytes for all files */
  63. long total_out = 0;        /* output bytes for all files */
  64. /*char ifname[MAX_PATH_LEN];  input file name */
  65. /*char ofname[MAX_PATH_LEN];  output file name */
  66. int  remove_ofname = 0;       /* remove output file on error */
  67. /*struct stat istat;         status for input file */
  68. int  ifd;                  /* input file descriptor */
  69. int  ofd;                  /* output file descriptor */
  70. unsigned insize;           /* valid bytes in inbuf */
  71. unsigned inptr;            /* index of next byte to be processed in inbuf */
  72. unsigned outcnt;           /* bytes in output buffer */
  73.  
  74. DECLARE(uch, inbuf,  INBUFSIZ +INBUF_EXTRA);
  75. DECLARE(uch, window, 2L*WSIZE);
  76.  
  77. /*unsigned char *window = NULL;*/ /**/ 
  78. /*uch *inbuf=NULL;  */ /* input buffer */
  79. long header_bytes=0;        /**/
  80. int uncompress();
  81. static char eof_flag=0;
  82. long sfx_header_length;
  83.  
  84. int (*work) OF((int infile, int outfile)); /* function to call */
  85.  
  86. int get_new_file(char *fname)
  87. {
  88.     fprintf(stderr,"\n\aExchange FD for new file %s\n",fname);
  89.     fprintf(stderr," <<<<<<<< Hit anykey >>>>>>>\n");
  90.     getch();
  91.     if ((ifd = open_dev(fname,'r')) == -1){
  92.         printf("file %s dose not exist\n",fname);
  93.         return EOF;
  94.     }
  95.     return OK;
  96. }
  97.  
  98. #define unget_byte()    (inptr > 0 ? inptr--:0)
  99.  
  100. /* get_byteの内部変数*/
  101. static int get_byte_divid = -1,get_byte_filenumber;
  102. static char *get_byte_inner_fname;
  103. int    get_byte(void)
  104. {
  105. /*#define    INSIZE 30000*/
  106. #define    INSIZE INBUFSIZ
  107.  
  108.     extern char *Archives[];
  109.     extern int Afd;
  110.     //static int divid = -1,filenumber;
  111.     //static char *inner_fname;
  112.     int readlen;
  113.     
  114. /*
  115.     if (eof_flag)
  116.         fatal("get_byte","file read error");
  117. */
  118.     if (inbuf == NULL){
  119.         ALLOC(uch,inbuf,INSIZE);
  120.         if (inbuf == NULL)
  121.             fatal("get_byte","Out of memory");
  122.     }
  123.     if (insize == 0 || insize == inptr){
  124.         if (ifd == -1) return EOF;
  125.         if ((readlen = read(ifd,inbuf,INSIZE)) == 0){
  126.             if (readlen < 0)
  127.                 fatal("get_byte","read error\n");
  128.             if (get_byte_divid == -1){
  129.                 if (get_byte_divid = chk_DIVI()){
  130.                     get_byte_filenumber=1;
  131.                     get_byte_inner_fname = strdup(Archives[0]);
  132.                 }
  133.             }
  134.             close(ifd);
  135.             ifd = -1;
  136.             if (get_byte_divid){
  137.                 int ret,size;
  138.                 sprintf(get_byte_inner_fname+strlen(get_byte_inner_fname)-3,
  139.                 "%03d",get_byte_filenumber++);
  140.                 if ((ifd = open_dev(get_byte_inner_fname,'r')) == -1)
  141.                     ret = get_new_file(get_byte_inner_fname);
  142.                     if (ret == EOF)
  143.                         return EOF;
  144.                 size = read(ifd,inbuf,INSIZE);
  145. /*                if (size != INSIZE){
  146.                     fprintf(stderr,"internal error %s too small\n",
  147.                         inner_fname);
  148.                     exit(1);
  149.                 }
  150. */
  151.                 insize = size;
  152.                 Afd = ifd;
  153.             }else{
  154. /*                eof_flag = 1;*/
  155.                 return EOF;
  156.             }
  157.         }else if (readlen == -1){
  158. /*            eof_flag = 1;*/
  159.             return EOF;
  160.         }else
  161.             insize = readlen;
  162.  
  163.         inptr=0;
  164.     }
  165.  
  166.     return inbuf[inptr++];
  167. }
  168. /*
  169.  *  init read buffer
  170.  */
  171. void init_read_buf(void)
  172. {
  173.     extern unsigned long innerbuf_len;
  174.     extern int vm_page,read_page;
  175.     extern void init_unzip(void);
  176.     
  177.     eof_flag=0;
  178.     innerbuf_len=0L;
  179. #ifdef    DYN_ALLOC
  180.     if (inbuf == NULL)
  181.         ALLOC(uch, inbuf,  INBUFSIZ +INBUF_EXTRA);
  182.     if (window == NULL)
  183.         ALLOC(uch, window, 2L*WSIZE);
  184. #endif
  185.     init_comp();
  186.     init_unzip();
  187. }
  188.  
  189. /* ========================================================================
  190.  * Check the magic number of the input file and update ofname if an
  191.  * original name was given and to_stdout is not set.
  192.  * Return the compression method, -1 for error, -2 for warning.
  193.  * Set inptr to the offset of the next byte to be processed.
  194.  * This function may be called repeatedly for an input file consisting
  195.  * of several contiguous gzip'ed members.
  196.  * IN assertions: there is at least one remaining compressed member.
  197.  *   If the member is a zip file, it must be the only one.
  198.  */
  199. int get_method(int in)
  200. /*    int in;      input file descriptor */
  201. {
  202.     uch flags;
  203.     char magic[2]; /* magic header */
  204.  
  205.     sfx_header_length = 0;
  206.     ifd = in; /* local copy of in */
  207.     /* If --force and --stdout, zcat == cat, so do not complain about
  208.      * premature end of file: use try_byte instead of get_byte.
  209.      */
  210.     magic[0] = (char)get_byte();
  211.     magic[1] = (char)get_byte();
  212.  
  213.     /* for kmtarsef.exe 's SFX(Self Extracting) by tsuneo...*/
  214.     if(magic[0]=='M' && magic[1]=='Z'){
  215.         insize=0;
  216.         lseek(ifd,64000,SEEK_SET);
  217.         magic[0]=(char)get_byte();
  218.         magic[1]=(char)get_byte();
  219.         sfx_header_length = 64000;
  220.     }
  221.  
  222. /*    gzip_time_stamp = istat.st_mtime; may be modified later for some methods */
  223.     method = -1;                 /* unknown yet */
  224.     part_nb++;                   /* number of parts in gzip file */
  225.     header_bytes = 0;
  226. /*    last_member = RECORD_IO; */
  227. /* assume multiple members in gzip file except for record oriented I/O */
  228.  
  229.     if (memcmp(magic, GZIP_MAGIC, 2) == 0
  230.         || memcmp(magic, OLD_GZIP_MAGIC, 2) == 0) {
  231.  
  232.         method = (int)get_byte();
  233.         if (method != DEFLATED) {
  234.             fprintf(stderr,
  235.                 "unknown compress method %d -- get newer version of gzip\n",
  236.                 method);
  237.             exit_code = GZIP_ERROR;
  238.             return -1;
  239.         }
  240.         work = unzip;  /* do unzip function */
  241.         flags  = (uch)get_byte();
  242.  
  243.         if ((flags & ENCRYPTED) != 0) {
  244.             fprintf(stderr,
  245.                 "Archive is encrypted -- get newer version of gzip\n");
  246.             exit_code = GZIP_ERROR;
  247.             return -1;
  248.         }
  249.         if ((flags & CONTINUATION) != 0) {
  250.             fprintf(stderr,
  251.            "Archive is a a multi-part gzip file -- get newer version of gzip\n");
  252.             exit_code = GZIP_ERROR;
  253.             if (force <= 1) return -1;
  254.         }
  255.         if ((flags & RESERVED) != 0) {
  256.             fprintf(stderr,
  257.                 "Archive has flags 0x%x -- get newer version of gzip\n",
  258.                 flags);
  259.             exit_code = GZIP_ERROR;
  260.             if (force <= 1) return -1;
  261.         }
  262.         gzip_time_stamp  = (ulg)get_byte();
  263.         gzip_time_stamp |= ((ulg)get_byte()) << 8;
  264.         gzip_time_stamp |= ((ulg)get_byte()) << 16;
  265.         gzip_time_stamp |= ((ulg)get_byte()) << 24;
  266.  
  267.         (void)get_byte();  /* Ignore extra flags for the moment */
  268.         gzip_ostype = get_byte();  /* Ignore OS type for the moment */
  269.  
  270.         if ((flags & CONTINUATION) != 0) {
  271.             unsigned part = (unsigned)get_byte();
  272.             part |= ((unsigned)get_byte())<<8;
  273.             if (verbose) {
  274.             fprintf(stderr," part number %u\n", part);
  275.             }
  276.         }
  277.         if ((flags & EXTRA_FIELD) != 0) {
  278.             unsigned len = (unsigned)get_byte();
  279.             len |= ((unsigned)get_byte())<<8;
  280.             if (verbose) {
  281.             fprintf(stderr,"extra field of %u bytes ignored\n",len);
  282.             }
  283.             while (len--) (void)get_byte();
  284.         }
  285.  
  286.         /* Get original file name if it was truncated */
  287.         if ((flags & ORIG_NAME) != 0) {
  288.             /* Discard the ORIG_NAME */
  289. #if 0
  290.             char c; /* dummy used for NeXTstep 3.0 cc optimizer bug */
  291.             while ((c=get_byte()) != 0) c++;
  292. #else
  293.             char *ptr = gzip_filename;
  294.             while((*ptr=get_byte()) != 0){ptr++;}
  295. #endif
  296.         } /* ORIG_NAME */
  297.  
  298.         /* Discard file comment if any */
  299.         if ((flags & COMMENT) != 0) {
  300.             while (get_char() != 0) /* null */ ;
  301.         }
  302.         if (part_nb == 1) {
  303.             header_bytes = inptr + 2*sizeof(long); /* include crc and size */
  304.         }
  305.  
  306.     }else if (memcmp(magic, LZW_MAGIC, 2) == 0) {
  307.         work = uncompress;
  308.         method = COMPRESSED;
  309.         last_member = 1;
  310.     }else if(memcmp(magic,"BZ",2) == 0){
  311.         if(get_byte() == 'h'
  312.             && get_byte() /* level ('1' - '9')*/
  313.             && get_byte() == '\x31'
  314.             && get_byte() == '\x41'
  315.             && get_byte() == '\x59'
  316.             && get_byte() == '\x26'
  317.             && get_byte() == '\x53'
  318.             && get_byte() == '\x59'
  319.             ){
  320.             method = BZIP2ED;
  321.         }else{
  322.             method = NO_COMP;
  323.         }
  324.     }else {
  325.         method = NO_COMP;
  326.     }
  327. #if 0    
  328.     else if (memcmp(magic, PKZIP_MAGIC, 2) == 0 && inptr == 2
  329.         && memcmp((char*)inbuf, PKZIP_MAGIC, 4) == 0) {
  330.     /* To simplify the code, we support a zip file when alone only.
  331.      * We are thus guaranteed that the entire local header fits in inbuf.
  332.      */
  333.     inptr = 0;
  334.     work = unzip;
  335.     if (check_zipfile(in) != OK) return -1;
  336.     /* check_zipfile may get ofname from the local header */
  337.     last_member = 1;
  338.  
  339.     } else if (memcmp(magic, PACK_MAGIC, 2) == 0) {
  340.     work = unpack;
  341.     method = PACKED;
  342.  
  343.     }
  344.      else if (memcmp(magic, LZW_MAGIC, 2) == 0) {
  345.     work = unlzw;
  346.     method = COMPRESSED;
  347.     last_member = 1;
  348.  
  349.     } else if (memcmp(magic, LZH_MAGIC, 2) == 0) {
  350.     work = unlzh;
  351.     method = LZHED;
  352.     last_member = 1;
  353.  
  354.     }
  355. #endif
  356. /*
  357.     if (method != DEFLATED){
  358.         exit_code = GZIP_ERROR;
  359.         return -1;
  360.     }
  361. */
  362.     if (method >= 0) return method;
  363.  
  364. }
  365. #ifdef DLL
  366. void gzip_static_init(void)
  367. {
  368.     force = 0;        /* */
  369.     no_name = 0;      /* don't save or restore the original file name */
  370.     recursive = 0;    /* recurse through directories (-r) */
  371.     list = 0;         /* list the file contents (-l) */
  372.     verbose = 0;      /* be verbose (-v) */
  373.     quiet = 0;        /* be very quiet (-q) */
  374.     do_lzw = 0;       /* generate output compatible with old compress (-Z) */
  375.     test = 0;         /* test .gz file integrity */
  376.     foreground=0;       /* set if program run in foreground */
  377.     progname=NULL;       /* program name */
  378.     maxbits = BITS;   /* max bits per code for LZW */
  379.     method = DEFLATED;/* compression method */
  380.     level = 6;        /* compression level */
  381.     exit_code = OK;   /* program exit code */
  382.     save_orig_name=0;   /* set if original name must be saved */
  383.     last_member=0;      /* set for .zip and .Z files */
  384.     part_nb=0;          /* number of parts in .gz file */
  385.     gzip_time_stamp=0;       /* original time stamp (modification time) */
  386.  
  387.     memset(gzip_filename,0,sizeof(gzip_filename));
  388.     gzip_ostype = -1;
  389.     
  390.     ifile_size=0;      /* input file size, -1 for devices (debug only) */
  391.     env=NULL;            /* contents of GZIP env variable */
  392.     args = NULL;   /* argv pointer if GZIP env variable defined */
  393.     memset(z_suffix,0,sizeof(char)*(MAX_SUFFIX+1)); /* default suffix (can be set with --suffix) */
  394.     z_len=0;           /* strlen(z_suffix) */
  395.  
  396.     bytes_in=0;             /* number of input bytes */
  397.     bytes_out=0;            /* number of output bytes */
  398.     total_in = 0;         /* input bytes for all files */
  399.     total_out = 0;        /* output bytes for all files */
  400.     /*char ifname[MAX_PATH_LEN];  input file name */
  401.     /*char ofname[MAX_PATH_LEN];  output file name */
  402.     remove_ofname = 0;       /* remove output file on error */
  403.     /*struct stat istat;         status for input file */
  404.     ifd=0;                  /* input file descriptor */
  405.     ofd=0;                  /* output file descriptor */
  406.     insize=0;           /* valid bytes in inbuf */
  407.     inptr=0;            /* index of next byte to be processed in inbuf */
  408.     outcnt=0;           /* bytes in output buffer */
  409.  
  410.     //DECLARE(uch, inbuf,  INBUFSIZ +INBUF_EXTRA);
  411.     //DECLARE(uch, window, 2L*WSIZE);
  412. #ifdef DYN_ALLOC
  413.     inbuf=NULL;
  414.     window=NULL;
  415. #else
  416.     memset(inbuf,0,INBUFSIZ+INBUF_EXTRA);
  417.     memset(window,0,2L*WSIZE);
  418. #endif
  419.     /*unsigned char *window = NULL;*/ /**/ 
  420.     /*uch *inbuf=NULL;  */ /* input buffer */
  421.     header_bytes=0;        /**/
  422.     //int uncompress();
  423.     eof_flag=0;
  424.  
  425.     // int (*work) OF((int infile, int outfile)); /* function to call */
  426.     work=NULL;
  427.  
  428.     get_byte_divid = -1,get_byte_filenumber=0;
  429.     get_byte_inner_fname=NULL;
  430.  
  431. }
  432. #endif /* DLL */