]> Pileus Git - ~andy/fetchmail/blob - base64.c
Cast arguments of is*() ctype.h functions to unsigned char to be 8-bit safe.
[~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 "config.h"
11 #include "fetchmail.h"
12 #include <ctype.h>
13
14 static const char base64digits[] =
15    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16
17 #define BAD     -1
18 static const char base64val[] = {
19     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
20     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
21     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
22      52, 53, 54, 55,  56, 57, 58, 59,  60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
23     BAD,  0,  1,  2,   3,  4,  5,  6,   7,  8,  9, 10,  11, 12, 13, 14,
24      15, 16, 17, 18,  19, 20, 21, 22,  23, 24, 25,BAD, BAD,BAD,BAD,BAD,
25     BAD, 26, 27, 28,  29, 30, 31, 32,  33, 34, 35, 36,  37, 38, 39, 40,
26      41, 42, 43, 44,  45, 46, 47, 48,  49, 50, 51,BAD, BAD,BAD,BAD,BAD
27 };
28 #define DECODE64(c)  (isascii(c) ? base64val[c] : BAD)
29
30 void to64frombits(unsigned char *out, const unsigned char *in, int inlen)
31 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
32 {
33     for (; inlen >= 3; inlen -= 3)
34     {
35         *out++ = base64digits[in[0] >> 2];
36         *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
37         *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
38         *out++ = base64digits[in[2] & 0x3f];
39         in += 3;
40     }
41     if (inlen > 0)
42     {
43         unsigned char fragment;
44     
45         *out++ = base64digits[in[0] >> 2];
46         fragment = (in[0] << 4) & 0x30;
47         if (inlen > 1)
48             fragment |= in[1] >> 4;
49         *out++ = base64digits[fragment];
50         *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
51         *out++ = '=';
52     }
53     *out = '\0';
54 }
55
56 int from64tobits(char *out, const char *in, int maxlen)
57 /* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */
58 /* maxlen limits output buffer size, set to zero to ignore */
59 {
60     int len = 0;
61     register unsigned char digit1, digit2, digit3, digit4;
62
63     if (in[0] == '+' && in[1] == ' ')
64         in += 2;
65     if (*in == '\r')
66         return(0);
67
68     do {
69         digit1 = in[0];
70         if (DECODE64(digit1) == BAD)
71             return(-1);
72         digit2 = in[1];
73         if (DECODE64(digit2) == BAD)
74             return(-1);
75         digit3 = in[2];
76         if (digit3 != '=' && DECODE64(digit3) == BAD)
77             return(-1); 
78         digit4 = in[3];
79         if (digit4 != '=' && DECODE64(digit4) == BAD)
80             return(-1);
81         in += 4;
82         ++len;
83         if (maxlen && len > maxlen)
84             return(-1);
85         *out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
86         if (digit3 != '=')
87         {
88             ++len;
89             if (maxlen && len > maxlen)
90                 return(-1);
91             *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
92             if (digit4 != '=')
93             {
94                 ++len;
95                 if (maxlen && len > maxlen)
96                     return(-1);
97                 *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
98             }
99         }
100     } while 
101         (*in && *in != '\r' && digit4 != '=');
102
103     return (len);
104 }
105
106 /* base64.c ends here */