aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-08-25 15:48:37 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-08-25 15:48:37 +0100
commit91e064d0fdbc45f75f3e2e181ee84d8e879ac91c (patch)
treefd14c832351016b1e2f184e320c1854d56eafb60 /src
parentf5e66d60bce9dad37e7326119d8a94762c397e85 (diff)
downloadrspamd-91e064d0fdbc45f75f3e2e181ee84d8e879ac91c.tar.gz
rspamd-91e064d0fdbc45f75f3e2e181ee84d8e879ac91c.zip
Add folded version of base64 encoding
Diffstat (limited to 'src')
-rw-r--r--src/libutil/str_util.c25
-rw-r--r--src/libutil/str_util.h10
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
@@ -129,6 +129,16 @@ 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
* @param src