NeoMutt  2019-11-11
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 || !out)
139  return -1;
140 
141  int len = 0;
142  unsigned char digit4;
143 
144  do
145  {
146  const unsigned char digit1 = in[0];
147  if ((digit1 > 127) || (base64val(digit1) == BAD))
148  return -1;
149  const unsigned char digit2 = in[1];
150  if ((digit2 > 127) || (base64val(digit2) == BAD))
151  return -1;
152  const unsigned char digit3 = in[2];
153  if ((digit3 > 127) || ((digit3 != '=') && (base64val(digit3) == BAD)))
154  return -1;
155  digit4 = in[3];
156  if ((digit4 > 127) || ((digit4 != '=') && (base64val(digit4) == BAD)))
157  return -1;
158  in += 4;
159 
160  /* digits are already sanity-checked */
161  if (len == olen)
162  return len;
163  *out++ = (base64val(digit1) << 2) | (base64val(digit2) >> 4);
164  len++;
165  if (digit3 != '=')
166  {
167  if (len == olen)
168  return len;
169  *out++ = ((base64val(digit2) << 4) & 0xf0) | (base64val(digit3) >> 2);
170  len++;
171  if (digit4 != '=')
172  {
173  if (len == olen)
174  return len;
175  *out++ = ((base64val(digit3) << 6) & 0xc0) | base64val(digit4);
176  len++;
177  }
178  }
179  } while (*in && digit4 != '=');
180 
181  return len;
182 }
#define base64val(ch)
Definition: base64.h:30
#define BAD
Definition: base64.c:40
+ 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 > 10))
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 209 of file base64.c.

210 {
211  if (!buf)
212  return -1;
213 
215  int olen = mutt_b64_decode(in, buf->data, buf->dsize);
216  /* mutt_from_base64 returns raw bytes, so don't terminate the buffer either */
217  if (olen > 0)
218  buf->dptr = buf->data + olen;
219  else
220  buf->dptr = buf->data;
221 
222  return olen;
223 }
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
size_t dsize
Length of data.
Definition: buffer.h:37
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
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
+ 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 191 of file base64.c.

192 {
193  if (!buf)
194  return 0;
195 
196  mutt_buffer_alloc(buf, MAX((len * 2), 1024));
197  size_t num = mutt_b64_encode(in, len, buf->data, buf->dsize);
199  return num;
200 }
#define MAX(a, b)
Definition: memory.h:30
size_t dsize
Length of data.
Definition: buffer.h:37
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:181
char * data
Pointer to data.
Definition: buffer.h:35
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ Index64

const int Index64[]

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