diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-02-02 12:47:10 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-02-02 12:47:10 +0000 |
commit | 15f18d0c0c326dc789c2af1715fefecb01abfcce (patch) | |
tree | cd6dd6ab03ac94aae105b2535e7cac4e2db565a6 | |
parent | 91001260bc87b72668381b3a8536b76f93bac26a (diff) | |
download | rspamd-15f18d0c0c326dc789c2af1715fefecb01abfcce.tar.gz rspamd-15f18d0c0c326dc789c2af1715fefecb01abfcce.zip |
Add hex encoding/decoding routines
-rw-r--r-- | src/libutil/str_util.c | 71 | ||||
-rw-r--r-- | src/libutil/str_util.h | 16 |
2 files changed, 87 insertions, 0 deletions
diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index d3851c89c..17039bd6c 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -1274,6 +1274,77 @@ rspamd_string_find_eoh (GString *input) return -1; } +gchar * +rspamd_encode_hex (const guchar *in, gsize inlen) +{ + gchar *out, *o; + const guchar *p; + gsize outlen = inlen * 2 + 1; + static const gchar hexdigests[16] = "0123456789ABCDEF"; + + if (in == NULL) { + return NULL; + } + + out = g_malloc (outlen); + o = out; + p = in; + + while (inlen > 0) { + *o++ = hexdigests[((*p >> 4) & 0xFF)]; + *o++ = hexdigests[((*p++) & 0xFF)]; + inlen --; + } + + *o = '\0'; + + return out; +} + + +guchar* +rspamd_decode_hex (const gchar *in, gsize inlen) +{ + guchar *out, *o, ret; + const gchar *p; + gchar c; + gsize outlen = (inlen / 2 + inlen % 2) + 1; + + if (in == NULL) { + return NULL; + } + + out = g_malloc (outlen); + o = out; + p = in; + + /* We ignore trailing chars if we have not even input */ + inlen = inlen - inlen % 2; + + while (inlen > 0) { + c = *p++; + + if (c >= '0' && c <= '9') ret = c - '0'; + else if (c >= 'A' && c <= 'F') ret = c - 'A' + 10; + else if (c >= 'a' && c <= 'f') ret = c - 'a' + 10; + + c = *p++; + ret *= 16; + + if (c >= '0' && c <= '9') ret += c - '0'; + else if (c >= 'A' && c <= 'F') ret += c - 'A' + 10; + else if (c >= 'a' && c <= 'f') ret += c - 'a' + 10; + + *o++ = ret; + + inlen -= 2; + } + + *o = '\0'; + + return out; +} + /* * GString ucl emitting functions */ diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index da794bc16..03798c80e 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -125,6 +125,22 @@ gchar * rspamd_encode_base32 (const guchar *in, gsize inlen); guchar* rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen); /** + * Encode string using hex 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); + +/** + * 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_hex (const gchar *in, gsize inlen); + +/** * Encode string using base64 encoding * @param in input * @param inlen input length |