From f3a96bc18cfc1b0b434682fccc313c477d2ca86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Valverde?= Date: Fri, 18 Nov 2022 01:07:17 +0000 Subject: [PATCH] IAX2: Fix UTF-8 string encoding Fixes #18651. --- epan/dissectors/packet-iax2.c | 2 +- wsutil/str_util.c | 28 ++++++++++++++++++++++++++++ wsutil/str_util.h | 18 ++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/epan/dissectors/packet-iax2.c b/epan/dissectors/packet-iax2.c index 7368848b2d..f65633c490 100644 --- a/epan/dissectors/packet-iax2.c +++ b/epan/dissectors/packet-iax2.c @@ -1853,7 +1853,7 @@ dissect_fullpacket(tvbuff_t *tvb, guint32 offset, proto_tree_add_item(packet_type_tree, hf_iax2_dtmf_csub, tvb, offset+9, 1, ENC_ASCII); offset += 10; - col_append_fstr(pinfo->cinfo, COL_INFO, " digit %c", csub); + col_append_fstr(pinfo->cinfo, COL_INFO, " digit %s", format_char(pinfo->pool, csub)); break; case AST_FRAME_CONTROL: diff --git a/wsutil/str_util.c b/wsutil/str_util.c index bc6c04b024..2b87c970fa 100644 --- a/wsutil/str_util.c +++ b/wsutil/str_util.c @@ -1028,6 +1028,34 @@ format_text_chr(wmem_allocator_t *allocator, const char *string, size_t len, cha return wmem_strbuf_finalize(buf); } +char * +format_char(wmem_allocator_t *allocator, char c) +{ + char *buf; + char r; + + if (g_ascii_isprint(c)) { + buf = wmem_alloc_array(allocator, char, 2); + buf[0] = c; + buf[1] = '\0'; + return buf; + } + if (escape_char(c, &r)) { + buf = wmem_alloc_array(allocator, char, 3); + buf[0] = '\\'; + buf[1] = r; + buf[2] = '\0'; + return buf; + } + buf = wmem_alloc_array(allocator, char, 5); + buf[0] = '\\'; + buf[1] = 'x'; + buf[2] = hex[((uint8_t)c >> 4) & 0xF]; + buf[3] = hex[((uint8_t)c >> 0) & 0xF]; + buf[4] = '\0'; + return buf; +} + char* ws_utf8_truncate(char *string, size_t len) { diff --git a/wsutil/str_util.h b/wsutil/str_util.h index 03bd76498c..3542235033 100644 --- a/wsutil/str_util.h +++ b/wsutil/str_util.h @@ -288,6 +288,24 @@ WS_DLL_PUBLIC char *format_text_chr(wmem_allocator_t *allocator, const char *string, size_t len, char chr); +/** Given a wmem scope and an 8-bit character + * generate a valid UTF-8 string from it, allocated in the specified + * wmem scope, that: + * + * shows printable Unicode characters as themselves; + * + * shows non-printable ASCII characters as C-style escapes (hex + * if not one of the standard ones such as LF -> '\n'); + * + * and return a pointer to it. + * + * @param allocator The wmem scope + * @param c A character to format + * @return A pointer to the formatted string + */ +WS_DLL_PUBLIC +char *format_char(wmem_allocator_t *allocator, char c); + /** * Truncate a UTF-8 string in place so that it is no larger than len bytes, * ensuring that the string is null terminated and ends with a complete