NeoMutt  2022-04-29-178-g3b62e6
Teaching an old dog new tricks
DOXYGEN
base64.h File Reference

Conversion to/from base64 encoding. More...

#include <stdio.h>
+ Include dependency graph for base64.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define base64val(ch)   Index64[(unsigned int) (ch)]
 

Functions

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_encode (const char *in, size_t inlen, char *out, size_t outlen)
 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...
 
size_t mutt_b64_buffer_encode (struct Buffer *buf, const char *in, size_t len)
 Convert raw bytes to null-terminated base64 string. More...
 

Variables

const int Index64 []
 Lookup table for Base64 encoding characters. More...
 

Detailed Description

Conversion to/from base64 encoding.

Authors

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.h.

Macro Definition Documentation

◆ base64val

#define base64val (   ch)    Index64[(unsigned int) (ch)]

Definition at line 30 of file base64.h.

Function Documentation

◆ 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_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_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
223 int olen = mutt_b64_decode(in, buf->data, buf->dsize);
224 /* mutt_from_base64 returns raw bytes, so don't terminate the buffer either */
225 if (olen > 0)
226 mutt_buffer_seek(buf, olen);
227 else
228 mutt_buffer_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 mutt_buffer_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:265
void mutt_buffer_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:466
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:544
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_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 mutt_buffer_alloc(buf, MAX((len * 2), 1024));
205 size_t num = mutt_b64_encode(in, len, buf->data, buf->dsize);
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 mutt_buffer_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:179
#define MAX(a, b)
Definition: memory.h:30
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ Index64

const int Index64[]
extern

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.