IAX2: Fix UTF-8 string encoding

Fixes #18651.
This commit is contained in:
João Valverde 2022-11-18 01:07:17 +00:00
parent 8037ecf93f
commit f3a96bc18c
3 changed files with 47 additions and 1 deletions

View File

@ -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:

View File

@ -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)
{

View File

@ -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