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