]>
Commit | Line | Data |
---|---|---|
f4b80497 AF |
1 | diff -ru beecrypt/base64.c beecrypt.rpm/base64.c |
2 | --- beecrypt/base64.c Thu May 15 15:35:52 2003 | |
3 | +++ beecrypt.rpm/base64.c Fri May 16 16:16:40 2003 | |
4 | @@ -63,10 +55,10 @@ | |
5 | ||
6 | while (div > 0) | |
7 | { | |
8 | - buf[0] = to_b64[ (data[0] >> 2) & 0x3f]; | |
9 | - buf[1] = to_b64[((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf)]; | |
10 | - buf[2] = to_b64[((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3)]; | |
11 | - buf[3] = to_b64[ data[2] & 0x3f]; | |
12 | + buf[0] = to_b64[ ((unsigned)data[0] >> 2) & 0x3f]; | |
13 | + buf[1] = to_b64[(((unsigned)data[0] << 4) & 0x30) | (((unsigned)data[1] >> 4) & 0xf)]; | |
14 | + buf[2] = to_b64[(((unsigned)data[1] << 2) & 0x3c) | (((unsigned)data[2] >> 6) & 0x3)]; | |
15 | + buf[3] = to_b64[ (unsigned)data[2] & 0x3f]; | |
16 | data += 3; | |
17 | buf += 4; | |
18 | div--; | |
19 | @@ -81,16 +73,16 @@ | |
20 | switch (rem) | |
21 | { | |
22 | case 2: | |
23 | - buf[0] = to_b64[ (data[0] >> 2) & 0x3f]; | |
24 | - buf[1] = to_b64[((data[0] << 4) & 0x30) + ((data[1] >> 4) & 0xf)]; | |
25 | - buf[2] = to_b64[ (data[1] << 2) & 0x3c]; | |
26 | + buf[0] = to_b64[ ((unsigned)data[0] >> 2) & 0x3f]; | |
27 | + buf[1] = to_b64[(((unsigned)data[0] << 4) & 0x30) + (((unsigned)data[1] >> 4) & 0xf)]; | |
28 | + buf[2] = to_b64[ ((unsigned)data[1] << 2) & 0x3c]; | |
29 | buf[3] = '='; | |
30 | buf += 4; | |
31 | chars += 4; | |
32 | break; | |
33 | case 1: | |
34 | - buf[0] = to_b64[ (data[0] >> 2) & 0x3f]; | |
35 | - buf[1] = to_b64[ (data[0] << 4) & 0x30]; | |
36 | + buf[0] = to_b64[ ((unsigned)data[0] >> 2) & 0x3f]; | |
37 | + buf[1] = to_b64[ ((unsigned)data[0] << 4) & 0x30]; | |
38 | buf[2] = '='; | |
39 | buf[3] = '='; | |
40 | buf += 4; | |
41 | @@ -183,6 +177,7 @@ | |
42 | if (isspace(ch)) | |
43 | continue; | |
44 | ||
45 | + bits = 0; | |
46 | if ((ch >= 'A') && (ch <= 'Z')) | |
47 | { | |
48 | bits = (byte) (ch - 'A'); | |
49 | @@ -237,3 +232,222 @@ | |
50 | ||
51 | return rc; | |
52 | } | |
53 | + | |
54 | +int b64encode_chars_per_line = B64ENCODE_CHARS_PER_LINE; | |
55 | + | |
56 | +const char * b64encode_eolstr = B64ENCODE_EOLSTR; | |
57 | + | |
58 | +/*@-internalglobs -modfilesys @*/ | |
59 | +char * b64encode (const void * data, size_t ns) | |
60 | +{ | |
61 | + static char b64enc[] = | |
62 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
63 | + const char *e; | |
64 | + const unsigned char *s = data; | |
65 | + unsigned char *t, *te; | |
66 | + int nt; | |
67 | + int lc; | |
68 | + unsigned c; | |
69 | + | |
70 | + if (s == NULL) return NULL; | |
71 | + if (*s == '\0') return calloc(1, sizeof(*t)); | |
72 | + | |
73 | + if (ns == 0) ns = strlen(s); | |
74 | + nt = ((ns + 2) / 3) * 4; | |
75 | + | |
76 | + /* Add additional bytes necessary for eol string(s). */ | |
77 | + if (b64encode_chars_per_line > 0 && b64encode_eolstr != NULL) { | |
78 | + lc = (nt + b64encode_chars_per_line - 1) / b64encode_chars_per_line; | |
79 | + if (((nt + b64encode_chars_per_line - 1) % b64encode_chars_per_line) != 0) | |
80 | + ++lc; | |
81 | + nt += lc * strlen(b64encode_eolstr); | |
82 | + } | |
83 | + | |
84 | + t = te = malloc(nt + 1); | |
85 | + | |
86 | + lc = 0; | |
87 | + if (te) | |
88 | + while (ns > 0) { | |
89 | + | |
90 | +if (0) | |
91 | +fprintf(stderr, "%7u %02x %02x %02x -> %02x %02x %02x %02x\n", | |
92 | +(unsigned)ns, (unsigned)s[0], (unsigned)s[1], (unsigned)s[2], | |
93 | +(unsigned)(s[0] >> 2), | |
94 | +(unsigned)((s[0] & 0x3) << 4) | (s[1] >> 4), | |
95 | +(unsigned)((s[1] & 0xf) << 2) | (s[2] >> 6), | |
96 | +(unsigned)(s[2]& 0x3f)); | |
97 | + c = *s++; | |
98 | + *te++ = b64enc[ (c >> 2) ], lc++; | |
99 | + *te++ = b64enc[ ((c & 0x3) << 4) | (*s >> 4) ], lc++; | |
100 | + if (--ns == 0) { | |
101 | + *te++ = '='; | |
102 | + *te++ = '='; | |
103 | + continue; | |
104 | + } | |
105 | + c = *s++; | |
106 | + *te++ = b64enc[ ((c & 0xf) << 2) | (*s >> 6) ], lc++; | |
107 | + if (--ns == 0) { | |
108 | + *te++ = '='; | |
109 | + continue; | |
110 | + } | |
111 | + *te++ = b64enc[ (int)(*s & 0x3f) ], lc++; | |
112 | + | |
113 | + /* Append eol string if desired. */ | |
114 | + if (b64encode_chars_per_line > 0 && b64encode_eolstr != NULL) { | |
115 | + if (lc >= b64encode_chars_per_line) { | |
116 | + for (e = b64encode_eolstr; *e != '\0'; e++) | |
117 | + *te++ = *e; | |
118 | + lc = 0; | |
119 | + } | |
120 | + } | |
121 | + s++; | |
122 | + --ns; | |
123 | + } | |
124 | + | |
125 | + if (te) { | |
126 | + /* Append eol string if desired. */ | |
127 | + if (b64encode_chars_per_line > 0 && b64encode_eolstr != NULL) { | |
128 | + if (lc != 0) { | |
129 | + for (e = b64encode_eolstr; *e != '\0'; e++) | |
130 | + *te++ = *e; | |
131 | + } | |
132 | + } | |
133 | + *te = '\0'; | |
134 | + } | |
135 | + | |
136 | + /*@-mustfree -compdef @*/ | |
137 | + return t; | |
138 | + /*@=mustfree =compdef @*/ | |
139 | +} | |
140 | +/*@=globs =internalglobs =modfilesys @*/ | |
141 | + | |
142 | +/*@-internalglobs -modfilesys @*/ | |
143 | +#define CRC24_INIT 0xb704ceL | |
144 | +#define CRC24_POLY 0x1864cfbL | |
145 | + | |
146 | +char * b64crc (const unsigned char * data, size_t ns) | |
147 | +{ | |
148 | + const unsigned char *s = data; | |
149 | + uint32_t crc = CRC24_INIT; | |
150 | + | |
151 | + while (ns-- > 0) { | |
152 | + int i; | |
153 | + crc ^= (*s++) << 16; | |
154 | + for (i = 0; i < 8; i++) { | |
155 | + crc <<= 1; | |
156 | + if (crc & 0x1000000) | |
157 | + crc ^= CRC24_POLY; | |
158 | + } | |
159 | + } | |
160 | + crc &= 0xffffff; | |
161 | + /*@-unrecog@*/ /* FIX: include endianness.h? */ | |
162 | + #if !WORDS_BIGENDIAN | |
163 | + crc = swapu32(crc); | |
164 | + #endif | |
165 | + /*@=unrecog@*/ | |
166 | + data = (byte *)&crc; | |
167 | + data++; | |
168 | + ns = 3; | |
169 | + return b64encode(data, ns); | |
170 | +} | |
171 | +/*@=internalglobs =modfilesys @*/ | |
172 | + | |
173 | +const char * b64decode_whitespace = B64DECODE_WHITESPACE; | |
174 | + | |
175 | +/*@-internalglobs -modfilesys @*/ | |
176 | +int b64decode (const char * s, void ** datap, size_t *lenp) | |
177 | +{ | |
178 | + unsigned char b64dec[256]; | |
179 | + const unsigned char *t; | |
180 | + unsigned char *te; | |
181 | + int ns, nt; | |
182 | + unsigned a, b, c, d; | |
183 | + | |
184 | + if (s == NULL) return 1; | |
185 | + | |
186 | + /* Setup character lookup tables. */ | |
187 | + memset(b64dec, 0x80, sizeof(b64dec)); | |
188 | + for (c = 'A'; c <= 'Z'; c++) | |
189 | + b64dec[ c ] = 0 + (c - 'A'); | |
190 | + for (c = 'a'; c <= 'z'; c++) | |
191 | + b64dec[ c ] = 26 + (c - 'a'); | |
192 | + for (c = '0'; c <= '9'; c++) | |
193 | + b64dec[ c ] = 52 + (c - '0'); | |
194 | + b64dec[(unsigned)'+'] = 62; | |
195 | + b64dec[(unsigned)'/'] = 63; | |
196 | + b64dec[(unsigned)'='] = 0; | |
197 | + | |
198 | + /* Mark whitespace characters. */ | |
199 | + if (b64decode_whitespace) { | |
200 | + const char *e; | |
201 | + for (e = b64decode_whitespace; *e != '\0'; e++) { | |
202 | + if (b64dec[ (unsigned)*e ] == 0x80) | |
203 | + b64dec[ (unsigned)*e ] = 0x81; | |
204 | + } | |
205 | + } | |
206 | + | |
207 | + /* Validate input buffer */ | |
208 | + ns = 0; | |
209 | + for (t = s; *t != '\0'; t++) { | |
210 | + switch (b64dec[ (unsigned)*t ]) { | |
211 | + case 0x80: /* invalid chararcter */ | |
212 | +if (0) | |
213 | +fprintf(stderr, "--- b64decode %c(%02x) %02x\n", *t, (unsigned)(*t & 0xff), (unsigned)b64dec[ (unsigned)*t ]); | |
214 | + return 3; | |
215 | + /*@notreached@*/ /*@switchbreak@*/ break; | |
216 | + case 0x81: /* white space */ | |
217 | + /*@switchbreak@*/ break; | |
218 | + default: | |
219 | + ns++; | |
220 | + /*@switchbreak@*/ break; | |
221 | + } | |
222 | + } | |
223 | + | |
224 | + if (((unsigned)ns) & 0x3) return 2; | |
225 | + | |
226 | + nt = (ns / 4) * 3; | |
227 | + t = te = malloc(nt + 1); | |
228 | + | |
229 | + while (ns > 0) { | |
230 | + | |
231 | + /* Get next 4 characters, ignoring whitespace. */ | |
232 | + while ((a = b64dec[ (unsigned)*s++ ]) == 0x81) | |
233 | + ; | |
234 | + while ((b = b64dec[ (unsigned)*s++ ]) == 0x81) | |
235 | + ; | |
236 | + while ((c = b64dec[ (unsigned)*s++ ]) == 0x81) | |
237 | + ; | |
238 | + while ((d = b64dec[ (unsigned)*s++ ]) == 0x81) | |
239 | + ; | |
240 | + | |
241 | +if (0) | |
242 | +fprintf(stderr, "%7u %02x %02x %02x %02x -> %02x %02x %02x\n", | |
243 | +(unsigned)ns, a, b, c, d, | |
244 | +(((a << 2) | (b >> 4)) & 0xff), | |
245 | +(((b << 4) | (c >> 2)) & 0xff), | |
246 | +(((c << 6) | d) & 0xff)); | |
247 | + | |
248 | + ns -= 4; | |
249 | + *te++ = (a << 2) | (b >> 4); | |
250 | + if (s[-2] == '=') break; | |
251 | + *te++ = (b << 4) | (c >> 2); | |
252 | + if (s[-1] == '=') break; | |
253 | + *te++ = (c << 6) | d; | |
254 | + } | |
255 | + | |
256 | + if (ns != 0) { /* XXX can't happen, just in case */ | |
257 | + if (t) free((void *)t); | |
258 | + return 1; | |
259 | + } | |
260 | + if (lenp) | |
261 | + *lenp = (te - t); | |
262 | + | |
263 | + if (datap) | |
264 | + *datap = (void *)t; | |
265 | + else | |
266 | + if (t) free((void *)t); | |
267 | + | |
268 | + return 0; | |
269 | +} | |
270 | +/*@=globs =internalglobs =modfilesys @*/ | |
271 | +/*@=type@*/ | |
272 | diff -ru beecrypt/base64.h beecrypt.rpm/base64.h | |
273 | --- beecrypt/base64.h Fri Jun 21 14:17:24 2002 | |
274 | +++ beecrypt.rpm/base64.h Fri May 16 16:16:40 2003 | |
275 | @@ -27,13 +27,76 @@ | |
276 | ||
277 | #include "beecrypt.h" | |
278 | ||
279 | +/** | |
280 | + * Decode white space character set (default). | |
281 | + */ | |
282 | +/*@-exportlocal@*/ | |
283 | +/*@unchecked@*/ /*@observer@*/ /*@null@*/ | |
284 | +extern const char * b64decode_whitespace; | |
285 | +/*@=exportlocal@*/ | |
286 | +#define B64DECODE_WHITESPACE " \f\n\r\t\v" | |
287 | + | |
288 | +/** | |
289 | + * Encode 72 characters per line (default). | |
290 | + */ | |
291 | +/*@-exportlocal@*/ | |
292 | +/*@unchecked@*/ | |
293 | +extern int b64encode_chars_per_line; | |
294 | +/*@=exportlocal@*/ | |
295 | +#define B64ENCODE_CHARS_PER_LINE 72 | |
296 | + | |
297 | +/** | |
298 | + * Encode end-of-line string (default). | |
299 | + */ | |
300 | +/*@-exportlocal@*/ | |
301 | +/*@unchecked@*/ /*@observer@*/ /*@null@*/ | |
302 | +extern const char * b64encode_eolstr; | |
303 | +/*@=exportlocal@*/ | |
304 | +#define B64ENCODE_EOLSTR "\n" | |
305 | + | |
306 | #ifdef __cplusplus | |
307 | extern "C" { | |
308 | #endif | |
309 | ||
310 | -BEECRYPTAPI | |
311 | +/** | |
312 | + * Encode chunks of 3 bytes of binary input into 4 bytes of base64 output. | |
313 | + * @param data binary data | |
314 | + * @param ns no. bytes of data (0 uses strlen(data)) | |
315 | + * @return (malloc'd) base64 string | |
316 | + */ | |
317 | +BEECRYPTAPI /*@only@*/ /*@null@*/ /*@unused@*/ | |
318 | +char * b64encode (const void * data, size_t ns) | |
319 | + /*@*/; | |
320 | + | |
321 | +/** | |
322 | + * Encode crc of binary input data into 5 bytes of base64 output. | |
323 | + * @param data binary data | |
324 | + * @param ns no. bytes of binary data | |
325 | + * @return (malloc'd) base64 string | |
326 | + */ | |
327 | +BEECRYPTAPI /*@only@*/ /*@null@*/ /*@unused@*/ | |
328 | +char * b64crc (const unsigned char * data, size_t ns) | |
329 | + /*@*/; | |
330 | + | |
331 | +/** | |
332 | + * Decode chunks of 4 bytes of base64 input into 3 bytes of binary output. | |
333 | + * @param s base64 string | |
334 | + * @retval datap address of (malloc'd) binary data | |
335 | + * @retval lenp address of no. bytes of binary data | |
336 | + * @return 0 on success, 1: s == NULL, 2: bad length, 3: bad char | |
337 | + */ | |
338 | +BEECRYPTAPI /*@unused@*/ | |
339 | +int b64decode (const char * s, /*@out@*/ void ** datap, /*@out@*/ size_t *lenp) | |
340 | + /*@modifies *datap, *lenp @*/; | |
341 | + | |
342 | +/** | |
343 | + */ | |
344 | +BEECRYPTAPI /*@only@*/ /*@null@*/ /*@unused@*/ | |
345 | char* b64enc(const memchunk*); | |
346 | -BEECRYPTAPI | |
347 | + | |
348 | +/** | |
349 | + */ | |
350 | +BEECRYPTAPI /*@only@*/ /*@null@*/ /*@unused@*/ | |
351 | memchunk* b64dec(const char*); | |
352 | ||
353 | #ifdef __cplusplus |