diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-04-07 13:03:38 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-04-07 13:03:38 +0100 |
commit | d3385c8130e4af64e5b2a1648683f6ef6c279849 (patch) | |
tree | 41f434f883ba9e8bb067ca1662c010e411ecdd6e /src/libutil | |
parent | 406d1943a4f286cd59c82e69d5706d0bc9c3e14c (diff) | |
download | rspamd-d3385c8130e4af64e5b2a1648683f6ef6c279849.tar.gz rspamd-d3385c8130e4af64e5b2a1648683f6ef6c279849.zip |
[Minor] Fix substring search in case if inlen == srchlen
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/str_util.c | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index cb110e136..fbf223020 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -1229,36 +1229,41 @@ rspamd_substring_search_caseless (const gchar *in, gsize inlen, gsize i, j; gchar c1, c2; - if (inlen < srchlen) { - return -1; - } + /* Searching */ + if (inlen > srchlen) { + /* Preprocessing */ + for (d = i = 1; i < srchlen; ++i) { + /* computes d = 2^(m-1) with the left-shift operator */ + d = (d << 1); + } - /* Preprocessing */ - for (d = i = 1; i < srchlen; ++i) { - /* computes d = 2^(m-1) with the left-shift operator */ - d = (d << 1); - } + for (hash_in = hash_srch = i = 0; i < srchlen; ++i) { + hash_srch = ((hash_srch << 1) + g_ascii_tolower (srch[i])); + hash_in = ((hash_in << 1) + g_ascii_tolower (in[i])); + } - for (hash_in = hash_srch = i = 0; i < srchlen; ++i) { - hash_srch = ((hash_srch << 1) + g_ascii_tolower (srch[i])); - hash_in = ((hash_in << 1) + g_ascii_tolower (in[i])); - } + j = 0; + while (j <= inlen - srchlen) { - /* Searching */ - j = 0; - while (j <= inlen - srchlen) { + if (hash_srch == hash_in && + rspamd_lc_cmp (srch, in + j, srchlen) == 0) { + return (goffset) j; + } - if (hash_srch == hash_in && rspamd_lc_cmp (srch, in + j, srchlen) == 0) { - return (goffset) j; + c1 = g_ascii_tolower (in[j]); + c2 = g_ascii_tolower (in[j + srchlen]); + hash_in = RKHASH (c1, c2, hash_in); + ++j; } - - c1 = g_ascii_tolower (in[j]); - c2 = g_ascii_tolower (in[j + srchlen]); - hash_in = RKHASH (c1, c2, hash_in); - ++j; + } + else if (inlen == srchlen) { + return rspamd_lc_cmp (srch, in, srchlen) == 0; + } + else { + return (-1); } - return -1; + return (-1); } /* Computing of the maximal suffix for <= */ |