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

  1. D PRG/ATOB-22.C
  2.  
  3. /*
  4.  * atob  version 2.2 for cpm/msdos on packet radio
  5.  *       a program to transform binary to printable ASCII at a minimal*
  6.  *       'cost' in conversion space - ratio is about 5/4
  7.  *
  8.  * atob: version 4.0
  9.  * stream filter to change printable ascii from "btoa" back into 8 bit*
  10.  * bytes if bad chars, or Csums do not match: exit(1) [and NO output] *
  11.  *
  12.  *  Paul Rutter      Joe Orost
  13.  *  philabs!per      petsd!joe
  14.  *
  15.  *
  16.  *                                                                    *
  17.  *  86May30 - corrected 'y' as file name in duplicate target file and *
  18.  *            added ability to have garbage in front of, on the same  *
  19.  *            line with the 'xbtoa Begin' line |br| ka2bqe         *
  20.  *  86May19 - modified to command line parsing, and recovery of input *
  21.  *            file name from header line. Should be able to handle ver*
  22.  *            sion 1.0 files w/o file name in header |br| - ka2bqe    *
  23.  *  86May05 - modified to compile/run for packet radio under cpm-80 & *
  24.  *            msdos by Brian B. Riley (ka2bqe) - RATS            *
  25.  *  Hacked to work with non-UNIX machines by Brian Lloyd, 86/04/17    *
  26.  *                                                                    *
  27.  */
  28.  
  29. #include <stdio.h>
  30. #define reg register
  31. #define streq(s0, s1)  strcmp(s0, s1) == 0
  32. #define times85(x)   ((((((x<<2)+x)<<2)+x)<<2)+x)
  33.  
  34. long int Ceor = 0;
  35. long int Csum = 0;
  36. long int Crot = 0;
  37. long int word = 0;
  38. long int bcount = 0;
  39.  
  40. FILE *      inf;
  41. FILE *      outf;
  42.  
  43. #define DE(c) ((c) - '!')
  44.  
  45. /*
  46.  * main - this is the ASCII-to-BINARY recovery routine. It queries for
  47.  *        and opens files and stream decodes and verifies checksum
  48.  */
  49.  
  50. main(argc, argv)
  51. char **argv;
  52. {
  53.    reg c;
  54.    reg long int i;
  55.    char   infile[64];
  56.    char   outfile[64];
  57.    char   buf[100];
  58.    long int n1, n2, oeor, osum, orot;
  59.  
  60.    printf("\n atob: Ascii-to-Binary recovery\n RATS v2.2\n");
  61.  
  62.    if(argc<2) {printf("Usage: atob infile \n");exit(2);}
  63.    strcpy(infile,argv[1]);  /* get inputfile name */
  64.    while ((inf = fopen(infile, "r")) == NULL) {
  65.        printf("\n Error opening file - %s\n Input file:  ",infile);
  66.        if (gets(infile)==NULL) exit();
  67.         }
  68.  
  69.    /*search for header line*/
  70.    while ((c=getc(inf))!=EOF) {/* look for 'x'*/
  71.        if (c=='x') {
  72.       if (fgets(buf, sizeof buf, inf) != NULL) {
  73.           if (strncmp(buf, "btoa Begin",10) == 0) break;
  74.       } else {printf("\n\7 *** EOF in header scan.\n" ); exit(0);}
  75.        }
  76.    }
  77.    if (c==EOF) {printf("\n\7 *** EOF in header scan.\n");exit(0);}
  78.  
  79.    outfile[0] = '\0';   /* null outfile name string */
  80.    sscanf(buf,"btoa Begin %s\n",outfile);
  81.         if (strlen(outfile) ) {
  82.        printf("\n Header found ===> File: [%s] ",outfile);
  83.    } else {
  84.        printf( "\n Possible OLD header - Enter output file:  ");
  85.        if (gets(outfile)==NULL) exit();
  86.         }
  87.    while ((outf = fopen(outfile,"r")) != NULL) {
  88.        fclose(outf);
  89.        printf("\n Target file name exists! Overwrite (Y/N) ? ");
  90.        gets(buf);
  91.        if (toupper(buf[0]) == 'Y') {break;}
  92.         else {
  93.       printf (" Enter new output filename : ");
  94.       gets(outfile);
  95.        }
  96.    }
  97.    fclose(outf);
  98.    printf(" ===> Decoding ");
  99.         if ((outf = fopen(outfile, "w")) == NULL) {
  100.        printf( " File OPEN ERROR - [%s] - Disk full ???\n",outfile);
  101.        fclose(outf);
  102.        exit();
  103.         }
  104.  
  105.    while ((c = getc(inf)) != EOF) {
  106.       if (c == '\n') {continue;}
  107.        else if (c=='\r') {continue;}
  108.        else if (c == 'x') {break;}
  109.        else {decode(c);}
  110.    }
  111.  
  112.    printf(" ===> *** EOF ***\n Checking trailer now");
  113.  
  114.    if(fscanf(inf,"btoa End N %ld %lx E %lx S %lx R %lx\n",
  115.              &n1, &n2, &oeor, &osum, &orot) != 5) {
  116.       printf(" ===> \7Failure reading the trailer line.\n" );
  117.       exit(0);
  118.    }
  119.    if ((n1 != n2) || (oeor != Ceor) || (osum != Csum) || (orot != Crot)) {
  120.       printf(" ===> \7File checksum is bad.\n");
  121.       exit(0);
  122.    } else {
  123.       printf(" ===> File checksum is good.\n");
  124.    }
  125.    fclose( inf );
  126.    fclose( outf );
  127.    exit(0);
  128. }
  129.  
  130. /*
  131.  * byteout - outputs a single binary byte to file                       *
  132.  */
  133. byteout(c)
  134.    reg c;
  135. {
  136.    Ceor ^= c;
  137.    Csum += c;
  138.    Csum += 1;
  139.    if ((Crot & 0x80000000L)) {
  140.       Crot <<= 1;
  141.       Crot += 1;
  142.    } else {
  143.       Crot <<= 1;
  144.    }
  145.    Crot += c;
  146.    putc(c, outf);
  147. }
  148.  
  149.  
  150. /*
  151.  * decode - stream decodes 5 'digit' ASCII  back to 4 byte binary       *
  152.  */
  153. decode(c)
  154.    reg c;
  155. {
  156.  
  157.    if (c == 'z') {
  158.       if (bcount != 0) {
  159.          printf("z not on word boundary in decode.\n");
  160.          exit(0);
  161.       } else {
  162.          byteout(0);
  163.          byteout(0);
  164.          byteout(0);
  165.          byteout(0);
  166.       }
  167.    } else if ((c >= '!') && (c < ('!' + 85))) {
  168.       if (bcount == 0) {
  169.          word = DE(c);
  170.          ++bcount;
  171.       } else if (bcount < 4) {
  172.          word = times85(word);
  173.          word += DE(c);
  174.          ++bcount;
  175.       } else {
  176.          word = times85(word) + DE(c);
  177.          byteout((int)((word >> 24) & 255));
  178.          byteout((int)((word >> 16) & 255));
  179.          byteout((int)((word >> 8) & 255));
  180.          byteout((int)(word & 255));
  181.          word = 0;
  182.          bcount = 0;
  183.       }
  184.    } else {
  185.       printf("Input character out of range in decode.(%02x)\n",c);
  186.       exit(0);
  187.    }
  188. }
  189.  
  190.