NeoMutt  2023-05-17-33-gce4425
Teaching an old dog new tricks
DOXYGEN
base64.c File Reference

Conversion to/from base64 encoding. More...

#include "config.h"
#include "base64.h"
#include "buffer.h"
#include "memory.h"
#include "string2.h"
+ Include dependency graph for base64.c:

Go to the source code of this file.

Macros

#define BAD   -1
 

Functions

size_t mutt_b64_encode (const char *in, size_t inlen, char *out, size_t outlen)
 Convert raw bytes to null-terminated base64 string. More...
 
int mutt_b64_decode (const char *in, char *out, size_t olen)
 Convert null-terminated base64 string to raw bytes. More...
 
size_t mutt_b64_buffer_encode (struct Buffer *buf, const char *in, size_t len)
 Convert raw bytes to null-terminated base64 string. More...
 
int mutt_b64_buffer_decode (struct Buffer *buf, const char *in)
 Convert null-terminated base64 string to raw bytes. More...
 

Variables

static const char B64Chars [64]
 Characters of the Base64 encoding. More...
 
const int Index64 [128]
 Lookup table for Base64 encoding characters. More...
 

Detailed Description

Conversion to/from base64 encoding.

Authors
  • Carl Harris
  • Eric S. Raymond
  • Brendan Cully

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file base64.c.

Macro Definition Documentation

◆ BAD

#define BAD   -1

Definition at line 40 of file base64.c.

Function Documentation

◆ mutt_b64_encode()

size_t mutt_b64_encode ( const char *  in,
size_t  inlen,
char *  out,
size_t  outlen 
)

Convert raw bytes to null-terminated base64 string.

Parameters
inInput buffer for the raw bytes
inlenLength of the input buffer
outOutput buffer for the base64 encoded string
outlenLength of the output buffer
Return values
numLength of the string written to the output buffer

This function performs base64 encoding. The resulting string is guaranteed to be null-terminated. The number of characters up to the terminating NUL-byte is returned (equivalent to calling strlen() on the output buffer after this function returns).

Definition at line 88 of file base64.c.

89{
90 if (!in || !out)
91 return 0;
92
93 unsigned char *begin = (unsigned char *) out;
94 const unsigned char *inu = (const unsigned char *) in;
95
96 while ((inlen >= 3) && (outlen > 4))
97 {
98 *out++ = B64Chars[inu[0] >> 2];
99 *out++ = B64Chars[((inu[0] << 4) & 0x30) | (inu[1] >> 4)];
100 *out++ = B64Chars[((inu[1] << 2) & 0x3c) | (inu[2] >> 6)];
101 *out++ = B64Chars[inu[2] & 0x3f];
102 outlen -= 4;
103 inlen -= 3;
104 inu += 3;
105 }
106
107 /* clean up remainder */
108 if ((inlen > 0) && (outlen > 4))
109 {
110 unsigned char fragment;
111
112 *out++ = B64Chars[inu[0] >> 2];
113 fragment = (inu[0] << 4) & 0x30;
114 if (inlen > 1)
115 fragment |= inu[1] >> 4;
116 *out++ = B64Chars[fragment];
117 *out++ = (inlen < 2) ? '=' : B64Chars[(inu[1] << 2) & 0x3c];
118 *out++ = '=';
119 }
120 *out = '\0';
121 return out - (char *) begin;
122}
static const char B64Chars[64]
Characters of the Base64 encoding.
Definition: base64.c:45
+ Here is the caller graph for this function:

◆ mutt_b64_decode()

int mutt_b64_decode ( const char *  in,
char *  out,
size_t  olen 
)

Convert null-terminated base64 string to raw bytes.

Parameters
inInput buffer for the null-terminated base64-encoded string
outOutput buffer for the raw bytes
olenLength of the output buffer
Return values
numSuccess, bytes written
-1Error

This function performs base64 decoding. The resulting buffer is NOT null-terminated. If the input buffer contains invalid base64 characters, this function returns -1.

Definition at line 136 of file base64.c.

137{
138 if (!in || !*in || !out)
139 return -1;
140
141 int len = 0;
142
143 for (; *in; in += 4)
144 {
145 const unsigned char digit1 = in[0];
146 if ((digit1 > 127) || (base64val(digit1) == BAD))
147 return -1;
148 const unsigned char digit2 = in[1];
149 if ((digit2 > 127) || (base64val(digit2) == BAD))
150 return -1;
151
152 /* The 3rd and 4th bytes can be terminating padding chars ('='). Some
153 * mailers don't properly terminate base64-encoded strings, so we allow for
154 * the input string to terminate without padding. */
155 const unsigned char digit3 = in[2] ? in[2] : '=';
156 if ((digit3 > 127) || ((digit3 != '=') && (base64val(digit3) == BAD)))
157 return -1;
158 const unsigned char digit4 = (digit3 == '=') ? '=' : in[3];
159 if ((digit4 > 127) || ((digit4 != '=') && (base64val(digit4) == BAD)))
160 return -1;
161
162 /* digits are already sanity-checked */
163 if (len == olen)
164 return len;
165 *out++ = (base64val(digit1) << 2) | (base64val(digit2) >> 4);
166 len++;
167 if (digit3 != '=')
168 {
169 if (len == olen)
170 return len;
171 *out++ = ((base64val(digit2) << 4) & 0xf0) | (base64val(digit3) >> 2);
172 len++;
173 if (digit4 != '=')
174 {
175 if (len == olen)
176 return len;
177 *out++ = ((base64val(digit3) << 6) & 0xc0) | base64val(digit4);
178 len++;
179 }
180 }
181
182 /* did we reach the end? */
183 if (digit4 == '=')
184 {
185 break;
186 }
187 }
188
189 return len;
190}
#define BAD
Definition: base64.c:40
#define base64val(ch)
Definition: base64.h:30
+ Here is the caller graph for this function:

◆ mutt_b64_buffer_encode()

size_t mutt_b64_buffer_encode ( struct Buffer buf,
const char *  in,
size_t  len 
)

Convert raw bytes to null-terminated base64 string.

Parameters
bufBuffer for the result
inInput buffer for the raw bytes
lenLength of the input buffer
Return values
numLength of the string written to the output buffer

Definition at line 199 of file base64.c.

200{
201 if (!buf)
202 return 0;
203
204 buf_alloc(buf, MAX((len * 2), 1024));
205 size_t num = mutt_b64_encode(in, len, buf->data, buf->dsize);
206 buf_fix_dptr(buf);
207 return num;
208}
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:88
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:192
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:316
#define MAX(a, b)
Definition: memory.h:30
size_t dsize
Length of data.
Definition: buffer.h:37
char * data
Pointer to data.
Definition: buffer.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_b64_buffer_decode()

int mutt_b64_buffer_decode ( struct Buffer buf,
const char *  in 
)

Convert null-terminated base64 string to raw bytes.

Parameters
bufBuffer for the result
inInput buffer for the null-terminated base64-encoded string
Return values
numSuccess, bytes written
-1Error

Definition at line 217 of file base64.c.

218{
219 if (!buf)
220 return -1;
221
222 buf_alloc(buf, mutt_str_len(in));
223 int olen = mutt_b64_decode(in, buf->data, buf->dsize);
224 // mutt_b64_decode returns raw bytes, so don't terminate the buffer either
225 if (olen > 0)
226 buf_seek(buf, olen);
227 else
228 buf_seek(buf, 0);
229
230 return olen;
231}
int mutt_b64_decode(const char *in, char *out, size_t olen)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:136
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:526
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:568
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ B64Chars

const char B64Chars[64]
static
Initial value:
= {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
}

Characters of the Base64 encoding.

Definition at line 45 of file base64.c.

◆ Index64

const int Index64[128]
Initial value:
= {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
}

Lookup table for Base64 encoding characters.

Note
This is very similar to the table in imap/utf7.c

Encoding chars:

  • utf7 A-Za-z0-9+,
  • mime A-Za-z0-9+/

Definition at line 62 of file base64.c.