NeoMutt  2024-04-25-85-g27bab4
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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.
 
size_t mutt_b64_encode (const char *in, size_t inlen, char *out, size_t outlen)
 Convert raw bytes to null-terminated base64 string.
 
int mutt_b64_buffer_decode (struct Buffer *buf, const char *in)
 Convert null-terminated base64 string to raw bytes.
 
size_t mutt_b64_buffer_encode (struct Buffer *buf, const char *in, size_t len)
 Convert raw bytes to null-terminated base64 string.
 

Variables

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

Detailed Description

Conversion to/from base64 encoding.

Authors
  • Richard Russon

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 32 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 135 of file base64.c.

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}
#define BAD
Definition: base64.c:39
#define base64val(ch)
Definition: base64.h:32
+ 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 87 of file base64.c.

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}
static const char B64Chars[64]
Characters of the Base64 encoding.
Definition: base64.c:44
+ 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 216 of file base64.c.

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}
int mutt_b64_decode(const char *in, char *out, size_t olen)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:135
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:622
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
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
+ 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 198 of file base64.c.

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}
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
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:182
#define MAX(a, b)
Definition: memory.h:31
+ 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 61 of file base64.c.