home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 January / VPR0101A.BIN / OLS / TAR32053 / tar32053.exe / SRC / COMPRESS.C < prev    next >
C/C++ Source or Header  |  1999-05-23  |  12KB  |  263 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. /*@H************************ < COMPRESS utility> ****************************
  10. *   $@(#) compress.c,v 4.3d 90/01/18 03:00:00 don Release ^                 *
  11. *                                                                           *
  12. *   compress : compress.c                                                   *
  13. *              Main and Operating System Independent support functions      *
  14. *                                                                           *
  15. *   port by  : Donald J. Gloistein                                          *
  16. *                                                                           *
  17. *   Source, Documentation, Object Code:                                     *
  18. *   released to Public Domain. This code is ported from compress v4.0       *
  19. *   release joe.                                                            *
  20. *---------------------------  Module Description  --------------------------*
  21. *   The compress program is compatible with the compression/decompression   *
  22. *   used on the Unix systems compress programs.  This is version 4 and      *
  23. *   supports up to 16 bits compression. The porting retained the Unix       *
  24. *   meanings of all options, added a couple for MsDos and modified the      *
  25. *   file name conventions to make more sense.                               *
  26. *                                                                           *
  27. *--------------------------- Implementation Notes --------------------------*
  28. *                                                                           *
  29. *   compiled with : compress.h compress.fns                                 *
  30. *   linked with   : compapi.obj  compusi.obj                                *
  31. *   problems:                                                               *
  32. *              See notes in compress.h for defines needed.                  *
  33. *              It should work now with Xenix                                *
  34. *                                                                           *
  35. *              Check the signal() handler functions in your compiler        *
  36. *              documentation. This code assumes ANSI SYS V compatible       *
  37. *              header and return values. Change as appropriate for your     *
  38. *              compiler and operating system.                               *
  39. *                                                                           *
  40. *              This source compiles properly with Microsoft C compiler      *
  41. *              version 5.1.                                                 *
  42. *                                                                           *
  43. *   CAUTION:   because the program is in modules, make sure you recompile   *
  44. *              all modules if you change the header or a define in the      *
  45. *              compress.c file                                              *
  46. *                                                                           *
  47. * Algorithm from "A Technique for High Performance Data Compression",       *
  48. * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.          *
  49. *                                                                           *
  50. * Assumptions:                                                              *
  51. *  When filenames are given, replaces with the compressed version           *
  52. *  (.Z suffix) only if the file decreases in size.                          *
  53. * Algorithm:                                                                *
  54. *  Modified Lempel-Ziv method (LZW).  Basically finds common                *
  55. * substrings and replaces them with a variable size code.  This is          *
  56. * deterministic, and can be done on the fly.  Thus, the decompression       *
  57. * procedure needs no input table, but tracks the way the table was built.   *
  58. *                                                                           *
  59. *                                                                           *
  60. *---------------------------      Author(s)        -------------------------*
  61. *     Initials ---- Name ---------------------------------                  *
  62. *      DjG          Donald J. Gloistein                                     *
  63. *                   Plus many others, see rev.hst file for full list        *
  64. *      Dal          Dale A. Schumacher (Sozobon C port)                     *
  65. *      LvR          Lyle V. Rains, many thanks for improved implementation  *
  66. *************************************************************************@H*/
  67.  
  68. /*@R************************< Revision History >*****************************
  69. *                                                                           *
  70. *   version -- date -- init ---Notes----------------------                  *
  71. *    4.01    08-29-88  DjG    first cut  for 16 bit MsDos version           *
  72. *            09-04-88  DjG    fixed unlink on zcat if interupted.           *
  73. *                             added msdos filename logic and functions      *
  74. *    4.10    10-27-88  DjG  revised API with coding changes by LvR.         *
  75. *    4.10a   10-30-88  DjG  cleaned up code and fixed bug in freeing ptr.   *
  76. *    4.10b   11-01-88  DjG  cleaned up the logic for inpath/outpath         *
  77. *                           Changed the logic to finding the file name      *
  78. *                           Fixed the allocation bug in the api             *
  79. *                           Added some more portability macros              *
  80. *    4.10c   11-04-88  DjG  Changed maxcode from global to static in api.   *
  81. *                           Supplied some library functions for those who   *
  82. *                           don't have them, changed dos usi to use the     *
  83. *                           strrpbrk(). Checked casts in api again. Compiles*
  84. *                           without warnings at pick level 3.               *
  85. *    4.10d   11-25-88  DjG  revised some memory allocation, put more in the *
  86. *                           header file. Corrected some typos.              *
  87. *                           Changed prog_name() to force lower case         *
  88. *                           Corrected bug, no longer unlinks existing file  *
  89. *                           if not enough memory to compress or decompress  *
  90. *            12-06-88  DjG  VERY minor changes for casts and header defines *
  91. *            12-08-88  DjG  Adjusted path separator check in main function  *
  92. *                           Amiga uses split seg because of compiler        *
  93. *            12-09-88  DjG  Debugging done, all defaults now Unix compress  *
  94. *                           defaults, including unlinking input file and    *
  95. *                           acting as a filter. Must use -h option to get   *
  96. *                           help screen.                                    *
  97. *    4.10e   12-11-88  DjG  Fixed more casts, prototypes and header file.   *
  98. *    4.10f   12-12-88  DjG  Fixed unlinking open files on error. This fails *
  99. *                           on shared or os/2 platforms.                    *
  100. *            12-15-88  DjG  Fixed SIGTYPE for function passed to signal     *
  101. *                           Fixed problems with Xenix 2.2.1                 *
  102. *    4.2     12-19-88  DjG  Replaced adaptive reset as an option.           *
  103. *    4.3     12-26-88  DjG  Fixed long file name bug, fixed bug with        *
  104. *                           compressdir. -B option added, same as -b option *
  105. *            05-06-89  Dal  Ported to Sozobon/Alcyon C for Atari ST.  Also, *
  106. *                           created get_one() for console prompting.        *
  107. *            05-08-89  Dal  Ported to Minix-ST                              *
  108. *    4.3a    05-29-89  DjG  Combined source code changes and now compiles   *
  109. *                           on Minix.                                       *
  110. *    4.3b    08-20-89  DjG  Changed the version() to simplify it. Changed   *
  111. *                           the order of testing for linked files           *
  112. *            10-02-89  DjG  Changed the double negative #ifndef NDEBUG      *
  113. *                           to a more logical coding.                       *
  114. *    4.3c    12-25-89  DjG  Fixed pointer bug in error message code         *
  115. *            01-06-90  LvR  Fixed signed expansion on 68000 cpu's           *
  116. *    4.3d    01-18-90  LvR  Fixed problem with token[] overrunning on some  *
  117. *                           files with large amounts of repeating characters*
  118. *************************************************************************@R*/
  119.  
  120. #include <stdio.h>
  121.  
  122. #define MAIN        /* header has defining instances of globals */
  123. #include "compress.h" /* contains the rest of the include file declarations */
  124. #include "misc.h"
  125. #include "gzip.h"
  126.  
  127. #define ARGVAL() (*++(*argv) || (--argc && *++argv))
  128. char suffix[] = SUFFIX ;          /* only used in this file */
  129.  
  130. int    open_infile(char *in_name);
  131. extern char divi_flag;
  132. extern int    Afd;
  133. FILE    *Afp;
  134.  
  135. #if 0
  136. int main(int argc,char **argv)
  137. {
  138.     int    chk_divi(char *name);
  139.     int ret;
  140.     char rets[10];
  141.     
  142.    /* set up array for files to be converted */
  143.     if (argc < 3)
  144.         exit(1);
  145.     
  146.     if (open_infile(argv[1]) == NO)
  147.         exit(2);
  148.  
  149.     if (open_outfile(argv[2]) == NO)
  150.         exit(2);
  151.  
  152.     divi_flag = chk_divi(argv[1]);
  153.     if ((ret = de_comp_init()) != OK)
  154.         return ret;
  155.  
  156.     while(de_comp() != EOF)
  157.         ;
  158. /*    fprintf(stderr,"SET next file[%s] and Hit Ret key\n",next_name(ifname));
  159.         gets(rets);*/
  160.     de_comp_end();
  161.     check_error();
  162.     return exit_stat;
  163. }
  164. #endif
  165.  
  166. int de_comp_init(void)
  167. {
  168.     void de_comp_init2(void);
  169.     //void    close_arch(char c);
  170.  
  171.     /* assume zcat */
  172.         keep = TRUE;
  173.         zcat_flg = do_decomp = 1;
  174.  
  175.      maxbits = get_byte();    /* set -b from file */
  176.      block_compress = maxbits & BLOCK_MASK;
  177.      maxbits &= BIT_MASK;
  178.      if(maxbits > MAXBITS) {
  179.        fprintf(stderr," compressed with %d bits, can only handle %d bits\n",
  180.           maxbits, MAXBITS);
  181.          return GZIP_ERROR;
  182.      }
  183.      de_comp_init2();
  184.      return OK;
  185. }
  186.  
  187.  
  188. /*
  189.  * All previous #ifdef'ed code should return() a value.
  190.  * If no other option is available, the following is the original code.
  191.  * It not only reads from stderr (not a defined operation)
  192.  * but it does so via an explicit read() call on file descriptor 2!
  193.  * So much for portability.                    -Dal
  194.  */
  195. /* Dal included code to use the MSC getche() but it crashes because of
  196.    the freopening of stdin. Have to use the cludge for MSC. This function is
  197.    included so that others who have problems with the keyboard read can
  198.    change this function for their system.  DjG
  199.  */
  200.  
  201.  
  202.  
  203. ALLOCTYPE FAR *emalloc(x,y)
  204. unsigned int x;
  205. int y;
  206. {
  207.     ALLOCTYPE FAR *p;
  208.     p = (ALLOCTYPE FAR *)ALLOCATE(x,y);
  209.     return(p);
  210. }
  211.  
  212. void efree(ptr)
  213. ALLOCTYPE FAR *ptr;
  214. {
  215.     FREEIT(ptr);
  216. }
  217.  
  218. /*
  219.  * read by get_byf
  220.  */
  221. int v_read(unsigned char *buf,int len)
  222. {
  223.     int i,c;
  224.     
  225.     for(i=0;i<len;i++,buf++){
  226.         if ((c = get_byte()) == EOF){
  227.             break;
  228.         }
  229.         *buf = c;
  230.     }
  231.     return i;
  232. }
  233.  
  234. static int entry_flag=0;
  235. int uncompress(void)
  236. {
  237. /*    extern unsigned char *window;*/
  238.     
  239.     if (entry_flag == 0){
  240.         de_comp_init();
  241.         entry_flag = 1;
  242.     }else if(entry_flag == -1){
  243.         compapi_end();    
  244.         return EOF;
  245.     }
  246.             
  247.     if (de_comp() == OK)
  248.         return OK;
  249.     
  250.     entry_flag = -1;
  251.     flush_window();
  252.     return OK;
  253.     
  254. }
  255.  
  256. /*
  257.  * call by archio.c
  258.  */
  259. void init_comp(void)
  260. {
  261.     entry_flag=0;
  262. }
  263.