From 7689f11df45d520eb7b251e3d498c08693493226 Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Tue, 12 Feb 2019 15:58:13 -0800 Subject: Add mutt_buffer helpers for base64 conversion. Add mutt_buffer_from_base64() mutt_buffer_to_base64() to help with transitioning to buffers. --- base64.c | 22 ++++++++++++++++++++++ protos.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/base64.c b/base64.c index 4e772ee1..d942e546 100644 --- a/base64.c +++ b/base64.c @@ -48,6 +48,14 @@ #define BAD -1 +void mutt_buffer_to_base64 (BUFFER *out, const unsigned char *in, size_t len) +{ + mutt_buffer_increase_size (out, + ((len * 2) > LONG_STRING) ? (len * 2) : LONG_STRING); + mutt_to_base64 ((unsigned char *)out->data, in, len, out->dsize); + mutt_buffer_fix_dptr (out); +} + /* 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) @@ -79,6 +87,20 @@ void mutt_to_base64 (unsigned char *out, const unsigned char *in, size_t len, *out = '\0'; } +int mutt_buffer_from_base64 (BUFFER *out, const char *in) +{ + int olen; + + mutt_buffer_increase_size (out, mutt_strlen (in)); + olen = mutt_from_base64 (out->data, in, out->dsize); + if (olen > 0) + out->dptr = out->data + olen; + else + out->dptr = out->data; + + return olen; +} + /* Convert '\0'-terminated base 64 string to raw bytes. * Returns length of returned buffer, or -1 on error */ int mutt_from_base64 (char *out, const char *in, size_t olen) diff --git a/protos.h b/protos.h index 18798f80..68a617e4 100644 --- a/protos.h +++ b/protos.h @@ -406,6 +406,8 @@ ADDRESS *alias_reverse_lookup (ADDRESS *); /* base64.c */ void mutt_to_base64 (unsigned char*, const unsigned char*, size_t, size_t); int mutt_from_base64 (char*, const char*, size_t); +void mutt_buffer_to_base64 (BUFFER *, const unsigned char *, size_t); +int mutt_buffer_from_base64 (BUFFER *, const char *); /* utf8.c */ int mutt_wctoutf8 (char *s, unsigned int c, size_t buflen); -- cgit v1.2.3