aboutsummaryrefslogtreecommitdiffstats
path: root/epan/ftypes
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2023-06-24 14:54:03 +0100
committerJoão Valverde <j@v6e.pt>2023-06-26 00:46:18 +0000
commitbd25b9f4cdce820dcb1951fd3c0acff6c5306d6b (patch)
tree516b2768d7d08e1e49cd94ff971e98da42ca3e5e /epan/ftypes
parentbf9c4ff0fff01f46affa4c130cf9c4fce84c4bfa (diff)
dfilter: Make string slices a return an FT_STRING
Allow string slices (indexing) to work with internationalized strings. The old behavior of indexing on byte boundaries can be obtained using raw slices.
Diffstat (limited to 'epan/ftypes')
-rw-r--r--epan/ftypes/ftype-bytes.c23
-rw-r--r--epan/ftypes/ftype-ipv4.c2
-rw-r--r--epan/ftypes/ftype-ipv6.c2
-rw-r--r--epan/ftypes/ftype-protocol.c2
-rw-r--r--epan/ftypes/ftype-string.c35
-rw-r--r--epan/ftypes/ftypes-int.h2
-rw-r--r--epan/ftypes/ftypes.c61
7 files changed, 82 insertions, 45 deletions
diff --git a/epan/ftypes/ftype-bytes.c b/epan/ftypes/ftype-bytes.c
index 5973a85c4c..61e9c0569d 100644
--- a/epan/ftypes/ftype-bytes.c
+++ b/epan/ftypes/ftype-bytes.c
@@ -501,10 +501,7 @@ len(fvalue_t *fv)
static void
slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
{
- guint8* data;
-
- data = (guint8 *)g_bytes_get_data(fv->value.bytes, NULL) + offset;
-
+ const guint8 *data = (const guint8 *)g_bytes_get_data(fv->value.bytes, NULL) + offset;
g_byte_array_append(bytes, data, length);
}
@@ -627,7 +624,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -664,7 +661,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -701,7 +698,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -738,7 +735,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -775,7 +772,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -812,7 +809,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -849,7 +846,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -886,7 +883,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -923,7 +920,7 @@ ftype_register_bytes(void)
bytes_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
bytes_bitwise_and, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
diff --git a/epan/ftypes/ftype-ipv4.c b/epan/ftypes/ftype-ipv4.c
index dc22944a0f..e38c056c00 100644
--- a/epan/ftypes/ftype-ipv4.c
+++ b/epan/ftypes/ftype-ipv4.c
@@ -203,7 +203,7 @@ ftype_register_ipv4(void)
is_zero,
NULL,
len,
- slice,
+ (FvalueSlice)slice,
bitwise_and,
NULL, /* unary_minus */
NULL, /* add */
diff --git a/epan/ftypes/ftype-ipv6.c b/epan/ftypes/ftype-ipv6.c
index 3be91b9e30..32c949b021 100644
--- a/epan/ftypes/ftype-ipv6.c
+++ b/epan/ftypes/ftype-ipv6.c
@@ -232,7 +232,7 @@ ftype_register_ipv6(void)
is_zero,
NULL,
len,
- slice,
+ (FvalueSlice)slice,
bitwise_and,
NULL, /* unary_minus */
NULL, /* add */
diff --git a/epan/ftypes/ftype-protocol.c b/epan/ftypes/ftype-protocol.c
index 8e5f6f4ba6..8aa6ccbe93 100644
--- a/epan/ftypes/ftype-protocol.c
+++ b/epan/ftypes/ftype-protocol.c
@@ -396,7 +396,7 @@ ftype_register_tvbuff(void)
is_zero,
NULL,
len,
- slice,
+ (FvalueSlice)slice,
NULL,
NULL, /* unary_minus */
NULL, /* add */
diff --git a/epan/ftypes/ftype-string.c b/epan/ftypes/ftype-string.c
index 195267f7aa..6f9eb1d970 100644
--- a/epan/ftypes/ftype-string.c
+++ b/epan/ftypes/ftype-string.c
@@ -127,17 +127,28 @@ string_is_zero(const fvalue_t *fv)
static guint
len(fvalue_t *fv)
{
- return (guint)fv->value.strbuf->len;
+ /* g_utf8_strlen returns glong for no apparent reason*/
+ glong len = g_utf8_strlen(fv->value.strbuf->str, -1);
+ if (len < 0)
+ return 0;
+ return (guint)len;
}
static void
-slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
+slice(fvalue_t *fv, wmem_strbuf_t *buf, guint offset, guint length)
{
- guint8* data;
-
- data = (guint8*)fv->value.strbuf->str + offset;
-
- g_byte_array_append(bytes, data, length);
+ const char *str = fv->value.strbuf->str;
+
+ /* Go to the starting offset */
+ const char *p = g_utf8_offset_to_pointer(str, (glong)offset);
+ const char *n;
+ /* Copy 'length' codepoints to dst. Skip the terminating NULL */
+ while (*p != '\0' && length-- > 0) {
+ n = g_utf8_next_char(p);
+ /* Append n - p bytes (one codepoint)*/
+ wmem_strbuf_append_len(buf, p, n - p);
+ p = n;
+ }
}
static enum ft_result
@@ -213,7 +224,7 @@ ftype_register_string(void)
string_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
NULL, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -249,7 +260,7 @@ ftype_register_string(void)
string_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
NULL, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -285,7 +296,7 @@ ftype_register_string(void)
string_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
NULL, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -321,7 +332,7 @@ ftype_register_string(void)
string_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
NULL, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
@@ -357,7 +368,7 @@ ftype_register_string(void)
string_is_zero, /* is_zero */
NULL, /* is_negative */
len,
- slice,
+ (FvalueSlice)slice,
NULL, /* bitwise_and */
NULL, /* unary_minus */
NULL, /* add */
diff --git a/epan/ftypes/ftypes-int.h b/epan/ftypes/ftypes-int.h
index af7c7a9ae6..4f15fb26dd 100644
--- a/epan/ftypes/ftypes-int.h
+++ b/epan/ftypes/ftypes-int.h
@@ -86,7 +86,7 @@ typedef enum ft_result (*FvalueMatches)(const fvalue_t*, const ws_regex_t*, gboo
typedef gboolean (*FvalueIs)(const fvalue_t*);
typedef guint (*FvalueLen)(fvalue_t*);
typedef guint (*FvalueHashFunc)(const fvalue_t *);
-typedef void (*FvalueSlice)(fvalue_t*, GByteArray *, guint offset, guint length);
+typedef void (*FvalueSlice)(fvalue_t*, void *, guint offset, guint length);
typedef enum ft_result (*FvalueUnaryOp)(fvalue_t *, const fvalue_t*, gchar **);
typedef enum ft_result (*FvalueBinaryOp)(fvalue_t *, const fvalue_t*, const fvalue_t*, gchar **);
diff --git a/epan/ftypes/ftypes.c b/epan/ftypes/ftypes.c
index 35dafb2674..e48264272c 100644
--- a/epan/ftypes/ftypes.c
+++ b/epan/ftypes/ftypes.c
@@ -577,16 +577,16 @@ fvalue_to_sinteger64(const fvalue_t *fv, gint64 *repr)
typedef struct {
fvalue_t *fv;
- GByteArray *bytes;
+ void *ptr;
gboolean slice_failure;
} slice_data_t;
static gboolean
-compute_drnode(guint field_length, drange_node *drnode, guint *offset_ptr, guint *length_ptr)
+compute_drnode(gsize field_length, drange_node *drnode, gsize *offset_ptr, gsize *length_ptr)
{
- gint start_offset;
- gint length = 0;
- gint end_offset = 0;
+ gssize start_offset;
+ gssize length = 0;
+ gssize end_offset = 0;
drange_node_end_t ending;
start_offset = drange_node_get_start_offset(drnode);
@@ -640,8 +640,8 @@ slice_func(gpointer data, gpointer user_data)
{
drange_node *drnode = (drange_node *)data;
slice_data_t *slice_data = (slice_data_t *)user_data;
- gint start_offset;
- gint length = 0;
+ gsize start_offset;
+ gsize length = 0;
fvalue_t *fv;
if (slice_data->slice_failure) {
@@ -654,19 +654,40 @@ slice_func(gpointer data, gpointer user_data)
return;
}
- ws_assert(start_offset >=0 && length > 0);
- fv->ftype->slice(fv, slice_data->bytes, start_offset, length);
+ ws_assert(length > 0);
+ fv->ftype->slice(fv, slice_data->ptr, (guint)start_offset, (guint)length);
}
-/* Returns a new FT_BYTES fvalue_t* if possible, otherwise NULL */
-fvalue_t*
-fvalue_slice(fvalue_t *fv, drange_t *d_range)
+static fvalue_t *
+slice_string(fvalue_t *fv, drange_t *d_range)
+{
+ slice_data_t slice_data;
+ fvalue_t *new_fv;
+
+ slice_data.fv = fv;
+ slice_data.ptr = wmem_strbuf_create(NULL);
+ slice_data.slice_failure = FALSE;
+
+ /* XXX - We could make some optimizations here based on
+ * drange_has_total_length() and
+ * drange_get_max_offset().
+ */
+
+ drange_foreach_drange_node(d_range, slice_func, &slice_data);
+
+ new_fv = fvalue_new(FT_STRING);
+ fvalue_set_strbuf(new_fv, slice_data.ptr);
+ return new_fv;
+}
+
+static fvalue_t *
+slice_bytes(fvalue_t *fv, drange_t *d_range)
{
slice_data_t slice_data;
fvalue_t *new_fv;
slice_data.fv = fv;
- slice_data.bytes = g_byte_array_new();
+ slice_data.ptr = g_byte_array_new();
slice_data.slice_failure = FALSE;
/* XXX - We could make some optimizations here based on
@@ -677,12 +698,20 @@ fvalue_slice(fvalue_t *fv, drange_t *d_range)
drange_foreach_drange_node(d_range, slice_func, &slice_data);
new_fv = fvalue_new(FT_BYTES);
- GBytes *bytes = g_byte_array_free_to_bytes(slice_data.bytes);
- fvalue_set_bytes(new_fv, bytes);
- g_bytes_unref(bytes);
+ fvalue_set_byte_array(new_fv, slice_data.ptr);
return new_fv;
}
+/* Returns a new slice fvalue_t* if possible, otherwise NULL */
+fvalue_t*
+fvalue_slice(fvalue_t *fv, drange_t *d_range)
+{
+ if (FT_IS_STRING(fvalue_type_ftenum(fv))) {
+ return slice_string(fv, d_range);
+ }
+ return slice_bytes(fv, d_range);
+}
+
void
fvalue_set_bytes(fvalue_t *fv, GBytes *value)
{