diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-10-07 12:24:07 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-10-07 12:24:07 +0100 |
commit | 2567f0c4c7408fc3cd83794e7b12fed9bc33da83 (patch) | |
tree | 01103c9eabf7be3e0bf121dc63c2fa0354e8ebb5 /src/libutil/fstring.c | |
parent | 9a563389b1c9ecbbc696cbb5ab18ef8dbd394d24 (diff) | |
download | rspamd-2567f0c4c7408fc3cd83794e7b12fed9bc33da83.tar.gz rspamd-2567f0c4c7408fc3cd83794e7b12fed9bc33da83.zip |
Add tokens comparision routines.
Diffstat (limited to 'src/libutil/fstring.c')
-rw-r--r-- | src/libutil/fstring.c | 113 |
1 files changed, 75 insertions, 38 deletions
diff --git a/src/libutil/fstring.c b/src/libutil/fstring.c index 6566f5af9..d59ccaa4d 100644 --- a/src/libutil/fstring.c +++ b/src/libutil/fstring.c @@ -259,58 +259,64 @@ rspamd_fstring_equal (const rspamd_fstring_t *s1, extern const guchar lc_map[256]; -gint -rspamd_fstring_casecmp (const rspamd_fstring_t *s1, - const rspamd_fstring_t *s2) +static gint +rspamd_fstring_lc_cmp (const gchar *s, const gchar *d, gsize l) { - gint ret = 0; - gsize leftover = s1->len % 4; guint fp, i; - const uint8_t *s, *d; guchar c1, c2, c3, c4; union { guchar c[4]; guint32 n; } cmp1, cmp2; + gsize leftover = l % 4; + gint ret = 0; - g_assert (s1 != NULL && s2 != NULL); + fp = l - leftover; - if (s1->len == s2->len) { - leftover = s1->len % 4; - s = (const uint8_t *) s1->str; - d = (const uint8_t *) s2->str; - fp = s1->len - leftover; - - for (i = 0; i != fp; i += 4) { - c1 = s[i], c2 = s[i + 1], c3 = s[i + 2], c4 = s[i + 3]; - cmp1.c[0] = lc_map[c1]; - cmp1.c[1] = lc_map[c2]; - cmp1.c[2] = lc_map[c3]; - cmp1.c[3] = lc_map[c4]; - - c1 = d[i], c2 = d[i + 1], c3 = d[i + 2], c4 = d[i + 3]; - cmp2.c[0] = lc_map[c1]; - cmp2.c[1] = lc_map[c2]; - cmp2.c[2] = lc_map[c3]; - cmp2.c[3] = lc_map[c4]; - - if (cmp1.n != cmp2.n) { - return cmp1.n - cmp2.n; - } + for (i = 0; i != fp; i += 4) { + c1 = s[i], c2 = s[i + 1], c3 = s[i + 2], c4 = s[i + 3]; + cmp1.c[0] = lc_map[c1]; + cmp1.c[1] = lc_map[c2]; + cmp1.c[2] = lc_map[c3]; + cmp1.c[3] = lc_map[c4]; + + c1 = d[i], c2 = d[i + 1], c3 = d[i + 2], c4 = d[i + 3]; + cmp2.c[0] = lc_map[c1]; + cmp2.c[1] = lc_map[c2]; + cmp2.c[2] = lc_map[c3]; + cmp2.c[3] = lc_map[c4]; - s += 4; - d += 4; + if (cmp1.n != cmp2.n) { + return cmp1.n - cmp2.n; } - while (leftover > 0) { - if (g_ascii_tolower (*s) != g_ascii_tolower (*d)) { - return (*s) - (*d); - } + s += 4; + d += 4; + } - leftover --; - s ++; - d ++; + while (leftover > 0) { + if (g_ascii_tolower (*s) != g_ascii_tolower (*d)) { + return (*s) - (*d); } + + leftover--; + s++; + d++; + } + + return ret; +} + +gint +rspamd_fstring_casecmp (const rspamd_fstring_t *s1, + const rspamd_fstring_t *s2) +{ + gint ret = 0; + + g_assert (s1 != NULL && s2 != NULL); + + if (s1->len == s2->len) { + ret = rspamd_fstring_lc_cmp (s1->str, s2->str, s1->len); } else { ret = s1->len - s2->len; @@ -331,3 +337,34 @@ rspamd_fstring_cmp (const rspamd_fstring_t *s1, return s1->len - s2->len; } + +gint +rspamd_ftok_casecmp (const rspamd_ftok_t *s1, + const rspamd_ftok_t *s2) +{ + gint ret = 0; + + g_assert (s1 != NULL && s2 != NULL); + + if (s1->len == s2->len) { + ret = rspamd_fstring_lc_cmp (s1->begin, s2->begin, s1->len); + } + else { + ret = s1->len - s2->len; + } + + return ret; +} + +gint +rspamd_ftok_cmp (const rspamd_ftok_t *s1, + const rspamd_ftok_t *s2) +{ + g_assert (s1 != NULL && s2 != NULL); + + if (s1->len == s2->len) { + return memcmp (s1->begin, s2->begin, s1->len); + } + + return s1->len - s2->len; +} |