home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d050 / unixarc.lha / UnixArc / arcpack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-17  |  9.0 KB  |  241 lines

  1. /*  ARC - Archive utility - ARCPACK
  2.  
  3. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  4.  
  5.     By:  Thom Henderson
  6.  
  7.     Description:
  8.          This file contains the routines used to compress a file
  9.          when placing it in an archive.
  10.  
  11.     Language:
  12.          Computer Innovations Optimizing C86
  13. */
  14. #include <stdio.h>
  15. #include "arc.h"
  16.  
  17. /* stuff for non-repeat packing */
  18.  
  19. #define DLE 0x90                       /* repeat sequence marker */
  20.  
  21. static unsigned char state;            /* current packing state */
  22.  
  23. /* non-repeat packing states */
  24.  
  25. #define NOHIST  0                      /* don't consider previous input*/
  26. #define SENTCHAR 1                     /* lastchar set, no lookahead yet */
  27. #define SENDNEWC 2                     /* run over, send new char next */
  28. #define SENDCNT 3                      /* newchar set, send count next */
  29.  
  30. /* packing results */
  31.  
  32. static long stdlen;                    /* length for standard packing */
  33. static int crcval;                     /* CRC check value */
  34.  
  35. pack(f,t,hdr)                          /* pack file into an archive */
  36. FILE *f, *t;                           /* source, destination */
  37. struct heads *hdr;                     /* pointer to header data */
  38. {
  39.     int c;                             /* one character of stream */
  40.     long ncrlen;                       /* length after packing */
  41.     long huflen;                       /* length after squeezing */
  42.     long lzwlen;                       /* length after crunching */
  43.     long pred_sq(), file_sq();         /* stuff for squeezing */
  44.     long pred_cm();                    /* dynamic crunching cleanup */
  45.     char tnam[STRLEN];                /* temporary name buffer */
  46.     char *makefnam();                  /* filename fixer upper */
  47.     FILE *crn = NULL;                  /* temporary crunch file */
  48.     FILE *fopen();
  49.  
  50.     /* first pass - see which method is best */
  51.  
  52.     if(!nocomp)                        /* if storage kludge not active */
  53.     {    if(note)
  54.               printf(" analyzing, ");
  55.  
  56.          if(arctemp)                   /* use temp area if specified */
  57.               sprintf(tnam,"%s/$ARCTEMP.CRN",arctemp);
  58.          else makefnam("$ARCTEMP.CRN",arcname,tnam);
  59.          crn = fopen(tnam,"w+");
  60.  
  61.          state = NOHIST;               /* initialize ncr packing */
  62.          stdlen =  ncrlen = 0;         /* reset size counters */
  63.          crcval = 0;                   /* initialize CRC check value */
  64.          setcode();                    /* initialize encryption */
  65.  
  66.          init_cm(f,crn);               /* initialize for crunching */
  67.          init_sq();                    /* initialize for squeeze scan */
  68.  
  69.          while((c=getc_ncr(f))!=EOF)   /* for each byte of file */
  70.          {    ncrlen++;                /* one more packed byte */
  71.               scan_sq(c);              /* see what squeezing can do */
  72.               putc_cm(c,crn);          /* see what crunching can do */
  73.          }
  74.          huflen = pred_sq();           /* finish up after squeezing */
  75.          lzwlen = pred_cm(crn);        /* finish up after crunching */
  76.     }
  77.     else                               /* else kludge the method */
  78.     {    stdlen = 0;                   /* make standard look best */
  79.          ncrlen = huflen = lzwlen = 1;
  80.     }
  81.  
  82.     /* standard set-ups common to all methods */
  83.  
  84.     fseek(f,0L,0);                     /* rewind input */
  85.     hdr->crc = crcval;                 /* note CRC check value */
  86.     hdr->length = stdlen;              /* set actual file length */
  87.     state = NOHIST;                    /* reinitialize ncr packing */
  88.     setcode();                         /* reinitialize encryption */
  89.  
  90.     /* choose and use the shortest method */
  91.  
  92.     if(stdlen<=ncrlen && stdlen<=huflen && stdlen<=lzwlen)
  93.     {    if(kludge)                    /*DEBUG*/
  94.               printf("(%ld) ",lzwlen-stdlen);
  95.          if(note)
  96.               printf("storing, ");     /* store without compression */
  97.          hdrver = 2;                   /* note packing method */
  98.          stdlen = crcval = 0;          /* recalc these for kludge */
  99.          while((c=getch(f))!=EOF)      /* store it straight */
  100.               putc_pak(c,t);
  101.          hdr->crc = crcval;
  102.          hdr->length = hdr->size = stdlen;
  103.     }
  104.  
  105.     else if(ncrlen<huflen && ncrlen<lzwlen)
  106.     {    if(kludge)                    /*DEBUG*/
  107.               printf("(%ld) ",lzwlen-ncrlen);
  108.          if(note)
  109.               printf("packing, ");     /* pack with repeat suppression */
  110.          hdrver = 3;                   /* note packing method */
  111.          hdr->size = ncrlen;           /* set data length */
  112.          while((c=getc_ncr(f))!=EOF)
  113.               putc_pak(c,t);
  114.     }
  115.  
  116.     else if(huflen<lzwlen)
  117.     {    if(kludge)                    /*DEBUG*/
  118.               printf("(%ld) ",lzwlen-huflen);
  119.          if(note)
  120.               printf("squeezing, ");
  121.          hdrver = 4;                   /* note packing method */
  122.          hdr->size = file_sq(f,t);     /* note final size */
  123.     }
  124.  
  125.     else
  126.     {    if(kludge)                    /*DEBUG*/
  127.               printf("(%ld) ",huflen-lzwlen);
  128.          if(note)
  129.               printf("crunching, ");
  130.          hdrver = 8;
  131.          hdr->size = lzwlen;           /* size should not change */
  132.          if(crn)                       /* if temp was created */
  133.          {    fseek(crn,0L,0);         /* then copy over crunched temp */
  134.               while((c=fgetc(crn))!=EOF)
  135.                    putc_tst(c,t);
  136.          }
  137.          else                          /* else re-crunch */
  138.          {    init_cm(f,t);
  139.               while((c=getc_ncr(f))!=EOF)
  140.                    putc_cm(c,t);
  141.               pred_cm(t);              /* finish up after crunching */
  142.          }
  143.     }
  144.  
  145.     /* standard cleanups common to all methods */
  146.  
  147.     if(crn)                            /* get rid of crunch temporary */
  148.     {    fclose(crn);
  149.          if(unlink(tnam) && warn)
  150.          {    printf("Cannot delete temporary file %s\n",tnam);
  151.               nerrs++;
  152.          }
  153.     }
  154.     if(note)
  155.          printf("done.\n");
  156. }
  157.  
  158. /*  Non-repeat compression - text is passed through normally, except that
  159.     a run of more than two is encoded as:
  160.  
  161.          <char> <DLE> <count>
  162.  
  163.     Special case: a count of zero indicates that the DLE is really a DLE,
  164.     not a repeat marker.
  165. */
  166.  
  167. int getc_ncr(f)                        /* get bytes with collapsed runs */
  168. FILE *f;                               /* file to get from */
  169. {
  170.     static int lastc;                  /* value returned on last call */
  171.     static int repcnt;                 /* repetition counter */
  172.     static int c;                      /* latest value seen */
  173.  
  174.     switch(state)                      /* depends on our state */
  175.     {
  176.     case NOHIST:                       /* no relevant history */
  177.          state = SENTCHAR;
  178.          return lastc = getch(f);      /* remember the value next time */
  179.  
  180.     case SENTCHAR:                     /* char was sent. look ahead */
  181.          switch(lastc)                 /* action depends on char */
  182.          {
  183.          case DLE:                     /* if we sent a real DLE */
  184.               state = NOHIST;          /* then start over again */
  185.               return 0;                /* but note that the DLE was real */
  186.  
  187.          case EOF:                     /* EOF is always a special case */
  188.               return EOF;
  189.  
  190.          default:                      /* else test for a repeat */
  191.               for(repcnt=1; (c=getch(f))==lastc && repcnt<255; repcnt++)
  192.                    ;                   /* find end of run */
  193.  
  194.               switch(repcnt)           /* action depends on run size */
  195.               {
  196.               case 1:                  /* not a repeat */
  197.                    return lastc = c;   /* but remember value next time */
  198.  
  199.               case 2:                  /* a repeat, but too short */
  200.                    state = SENDNEWC;   /* send the second one next time */
  201.                    return lastc;
  202.  
  203.               default:                 /* a run - compress it */
  204.                    state = SENDCNT;    /* send repeat count next time */
  205.                    return DLE;         /* send repeat marker this time */
  206.               }
  207.          }
  208.  
  209.     case SENDNEWC:                     /* send second char of short run */
  210.          state = SENTCHAR;
  211.          return lastc = c;
  212.  
  213.     case SENDCNT:                      /* sent DLE, now send count */
  214.          state = SENDNEWC;
  215.          return repcnt;
  216.  
  217.     default:
  218.          abort("Bug - bad ncr state\n");
  219.     }
  220. }
  221.  
  222. static int getch(f)                    /* special get char for packing */
  223. FILE *f;                               /* file to get from */
  224. {
  225.     int c;                             /* a char from the file */
  226.  
  227.     if((c=fgetc(f))!=EOF)              /* if not the end of file */
  228.     {    crcval = addcrc(crcval,c);    /* then update CRC check value */
  229.          stdlen++;                     /* and bump length counter */
  230.     }
  231.  
  232.     return c;
  233. }
  234.  
  235. putc_pak(c,f)                          /* put a packed byte into archive */
  236. char c;                                /* byte to put */
  237. FILE *f;                               /* archive to put it in */
  238. {
  239.     putc_tst(code(c),f);               /* put encoded byte, with checks */
  240. }
  241.