home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / ZOOFILT.C < prev    next >
C/C++ Source or Header  |  1991-07-14  |  2KB  |  89 lines

  1. /* derived from: zoofilt.c 1.8 88/01/30 23:47:05 */
  2.  
  3. #ifndef LINT
  4. static char sccsid[]="@(#) $Id: zoofilt.c,v 1.5 91/07/09 01:54:15 dhesi Exp $";
  5. #endif
  6.  
  7. /*
  8. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  9. (C) Copyright 1991 Rahul Dhesi -- All rights reserved
  10.  
  11. Filter mode -- compress or decompress standard input and write
  12. to standard output.
  13. */
  14.  
  15. #include "options.h"
  16.  
  17. #ifdef FILTER
  18.  
  19. #include "zooio.h"
  20. #include "errors.i"
  21. #include "zoofns.h"
  22.  
  23. /* action */
  24. #define COMPRESS        0
  25. #define UNCOMPRESS    1
  26.  
  27. #define FTAG    ((unsigned int) 0x5a32)    /* magic number */
  28.  
  29. extern unsigned int crccode;
  30.  
  31. int rdint PARMS((unsigned int *));    /* read an unsigned int */
  32. int wrint PARMS((unsigned int));    /* write an unsigned int */
  33.  
  34. /* global variable used to pass two bytes (CRC value) back from lzd to here */
  35. unsigned int filt_lzd_word;
  36.  
  37. void zoofilt (option)
  38. char *option;
  39. {
  40.     int choice;                                            /* what to do -- [de]compress */
  41.     unsigned int filetag;                            /* tag stored in input */
  42.     int stat1, stat2, stat3;                        /* status codes */
  43.     int use_lzh = 0;                                    /* use lzh instead */
  44.     extern lzc(), lzh_encode();                    /* possible encoders */
  45.     extern lzd(), lzh_decode();                    /* and decoders */
  46.  
  47.     while (*++option) {
  48.         switch (*option) {
  49.             case 'c':    choice = COMPRESS;    break;
  50.             case 'u':    choice = UNCOMPRESS;  break;
  51.             case 'h':    use_lzh = 1; break;
  52.             default:
  53.                        prterror ('f', inv_option, *option);    /* fatal error -- abort */
  54.                 }
  55.     }
  56.  
  57.         MODE_BIN(stdin);
  58.         MODE_BIN(stdout);
  59.     crccode = 0;    /* needed whether compressing or uncompressing */
  60.  
  61.     switch (choice) {
  62.         case COMPRESS:
  63.             stat1 = wrint (FTAG);
  64.             stat2 = (use_lzh ? lzh_encode : lzc) (STDIN, STDOUT);
  65.             stat3 = wrint (crccode);
  66.             if (stat1 == 0 && stat2 == 0 && stat3 == 0)
  67.                 zooexit (0);
  68.             else {
  69.                 fprintf (stderr, "Zoo: FATAL: Compression error.\n");
  70.                 zooexit (1);
  71.             }
  72.             break;
  73.         case UNCOMPRESS:
  74.             stat1 = rdint (&filetag);
  75.             if (stat1 != 0 || filetag != FTAG)
  76.                 zooexit (1);
  77.             stat2 = (use_lzh ? lzh_decode : lzd) (STDIN, STDOUT);
  78.             if (stat2 == 0 && filt_lzd_word == crccode)
  79.                 zooexit (0);
  80.             else {
  81.                 fprintf (stderr, "Zoo: FATAL: Uncompression error.\n");
  82.                 zooexit (1);
  83.             }
  84.             break;
  85.     }
  86. } /* zoofilt */
  87.  
  88. #endif /* FILTER */
  89.