NeoMutt  2024-12-12-19-ge4b57e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
base64.c
Go to the documentation of this file.
1
33#include "config.h"
34#include "base64.h"
35#include "buffer.h"
36#include "memory.h"
37#include "string2.h"
38
39#define BAD -1
40
44static const char B64Chars[64] = {
45 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
46 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
47 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
48 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
49 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
50};
51
61const int Index64[128] = {
62 // clang-format off
63 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
64 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
65 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
66 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
67 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
68 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
69 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
70 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
71 // clang-format on
72};
73
87size_t mutt_b64_encode(const char *in, size_t inlen, char *out, size_t outlen)
88{
89 if (!in || !out)
90 return 0;
91
92 unsigned char *begin = (unsigned char *) out;
93 const unsigned char *inu = (const unsigned char *) in;
94
95 while ((inlen >= 3) && (outlen > 4))
96 {
97 *out++ = B64Chars[inu[0] >> 2];
98 *out++ = B64Chars[((inu[0] << 4) & 0x30) | (inu[1] >> 4)];
99 *out++ = B64Chars[((inu[1] << 2) & 0x3c) | (inu[2] >> 6)];
100 *out++ = B64Chars[inu[2] & 0x3f];
101 outlen -= 4;
102 inlen -= 3;
103 inu += 3;
104 }
105
106 /* clean up remainder */
107 if ((inlen > 0) && (outlen > 4))
108 {
109 unsigned char fragment;
110
111 *out++ = B64Chars[inu[0] >> 2];
112 fragment = (inu[0] << 4) & 0x30;
113 if (inlen > 1)
114 fragment |= inu[1] >> 4;
115 *out++ = B64Chars[fragment];
116 *out++ = (inlen < 2) ? '=' : B64Chars[(inu[1] << 2) & 0x3c];
117 *out++ = '=';
118 }
119 *out = '\0';
120 return out - (char *) begin;
121}
122
135int mutt_b64_decode(const char *in, char *out, size_t olen)
136{
137 if (!in || !*in || !out)
138 return -1;
139
140 int len = 0;
141
142 for (; *in; in += 4)
143 {
144 const unsigned char digit1 = in[0];
145 if ((digit1 > 127) || (base64val(digit1) == BAD))
146 return -1;
147 const unsigned char digit2 = in[1];
148 if ((digit2 > 127) || (base64val(digit2) == BAD))
149 return -1;
150
151 /* The 3rd and 4th bytes can be terminating padding chars ('='). Some
152 * mailers don't properly terminate base64-encoded strings, so we allow for
153 * the input string to terminate without padding. */
154 const unsigned char digit3 = in[2] ? in[2] : '=';
155 if ((digit3 > 127) || ((digit3 != '=') && (base64val(digit3) == BAD)))
156 return -1;
157 const unsigned char digit4 = (digit3 == '=') ? '=' : in[3];
158 if ((digit4 > 127) || ((digit4 != '=') && (base64val(digit4) == BAD)))
159 return -1;
160
161 /* digits are already sanity-checked */
162 if (len == olen)
163 return len;
164 *out++ = (base64val(digit1) << 2) | (base64val(digit2) >> 4);
165 len++;
166 if (digit3 != '=')
167 {
168 if (len == olen)
169 return len;
170 *out++ = ((base64val(digit2) << 4) & 0xf0) | (base64val(digit3) >> 2);
171 len++;
172 if (digit4 != '=')
173 {
174 if (len == olen)
175 return len;
176 *out++ = ((base64val(digit3) << 6) & 0xc0) | base64val(digit4);
177 len++;
178 }
179 }
180
181 /* did we reach the end? */
182 if (digit4 == '=')
183 {
184 break;
185 }
186 }
187
188 return len;
189}
190
198size_t mutt_b64_buffer_encode(struct Buffer *buf, const char *in, size_t len)
199{
200 if (!buf)
201 return 0;
202
203 buf_alloc(buf, MAX((len * 2), 1024));
204 size_t num = mutt_b64_encode(in, len, buf->data, buf->dsize);
205 buf_fix_dptr(buf);
206 return num;
207}
208
216int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
217{
218 if (!buf)
219 return -1;
220
221 buf_alloc(buf, mutt_str_len(in));
222 int olen = mutt_b64_decode(in, buf->data, buf->dsize);
223 // mutt_b64_decode returns raw bytes, so don't terminate the buffer either
224 if (olen > 0)
225 buf_seek(buf, olen);
226 else
227 buf_seek(buf, 0);
228
229 return olen;
230}
size_t mutt_b64_encode(const char *in, size_t inlen, char *out, size_t outlen)
Convert raw bytes to null-terminated base64 string.
Definition: base64.c:87
size_t mutt_b64_buffer_encode(struct Buffer *buf, const char *in, size_t len)
Convert raw bytes to null-terminated base64 string.
Definition: base64.c:198
int mutt_b64_decode(const char *in, char *out, size_t olen)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:135
const int Index64[128]
Lookup table for Base64 encoding characters.
Definition: base64.c:61
int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:216
static const char B64Chars[64]
Characters of the Base64 encoding.
Definition: base64.c:44
#define BAD
Definition: base64.c:39
Conversion to/from base64 encoding.
#define base64val(ch)
Definition: base64.h:32
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:622
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:182
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:337
General purpose object for storing and parsing strings.
Memory management wrappers.
#define MAX(a, b)
Definition: memory.h:31
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
String manipulation functions.
String manipulation buffer.
Definition: buffer.h:36
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37