From 102a5e58fb8607bb4b55a3e04d9d4beb4663987b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 19 Jul 2018 10:46:57 +0100 Subject: [PATCH] [Minor] Support base64 printing --- src/libutil/printf.c | 31 ++++++++++++++++++++++++++++++- src/libutil/printf.h | 1 + 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/libutil/printf.c b/src/libutil/printf.c index be4ef402e..74940cc5f 100644 --- a/src/libutil/printf.c +++ b/src/libutil/printf.c @@ -597,7 +597,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, b32; + guint width, sign, hex, humanize, bytes, frac_width, b32, b64; rspamd_fstring_t *v; rspamd_ftok_t *tok; GString *gs; @@ -670,6 +670,11 @@ rspamd_vprintf_common (rspamd_printf_append_func func, sign = 0; fmt++; continue; + case 'B': + b64 = 1; + sign = 0; + fmt++; + continue; case 'H': humanize = 1; bytes = 1; @@ -845,6 +850,30 @@ rspamd_vprintf_common (rspamd_printf_append_func func, buf_start = fmt; } + else if (G_UNLIKELY (b64)) { + gchar *b64buf; + gsize olen = 0; + + if (G_UNLIKELY (slen == -1)) { + if (G_LIKELY (width != 0)) { + slen = width; + } + else { + /* NULL terminated string */ + slen = strlen (p); + } + } + + b64buf = rspamd_encode_base64 (p, slen, 0, &olen); + + if (b64buf) { + RSPAMD_PRINTF_APPEND (b64buf, olen); + g_free (b64buf); + } + else { + RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1); + } + } else { if (slen == -1) { /* NULL terminated string */ diff --git a/src/libutil/printf.h b/src/libutil/printf.h index 7cf3bd4a8..73787d3a5 100644 --- a/src/libutil/printf.h +++ b/src/libutil/printf.h @@ -42,6 +42,7 @@ * %s null-terminated string * %xs hex encoded string * %bs base32 encoded string + * %Bs base64 encoded string * %*s length and string * %Z '\0' * %N '\n' -- 2.39.5