#define BAD -1
-/* raw bytes to null-terminated base 64 string */
-void mutt_to_base64 (unsigned char *out, const unsigned char *in, size_t len,
- size_t olen)
+/**
+ * mutt_to_base64 - convert raw bytes to null-terminated base64 string.
+ *
+ * @param out Output buffer for the base64 encoded string.
+ * @param cin Input buffer for the raw bytes.
+ * @param len Length of the input buffer.
+ * @param olen Length of the output buffer.
+ * @return The length 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
+ * null-byte is returned (equivalent to calling strlen() on the output buffer
+ * after this function returns).
+ */
+size_t mutt_to_base64 (char *out, const char *cin, size_t len, size_t olen)
{
+ unsigned char *begin = (unsigned char *)out;
+ const unsigned char *in = (const unsigned char *)cin;
while (len >= 3 && olen > 10)
{
*out++ = B64Chars[in[0] >> 2];
*out++ = '=';
}
*out = '\0';
+ return out - (char *)begin;
}
-/* Convert '\0'-terminated base 64 string to raw bytes.
- * Returns length of returned buffer, or -1 on error */
+/**
+ * mutt_from_base64 - convert null-terminated base64 string to raw bytes.
+ *
+ * @param out Output buffer for the raw bytes.
+ * @param cin Input buffer for the null-terminated base64-encoded string
+ * @return The number of bytes written on success, -1 on error.
+ *
+ * This function performs base64 decoding. The resulting buffer is NOT
+ * null-terminated. If the input buffer contains invalid base64 characters,
+ * this function returns -1.
+ */
int mutt_from_base64 (char *out, const char *in)
{
int len = 0;
* plus the additional debris
*/
- mutt_to_base64 ((unsigned char*) ibuf, (unsigned char*) obuf, strlen (obuf),
- sizeof (ibuf) - 2);
+ mutt_to_base64 (ibuf, obuf, strlen (obuf), sizeof (ibuf) - 2);
safe_strcat (ibuf, sizeof (ibuf), "\r\n");
mutt_socket_write (idata->conn, ibuf);
/* now start the security context initialisation loop... */
dprint (2, (debugfile, "Sending credentials\n"));
- mutt_to_base64 ((unsigned char*) buf1, send_token.value, send_token.length,
+ mutt_to_base64 (buf1, send_token.value, send_token.length,
sizeof (buf1) - 2);
gss_release_buffer (&min_stat, &send_token);
safe_strcat (buf1, sizeof (buf1), "\r\n");
goto err_abort_cmd;
}
- mutt_to_base64 ((unsigned char*) buf1, send_token.value,
- send_token.length, sizeof (buf1) - 2);
+ mutt_to_base64 (buf1, send_token.value, send_token.length,
+ sizeof (buf1) - 2);
gss_release_buffer (&min_stat, &send_token);
safe_strcat (buf1, sizeof (buf1), "\r\n");
mutt_socket_write (idata->conn, buf1);
goto err_abort_cmd;
}
- mutt_to_base64 ((unsigned char*) buf1, send_token.value, send_token.length,
+ mutt_to_base64 (buf1, send_token.value, send_token.length,
sizeof (buf1) - 2);
dprint (2, (debugfile, "Requesting authorisation as %s\n",
idata->conn->account.user));
ADDRESS *alias_reverse_lookup (ADDRESS *);
/* base64.c */
-void mutt_to_base64 (unsigned char*, const unsigned char*, size_t, size_t);
+size_t mutt_to_base64 (char*, const char*, size_t, size_t);
int mutt_from_base64 (char*, const char*);
/* utf8.c */