home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / disk / archive / nspark_1 / nspark-1.7.5 / pack.c < prev    next >
C/C++ Source or Header  |  1994-12-12  |  2KB  |  98 lines

  1. /*
  2.  * pack/unpack archive files
  3.  *
  4.  * $Header: pack.c 1.4 92/12/07 $
  5.  * $Log:    pack.c,v $
  6.  * Revision 1.4  92/12/07  17:19:29  duplain
  7.  * reformatted source.
  8.  * 
  9.  * Revision 1.3  92/11/09  14:48:36  duplain
  10.  * Added putc_init() to re-initialise variables.
  11.  * 
  12.  * Revision 1.2  92/10/01  11:22:35  duplain
  13.  * Added check for EOF.
  14.  * 
  15.  * Revision 1.1  92/09/29  18:02:25  duplain
  16.  * Initial revision
  17.  * 
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include "spark.h"
  22. #include "main.h"
  23. #include "crc.h"
  24. #include "io.h"
  25.  
  26. #ifdef UNIX
  27. static char rcsid[] = "$Header: pack.c 1.4 92/12/07 $";
  28. #endif /* UNIX */
  29.  
  30. static short running;
  31.  
  32. void
  33. putc_init()
  34. {
  35.     running = 0;
  36. }
  37.  
  38. /*
  39.  * write run-length encoding to output file
  40.  */
  41. void
  42. putc_ncr(ofp, byte)
  43.     FILE *ofp;
  44.     Byte byte;
  45. {
  46.     static Byte prevbyte;
  47.  
  48.     if (running) {
  49.     if (!byte) {    /* means write RUNMARK to output */
  50.         calccrc(RUNMARK);
  51.         if (!testing)
  52.         write_byte(ofp, RUNMARK);
  53.     } else {
  54.         while (--byte) {
  55.         calccrc(prevbyte);
  56.         if (!testing)
  57.             write_byte(ofp, prevbyte);
  58.         }
  59.     }
  60.     running = 0;
  61.     } else if (byte == RUNMARK) {
  62.     running++;
  63.     } else {
  64.     prevbyte = byte; /* save in case next byte is RUNMARK */
  65.     calccrc(byte);
  66.     if (!testing)
  67.         write_byte(ofp, byte);
  68.     }
  69. }
  70.  
  71. Status
  72. unpack(header, ifp, ofp)
  73.     Header *header;
  74.     FILE *ifp, *ofp;
  75. {
  76.     register len = header->complen;
  77.  
  78.     crc = 0;
  79.     putc_init();
  80.     while (len--) {
  81.     if (check_stream(ifp) != FNOERR)
  82.         break;
  83.     putc_ncr(ofp, read_byte(ifp));
  84.     }
  85.  
  86.     if (check_stream(ifp) == FRWERR)
  87.     return (RERR);
  88.     if (!testing && check_stream(ofp) == FRWERR)
  89.     return (WERR);
  90.     if ((Halfword)crc != header->crc)
  91.     return (CRCERR);
  92.     if (testing)
  93.     printf("OK (packed)");
  94.     else
  95.     printf("unpacked");
  96.     return (NOERR);
  97. }
  98.