home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / comm / atob-4.0.lha / ATOB / BTOA-22.C < prev    next >
C/C++ Source or Header  |  1987-04-17  |  6KB  |  224 lines

  1. BTOA-22.C
  2. /*
  3.  * btoa: version 2.2 for cpm/msdos on packet radio            *
  4.  *
  5.  * btoa: version 4.0                         *
  6.  * purpose: stream filter to change 8 bit bytes into printable ascii  *
  7.  * computes the number of bytes, and three kinds of simple checksums  *
  8.  * incoming bytes are collected into 32-bit words, then printed in    *
  9.  * base 85  exp(85,5) > exp(2,32). The ASCII characters used are be-  *
  10.  * tween '!' and 'u'. 'z' encodes 32-bit zero; 'x' is used to mark    *
  11.  * the end of encoded data.                     *
  12.  *                              *
  13.  *  Paul Rutter      Joe Orost                  *
  14.  *  philabs!per      petsd!joe                  *
  15.  *                              *
  16.  *  WARNING: this version is not compatible with the original as sent *
  17.  *  out on the net.  The original encoded from ' ' to 't'; which      *
  18.  *  cause problems with some mailers (stripping off trailing blanks). *
  19.  *
  20.  *                                                                    *
  21.  *  86May30 - modified progress displays and removed a debugging dis- *
  22.  *            inadvertently left in |br| ka2bqe               *
  23.  *  86May23 - modified for '-h' or '-H', also added variable line     *
  24.  *            length, and byte counts output to console, and fixed    *
  25.  *            bug in ouput overwrite query |br| ka2bqe                *
  26.  *  86May19 - modified for command line input and '-h' option to pro- *
  27.  *            duce 62 char output lines for HF packet, and place in-  *
  28.  *            put file name in header of output file  |br| ka2bqe     *
  29.  *  86May05 - modified to compile/run for packet radio under cpm-80 & *
  30.  *            msdos by Brian B. Riley ( |br| - ka2bqe) - RATS         *
  31.  *  Hacked to work with non-UNIX machines by Brian Lloyd, 86/04/14    *
  32.  *                                                                    *
  33.  */
  34.  
  35. #include <stdio.h>
  36.  
  37. #define reg register
  38.  
  39. #define MAXPERLINE    78   /* use standard unit record line 78+2*/
  40. #define MAX_HF_PERLINE   60   /* HF packet uses 64 byte packets so */
  41.             /* we'll use 60 */
  42.  
  43. #define streq(s0, s1)  strcmp(s0, s1) == 0
  44.  
  45. long int Ceor = 0;
  46. long int Csum = 0;
  47. long int Crot = 0;
  48.  
  49. long int ccount = 0;
  50. long int bcount = 0;
  51. long unsigned int word;
  52.  
  53. long int out_count;
  54. int line_length;
  55.  
  56. FILE * inf;
  57. FILE * outf;
  58.  
  59. #define EN(c)   (int) ((c) + '!')
  60.  
  61. /*
  62.  * main
  63.  */
  64. main(argc, argv)
  65. char **argv;
  66. {
  67.    reg      c;
  68.    reg      long int n;
  69.    char      infile[20];
  70.    char      outfile[20];
  71.    char      line_buf[16];
  72.  
  73.    line_length = MAXPERLINE;   /* default to 78      */
  74.    printf(" btoa: Binary-to-Ascii conversion\n       RATS  v2.2\n");
  75.  
  76.         if (argc < 2) {
  77.                 printf("Usage: btoa [-h || -l xxx] infile outfile \n");
  78.                 exit(2);
  79.         }
  80.    if (argv[1][0] == '-') {
  81.        switch (argv[1][1]) {
  82.          case 'h':
  83.          case 'H':
  84.       line_length = MAX_HF_PERLINE;     /* less for HF packet */
  85.       break;
  86.          case 'l':
  87.          case 'L':
  88.       line_length = atoi(argv[2]);
  89.       if (line_length < 4 || line_length > 251) {
  90.           printf("\m LINELENGTH specified out of range - default to 78\n");
  91.           line_length = 78;
  92.       }
  93.            argv++;                   /* advance the arg pointers   */
  94.            argc--;                   /* decrement arg counter   */
  95.       break;
  96.          default:
  97.       printf("\n UNRECOGNIZED option - ignored !\n");
  98.       break;
  99.        }
  100.        argv++;                   /* advance the arg pointers   */
  101.        argc--;                   /* decrement arg counter   */
  102.    } 
  103.         if (argc < 2) {
  104.        printf("Usage: btoa [-h || -l xxx] infile outfile \n");
  105.        exit();
  106.           }
  107.    strcpy(infile,argv[1]);  /* get inputfile name */
  108.  
  109.         while ((inf = fopen(infile, "r")) == NULL) {
  110.        printf( " Error opening file - %s\n Input file:  ",
  111.          infile);
  112.        if (gets( infile ) == NULL)
  113.       exit();
  114.         }
  115.    strcpy(outfile,argv[2]);
  116.  
  117.    while ((outf = fopen(outfile,"r")) != NULL) {
  118.        fclose(outf);
  119.        printf("\n Target file name exists! Overwrite (Y/N) ? ");
  120.        gets(line_buf);
  121.        if (toupper(line_buf[0]) == 'Y') {
  122.       break;
  123.        } else {
  124.       printf (" Enter new output filename : ");
  125.       gets(outfile);
  126.        }
  127.    }
  128.    fclose(outf);
  129.  
  130.         while ((outf = fopen(outfile, "w")) == NULL) {
  131.        printf( " Error opening file - %s\n Output file: ",
  132.          infile);
  133.        fclose (outf);
  134.        if (gets( outfile ) == NULL)
  135.       exit();
  136.         }
  137.    printf(" encoding file: %s ===> %s",infile, outfile);
  138.    fprintf( outf, "xbtoa Begin %s\n", infile);
  139.    n = 0;
  140.    while ((c=getc(inf))!=EOF) {encode(c);n+=1;}
  141.    while (bcount!=0) {encode(0);
  142.    }
  143.  
  144.    /* n twice as crude cross check*/
  145.  
  146.    fprintf(outf,"\nxbtoa End N %ld %lx E %lx S %lx R %lx\n",n,n,Ceor,Csum,Crot);
  147.    putc('\n',outf);
  148.    fclose(inf);
  149.    fclose(outf);
  150.    printf("\n ===> *** EOF *** ===> checksum written\n %ld bytes read, %ld bytes en
  151. coded\n"
  152.       ,n, out_count);
  153.    exit();
  154. }
  155.  
  156. /*
  157.  * charout - output encoded character to the file and count chars to    *
  158.  *           'break the data into manageable lines                      *
  159.  */
  160. charout(c)
  161. {
  162.    putc(c,outf);
  163.    ccount += 1;
  164.    out_count += 1;
  165.    if (ccount == line_length) {
  166.       putc('\r',outf);   /* original function didn't have */
  167.       putc('\n',outf);   /* the /r - packet NEEDS returns */
  168.       out_count += 2;
  169.       ccount = 0;
  170.    }
  171. }
  172.  
  173. /*
  174.  * encode - stream encoding of the data flow                            *
  175.  */
  176. encode(c)
  177.    reg c;
  178. {
  179.    Ceor ^= c;
  180.    Csum += c;
  181.    Csum += 1;
  182.    if ((Crot & 0x80000000L)) {
  183.       Crot <<= 1;
  184.       Crot += 1;
  185.    } else {
  186.       Crot <<= 1;
  187.    }
  188.    Crot += c;
  189.  
  190.    word <<= 8;
  191.    word |= c;
  192.    if (bcount==3) {wordout(word);bcount=0;}
  193.     else {bcount+=1;}
  194. }
  195.  
  196. /*
  197.  * wordout - output the encoded 4 byte stream as 5 'digit' word         *
  198.  */
  199. wordout(word)
  200.    reg unsigned long int word;
  201. {
  202.    if (word==0) {charout('z');}
  203.     else {
  204.  
  205.       /* 85^4 */
  206.       charout(EN(word / 52200625L ));
  207.       word %= 52200625L;
  208.  
  209.       /* 85^3 */
  210.       charout(EN(word / 614125L ));
  211.       word %= 614125L;
  212.  
  213.       /* 85^2 */
  214.       charout(EN(word / 7225L ));
  215.       word %= 7225L;
  216.  
  217.       /* 85^1 */
  218.       charout(EN(word / 85));
  219.       word %= 85;
  220.       charout(EN(word));
  221.    }
  222. }
  223.  
  224.