ftypes: Rewrite FT_PROTOCOL comparison operator

For efficiency do the comparison in a single function call
instead of trying to preserving exactly the previous semantics.

Still I tried not to deviate much.
This commit is contained in:
João Valverde 2021-10-10 20:22:42 +01:00 committed by Wireshark GitLab Utility
parent 13e9e7199c
commit 041aa24a37
1 changed files with 24 additions and 55 deletions

View File

@ -198,68 +198,37 @@ slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
} }
} }
static gboolean static int
cmp_eq(const fvalue_t *fv_a, const fvalue_t *fv_b) _tvbcmp(tvbuff_t *a, tvbuff_t *b)
{ {
const protocol_value_t *a = (const protocol_value_t *)&fv_a->value.protocol; guint a_len = tvb_captured_length(a);
const protocol_value_t *b = (const protocol_value_t *)&fv_b->value.protocol; guint b_len = tvb_captured_length(b);
volatile gboolean eq = FALSE;
TRY { if (a_len != b_len)
if ((a->tvb != NULL) && (b->tvb != NULL)) { return a_len < b_len ? -1 : 1;
guint a_len = tvb_captured_length(a->tvb); return memcmp(tvb_get_ptr(a, 0, a_len), tvb_get_ptr(b, 0, a_len), a_len);
if (a_len == tvb_captured_length(b->tvb))
eq = (memcmp(tvb_get_ptr(a->tvb, 0, a_len), tvb_get_ptr(b->tvb, 0, a_len), a_len) == 0);
} else {
eq = (strcmp(a->proto_string, b->proto_string) == 0);
}
}
CATCH_ALL {
/* nothing */
}
ENDTRY;
return eq;
}
static gboolean
cmp_lt(const fvalue_t *fv_a, const fvalue_t *fv_b)
{
const protocol_value_t *a = (const protocol_value_t *)&fv_a->value.protocol;
const protocol_value_t *b = (const protocol_value_t *)&fv_b->value.protocol;
volatile gboolean lt = FALSE;
TRY {
if ((a->tvb != NULL) && (b->tvb != NULL)) {
guint a_len = tvb_captured_length(a->tvb);
guint b_len = tvb_captured_length(b->tvb);
if (a_len < b_len) {
lt = TRUE;
} else if (a_len == b_len) {
lt = (memcmp(tvb_get_ptr(a->tvb, 0, a_len), tvb_get_ptr(b->tvb, 0, a_len), a_len) < 0);
}
} else {
lt = (strcmp(a->proto_string, b->proto_string) < 0);
}
}
CATCH_ALL {
/* nothing */
}
ENDTRY;
return lt;
} }
static int static int
cmp_order(const fvalue_t *fv_a, const fvalue_t *fv_b) cmp_order(const fvalue_t *fv_a, const fvalue_t *fv_b)
{ {
if (cmp_lt(fv_a, fv_b)) const protocol_value_t *a = (const protocol_value_t *)&fv_a->value.protocol;
return -1; const protocol_value_t *b = (const protocol_value_t *)&fv_b->value.protocol;
if (cmp_eq(fv_a, fv_b)) volatile int c = 0;
return 0;
return 1; TRY {
if ((a->tvb != NULL) && (b->tvb != NULL)) {
c = _tvbcmp(a->tvb, b->tvb);
} else {
c = strcmp(a->proto_string, b->proto_string);
}
}
CATCH_ALL {
/* nothing */
}
ENDTRY;
return c;
} }
static gboolean static gboolean