From 91e064d0fdbc45f75f3e2e181ee84d8e879ac91c Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 25 Aug 2015 15:48:37 +0100 Subject: [PATCH] Add folded version of base64 encoding --- src/libutil/str_util.c | 25 ++++++++++++++++++++++--- src/libutil/str_util.h | 10 ++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 00ef4e4ed..75a200db5 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -565,13 +565,15 @@ rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen) } -gchar * -rspamd_encode_base64 (const guchar *in, gsize inlen, gint str_len, gsize *outlen) +static gchar * +rspamd_encode_base64_common (const guchar *in, gsize inlen, gint str_len, + gsize *outlen, gboolean fold) { #define CHECK_SPLIT \ do { if (str_len > 0 && cols >= str_len) { \ *o++ = '\r'; \ *o++ = '\n'; \ + if (fold) *o++ = '\t'; \ cols = 0; \ } } \ while (0) @@ -588,7 +590,7 @@ while (0) if (str_len > 0) { g_assert (str_len > 8); - allocated_len += (allocated_len / str_len + 1) * 2 + 1; + allocated_len += (allocated_len / str_len + 1) * (fold ? 3 : 2) + 1; } out = g_malloc (allocated_len); @@ -621,6 +623,9 @@ while (0) *o++ = '\r'; *o++ = '\n'; + if (fold) { + *o ++ = '\t'; + } /* Remaining bytes */ while (shift >= 16) { @@ -708,6 +713,20 @@ end: return out; } +gchar * +rspamd_encode_base64 (const guchar *in, gsize inlen, gint str_len, + gsize *outlen) +{ + return rspamd_encode_base64_common (in, inlen, str_len, outlen, FALSE); +} + +gchar * +rspamd_encode_base64_fold (const guchar *in, gsize inlen, gint str_len, + gsize *outlen) +{ + return rspamd_encode_base64_common (in, inlen, str_len, outlen, TRUE); +} + gsize rspamd_decode_url (gchar *dst, const gchar *src, gsize size) { diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index e07c51701..f8d9dd4dc 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -128,6 +128,16 @@ guchar* rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen); gchar * rspamd_encode_base64 (const guchar *in, gsize inlen, gint str_len, gsize *outlen); +/** + * Encode and fold string using base64 encoding + * @param in input + * @param inlen input length + * @param str_len maximum string length (if <= 0 then no lines are split) + * @return freshly allocated base64 encoded value or NULL if input is invalid + */ +gchar * rspamd_encode_base64_fold (const guchar *in, gsize inlen, gint str_len, + gsize *outlen); + /** * Decode URL encoded string in-place and return new length of a string, src and dst are NULL terminated * @param dst -- 2.39.5