Move escape_string() to wsutil
Move this utility function to wsutil. Rename to ws_escape_string(). Also add tests.
This commit is contained in:
parent
e11cdf2f46
commit
44121e2c3b
|
@ -47,9 +47,9 @@ string_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype, int
|
|||
return wmem_strdup(scope, fv->value.string);
|
||||
}
|
||||
if (rtype == FTREPR_DFILTER) {
|
||||
int len = escape_string_len(fv->value.string);
|
||||
size_t len = ws_escape_string_len(fv->value.string);
|
||||
char *buf = wmem_alloc(scope, len + 1);
|
||||
escape_string(buf, fv->value.string);
|
||||
ws_escape_string(buf, fv->value.string);
|
||||
return buf;
|
||||
}
|
||||
ws_assert_not_reached();
|
||||
|
|
|
@ -1456,71 +1456,6 @@ string_or_null(const char *string)
|
|||
return "[NULL]";
|
||||
}
|
||||
|
||||
int
|
||||
escape_string_len(const char *string)
|
||||
{
|
||||
const char *p;
|
||||
gchar c;
|
||||
int repr_len;
|
||||
|
||||
repr_len = 0;
|
||||
for (p = string; (c = *p) != '\0'; p++) {
|
||||
/* Backslashes and double-quotes must
|
||||
* be escaped */
|
||||
if (c == '\\' || c == '"') {
|
||||
repr_len += 2;
|
||||
}
|
||||
/* Values that can't nicely be represented
|
||||
* in ASCII need to be escaped. */
|
||||
else if (!g_ascii_isprint(c)) {
|
||||
/* c --> \xNN */
|
||||
repr_len += 4;
|
||||
}
|
||||
/* Other characters are just passed through. */
|
||||
else {
|
||||
repr_len++;
|
||||
}
|
||||
}
|
||||
return repr_len + 2; /* string plus leading and trailing quotes */
|
||||
}
|
||||
|
||||
char *
|
||||
escape_string(char *buf, const char *string)
|
||||
{
|
||||
const gchar *p;
|
||||
gchar c;
|
||||
char *bufp;
|
||||
char hexbuf[3];
|
||||
|
||||
bufp = buf;
|
||||
*bufp++ = '"';
|
||||
for (p = string; (c = *p) != '\0'; p++) {
|
||||
/* Backslashes and double-quotes must
|
||||
* be escaped. */
|
||||
if (c == '\\' || c == '"') {
|
||||
*bufp++ = '\\';
|
||||
*bufp++ = c;
|
||||
}
|
||||
/* Values that can't nicely be represented
|
||||
* in ASCII need to be escaped. */
|
||||
else if (!g_ascii_isprint(c)) {
|
||||
/* c --> \xNN */
|
||||
g_snprintf(hexbuf,sizeof(hexbuf), "%02x", (unsigned char) c);
|
||||
*bufp++ = '\\';
|
||||
*bufp++ = 'x';
|
||||
*bufp++ = hexbuf[0];
|
||||
*bufp++ = hexbuf[1];
|
||||
}
|
||||
/* Other characters are just passed through. */
|
||||
else {
|
||||
*bufp++ = c;
|
||||
}
|
||||
}
|
||||
*bufp++ = '"';
|
||||
*bufp = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
#define GN_CHAR_ALPHABET_SIZE 128
|
||||
|
||||
static gunichar IA5_default_alphabet[GN_CHAR_ALPHABET_SIZE] = {
|
||||
|
|
|
@ -290,13 +290,6 @@ char * convert_string_case(const char *string, gboolean case_insensitive);
|
|||
WS_DLL_PUBLIC
|
||||
const char * string_or_null(const char *string);
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
int escape_string_len(const char *string);
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
char * escape_string(char *dst, const char *string);
|
||||
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
|
||||
|
||||
|
|
|
@ -265,6 +265,71 @@ printable_char_or_period(gchar c)
|
|||
return g_ascii_isprint(c) ? c : '.';
|
||||
}
|
||||
|
||||
size_t
|
||||
ws_escape_string_len(const char *string)
|
||||
{
|
||||
const char *p;
|
||||
gchar c;
|
||||
size_t repr_len;
|
||||
|
||||
repr_len = 0;
|
||||
for (p = string; (c = *p) != '\0'; p++) {
|
||||
/* Backslashes and double-quotes must
|
||||
* be escaped */
|
||||
if (c == '\\' || c == '"') {
|
||||
repr_len += 2;
|
||||
}
|
||||
/* Values that can't nicely be represented
|
||||
* in ASCII need to be escaped. */
|
||||
else if (!g_ascii_isprint(c)) {
|
||||
/* c --> \xNN */
|
||||
repr_len += 4;
|
||||
}
|
||||
/* Other characters are just passed through. */
|
||||
else {
|
||||
repr_len++;
|
||||
}
|
||||
}
|
||||
return repr_len + 2; /* string plus leading and trailing quotes */
|
||||
}
|
||||
|
||||
char *
|
||||
ws_escape_string(char *buf, const char *string)
|
||||
{
|
||||
const gchar *p;
|
||||
gchar c;
|
||||
char *bufp;
|
||||
char hexbuf[3];
|
||||
|
||||
bufp = buf;
|
||||
*bufp++ = '"';
|
||||
for (p = string; (c = *p) != '\0'; p++) {
|
||||
/* Backslashes and double-quotes must
|
||||
* be escaped. */
|
||||
if (c == '\\' || c == '"') {
|
||||
*bufp++ = '\\';
|
||||
*bufp++ = c;
|
||||
}
|
||||
/* Values that can't nicely be represented
|
||||
* in ASCII need to be escaped. */
|
||||
else if (!g_ascii_isprint(c)) {
|
||||
/* c --> \xNN */
|
||||
g_snprintf(hexbuf,sizeof(hexbuf), "%02x", (unsigned char) c);
|
||||
*bufp++ = '\\';
|
||||
*bufp++ = 'x';
|
||||
*bufp++ = hexbuf[0];
|
||||
*bufp++ = hexbuf[1];
|
||||
}
|
||||
/* Other characters are just passed through. */
|
||||
else {
|
||||
*bufp++ = c;
|
||||
}
|
||||
}
|
||||
*bufp++ = '"';
|
||||
*bufp = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
/*
|
||||
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
||||
*
|
||||
|
|
|
@ -105,6 +105,12 @@ const guint8 *ws_memmem(const void *haystack, size_t haystack_len,
|
|||
WS_DLL_PUBLIC
|
||||
const char *ws_strcasestr(const char *haystack, const char *needle);
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
size_t ws_escape_string_len(const char *string);
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
char *ws_escape_string(char *dst, const char *string);
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
int ws_xton(char ch);
|
||||
|
||||
|
|
|
@ -32,6 +32,27 @@ static void test_format_size(void)
|
|||
g_free(str);
|
||||
}
|
||||
|
||||
static void test_escape_string(void)
|
||||
{
|
||||
size_t len;
|
||||
char *buf;
|
||||
const char *str;
|
||||
|
||||
str = "quoted \"\\\" backslash";
|
||||
len = ws_escape_string_len(str);
|
||||
buf = wmem_alloc(NULL, len + 1);
|
||||
ws_escape_string(buf, str);
|
||||
g_assert_cmpstr(buf, ==, "\"quoted \\\"\\\\\\\" backslash\"");
|
||||
wmem_free(NULL, buf);
|
||||
|
||||
str = "bytes \xfe\xff";
|
||||
len = ws_escape_string_len(str);
|
||||
buf = wmem_alloc(NULL, len + 1);
|
||||
ws_escape_string(buf, str);
|
||||
g_assert_cmpstr(buf, ==, "\"bytes \\xfe\\xff\"");
|
||||
wmem_free(NULL, buf);
|
||||
}
|
||||
|
||||
#include "to_str.h"
|
||||
|
||||
static void test_word_to_hex(void)
|
||||
|
@ -528,6 +549,7 @@ int main(int argc, char **argv)
|
|||
g_test_init(&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func("/str_util/format_size", test_format_size);
|
||||
g_test_add_func("/str_util/escape_string", test_escape_string);
|
||||
|
||||
g_test_add_func("/to_str/word_to_hex", test_word_to_hex);
|
||||
g_test_add_func("/to_str/bytes_to_str", test_bytes_to_str);
|
||||
|
|
Loading…
Reference in New Issue