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
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];
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];
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];
46 if ((ch >= 'A') && (ch <= 'Z'))
48 bits = (byte) (ch - 'A');
54 +int b64encode_chars_per_line = B64ENCODE_CHARS_PER_LINE;
56 +const char * b64encode_eolstr = B64ENCODE_EOLSTR;
58 +/*@-internalglobs -modfilesys @*/
59 +char * b64encode (const void * data, size_t ns)
61 + static char b64enc[] =
62 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
64 + const unsigned char *s = data;
65 + unsigned char *t, *te;
70 + if (s == NULL) return NULL;
71 + if (*s == '\0') return calloc(1, sizeof(*t));
73 + if (ns == 0) ns = strlen(s);
74 + nt = ((ns + 2) / 3) * 4;
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)
81 + nt += lc * strlen(b64encode_eolstr);
84 + t = te = malloc(nt + 1);
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));
98 + *te++ = b64enc[ (c >> 2) ], lc++;
99 + *te++ = b64enc[ ((c & 0x3) << 4) | (*s >> 4) ], lc++;
106 + *te++ = b64enc[ ((c & 0xf) << 2) | (*s >> 6) ], lc++;
111 + *te++ = b64enc[ (int)(*s & 0x3f) ], lc++;
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++)
126 + /* Append eol string if desired. */
127 + if (b64encode_chars_per_line > 0 && b64encode_eolstr != NULL) {
129 + for (e = b64encode_eolstr; *e != '\0'; e++)
136 + /*@-mustfree -compdef @*/
138 + /*@=mustfree =compdef @*/
140 +/*@=globs =internalglobs =modfilesys @*/
142 +/*@-internalglobs -modfilesys @*/
143 +#define CRC24_INIT 0xb704ceL
144 +#define CRC24_POLY 0x1864cfbL
146 +char * b64crc (const unsigned char * data, size_t ns)
148 + const unsigned char *s = data;
149 + uint32_t crc = CRC24_INIT;
153 + crc ^= (*s++) << 16;
154 + for (i = 0; i < 8; i++) {
156 + if (crc & 0x1000000)
161 + /*@-unrecog@*/ /* FIX: include endianness.h? */
162 + #if !WORDS_BIGENDIAN
163 + crc = swapu32(crc);
166 + data = (byte *)&crc;
169 + return b64encode(data, ns);
171 +/*@=internalglobs =modfilesys @*/
173 +const char * b64decode_whitespace = B64DECODE_WHITESPACE;
175 +/*@-internalglobs -modfilesys @*/
176 +int b64decode (const char * s, void ** datap, size_t *lenp)
178 + unsigned char b64dec[256];
179 + const unsigned char *t;
182 + unsigned a, b, c, d;
184 + if (s == NULL) return 1;
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;
198 + /* Mark whitespace characters. */
199 + if (b64decode_whitespace) {
201 + for (e = b64decode_whitespace; *e != '\0'; e++) {
202 + if (b64dec[ (unsigned)*e ] == 0x80)
203 + b64dec[ (unsigned)*e ] = 0x81;
207 + /* Validate input buffer */
209 + for (t = s; *t != '\0'; t++) {
210 + switch (b64dec[ (unsigned)*t ]) {
211 + case 0x80: /* invalid chararcter */
213 +fprintf(stderr, "--- b64decode %c(%02x) %02x\n", *t, (unsigned)(*t & 0xff), (unsigned)b64dec[ (unsigned)*t ]);
215 + /*@notreached@*/ /*@switchbreak@*/ break;
216 + case 0x81: /* white space */
217 + /*@switchbreak@*/ break;
220 + /*@switchbreak@*/ break;
224 + if (((unsigned)ns) & 0x3) return 2;
227 + t = te = malloc(nt + 1);
231 + /* Get next 4 characters, ignoring whitespace. */
232 + while ((a = b64dec[ (unsigned)*s++ ]) == 0x81)
234 + while ((b = b64dec[ (unsigned)*s++ ]) == 0x81)
236 + while ((c = b64dec[ (unsigned)*s++ ]) == 0x81)
238 + while ((d = b64dec[ (unsigned)*s++ ]) == 0x81)
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));
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;
256 + if (ns != 0) { /* XXX can't happen, just in case */
257 + if (t) free((void *)t);
264 + *datap = (void *)t;
266 + if (t) free((void *)t);
270 +/*@=globs =internalglobs =modfilesys @*/
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
277 #include "beecrypt.h"
280 + * Decode white space character set (default).
283 +/*@unchecked@*/ /*@observer@*/ /*@null@*/
284 +extern const char * b64decode_whitespace;
286 +#define B64DECODE_WHITESPACE " \f\n\r\t\v"
289 + * Encode 72 characters per line (default).
293 +extern int b64encode_chars_per_line;
295 +#define B64ENCODE_CHARS_PER_LINE 72
298 + * Encode end-of-line string (default).
301 +/*@unchecked@*/ /*@observer@*/ /*@null@*/
302 +extern const char * b64encode_eolstr;
304 +#define B64ENCODE_EOLSTR "\n"
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
317 +BEECRYPTAPI /*@only@*/ /*@null@*/ /*@unused@*/
318 +char * b64encode (const void * data, size_t ns)
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
327 +BEECRYPTAPI /*@only@*/ /*@null@*/ /*@unused@*/
328 +char * b64crc (const unsigned char * data, size_t ns)
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
338 +BEECRYPTAPI /*@unused@*/
339 +int b64decode (const char * s, /*@out@*/ void ** datap, /*@out@*/ size_t *lenp)
340 + /*@modifies *datap, *lenp @*/;
344 +BEECRYPTAPI /*@only@*/ /*@null@*/ /*@unused@*/
345 char* b64enc(const memchunk*);
350 +BEECRYPTAPI /*@only@*/ /*@null@*/ /*@unused@*/
351 memchunk* b64dec(const char*);