1 | /* |
---|
2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/tarmail/btoa.c,v $ |
---|
3 | * $Author: jtkohl $ |
---|
4 | * $Locker: $ |
---|
5 | * $Header: /afs/dev.mit.edu/source/repository/athena/bin/tarmail/btoa.c,v 1.1 1985-04-25 20:34:37 jtkohl Exp $ |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef lint |
---|
9 | static char *rcsid_btoa_c = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/tarmail/btoa.c,v 1.1 1985-04-25 20:34:37 jtkohl Exp $"; |
---|
10 | #endif lint |
---|
11 | |
---|
12 | /*stream filter to change 8 bit bytes into printable ascii*/ |
---|
13 | /*computes the number of bytes, and three kinds of simple checksums*/ |
---|
14 | /*assumes that int is 32 bits*/ |
---|
15 | |
---|
16 | /*incoming bytes are collected into 32-bit words, then printed in base 85*/ |
---|
17 | /* exp(85,5) > exp(2,32) */ |
---|
18 | /*the characters used are between ' ' and 't'*/ |
---|
19 | /*'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.*/ |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | |
---|
23 | #define reg register |
---|
24 | |
---|
25 | #define MAXPERLINE 78 |
---|
26 | |
---|
27 | int Ceor = 0; |
---|
28 | int Csum = 0; |
---|
29 | int Crot = 0; |
---|
30 | |
---|
31 | int ccount = 0; |
---|
32 | int bcount = 0; |
---|
33 | int word; |
---|
34 | |
---|
35 | #define EN(c) ((c) + ' ') |
---|
36 | |
---|
37 | encode(c) reg c; |
---|
38 | { |
---|
39 | Ceor ^= c; |
---|
40 | Csum += c; |
---|
41 | Csum += 1; |
---|
42 | if ((Crot & 0x80000000)) { |
---|
43 | Crot <<= 1; |
---|
44 | Crot += 1; |
---|
45 | } |
---|
46 | else{ |
---|
47 | Crot <<= 1; |
---|
48 | } |
---|
49 | Crot += c; |
---|
50 | |
---|
51 | word <<= 8; |
---|
52 | word |= c; |
---|
53 | if (bcount == 3) { |
---|
54 | wordout(word); |
---|
55 | bcount = 0; |
---|
56 | } |
---|
57 | else{ |
---|
58 | bcount += 1; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | wordout(word) reg word; |
---|
63 | { |
---|
64 | if (word == 0) { |
---|
65 | charout('z'); |
---|
66 | } |
---|
67 | else{ |
---|
68 | /*first division must be unsigned*/; |
---|
69 | charout(EN((unsigned) word / (unsigned)(85 * 85 * 85 * 85))); |
---|
70 | word = (unsigned) word % (unsigned)(85 * 85 * 85 * 85); |
---|
71 | charout(EN(word / (85 * 85 * 85))); |
---|
72 | word %= (85 * 85 * 85); |
---|
73 | charout(EN(word / (85 * 85))); |
---|
74 | word %= (85 * 85); |
---|
75 | charout(EN(word / 85)); |
---|
76 | word %= 85; |
---|
77 | charout(EN(word)); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | charout(c) { |
---|
82 | putchar(c); |
---|
83 | ccount += 1; |
---|
84 | if (ccount == MAXPERLINE) { |
---|
85 | putchar('\n'); |
---|
86 | ccount = 0; |
---|
87 | |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | main(argc,argv) char **argv; |
---|
92 | { |
---|
93 | reg c, n; |
---|
94 | if (argc != 1) { |
---|
95 | fprintf(stderr,"bad args to %s\n", argv[0]); |
---|
96 | exit(2); |
---|
97 | } |
---|
98 | printf("xbtoa Begin\n"); |
---|
99 | n = 0; |
---|
100 | while ((c = getchar()) != EOF) { |
---|
101 | encode(c); |
---|
102 | n += 1; |
---|
103 | } |
---|
104 | while (bcount != 0) { |
---|
105 | encode(0); |
---|
106 | } |
---|
107 | /* n is written twice as crude cross check*/ |
---|
108 | printf("\nxbtoa End N %d %x E %x S %x R %x\n", n, n, Ceor, Csum, Crot); |
---|
109 | } |
---|