From: Vsevolod Stakhov Date: Tue, 2 Apr 2019 15:26:21 +0000 (+0100) Subject: [Feature] Improve lua binary strings output X-Git-Tag: 1.9.1~10 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a76eda3c01c89242c2a63b71ca447dbdf67b4e74;p=rspamd.git [Feature] Improve lua binary strings output --- diff --git a/src/lua/lua_logger.c b/src/lua/lua_logger.c index 20b59efa7..67b70eaad 100644 --- a/src/lua/lua_logger.c +++ b/src/lua/lua_logger.c @@ -273,12 +273,57 @@ static gsize lua_logger_out_str (lua_State *L, gint pos, gchar *outbuf, gsize len, struct lua_logger_trace *trace) { - gsize slen; + gsize slen, flen; const gchar *str = lua_tolstring (L, pos, &slen); - gsize r = 0; + static const gchar hexdigests[16] = "0123456789abcdef"; + gsize r = 0, s; if (str) { - r = rspamd_strlcpy (outbuf, str, MIN (slen, len) + 1); + gboolean normal = TRUE; + flen = MIN (slen, len - 1); + + for (r = 0; r < flen; r ++) { + if (!(g_ascii_isprint (str[r]) || (str[r] & 0x80))) { + normal = FALSE; + break; + } + } + + if (normal) { + r = rspamd_strlcpy (outbuf, str, flen + 1); + } + else { + /* Need to escape non printed characters */ + r = 0; + s = 0; + + while (slen > 0 && len > 1) { + if (!g_ascii_isprint (str[s])) { + if (str[s] & 0x80) { + outbuf[r++] = str[s]; + } + else if (len >= 3) { + outbuf[r++] = '\\'; + outbuf[r++] = hexdigests[((str[s] >> 4) & 0xF)]; + outbuf[r++] = hexdigests[((str[s]) & 0xF)]; + + len -= 2; + } + else { + outbuf[r++] = '?'; + } + } + else { + outbuf[r++] = str[s]; + } + + s++; + slen --; + len --; + } + + outbuf[r] = '\0'; + } } return r;