]> granicus.if.org Git - neomutt/commitdiff
Add mutt_buffer helpers for base64 conversion
authorKevin McCarthy <kevin@8t8.us>
Tue, 12 Feb 2019 23:58:13 +0000 (15:58 -0800)
committerRichard Russon <rich@flatcap.org>
Wed, 20 Feb 2019 00:55:01 +0000 (00:55 +0000)
Add mutt_buffer_from_base64() mutt_buffer_to_base64() to help with
transitioning to buffers.

Co-authored-by: Richard Russon <rich@flatcap.org>
mutt/base64.c
mutt/base64.h

index e4ef8780cbb7054ca8e27bf8ed646a60b44d0e11..25801c8f7367a75af686fff72b96def48038ed0c 100644 (file)
@@ -33,6 +33,8 @@
 
 #include "config.h"
 #include "base64.h"
+#include "buffer.h"
+#include "string2.h"
 
 #define BAD -1
 
@@ -171,3 +173,37 @@ int mutt_b64_decode(const char *in, char *out, size_t olen)
 
   return len;
 }
+
+/**
+ * mutt_b64_buffer_encode - Convert raw bytes to null-terminated base64 string
+ * @param buf    Buffer for the result
+ * @param in     Input buffer for the raw bytes
+ * @param len    Length of the input buffer
+ * @retval num Length of the string written to the output buffer
+ */
+size_t mutt_b64_buffer_encode(struct Buffer *buf, const char *in, size_t len)
+{
+  mutt_buffer_increase_size(buf, ((len * 2) > LONG_STRING) ? (len * 2) : LONG_STRING);
+  size_t num = mutt_b64_encode(in, len, buf->data, buf->dsize);
+  mutt_buffer_fix_dptr(buf);
+  return num;
+}
+
+/**
+ * mutt_b64_buffer_decode - Convert null-terminated base64 string to raw bytes
+ * @param buf  Buffer for the result
+ * @param in   Input  buffer for the null-terminated base64-encoded string
+ * @retval num Success, bytes written
+ * @retval -1  Error
+ */
+int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
+{
+  mutt_buffer_increase_size(buf, mutt_str_strlen(in));
+  int olen = mutt_b64_decode(in, buf->data, buf->dsize);
+  if (olen > 0)
+    buf->dptr = buf->data + olen;
+  else
+    buf->dptr = buf->data;
+
+  return olen;
+}
index 745f2d13caf4025aecd1e039cee3bc97a11eb713..c55fced5b37bc63da30a601ccb928cc3ec260243 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <stdio.h>
 
+struct Buffer;
+
 extern const int Index64[];
 
 #define base64val(c) Index64[(unsigned int) (c)]
@@ -30,4 +32,7 @@ extern const int Index64[];
 int    mutt_b64_decode(const char *in, char *out, size_t olen);
 size_t mutt_b64_encode(const char *in, size_t inlen, char *out, size_t outlen);
 
+int    mutt_b64_buffer_decode(struct Buffer *buf, const char *in);
+size_t mutt_b64_buffer_encode(struct Buffer *buf, const char *in, size_t len);
+
 #endif /* MUTT_LIB_BASE64_H */