home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / macutils.lzh / MACUTILS / MACUNPACK / lzc.c < prev    next >
C/C++ Source or Header  |  1995-09-18  |  4KB  |  197 lines

  1. #include "macunpack.h"
  2. #ifdef LZC
  3. #include "globals.h"
  4. #include "lzc.h"
  5. #include "../util/util.h"
  6. #include "../fileio/machdr.h"
  7. #include "../fileio/wrfile.h"
  8. #include "../util/masks.h"
  9.  
  10. extern void de_compress();
  11. extern void core_compress();
  12. extern void mcb();
  13.  
  14. static void lzc_zivm();
  15. static void lzc_wrfile();
  16. static void lzc_zivu();
  17.  
  18. void lzc(ohdr)
  19. char *ohdr;
  20. {
  21.     core_compress((char *)NULL);
  22.     if(!strncmp(ohdr + I_TYPEOFF, "ZIVM", 4)) {
  23.     lzc_zivm(ohdr);
  24.     } else {
  25.     lzc_zivu(ohdr);
  26.     }
  27. }
  28.  
  29. static void lzc_zivm(ohdr)
  30. char *ohdr;
  31. {
  32.     char hdr[HEADERBYTES];
  33.     unsigned long dataLength, rsrcLength, dataCLength, rsrcCLength;
  34.     char ftype[5], fauth[5];
  35.  
  36.     if(fread(hdr, 1, HEADERBYTES, infp) != HEADERBYTES) {
  37.     (void)fprintf(stderr, "Can't read file header\n");
  38. #ifdef SCAN
  39.     do_error("macunpack: Can't read file header");
  40. #endif /* SCAN */
  41.     exit(1);
  42.     }
  43.     if(strncmp(hdr, MAGIC1, 4)) {
  44.     (void)fprintf(stderr, "Magic header mismatch\n");
  45. #ifdef SCAN
  46.     do_error("macunpack: Magic header mismatch");
  47. #endif /* SCAN */
  48.     exit(1);
  49.     }
  50.     dataLength = get4(hdr + C_DLENOFF);
  51.     dataCLength = get4(hdr + C_DLENOFFC);
  52.     rsrcLength = get4(hdr + C_RLENOFF);
  53.     rsrcCLength = get4(hdr + C_RLENOFFC);
  54.  
  55.     write_it = 1;
  56.     if(list) {
  57.     copy(info, ohdr, INFOBYTES);
  58.     copy(info + I_TYPEOFF, hdr + C_TYPEOFF, 4);
  59.     copy(info + I_AUTHOFF, hdr + C_AUTHOFF, 4);
  60.     copy(info + I_DLENOFF, hdr + C_DLENOFF, 4);
  61.     copy(info + I_RLENOFF, hdr + C_RLENOFF, 4);
  62.     copy(info + I_CTIMOFF, hdr + C_CTIMOFF, 4);
  63.     copy(info + I_MTIMOFF, hdr + C_MTIMOFF, 4);
  64.     copy(info + I_FLAGOFF, hdr + C_FLAGOFF, 8);
  65.  
  66.     transname(ohdr + I_NAMEOFF + 1, text, (int)ohdr[I_NAMEOFF]);
  67.     transname(hdr + C_TYPEOFF, ftype, 4);
  68.     transname(hdr + C_AUTHOFF, fauth, 4);
  69.     do_indent(indent);
  70.     (void)fprintf(stderr,
  71.         "name=\"%s\", type=%4.4s, author=%4.4s, data=%ld, rsrc=%ld",
  72.         text, ftype, fauth, (long)dataLength, (long)rsrcLength);
  73.     if(info_only) {
  74.         write_it = 0;
  75.     }
  76.     if(query) {
  77.         write_it = do_query();
  78.     } else {
  79.         (void)fputc('\n', stderr);
  80.     }
  81.     }
  82.  
  83.     if(write_it) {
  84.     define_name(text);
  85.     }
  86.     if(write_it) {
  87.     start_info(info, rsrcLength, dataLength);
  88.     }
  89.     if(verbose) {
  90.     (void)fprintf(stderr, "\tData: ");
  91.     }
  92.     if(write_it) {
  93.     start_data();
  94.     }
  95.     lzc_wrfile(dataLength, dataCLength);
  96.     if(verbose) {
  97.     (void)fprintf(stderr, ", Rsrc: ");
  98.     }
  99.     if(write_it) {
  100.     start_rsrc();
  101.     }
  102.     lzc_wrfile(rsrcLength, rsrcCLength);
  103.     if(write_it) {
  104.     end_file();
  105.     }
  106.     if(verbose) {
  107.     (void)fprintf(stderr, ".\n");
  108.     }
  109. }
  110.  
  111. static void lzc_wrfile(obytes, ibytes)
  112. unsigned long obytes, ibytes;
  113. {
  114.     int n, nbits;
  115.     char subheader[3];
  116.  
  117.     if(ibytes == 0) {
  118.     if(verbose) {
  119.         (void)fprintf(stderr, "empty");
  120.     }
  121.     return;
  122.     }
  123.     if(ibytes == obytes) {
  124.     if(verbose) {
  125.         (void)fprintf(stderr, "No compression");
  126.     }
  127.     if(write_it) {
  128.         n = fread(out_buffer, 1, (int)ibytes, infp);
  129.         if(n != ibytes) {
  130.         (void)fprintf(stderr, "Premature EOF\n");
  131. #ifdef SCAN
  132.         do_error("macunpack: Premature EOF");
  133. #endif /* SCAN */
  134.         exit(1);
  135.         }
  136.     } else {
  137.         n = ibytes;
  138.         while(n-- > 0) {
  139.         if(getc(infp) == EOF) {
  140.             (void)fprintf(stderr, "Premature EOF\n");
  141. #ifdef SCAN
  142.             do_error("macunpack: Premature EOF");
  143. #endif /* SCAN */
  144.             exit(1);
  145.         }
  146.         }
  147.     }
  148.     } else {
  149.     if(fread(subheader, 1, 3, infp) != 3) {
  150.         (void)fprintf(stderr, "Premature EOF\n");
  151. #ifdef SCAN
  152.         do_error("macunpack: Premature EOF");
  153. #endif /* SCAN */
  154.         exit(1);
  155.     }
  156.     if(strncmp(subheader, MAGIC2, 2)) {
  157.         (void)fprintf(stderr, "Magic subheader mismatch\n");
  158. #ifdef SCAN
  159.         do_error("macunpack: Magic subheader mismatch");
  160. #endif /* SCAN */
  161.         exit(1);
  162.     }
  163.     nbits = subheader[2] & 0x7f;
  164.     if(verbose) {
  165.         (void)fprintf(stderr, "LZC(%d) compressed (%4.1f%%)",
  166.             nbits, 100.0 * ibytes / obytes);
  167.     }
  168.     if(write_it) {
  169.         de_compress(ibytes - 3, nbits);
  170.     } else {
  171.         n = ibytes - 3;
  172.         while(n-- > 0) {
  173.         if(getc(infp) == EOF) {
  174.             (void)fprintf(stderr, "Premature EOF\n");
  175. #ifdef SCAN
  176.             do_error("macunpack: Premature EOF");
  177. #endif /* SCAN */
  178.             exit(1);
  179.         }
  180.         }
  181.     }
  182.     }
  183. }
  184.  
  185. static void lzc_zivu(ohdr)
  186. char *ohdr;
  187. {
  188.     (void)fprintf(stderr,
  189.         "\tMacCompress(Unix) not yet implemented, copied as MacBinary\n");
  190.     mcb(ohdr, (unsigned long)in_rsrc_size, (unsigned long)in_data_size,
  191.     in_ds + in_rs);
  192. }
  193. #else /* LZC */
  194. int lzc; /* keep lint and some compilers happy */
  195. #endif /* LZC */
  196.  
  197.