aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-12-17 18:36:51 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-12-17 18:36:51 +0000
commit62982c2012baa1f1afb562b64f5cb2c20ccf767c (patch)
tree1fd6d9f25b9d4ecfedb779f70f7bf932f0568e82 /src
parentfa1a67a410f3d19727a5e0a190d83394a9278349 (diff)
downloadrspamd-62982c2012baa1f1afb562b64f5cb2c20ccf767c.tar.gz
rspamd-62982c2012baa1f1afb562b64f5cb2c20ccf767c.zip
Add base32 encoding support for printf routines
Diffstat (limited to 'src')
-rw-r--r--src/libutil/printf.c35
-rw-r--r--src/libutil/printf.h1
2 files changed, 28 insertions, 8 deletions
diff --git a/src/libutil/printf.c b/src/libutil/printf.c
index 11c464be8..c558b805a 100644
--- a/src/libutil/printf.c
+++ b/src/libutil/printf.c
@@ -25,6 +25,7 @@
#include "printf.h"
#include "fstring.h"
+#include "str_util.h"
#include <math.h>
/**
@@ -366,7 +367,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
glong written = 0, wr, slen;
gint64 i64;
guint64 ui64;
- guint width, sign, hex, humanize, bytes, frac_width, i;
+ guint width, sign, hex, humanize, bytes, frac_width, i, b32;
rspamd_fstring_t *v;
rspamd_ftok_t *tok;
GString *gs;
@@ -398,6 +399,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
width = 0;
sign = 1;
hex = 0;
+ b32 = 0;
bytes = 0;
humanize = 0;
frac_width = 0;
@@ -431,6 +433,11 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
sign = 0;
fmt++;
continue;
+ case 'b':
+ b32 = 1;
+ sign = 0;
+ fmt++;
+ continue;
case 'H':
humanize = 1;
bytes = 1;
@@ -566,23 +573,35 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
slen = MIN (slen, width);
}
- if (G_LIKELY(hex == 0)) {
- RSPAMD_PRINTF_APPEND (p, slen);
+ if (G_UNLIKELY (b32)) {
+ gchar *b32buf;
+
+ b32buf = rspamd_encode_base32 (p, slen);
+
+ if (b32buf) {
+ RSPAMD_PRINTF_APPEND_BUF (b32buf, strlen (b32buf));
+ g_free (b32buf);
+ }
}
- else {
+ else if (G_UNLIKELY (hex)) {
gchar hexbuf[2];
+
while (slen) {
hexbuf[0] = hex == 2 ? _HEX[*p & 0xf] : _hex[*p & 0xf];
hexbuf[1] = hex == 2 ? _HEX[(*p >> 8) & 0xf] :
- _hex[(*p >> 8) & 0xf];
+ _hex[(*p >> 8) & 0xf];
RSPAMD_PRINTF_APPEND_BUF (hexbuf, 2);
- p ++;
- slen --;
+ p++;
+ slen--;
}
- fmt ++;
+ fmt++;
buf_start = fmt;
}
+ else {
+ RSPAMD_PRINTF_APPEND (p, slen);
+ }
+
continue;
diff --git a/src/libutil/printf.h b/src/libutil/printf.h
index ea73cf0a0..f2c46ffc9 100644
--- a/src/libutil/printf.h
+++ b/src/libutil/printf.h
@@ -50,6 +50,7 @@
* %v GString *
* %s null-terminated string
* %xs hex encoded string
+ * %bs base32 encoded string
* %*s length and string
* %Z '\0'
* %N '\n'