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