forked from shattered/macro11
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rad50.c
160 lines (129 loc) · 4.22 KB
/
rad50.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Functions to convert RAD50 to or from ASCII. */
/*
Copyright (c) 2001, Richard Krehbiel
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
o Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
o Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
o Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "rad50.h"
static char radtbl[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$. 0123456789";
/* rad50 converts from 0 to 3 ASCII (or EBCDIC, if your compiler is so
inclined) characters into a RAD50 word. */
unsigned rad50(
char *cp,
char **endp)
{
unsigned long acc = 0;
char *rp;
if (endp)
*endp = cp;
if (!*cp) /* Got to check for end-of-string manually, because strchr will call it a hit. :-/ */
return acc;
rp = strchr(radtbl, toupper((unsigned char)*cp));
if (rp == NULL) /* Not a RAD50 character */
return acc;
acc = ((int) (rp - radtbl)) * 03100; /* Convert */
cp++;
/* Now, do the same thing two more times... */
if (endp)
*endp = cp;
if (!*cp)
return acc;
rp = strchr(radtbl, toupper((unsigned char)*cp));
if (rp == NULL)
return acc;
acc += ((int) (rp - radtbl)) * 050;
cp++;
if (endp)
*endp = cp;
if (!*cp)
return acc;
rp = strchr(radtbl, toupper((unsigned char)*cp));
if (rp == NULL)
return acc;
acc += (int) (rp - radtbl);
cp++;
if (endp)
*endp = cp;
return acc; /* Done. */
}
/* rad50x2 - converts from 0 to 6 characters into two words of RAD50. */
void rad50x2(
char *cp,
unsigned *rp)
{
*rp++ = rad50(cp, &cp);
*rp = 0;
if (*cp)
*rp = rad50(cp, &cp);
}
/* unrad50 - converts a RAD50 word to three characters of ASCII. */
void unrad50(
unsigned word,
char *cp)
{
if (word < 0175000) { /* Is it legal RAD50? */
cp[0] = radtbl[word / 03100];
cp[1] = radtbl[(word / 050) % 050];
cp[2] = radtbl[word % 050];
} else
cp[0] = cp[1] = cp[2] = ' ';
}
/* ascii2rad50 - convert a single character to a RAD50 character */
int ascii2rad50(
char c)
{
char *rp;
if (c == '\0') /* Not a RAD50 character */
return -1;
rp = strchr(radtbl, toupper((unsigned char)c));
if (rp == NULL) /* Not a RAD50 character */
return -1;
return (int) (rp - radtbl); /* Convert */
}
/* packrad50word - packs up to 3 characters into a RAD50 word.
*
* The characters should be in the range [0, 050),
* such as having been converted by ascii2rad50().
*/
unsigned packrad50word(
char *cp,
int len)
{
unsigned long acc = 0;
if (len >= 1) {
acc += (cp[0] % 050) * 050 * 050;
}
if (len >= 2) {
acc += (cp[1] % 050) * 050;
}
if (len >= 3) {
acc += cp[2] % 050;
}
return acc;
}