Browse Source

Allow hex encoded output of strings.

tags/0.7.2
Vsevolod Stakhov 9 years ago
parent
commit
6569d234e8
2 changed files with 37 additions and 14 deletions
  1. 36
    14
      src/libutil/printf.c
  2. 1
    0
      src/libutil/printf.h

+ 36
- 14
src/libutil/printf.c View File

* From FreeBSD libutil code * From FreeBSD libutil code
*/ */
static const int maxscale = 6; static const int maxscale = 6;
static const gchar _hex[] = "0123456789abcdef";
static const gchar _HEX[] = "0123456789ABCDEF";


static gchar * static gchar *
rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes) rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes)
gchar *p, temp[sizeof ("18446744073709551615")]; gchar *p, temp[sizeof ("18446744073709551615")];
size_t len; size_t len;
guint32 ui32; guint32 ui32;
static gchar hex[] = "0123456789abcdef";
static gchar HEX[] = "0123456789ABCDEF";


p = temp + sizeof(temp); p = temp + sizeof(temp);


do { do {


/* the "(guint32)" cast disables the BCC's warning */ /* the "(guint32)" cast disables the BCC's warning */
*--p = hex[(guint32) (ui64 & 0xf)];
*--p = _hex[(guint32) (ui64 & 0xf)];


} while (ui64 >>= 4); } while (ui64 >>= 4);


do { do {


/* the "(guint32)" cast disables the BCC's warning */ /* the "(guint32)" cast disables the BCC's warning */
*--p = HEX[(guint32) (ui64 & 0xf)];
*--p = _HEX[(guint32) (ui64 & 0xf)];


} while (ui64 >>= 4); } while (ui64 >>= 4);
} }
return r; return r;
} }


#define RSPAMD_PRINTF_APPEND(buf, len) \
do { \
wr = func ((buf), (len), apd); \
if (wr < (__typeof (wr))(len)) { \
goto oob; \
} \
written += wr; \
fmt++; \
buf_start = fmt; \
#define RSPAMD_PRINTF_APPEND(buf, len) \
do { \
RSPAMD_PRINTF_APPEND_BUF(buf, len); \
fmt++; \
buf_start = fmt; \
} while (0)

#define RSPAMD_PRINTF_APPEND_BUF(buf, len) \
do { \
wr = func ((buf), (len), apd); \
if (wr < (__typeof (wr))(len)) { \
goto oob; \
} \
written += wr; \
} while (0) } while (0)


glong glong
/* NULL terminated string */ /* NULL terminated string */
slen = strlen (p); slen = strlen (p);
} }
RSPAMD_PRINTF_APPEND (p, slen);

if (G_LIKELY(hex == 0)) {
RSPAMD_PRINTF_APPEND (p, slen);
}
else {
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];
RSPAMD_PRINTF_APPEND_BUF (hexbuf, 2);
p ++;
slen --;
}
fmt ++;
buf_start = fmt;

}


continue; continue;



+ 1
- 0
src/libutil/printf.h View File

* %V f_str_t * * %V f_str_t *
* %v GString * * %v GString *
* %s null-terminated string * %s null-terminated string
* %xs hex encoded string
* %*s length and string * %*s length and string
* %Z '\0' * %Z '\0'
* %N '\n' * %N '\n'

Loading…
Cancel
Save