home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / UTILS / SQUEEZE / SQ.C < prev    next >
C/C++ Source or Header  |  2000-06-30  |  6KB  |  212 lines

  1. /* This program compresses a file without loosing information.
  2.  * The "usq" program is required to unsqueeze the file
  3.  * before it can be used.
  4.  *
  5.  * Typical compression rates are between 30 and 50 percent for text files.
  6.  *
  7.  * Squeezing a really big file takes a few minutes.
  8.  *
  9.  * Useage:
  10.  *    sq [file1] [file2] ... [filen]
  11.  *
  12.  * where file1 through filen are the names of the files to be squeezed.
  13.  * The file type (under CP/M or MS-DOS) is changed to ".?Q?"; under UN*X,
  14.  * ".SQ" is appended to the file name. The original file name is stored
  15.  * in the squeezed file.
  16.  *
  17.  * If no file name is given on the command line you will be
  18.  * prompted for commands (one at a time). An empty command
  19.  * terminates the program.
  20.  *
  21.  * The transformations compress strings of identical bytes and
  22.  * then encode each resulting byte value and EOF as bit strings
  23.  * having lengths in inverse proportion to their frequency of
  24.  * occurrance in the intermediate input stream. The latter uses
  25.  * the Huffman algorithm. Decoding information is included in
  26.  * the squeezed file, so squeezing short files or files with
  27.  * uniformly distributed byte values will actually increase size.
  28.  */
  29.  
  30. /* CHANGE HISTORY:
  31.  * 1.3    Close files properly in case of error exit.
  32.  * 1.4    Break up long introductory lines.
  33.  * 1.4    Send introduction only to console.
  34.  * 1.4    Send errors only to console.
  35.  * 1.5  Fix BUG that caused a rare few squeezed files
  36.  *    to be incorrect and fail the USQ crc check.
  37.  *    The problem was that some 17 bit codes were
  38.  *    generated but are not supported by other code.
  39.  *    THIS IS A MAJOR CHANGE affecting TR2.C and SQ.H and
  40.  *    requires recompilation of all files which are part
  41.  *    of SQ. Two basic changes were made: tree depth is now
  42.  *    used as a tie breaker when weights are equal. This
  43.  *    makes the tree shallower. Although that may always be
  44.  *    sufficient, an error trap was added to cause rescaling
  45.  *    of the counts if any code > 16 bits long is generated.
  46.  * 1.5    Add debugging displays option '-'.
  47.  * 1.6  Fixed to work correctly under MP/M II.  Also shortened
  48.  *      signon message.
  49.  * 2.0    New version for use with CI-C86 compiler (CP/M-86 and MS-DOS)
  50.  * 2.1  Converted for use in MLINK
  51.  * 2.2  Converted for use with optimizing CI-C86 compiler (MS-DOS)
  52.  * 3.0  Generalized for UN*X use, changed output file naming convention
  53.  * 3.2  Change conversion of type from .SQ to .?Q? on non-UNIX machines
  54.  */
  55.  
  56. /* eject eject */
  57.  
  58. /*
  59.  * The following define MUST be set to the maximum length of a file name
  60.  * on the system "sq" is being compiled for.  If not, "sq" will not be
  61.  * able to check for whether the output file name it creates is valid
  62.  * or not.
  63.  */
  64.  
  65. #define FNM_LEN 12
  66. /* #define UNIX                /* comment out for CP/M, MS-DOS versions */
  67. #define SQMAIN
  68.  
  69. #define VERSION "3.2   03/12/85"
  70.  
  71. #include <stdio.h>
  72. #include "sqcom.h"
  73. #include "sq.h"
  74. #define FALSE 0
  75.  
  76. main(argc, argv)
  77. int argc;
  78. char *argv[];
  79. {
  80.     int i,c;
  81.     char inparg[128];    /* parameter from input */
  82.  
  83.     debug = FALSE;
  84.     printf("File squeezer version %s (original author: R. Greenlaw)\n\n", VERSION);
  85.  
  86.     /* Process the parameters in order */
  87.     for(i = 1; i < argc; ++i)
  88.         obey(argv[i]);
  89.  
  90.     if(argc < 2) {
  91.         printf("Enter file names, one line at a time, or type <RETURN> to quit.");
  92.         do {
  93.             printf("\n*");
  94.             for(i = 0; i < 16; ++i) {
  95.                 if((c = getchar()) == EOF)
  96.                     c = '\n';    /* fake empty (exit) command */
  97.                 if((inparg[i] = c) == '\n') {
  98.                     inparg[i] = '\0';
  99.                     break;
  100.                 }
  101.             }
  102.             if(inparg[0] != '\0')
  103.                 obey(inparg);
  104.         } while(inparg[0] != '\0');
  105.     }
  106. }
  107.  
  108. /* eject eject */
  109.  
  110. obey(p)
  111. char *p;
  112. {
  113.     char *q;
  114.     char outfile[128];    /* output file spec. */
  115.  
  116.     if(*p == '-') {
  117.         /* toggle debug option */
  118.         debug = !debug;
  119.         return;
  120.     }
  121.  
  122.     /* Check for ambiguous (wild-card) name */
  123.     for(q = p; *q != '\0'; ++q)
  124.         if(*q == '*' || *q == '?') {
  125.             printf("\nAmbiguous name %s ignored", p);
  126.             return;
  127.     }
  128.     /* First build output file name */
  129.     strcpy(outfile, p);        /* copy input name to output */
  130.  
  131.     /* Find and change output file suffix */
  132.  
  133.     if (strlen(outfile) + 3 > FNM_LEN) {    /* check for long file name */
  134.         q = outfile + FNM_LEN - 3;
  135.         *q = '\0';        /* make room for suffix */
  136.     }
  137.     else {
  138.         q = outfile + strlen(outfile);
  139. #ifndef UNIX
  140.         for(; --q >= outfile;)
  141.             if (*q == '.') {
  142.                 if (strlen(q) == 1)    /* if . */
  143.                     strcat(outfile,"SQ");
  144.                 else if (strlen(q) == 2) /* if .X */
  145.                     strcat(outfile,"Q");
  146.                 else            /* must be .XX or .XXX */
  147.                     *(q+2) = 'Q';
  148.                 break;
  149.             }
  150.         if (q < outfile)
  151.             strcat(outfile, ".SQ");
  152. #else
  153.         --q;
  154. #endif
  155.     }
  156.  
  157. #ifdef UNIX
  158.     strcat(outfile, ".SQ");
  159. #endif
  160.  
  161.     squeeze(p, outfile);
  162. }
  163.  
  164. /* eject eject */
  165.  
  166. squeeze(infile, outfile)
  167. char *infile, *outfile;
  168. {
  169.     int i, c,c2;
  170.     FILE *inbuff, *outbuff;        /* file buffers */
  171.  
  172.     printf("%s -> %s: ", infile, outfile);
  173.  
  174.     if(!(inbuff=fopen(infile, "rb"))) {
  175.         printf("Can't open %s for input pass 1\n", infile);
  176.         return;
  177.     }
  178.     if(!(outbuff=fopen(outfile, "wb"))) {
  179.         printf("Can't create %s\n", outfile);
  180.         fclose(inbuff);
  181.         return;
  182.     }
  183.  
  184.     /* First pass - get properties of file */
  185.     crc = 0;    /* initialize checksum */
  186.     printf("analyzing, ");
  187.     init_ncr();
  188.     init_huff(inbuff);   
  189.     fclose(inbuff);
  190.  
  191.     /* Write output file header with decoding info */
  192.     wrt_head(outbuff, infile);
  193.  
  194.     /* Second pass - encode the file */
  195.     printf("squeezing,");
  196.     if(!(inbuff=fopen(infile, "rb"))) {
  197.         printf("Can't open %s for input pass 2\n", infile);
  198.         goto closeout;
  199.     }
  200.     init_ncr();    /* For second pass */
  201.  
  202.     /* Translate the input file into the output file */
  203.     while((c = gethuff(inbuff)) != EOF)
  204.         putce(c, outbuff);
  205.     oflush(outbuff);
  206.     printf(" done.\n");
  207. closeall:
  208.     fclose(inbuff);
  209. closeout:
  210.     fclose(outbuff);
  211. }
  212.