home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / lhaxenix.zoo / lharc.xenix / lhio.c < prev    next >
C/C++ Source or Header  |  1990-04-02  |  6KB  |  329 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. /*  V0.03  Release #3  Beta Version        1989.07.02  Y.Tagawa    */
  8. /*  V0.03a Fix few bugs                1989.07.04  Y.Tagawa    */
  9. /*----------------------------------------------------------------------*/
  10.  
  11. #include <stdio.h>
  12. #include "lhio.h"
  13.  
  14. #ifndef BUFFER_SIZE
  15. #define BUFFER_SIZE    16384
  16. #endif
  17.  
  18.  
  19. extern int    text_mode;            /* in lharc.c */
  20. FILE            *crc_infile, *crc_outfile;      /* in lzhuf.c */
  21.  
  22. extern int rson[];
  23.  
  24. /* These functions are NO-RETURN */
  25. extern read_error ();
  26. extern write_error ();
  27.  
  28.  
  29. int        crc_getc_cashe;
  30. unsigned int    crc_value;
  31. unsigned int    crc_table[0x100];
  32. long        crc_size;
  33.  
  34.  
  35. crcsub (ptr, length)
  36.      char        *ptr;
  37.      register int    length;
  38. {
  39.   register unsigned char    *p;
  40.   register unsigned int        ctmp;
  41.  
  42.   if (length != 0)
  43.     {
  44.       ctmp = crc_value;
  45.       p = (unsigned char*)ptr;
  46.       for (; length; length --)
  47.     {
  48.       ctmp ^= (unsigned int)*p++;
  49.       ctmp = (ctmp >> 8) ^ crc_table [ ctmp & 0xff ];
  50.     }
  51.       crc_value = ctmp;
  52.     }
  53. }
  54.  
  55. #ifndef __GNUC__
  56. void putc_crc (c)
  57.      int c;
  58. {
  59.   CRC_CHAR (c);
  60.   if (!text_mode || (c != 0x0d && c != 0x1a))
  61.     {
  62.       putc (c, crc_outfile);
  63.     }
  64. }
  65.  
  66. int getc_crc ()
  67. {
  68.   int    c;
  69.  
  70.   if (crc_getc_cashe != EOF)
  71.     {
  72.       c = crc_getc_cashe;
  73.       crc_getc_cashe = EOF;
  74.       CRC_CHAR (c);
  75.       crc_size++;
  76.     }
  77.   else if ((c = getc (crc_infile)) != EOF)
  78.     {
  79.       if (text_mode && c == 0x0a)
  80.     {
  81.       crc_getc_cashe = c;
  82.       c = 0x0d;
  83.     }
  84.       CRC_CHAR (c);
  85.       crc_size++;
  86.     }
  87.   return c;
  88. }
  89. #endif
  90.  
  91.  
  92.  
  93. init_crc ()
  94. {
  95.   static int        inited = 0;
  96.   register unsigned int    *p = crc_table;
  97.   register int        i, j;
  98.   register unsigned int    x;
  99.  
  100.   if (!inited) {
  101.     for (j = 0; j < 256; j ++) {
  102.       x = j;
  103.       for (i = 0; i < 8; i ++) {
  104.     if ((x & 1) != 0) {
  105.       x = (x >> 1) ^ 0xa001;
  106.     } else {
  107.       x = (x >> 1);
  108.     }
  109.       }
  110.       *p ++ = x;
  111.     }
  112.     inited = 1;
  113.   }
  114.   crc_value = 0;
  115.   crc_getc_cashe = EOF;
  116.   crc_size = 0;
  117. }
  118.  
  119. /*----------------------------------------------------------------------*/
  120. /*                                    */
  121. /*----------------------------------------------------------------------*/
  122.  
  123. /* if return value is -1, see errno */
  124. copy_binary_file (ifp, ofp, size, crc_flag)
  125.      FILE    *ifp, *ofp;
  126.      long    size;
  127.      int    crc_flag;    /* as boolean value */
  128. {
  129.   char *buffer = (char *) rson;
  130.   int read_size;
  131.   int n;
  132.  
  133.   /* safty */
  134.   fflush (ofp);
  135.  
  136.   while (size > 0)
  137.     {
  138.       read_size = ((size < (long)BUFFER_SIZE) ? (int)size : BUFFER_SIZE);
  139.  
  140.       n = fread (buffer, sizeof (char), read_size, ifp);
  141.  
  142.       if (n == 0)
  143.     read_error ();
  144.  
  145.       if (fwrite (buffer, sizeof (char), n, ofp) < n)
  146.     write_error ();
  147.  
  148.       if (crc_flag)
  149.     crcsub (buffer, n);
  150.  
  151.       size -= (long)n;
  152.     }
  153. }
  154.  
  155. /* read UNIX text file '0A' and write generic text file '0D0A' */
  156. write_generic_text_file (ifp, ofp, size)
  157.      FILE    *ifp, *ofp;
  158.      long    size;
  159. {
  160.   char        buffer[BUFFER_SIZE];
  161.   int        read_size, write_count, n, m;
  162.   register char    *p, *p1, *e;
  163.  
  164.   /* safty */
  165.   fflush (ofp);
  166.  
  167.   write_count = 0;
  168.  
  169.   while (size > 0)
  170.     {
  171.       read_size = ((size < BUFFER_SIZE) ? (int)size : BUFFER_SIZE);
  172.  
  173.       n = fread (buffer, sizeof (char), read_size, ifp);
  174.  
  175.       if (n == 0)
  176.     read_error ();
  177.  
  178.       for (p1 = p = buffer, e = buffer + n; p < e; p++)
  179.     {
  180.       if (*p == '\n')
  181.         {
  182.           if ((m = p - p1) != 0)
  183.         {
  184.           if (fwrite (p1, sizeof (char), m, ofp) < m)
  185.             write_error ();
  186.           crcsub (p1, m);
  187.         }
  188.           putc (0x0d, ofp);
  189.           if (feof (ofp))
  190.         write_error ();
  191.           CRC_CHAR (0x0d);
  192.           p1 = p;
  193.           write_count ++;
  194.         }
  195.     }
  196.       if ((m = p - p1) != 0)
  197.     {
  198.       if (fwrite (p1, sizeof (char), m, ofp) < m)
  199.         write_error ();
  200.       crcsub (p1, m);
  201.     }
  202.  
  203.       write_count += (long)n;
  204.       size -= (long)n;
  205.     }
  206.  
  207.   crc_size = write_count;
  208. }
  209.  
  210. /* read generic text file '0D0A' and write UNIX text file '0A' */
  211. read_generic_text_file (ifp, ofp, size, crc_flag)
  212.      FILE    *ifp, *ofp;
  213.      long    size;
  214.      int    crc_flag;
  215. {
  216.   char        buffer[BUFFER_SIZE];
  217.   int        read_size, write_size, n, m;
  218.   register char *p, *p1, *e;
  219.  
  220.   /* safty */
  221.   fflush (ofp);
  222.  
  223.   while (size > 0)
  224.     {
  225.       read_size = ((size < BUFFER_SIZE) ? (int)size : BUFFER_SIZE);
  226.  
  227.       n = fread (buffer, sizeof (char), read_size, ifp);
  228.  
  229.       if (n == 0)
  230.     read_error ();
  231.  
  232.       crcsub (buffer, n);
  233.  
  234.       for (p1 = p = buffer, e = buffer + n; p < e; p ++)
  235.     {
  236.       if (*p == 0x0d)
  237.         {
  238.           if ((m = p - p1) != 0)
  239.         {
  240.           if (fwrite (p1, sizeof (char), m, ofp) < m)
  241.             write_error ();
  242.         }
  243.           p1 = p+1;
  244.         }
  245.     }
  246.       if ((m = p - p1) != 0)
  247.     {
  248.       if (fwrite (p1, sizeof (char), m, ofp) < m)
  249.         write_error ();
  250.     }
  251.  
  252.       size -= (long)n;
  253.     }
  254. }
  255.  
  256.  
  257. /*----------------------------------------------------------------------*/
  258. /*                                    */
  259. /*----------------------------------------------------------------------*/
  260.  
  261.  
  262. copy_file (ifp, ofp, size)
  263.      FILE    *ifp, *ofp;
  264.      long    size;
  265. {
  266.   copy_binary_file (ifp, ofp, size, 0);
  267. }
  268.  
  269. /*ARGSUSED*/
  270. int decode_stored_crc (ifp, ofp, original_size, name)
  271.      FILE    *ifp, *ofp;
  272.      long    original_size;
  273.      char    *name;
  274. {
  275.   init_crc ();
  276.  
  277.   if (text_mode)
  278.     {
  279.       read_generic_text_file (ifp, ofp, original_size, 1);
  280.       return crc_value;
  281.     }
  282.   else
  283.     {
  284.       copy_binary_file (ifp, ofp, original_size, 1);
  285.       return crc_value;
  286.     }
  287. }
  288.  
  289. /*ARGSUSED*/
  290. int decode_stored_nocrc (ifp, ofp, original_size, name)
  291.      FILE    *ifp, *ofp;
  292.      long    original_size;
  293.      char    *name;
  294. {
  295.   if (text_mode)
  296.     {
  297.       read_generic_text_file (ifp, ofp, original_size, 0);
  298.       return 0;            /* DUMMY */
  299.     }
  300.   else
  301.     {
  302.       copy_binary_file (ifp, ofp, original_size, 0);
  303.     }
  304.   return 0;            /* DUMMY */
  305. }
  306.  
  307. int encode_stored_crc (ifp, ofp, size, original_size_var, write_size_var)
  308.      FILE       *ifp, *ofp;
  309.      long       size;
  310.      long    *original_size_var;
  311.      long    *write_size_var;
  312. {
  313.   init_crc ();
  314.  
  315.   if (text_mode)
  316.     {
  317.       write_generic_text_file (ifp, ofp, size);
  318.       *original_size_var = *write_size_var = crc_size;
  319.       return crc_value;
  320.     }
  321.   else
  322.     {
  323.       copy_binary_file (ifp, ofp, size, 1);
  324.       *original_size_var = size;
  325.       *write_size_var = size;
  326.       return crc_value;
  327.     }
  328. }
  329.