[1133] | 1 | /* atob: version 4.0 |
---|
| 2 | * stream filter to change printable ascii from "btoa" back into 8 bit bytes |
---|
| 3 | * if bad chars, or Csums do not match: exit(1) [and NO output] |
---|
| 4 | * |
---|
| 5 | * Paul Rutter Joe Orost |
---|
| 6 | * philabs!per petsd!joe |
---|
[8] | 7 | */ |
---|
| 8 | |
---|
| 9 | #include <stdio.h> |
---|
| 10 | |
---|
| 11 | #define reg register |
---|
| 12 | |
---|
| 13 | #define streq(s0, s1) strcmp(s0, s1) == 0 |
---|
| 14 | |
---|
[1133] | 15 | #define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x) |
---|
[8] | 16 | |
---|
[1133] | 17 | long int Ceor = 0; |
---|
| 18 | long int Csum = 0; |
---|
| 19 | long int Crot = 0; |
---|
| 20 | long int word = 0; |
---|
| 21 | long int bcount = 0; |
---|
| 22 | |
---|
[8] | 23 | fatal() { |
---|
| 24 | fprintf(stderr, "bad format or Csum to atob\n"); |
---|
| 25 | exit(1); |
---|
| 26 | } |
---|
| 27 | |
---|
[1133] | 28 | #define DE(c) ((c) - '!') |
---|
[8] | 29 | |
---|
[1133] | 30 | decode(c) |
---|
| 31 | reg c; |
---|
[8] | 32 | { |
---|
| 33 | if (c == 'z') { |
---|
| 34 | if (bcount != 0) { |
---|
| 35 | fatal(); |
---|
[1133] | 36 | } else { |
---|
[8] | 37 | byteout(0); |
---|
| 38 | byteout(0); |
---|
| 39 | byteout(0); |
---|
| 40 | byteout(0); |
---|
| 41 | } |
---|
[1133] | 42 | } else if ((c >= '!') && (c < ('!' + 85))) { |
---|
[8] | 43 | if (bcount == 0) { |
---|
| 44 | word = DE(c); |
---|
| 45 | ++bcount; |
---|
[1133] | 46 | } else if (bcount < 4) { |
---|
| 47 | word = times85(word); |
---|
[8] | 48 | word += DE(c); |
---|
| 49 | ++bcount; |
---|
[1133] | 50 | } else { |
---|
| 51 | word = times85(word) + DE(c); |
---|
| 52 | byteout((int)((word >> 24) & 255)); |
---|
| 53 | byteout((int)((word >> 16) & 255)); |
---|
| 54 | byteout((int)((word >> 8) & 255)); |
---|
| 55 | byteout((int)(word & 255)); |
---|
[8] | 56 | word = 0; |
---|
| 57 | bcount = 0; |
---|
| 58 | } |
---|
[1133] | 59 | } else { |
---|
[8] | 60 | fatal(); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
[1133] | 64 | FILE *tmp_file; |
---|
[8] | 65 | |
---|
[1133] | 66 | byteout(c) |
---|
| 67 | reg c; |
---|
[8] | 68 | { |
---|
| 69 | Ceor ^= c; |
---|
| 70 | Csum += c; |
---|
| 71 | Csum += 1; |
---|
| 72 | if ((Crot & 0x80000000)) { |
---|
| 73 | Crot <<= 1; |
---|
| 74 | Crot += 1; |
---|
[1133] | 75 | } else { |
---|
[8] | 76 | Crot <<= 1; |
---|
| 77 | } |
---|
| 78 | Crot += c; |
---|
[1133] | 79 | putc(c, tmp_file); |
---|
[8] | 80 | } |
---|
| 81 | |
---|
[1133] | 82 | main(argc, argv) |
---|
| 83 | char **argv; |
---|
[8] | 84 | { |
---|
[1133] | 85 | reg c; |
---|
| 86 | reg long int i; |
---|
| 87 | char tmp_name[100]; |
---|
[8] | 88 | char buf[100]; |
---|
[1133] | 89 | long int n1, n2, oeor, osum, orot; |
---|
| 90 | |
---|
[8] | 91 | if (argc != 1) { |
---|
| 92 | fprintf(stderr,"bad args to %s\n", argv[0]); |
---|
| 93 | exit(2); |
---|
| 94 | } |
---|
[1133] | 95 | sprintf(tmp_name, "/usr/tmp/atob.%x", getpid()); |
---|
| 96 | tmp_file = fopen(tmp_name, "w+"); |
---|
| 97 | if (tmp_file == NULL) { |
---|
[8] | 98 | fatal(); |
---|
| 99 | } |
---|
[1133] | 100 | unlink(tmp_name); /* Make file disappear */ |
---|
[8] | 101 | /*search for header line*/ |
---|
| 102 | for (;;) { |
---|
| 103 | if (fgets(buf, sizeof buf, stdin) == NULL) { |
---|
| 104 | fatal(); |
---|
| 105 | } |
---|
| 106 | if (streq(buf, "xbtoa Begin\n")) { |
---|
| 107 | break; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | while ((c = getchar()) != EOF) { |
---|
| 112 | if (c == '\n') { |
---|
| 113 | continue; |
---|
[1133] | 114 | } else if (c == 'x') { |
---|
[8] | 115 | break; |
---|
[1133] | 116 | } else { |
---|
[8] | 117 | decode(c); |
---|
| 118 | } |
---|
| 119 | } |
---|
[1133] | 120 | if(scanf("btoa End N %ld %lx E %lx S %lx R %lx\n", |
---|
| 121 | &n1, &n2, &oeor, &osum, &orot) != 5) { |
---|
[8] | 122 | fatal(); |
---|
| 123 | } |
---|
| 124 | if ((n1 != n2) || (oeor != Ceor) || (osum != Csum) || (orot != Crot)) { |
---|
| 125 | fatal(); |
---|
[1133] | 126 | } else { |
---|
[8] | 127 | /*copy OK tmp file to stdout*/; |
---|
[1133] | 128 | fseek(tmp_file, 0L, 0); |
---|
[8] | 129 | for (i = n1; --i >= 0;) { |
---|
[1133] | 130 | putchar(getc(tmp_file)); |
---|
[8] | 131 | } |
---|
| 132 | } |
---|
[1133] | 133 | exit(0); |
---|
[8] | 134 | } |
---|