]> source.dussan.org Git - rspamd.git/commitdiff
Add base32 encoding support for printf routines
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 17 Dec 2015 18:36:51 +0000 (18:36 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 17 Dec 2015 18:36:51 +0000 (18:36 +0000)
src/libutil/printf.c
src/libutil/printf.h

index 11c464be86450e3739f564153a33384565a3f479..c558b805a005c47fb3ae96fe3b7f98ced67d131f 100644 (file)
@@ -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;
 
index ea73cf0a0f4665c23c888200574420c4ca370457..f2c46ffc9c837c6d2c42e444bef82c37e6fef052 100644 (file)
@@ -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'