aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2022-10-15 10:11:00 +0100
committerJoão Valverde <j@v6e.pt>2022-10-15 11:08:53 +0100
commitd2a488f5d50361797b1d3f2bbd362158bb5d0e63 (patch)
treeb1ecb583f4cee243b69f37209fbd5723295f55c4 /wsutil
parentd4ba8be9d7376a95a5f8088c229495f46c09f357 (diff)
wslog: Do not print control characters
Diffstat (limited to 'wsutil')
-rw-r--r--wsutil/wmem/wmem_strbuf.c61
-rw-r--r--wsutil/wmem/wmem_strbuf.h5
-rw-r--r--wsutil/wslog.c16
3 files changed, 74 insertions, 8 deletions
diff --git a/wsutil/wmem/wmem_strbuf.c b/wsutil/wmem/wmem_strbuf.c
index bb1ea41c83..4b8f8a9224 100644
--- a/wsutil/wmem/wmem_strbuf.c
+++ b/wsutil/wmem/wmem_strbuf.c
@@ -255,12 +255,14 @@ wmem_strbuf_append_unichar(wmem_strbuf_t *strbuf, const gunichar c)
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+#define HEX_CODELEN 4
+
void
wmem_strbuf_append_hex(wmem_strbuf_t *strbuf, uint8_t ch)
{
- wmem_strbuf_grow(strbuf, 4);
+ wmem_strbuf_grow(strbuf, HEX_CODELEN * 1);
- if (!strbuf->max_size || WMEM_STRBUF_ROOM(strbuf) >= 4) {
+ if (!strbuf->max_size || WMEM_STRBUF_ROOM(strbuf) >= HEX_CODELEN * 1) {
strbuf->str[strbuf->len++] = '\\';
strbuf->str[strbuf->len++] = 'x';
strbuf->str[strbuf->len++] = hex[(ch >> 4) & 0xF];
@@ -269,6 +271,61 @@ wmem_strbuf_append_hex(wmem_strbuf_t *strbuf, uint8_t ch)
}
}
+#define BMP_CODELEN 6
+
+static inline
+void append_hex_bmp(wmem_strbuf_t *strbuf, gunichar ch)
+{
+ wmem_strbuf_grow(strbuf, BMP_CODELEN * 1);
+
+ if (!strbuf->max_size || WMEM_STRBUF_ROOM(strbuf) >= BMP_CODELEN * 1) {
+ strbuf->str[strbuf->len++] = '\\';
+ strbuf->str[strbuf->len++] = 'u';
+ strbuf->str[strbuf->len++] = hex[(ch >> 12) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 8) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 4) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 0) & 0xF];
+ strbuf->str[strbuf->len] = '\0';
+ }
+}
+
+#define ANY_CODELEN 10
+
+static inline
+void append_hex_any(wmem_strbuf_t *strbuf, gunichar ch)
+{
+ wmem_strbuf_grow(strbuf, ANY_CODELEN * 1);
+
+ if (!strbuf->max_size || WMEM_STRBUF_ROOM(strbuf) >= ANY_CODELEN * 1) {
+ strbuf->str[strbuf->len++] = '\\';
+ strbuf->str[strbuf->len++] = 'U';
+ strbuf->str[strbuf->len++] = hex[(ch >> 28) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 24) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 20) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 16) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 12) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 8) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 4) & 0xF];
+ strbuf->str[strbuf->len++] = hex[(ch >> 0) & 0xF];
+ strbuf->str[strbuf->len] = '\0';
+ }
+}
+
+size_t
+wmem_strbuf_append_hex_unichar(wmem_strbuf_t *strbuf, gunichar ch)
+{
+ if (ch <= 0x7f) {
+ wmem_strbuf_append_hex(strbuf, (uint8_t)ch);
+ return HEX_CODELEN;
+ }
+ if (ch <= 0xffff) {
+ append_hex_bmp(strbuf, ch);
+ return BMP_CODELEN;
+ }
+ append_hex_any(strbuf, ch);
+ return ANY_CODELEN;
+}
+
void
wmem_strbuf_truncate(wmem_strbuf_t *strbuf, const size_t len)
{
diff --git a/wsutil/wmem/wmem_strbuf.h b/wsutil/wmem/wmem_strbuf.h
index e0efef0240..f2c21ecde0 100644
--- a/wsutil/wmem/wmem_strbuf.h
+++ b/wsutil/wmem/wmem_strbuf.h
@@ -106,6 +106,11 @@ WS_DLL_PUBLIC
void
wmem_strbuf_append_hex(wmem_strbuf_t *strbuf, uint8_t);
+/* Returns the number of characters written (4, 6 or 10). */
+WS_DLL_PUBLIC
+size_t
+wmem_strbuf_append_hex_unichar(wmem_strbuf_t *strbuf, gunichar);
+
WS_DLL_PUBLIC
void
wmem_strbuf_truncate(wmem_strbuf_t *strbuf, const size_t len);
diff --git a/wsutil/wslog.c b/wsutil/wslog.c
index 9f8afe0192..195f3816fb 100644
--- a/wsutil/wslog.c
+++ b/wsutil/wslog.c
@@ -1190,19 +1190,23 @@ static char *
make_utf8_display(const char *src, size_t src_length, size_t good_length)
{
wmem_strbuf_t *buf;
- unsigned char ch;
+ gunichar ch;
size_t offset = 0;
buf = wmem_strbuf_new(NULL, NULL);
- for (size_t pos = 0; pos < good_length; pos++) {
- ch = src[pos];
- wmem_strbuf_append_c(buf, ch);
- if ((ch >> 6) != 2) {
- /* first byte */
+ for (const char *s = src; s < src + good_length; s = g_utf8_next_char(s)) {
+ ch = g_utf8_get_char(s);
+
+ if (g_unichar_isprint(ch)) {
+ wmem_strbuf_append_unichar(buf, ch);
offset += 1;
}
+ else {
+ offset += wmem_strbuf_append_hex_unichar(buf, ch);
+ }
}
+
for (size_t pos = good_length; pos < src_length; pos++) {
ch = src[pos];
wmem_strbuf_append_hex(buf, ch);