home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / fileutil / scan.lha / src / lhio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  3.8 KB  |  173 lines

  1. /*----------------------------------------------------------------------*/
  2. /*              File I/O module for LHarc UNIX                          */
  3. /*                                                                      */
  4. /*              Copyright(C) MCMLXXXIX  Yooichi.Tagawa                  */
  5. /*                                                                      */
  6. /*  V0.00  Original                             1989.06.25  Y.Tagawa    */
  7. /*  V1.02  Fixed memory problems with copy()    1990.06.24  FvKempen    */
  8. /*----------------------------------------------------------------------*/
  9.  
  10. #include <stdio.h>
  11.  
  12. #include "lhio.h"
  13.  
  14. #       define BUFFER_SIZE      16384
  15.  
  16. FILE            *crc_infile, *crc_outfile;      /* in lzhuf.c */
  17.  
  18. /* These functions are NO-RETURN */
  19. extern void read_error ();
  20. extern long count;
  21. extern int  ForceReturn;
  22. extern long WinSiz;
  23. extern long TotSiz;
  24. extern long cursize;
  25. extern char *BfrPtr;
  26. extern int  NotInterruptedCall;
  27.  
  28.  
  29. int             crc_getc_cashe;
  30. unsigned int    crc_value;
  31. unsigned int    crc_table[0x100];
  32. long            crc_size;
  33. long            BufrCnt = 0;
  34.  
  35.  
  36. static void
  37. crcsub (ptr, length)
  38.      char *ptr;
  39.      register int length;
  40. {
  41.   register unsigned char *p;
  42.   register unsigned int ctmp;
  43.  
  44.   if (length != 0)
  45.     {
  46.       ctmp = crc_value;
  47.       p = (unsigned char*)ptr;
  48.       for (; length; length --)
  49.         {
  50.           ctmp ^= (unsigned int)*p++;
  51.           ctmp = (ctmp >> 8) ^ crc_table [ ctmp & 0xff ];
  52.         }
  53.       crc_value = ctmp;
  54.     }
  55. }
  56.  
  57. void
  58. putc_crc (c)
  59.      int c;
  60. {
  61.   CRC_CHAR (c);
  62.   *(BfrPtr + BufrCnt) = c;
  63.   TotSiz = BufrCnt + 1;
  64.   if(( TotSiz % WinSiz) == 0 ) { ForceReturn = 1; BufrCnt = 0; }
  65.   else                         { BufrCnt++; }
  66. }
  67.  
  68. int
  69. getc_crc ()
  70. {
  71.   int c;
  72.  
  73.   if (crc_getc_cashe != EOF)
  74.     {
  75.       c = crc_getc_cashe;
  76.       crc_getc_cashe = EOF;
  77.       CRC_CHAR (c);
  78.       crc_size++;
  79.     }
  80.   else if ((c = getc (crc_infile)) != EOF)
  81.     {
  82.       CRC_CHAR (c);
  83.       crc_size++;
  84.     }
  85.   return c;
  86. }
  87.  
  88. void
  89. init_crc ()
  90. {
  91.   static int inited = 0;
  92.   register unsigned int *p = crc_table;
  93.   register int i, j;
  94.   register unsigned int x;
  95.  
  96.   if (!inited) {
  97.     for (j = 0; j < 256; j ++) {
  98.       x = j;
  99.       for (i = 0; i < 8; i ++) {
  100.         if ((x & 1) != 0) {
  101.           x = (x >> 1) ^ 0xa001;
  102.         } else {
  103.           x = (x >> 1);
  104.         }
  105.       }
  106.       *p ++ = x;
  107.     }
  108.     inited = 1;
  109.   }
  110.   crc_value = 0;
  111.   crc_getc_cashe = EOF;
  112.   crc_size = 0;
  113. }
  114.  
  115. /*----------------------------------------------------------------------*/
  116. /*                                                                      */
  117. /*----------------------------------------------------------------------*/
  118.  
  119. /* if return value is -1, see errno */
  120. static void
  121. copy_binary_file (ifp, size, crc_flag)
  122.      FILE *ifp;
  123.      long size;
  124.      int crc_flag;      /* as boolean value */
  125. {
  126.   static int read_size;
  127.   static int n;
  128.  
  129.   if( !NotInterruptedCall ) NotInterruptedCall = 1;
  130.  
  131.   read_size = ((cursize < WinSiz) ? (int)cursize : WinSiz);
  132.  
  133.   n = fread (BfrPtr, sizeof (char), read_size, ifp);
  134.   if (n < 0)
  135.      read_error ();
  136.  
  137.   TotSiz = n;
  138.  
  139.   if (crc_flag)
  140.      crcsub (BfrPtr, n);
  141.  
  142.   cursize -= (long)n;
  143.  
  144.   if( cursize > 0 ) ForceReturn = 1;
  145. }
  146.  
  147. /*----------------------------------------------------------------------*/
  148. /*                                                                      */
  149. /*----------------------------------------------------------------------*/
  150.  
  151.  
  152. int
  153. decode_stored_crc (ifp, original_size, name)
  154.      FILE *ifp;
  155.      long original_size;
  156.      char *name;
  157. {
  158.   if( NotInterruptedCall ) init_crc ();
  159.  
  160.   copy_binary_file (ifp, original_size, 1);
  161.   return crc_value;
  162. }
  163.  
  164. int
  165. decode_stored_nocrc (ifp, original_size, name)
  166.      FILE *ifp;
  167.      long original_size;
  168.      char *name;
  169. {
  170.   copy_binary_file (ifp, original_size, 0);
  171.   return 0;                     /* DUMMY */
  172. }
  173.