diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-04-09 17:13:12 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-04-09 17:13:12 +0100 |
commit | c2351037b581724ce6d6cbcf4da5a0115b87c8de (patch) | |
tree | d55bd57d8bda456308b21cba7827fb2e2fc272a0 /src/libutil | |
parent | f5c0d7116e373fa48ab465e8d8dcae1ddaf44932 (diff) | |
download | rspamd-c2351037b581724ce6d6cbcf4da5a0115b87c8de.tar.gz rspamd-c2351037b581724ce6d6cbcf4da5a0115b87c8de.zip |
[Feature] Support multiple base32 alphabets
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/printf.c | 2 | ||||
-rw-r--r-- | src/libutil/rrd.c | 2 | ||||
-rw-r--r-- | src/libutil/str_util.c | 24 | ||||
-rw-r--r-- | src/libutil/str_util.h | 25 | ||||
-rw-r--r-- | src/libutil/upstream.c | 4 |
5 files changed, 39 insertions, 18 deletions
diff --git a/src/libutil/printf.c b/src/libutil/printf.c index 972d34e82..ed15d1389 100644 --- a/src/libutil/printf.c +++ b/src/libutil/printf.c @@ -815,7 +815,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func, } } - b32buf = rspamd_encode_base32 (p, slen); + b32buf = rspamd_encode_base32 (p, slen, RSPAMD_BASE32_DEFAULT); if (b32buf) { RSPAMD_PRINTF_APPEND (b32buf, strlen (b32buf)); diff --git a/src/libutil/rrd.c b/src/libutil/rrd.c index 9208c71da..a270d87c6 100644 --- a/src/libutil/rrd.c +++ b/src/libutil/rrd.c @@ -346,7 +346,7 @@ rspamd_rrd_calculate_checksum (struct rspamd_rrd_file *file) rspamd_cryptobox_hash_final (&st, sigbuf); - file->id = rspamd_encode_base32 (sigbuf, sizeof (sigbuf)); + file->id = rspamd_encode_base32 (sigbuf, sizeof (sigbuf), RSPAMD_BASE32_DEFAULT); } } diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 4c16cb685..473e68782 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -539,10 +539,11 @@ rspamd_str_pool_copy (gconstpointer data, gpointer ud) */ gint -rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, - gsize outlen) +rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, gsize outlen, + enum rspamd_base32_type type) { - static const char b32[]="ybndrfg8ejkmcpqxot1uwisza345h769"; + static const char b32_default[] = "ybndrfg8ejkmcpqxot1uwisza345h769", + b32_bleach[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l", *b32; gchar *o, *end; gsize i; gint remain = -1, x; @@ -550,6 +551,18 @@ rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, end = out + outlen; o = out; + switch (type) { + case RSPAMD_BASE32_DEFAULT: + b32 = b32_default; + break; + case RSPAMD_BASE32_BLEACH: + b32 = b32_bleach; + break; + default: + g_assert_not_reached (); + abort (); + } + for (i = 0; i < inlen && o < end - 1; i++) { switch (i % 5) { case 0: @@ -603,14 +616,15 @@ rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, } gchar * -rspamd_encode_base32 (const guchar *in, gsize inlen) +rspamd_encode_base32 (const guchar *in, gsize inlen, enum rspamd_base32_type type) { gsize allocated_len = inlen * 8 / 5 + 2; gchar *out; gint outlen; out = g_malloc (allocated_len); - outlen = rspamd_encode_base32_buf (in, inlen, out, allocated_len - 1); + outlen = rspamd_encode_base32_buf (in, inlen, out, + allocated_len - 1, type); if (outlen >= 0) { out[outlen] = 0; diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index 22643176b..e7e5532c3 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -143,36 +143,43 @@ gboolean rspamd_strtoul (const gchar *s, gsize len, gulong *value); gpointer rspamd_str_pool_copy (gconstpointer data, gpointer ud); /** - * Encode string using base32 encoding + * Encode string using hex encoding * @param in input * @param inlen input length * @return freshly allocated base32 encoding of a specified string */ -gchar *rspamd_encode_base32 (const guchar *in, gsize inlen); +gchar *rspamd_encode_hex (const guchar *in, gsize inlen); /** - * Decode string using base32 encoding + * Decode string using hex encoding * @param in input * @param inlen input length * @return freshly allocated base32 decoded value or NULL if input is invalid */ -guchar *rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen); +guchar *rspamd_decode_hex (const gchar *in, gsize inlen); + +enum rspamd_base32_type { + RSPAMD_BASE32_DEFAULT = 0, + RSPAMD_BASE32_ZBASE = 0, + RSPAMD_BASE32_BLEACH +}; /** - * Encode string using hex encoding + * Encode string using base32 encoding * @param in input * @param inlen input length * @return freshly allocated base32 encoding of a specified string */ -gchar *rspamd_encode_hex (const guchar *in, gsize inlen); +gchar *rspamd_encode_base32 (const guchar *in, gsize inlen, + enum rspamd_base32_type type); /** - * Decode string using hex encoding + * Decode string using base32 encoding * @param in input * @param inlen input length * @return freshly allocated base32 decoded value or NULL if input is invalid */ -guchar *rspamd_decode_hex (const gchar *in, gsize inlen); +guchar *rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen); /** * Encode string using base32 encoding @@ -183,7 +190,7 @@ guchar *rspamd_decode_hex (const gchar *in, gsize inlen); * @return encoded len if `outlen` is enough to encode `inlen` */ gint rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, - gsize outlen); + gsize outlen, enum rspamd_base32_type type); /** * Decode string using base32 encoding diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c index 65cbca105..6acf420ea 100644 --- a/src/libutil/upstream.c +++ b/src/libutil/upstream.c @@ -1130,8 +1130,8 @@ rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str, guint h = rspamd_cryptobox_fast_hash (upstream->name, strlen (upstream->name), 0); memset (upstream->uid, 0, sizeof (upstream->uid)); - rspamd_encode_base32_buf ((const guchar *)&h, sizeof (h), - upstream->uid, sizeof (upstream->uid) - 1); + rspamd_encode_base32_buf ((const guchar *) &h, sizeof (h), + upstream->uid, sizeof (upstream->uid) - 1, RSPAMD_BASE32_DEFAULT); msg_debug_upstream ("added upstream %s (%s)", upstream->name, upstream->flags & RSPAMD_UPSTREAM_FLAG_NORESOLVE ? "numeric ip" : "DNS name"); |