43 static int PreBmGs(
const uint8_t *x, uint16_t
m, uint16_t *bmGs);
44 static void PreBmBc(
const uint8_t *x, uint16_t
m, uint16_t *bmBc);
45 static void PreBmBcNocase(
const uint8_t *x, uint16_t
m, uint16_t *bmBc);
46 static void BoyerMooreSuffixesNocase(
const uint8_t *x, uint16_t
m,
48 static void PreBmGsNocase(
const uint8_t *x, uint16_t
m, uint16_t *bmGs);
61 memcpy_tolower(needle, needle, needle_len);
64 PreBmBcNocase(needle, needle_len, bm_ctx->
bmBc);
67 PreBmGsNocase(needle, needle_len, bm_ctx->
bmGs);
82 FatalError(
"Fatal error encountered in BoyerMooreCtxInit. Exiting...");
86 PreBmBc(needle, needle_len, new->bmBc);
89 if (PreBmGs(needle, needle_len, new->bmGs) == -1) {
90 FatalError(
"Fatal error encountered in BoyerMoreCtxInit. Exiting...");
137 static void PreBmBc(
const uint8_t *x, uint16_t
m, uint16_t *bmBc)
141 for (i = 0; i < 256; ++i) {
144 for (i = 0; i <
m - 1; ++i) {
145 bmBc[(
unsigned char)x[i]] =
m - i - 1;
156 static void BoyerMooreSuffixes(
const uint8_t *x, uint16_t
m, uint16_t *suff)
161 for (i =
m - 2; i >= 0; --i) {
162 if (i > g && suff[i +
m - 1 - f] < i - g)
163 suff[i] = suff[i +
m - 1 - f];
168 while (g >= 0 && x[g] == x[g +
m - 1 - f])
171 suff[i] = (uint16_t)(f - g);
184 static int PreBmGs(
const uint8_t *x, uint16_t
m, uint16_t *bmGs)
187 uint16_t suff[
m + 1];
189 BoyerMooreSuffixes(x,
m, suff);
191 for (i = 0; i <
m; ++i)
196 for (i =
m - 1; i >= -1; --i)
197 if (i == -1 || suff[i] == i + 1)
198 for (; j <
m - 1 - i; ++j)
200 bmGs[j] = (uint16_t)(
m - 1 - i);
202 for (i = 0; i <=
m - 2; ++i)
203 bmGs[
m - 1 - suff[i]] = (uint16_t)(
m - 1 - i);
215 static void PreBmBcNocase(
const uint8_t *x, uint16_t
m, uint16_t *bmBc)
219 for (i = 0; i < 256; ++i) {
222 for (i = 0; i <
m - 1; ++i) {
228 static void BoyerMooreSuffixesNocase(
const uint8_t *x, uint16_t
m,
235 for (i =
m - 2; i >= 0; --i) {
236 if (i > g && suff[i +
m - 1 - f] < i - g) {
237 suff[i] = suff[i +
m - 1 - f];
247 suff[i] = (uint16_t)(f - g);
260 static void PreBmGsNocase(
const uint8_t *x, uint16_t
m, uint16_t *bmGs)
263 uint16_t suff[
m + 1];
265 BoyerMooreSuffixesNocase(x,
m, suff);
267 for (i = 0; i <
m; ++i) {
271 for (i =
m; i > 0; --i) {
272 if (suff[i - 1] == i) {
273 for (; j <
m - i; ++j) {
280 for (i = 0; i <=
m - 2; ++i) {
281 bmGs[
m - 1 - suff[i]] =
m - 1 - i;
301 const uint8_t *x,
const uint16_t
m,
const uint8_t *y,
const uint32_t n,
const BmCtx *bm_ctx)
303 const uint16_t *bmGs = bm_ctx->
bmGs;
304 const uint16_t *bmBc = bm_ctx->
bmBc;
308 const int32_t int_n =
unlikely(n > INT32_MAX) ? INT32_MAX : n;
310 while (j <= int_n -
m ) {
311 for (i =
m - 1; i >= 0 && x[i] == y[i + j]; --i);
314 return (uint8_t *)(y + j);
316 j += (m1 = bmGs[i]) > (m2 = bmBc[y[i + j]] -
m + 1 + i) ? m1 : m2;
339 const uint8_t *x,
const uint16_t
m,
const uint8_t *y,
const uint32_t n,
const BmCtx *bm_ctx)
341 const uint16_t *bmGs = bm_ctx->
bmGs;
342 const uint16_t *bmBc = bm_ctx->
bmBc;
345 const int32_t int_n =
unlikely(n > INT32_MAX) ? INT32_MAX : n;
347 while (j <= int_n -
m ) {
349 for (i =
m - 1; i >= 0 && x[i] ==
u8_tolower(y[i + j]); --i);
352 return (uint8_t *)(y + j);
354 j += (m1 = bmGs[i]) > (m2 = bmBc[y[i + j]] -
m + 1 + i) ? m1 : m2;
367 static SpmCtx *BMInitCtx(
const uint8_t *needle, uint16_t needle_len,
int nocase,
385 if (sctx->
needle == NULL) {
391 memcpy(sctx->
needle, needle, needle_len);
415 if (sctx->
needle != NULL) {
425 const uint8_t *haystack, uint32_t haystack_len)
431 haystack_len, sctx->
bm_ctx);
434 haystack_len, sctx->
bm_ctx);
441 if (global_thread_ctx == NULL) {
446 return global_thread_ctx;
451 if (global_thread_ctx == NULL) {
454 SCFree(global_thread_ctx);
457 static void BMDestroyThreadCtx(
SpmThreadCtx *thread_ctx)
459 if (thread_ctx == NULL) {
467 if (thread_ctx == NULL) {