Browse Source

Add folded version of base64 encoding

tags/1.0.0
Vsevolod Stakhov 8 years ago
parent
commit
91e064d0fd
2 changed files with 32 additions and 3 deletions
  1. 22
    3
      src/libutil/str_util.c
  2. 10
    0
      src/libutil/str_util.h

+ 22
- 3
src/libutil/str_util.c View File

@@ -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)
{

+ 10
- 0
src/libutil/str_util.h View File

@@ -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

Loading…
Cancel
Save