]> source.dussan.org Git - rspamd.git/commitdiff
Add fixed strings comparision routines.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 7 Oct 2015 10:19:00 +0000 (11:19 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 7 Oct 2015 10:19:00 +0000 (11:19 +0100)
src/libutil/fstring.c
src/libutil/fstring.h
src/libutil/str_util.c

index 6a1304423d173aed5ce6454c6064aa935c900d1a..a03121f4227dc185e5b29ca1f5a1d4b2559a9e1e 100644 (file)
@@ -238,3 +238,78 @@ rspamd_fstring_equal (const rspamd_fstring_t *s1,
 
        return FALSE;
 }
+
+extern const guchar lc_map[256];
+
+gint
+rspamd_fstring_casecmp (const rspamd_fstring_t *s1,
+               const rspamd_fstring_t *s2)
+{
+       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;
+
+       g_assert (s1 != NULL && s2 != NULL);
+
+       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;
+                       }
+
+                       s += 4;
+                       d += 4;
+               }
+
+               while (leftover > 0) {
+                       if (g_ascii_tolower (*s) != g_ascii_tolower (*d)) {
+                               return (*s) - (*d);
+                       }
+
+                       leftover --;
+                       s ++;
+                       d ++;
+               }
+       }
+       else {
+               ret = s1->len - s2->len;
+       }
+
+       return ret;
+}
+
+gint
+rspamd_fstring_cmp (const rspamd_fstring_t *s1,
+               const rspamd_fstring_t *s2)
+{
+       g_assert (s1 != NULL && s2 != NULL);
+
+       if (s1->len == s2->len) {
+               return memcmp (s1->str, s2->str, s1->len);
+       }
+
+       return s1->len - s2->len;
+}
\ No newline at end of file
index 02b92bea3c217d2729db110f9b32be8790c3f18b..70d6624d15ea1dc4f354e7212fd6cd7818faa954 100644 (file)
@@ -94,4 +94,13 @@ guint32 rspamd_fstrhash_lc (const rspamd_ftok_t *str, gboolean is_utf);
 gboolean rspamd_fstring_equal (const rspamd_fstring_t *s1,
                const rspamd_fstring_t *s2);
 
+/**
+ * Compare two fixed strings ignoring case
+ */
+gint rspamd_fstring_casecmp (const rspamd_fstring_t *s1,
+               const rspamd_fstring_t *s2);
+
+gint rspamd_fstring_cmp (const rspamd_fstring_t *s1,
+               const rspamd_fstring_t *s2);
+
 #endif
index d92ceb1adc8895d3b4cba8614a83d27de8092935..f68b975a46a0dcb3e8bd273e78b10aadc2e2e91a 100644 (file)
@@ -28,7 +28,7 @@
 #include "mem_pool.h"
 #include "xxhash.h"
 
-static const guchar lc_map[256] = {
+const guchar lc_map[256] = {
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,