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