]> Pileus Git - ~andy/fetchmail/blob - base64.c
Nailed.
[~andy/fetchmail] / base64.c
1 /*
2  * base64.c -- base-64 conversion routines.
3  *
4  * For license terms, see the file COPYING in this directory.
5  *
6  * This base 64 encoding is defined in RFC2045 section 6.8,
7  * "Base64 Content-Transfer-Encoding", but lines must not be broken in the
8  * scheme used here.
9  */
10 #include <ctype.h>
11
12 static const char base64digits[] =
13    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
14
15 #define BAD     -1
16 static const char base64val[] = {
17     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
18     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
19     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
20      52, 53, 54, 55,  56, 57, 58, 59,  60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
21     BAD,  0,  1,  2,   3,  4,  5,  6,   7,  8,  9, 10,  11, 12, 13, 14,
22      15, 16, 17, 18,  19, 20, 21, 22,  23, 24, 25,BAD, BAD,BAD,BAD,BAD,
23     BAD, 26, 27, 28,  29, 30, 31, 32,  33, 34, 35, 36,  37, 38, 39, 40,
24      41, 42, 43, 44,  45, 46, 47, 48,  49, 50, 51,BAD, BAD,BAD,BAD,BAD
25 };
26 #define DECODE64(c)  (isascii(c) ? base64val[c] : BAD)
27
28 void to64frombits(unsigned char *out, const unsigned char *in, int inlen)
29 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
30 {
31     for (; inlen >= 3; inlen -= 3)
32     {
33         *out++ = base64digits[in[0] >> 2];
34         *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
35         *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
36         *out++ = base64digits[in[2] & 0x3f];
37         in += 3;
38     }
39     if (inlen > 0)
40     {
41         unsigned char fragment;
42     
43         *out++ = base64digits[in[0] >> 2];
44         fragment = (in[0] << 4) & 0x30;
45         if (inlen > 1)
46             fragment |= in[1] >> 4;
47         *out++ = base64digits[fragment];
48         *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
49         *out++ = '=';
50     }
51     *out = '\0';
52 }
53
54 int from64tobits(char *out, const char *in)
55 /* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */
56 {
57     int len = 0;
58     register char digit1, digit2, digit3, digit4;
59
60     if (in[0] == '+' && in[1] == ' ')
61         in += 2;
62     if (*in == '\r')
63         return(0);
64
65     do {
66         digit1 = in[0];
67         if (DECODE64(digit1) == BAD)
68             return(-1);
69         digit2 = in[1];
70         if (DECODE64(digit2) == BAD)
71             return(-1);
72         digit3 = in[2];
73         if (digit3 != '=' && DECODE64(digit3) == BAD)
74             return(-1); 
75         digit4 = in[3];
76         if (digit4 != '=' && DECODE64(digit4) == BAD)
77             return(-1);
78         in += 4;
79         *out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
80         ++len;
81         if (digit3 != '=')
82         {
83             *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
84             ++len;
85             if (digit4 != '=')
86             {
87                 *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
88                 ++len;
89             }
90         }
91     } while 
92         (*in != '\r' && digit4 != '=');
93
94     return (len);
95 }
96
97 /* base64.c ends here */