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