]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Support base64 printing
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 19 Jul 2018 09:46:57 +0000 (10:46 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 19 Jul 2018 10:15:19 +0000 (11:15 +0100)
src/libutil/printf.c
src/libutil/printf.h

index be4ef402eaf7032f6cb7b729f01172f8a8881a43..74940cc5fc91dd0d752ac3b89836c5e1e700ceae 100644 (file)
@@ -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 */
index 7cf3bd4a8fe51179d76ebcd64d9764bf4d9c07ff..73787d3a5a8916b9d060a14b6aa6b4ea493dd6b7 100644 (file)
@@ -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'