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((unsigned char)(c)) ? base64val[c] : BAD)
29
30
void to64frombits(char *out, const void *in_, int inlen)
31
/* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
32
{
33
    const unsigned char *in = (const unsigned char *)in_;
34
35
    for (; inlen >= 3; inlen -= 3)
36
    {
37
	*out++ = base64digits[in[0] >> 2];
38
	*out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
39
	*out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
40
	*out++ = base64digits[in[2] & 0x3f];
41
	in += 3;
42
    }
43
    if (inlen > 0)
44
    {
45
	unsigned char fragment;
46
    
47
	*out++ = base64digits[in[0] >> 2];
48
	fragment = (in[0] << 4) & 0x30;
49
	if (inlen > 1)
50
	    fragment |= in[1] >> 4;
51
	*out++ = base64digits[fragment];
52
	*out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
53
	*out++ = '=';
54
    }
55
    *out = '\0';
56
}
57
58
int from64tobits(void *out_, const char *in, int maxlen)
59
/* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */
60
/* maxlen limits output buffer size, set to zero to ignore */
61
{
62
    int len = 0;
63
    register unsigned char digit1, digit2, digit3, digit4;
64
    unsigned char *out = (unsigned char *)out_;
65
66
    if (in[0] == '+' && in[1] == ' ')
67
	in += 2;
68
    if (*in == '\r')
69
	return(0);
70
71
    do {
72
	digit1 = in[0];
73
	if (DECODE64(digit1) == BAD)
74
	    return(-1);
75
	digit2 = in[1];
76
	if (DECODE64(digit2) == BAD)
77
	    return(-1);
78
	digit3 = in[2];
79
	if (digit3 != '=' && DECODE64(digit3) == BAD)
80
	    return(-1); 
81
	digit4 = in[3];
82
	if (digit4 != '=' && DECODE64(digit4) == BAD)
83
	    return(-1);
84
	in += 4;
85
	++len;
86
	if (maxlen && len > maxlen)
87
	    return(-1);
88
	*out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
89
	if (digit3 != '=')
90
	{
91
	    ++len;
92
	    if (maxlen && len > maxlen)
93
	        return(-1);
94
	    *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
95
	    if (digit4 != '=')
96
	    {
97
	        ++len;
98
		if (maxlen && len > maxlen)
99
		    return(-1);
100
		*out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
101
	    }
102
	}
103
    } while 
104
	(*in && *in != '\r' && digit4 != '=');
105
106
    return (len);
107
}
108
109
/* base64.c ends here */