46 #ifdef BUILD_HYPERSCAN
50 void SCHSInitCtx(
MpmCtx *);
52 void SCHSDestroyCtx(
MpmCtx *);
54 int SCHSAddPatternCI(
MpmCtx *, uint8_t *, uint16_t, uint16_t, uint16_t,
56 int SCHSAddPatternCS(
MpmCtx *, uint8_t *, uint16_t, uint16_t, uint16_t,
58 int SCHSPreparePatterns(
MpmCtx *mpm_ctx);
61 void SCHSPrintInfo(
MpmCtx *mpm_ctx);
64 static void SCHSRegisterTests(
void);
68 #define INIT_HASH_SIZE 65536
71 #define INIT_DB_HASH_SIZE 1000
76 static hs_scratch_t *g_scratch_proto = NULL;
89 static void *SCHSMalloc(
size_t size)
99 static void SCHSFree(
void *ptr)
109 static void SCHSSetAllocators(
void)
111 hs_error_t err = hs_set_allocator(SCHSMalloc, SCHSFree);
112 if (err != HS_SUCCESS) {
113 FatalError(
"Failed to set Hyperscan allocator.");
127 static inline uint32_t SCHSInitHashRaw(uint8_t *pat, uint16_t patlen)
129 uint32_t hash = patlen * pat[0];
133 return (hash % INIT_HASH_SIZE);
149 uint16_t patlen, uint16_t
offset,
150 uint16_t depth,
char flags,
153 uint32_t hash = SCHSInitHashRaw(pat, patlen);
155 if (
ctx->init_hash == NULL) {
160 for (; t != NULL; t = t->
next) {
210 if (p != NULL && p->
sids != NULL) {
221 static inline uint32_t SCHSInitHash(
SCHSPattern *p)
227 return (hash % INIT_HASH_SIZE);
232 uint32_t hash = SCHSInitHash(p);
234 if (
ctx->init_hash == NULL) {
238 if (
ctx->init_hash[hash] == NULL) {
239 ctx->init_hash[hash] = p;
271 static int SCHSAddPattern(
MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen,
272 uint16_t
offset, uint16_t depth, uint32_t pid,
296 p = SCHSAllocPattern(mpm_ctx);
313 if (SCHSInitHashAdd(
ctx, p) != 0)
325 SCLogDebug(
"%p: alas, no depth for us", mpm_ctx);
329 if (mpm_ctx->
maxlen < patlen)
332 if (mpm_ctx->
minlen == 0) {
335 if (mpm_ctx->
minlen > patlen)
349 if (p->
sids[x] == sid) {
366 SCHSFreePattern(mpm_ctx, p);
374 typedef struct SCHSCompileData_ {
379 unsigned int pattern_cnt;
382 static SCHSCompileData *SCHSAllocCompileData(
unsigned int pattern_cnt)
384 SCHSCompileData *cd =
SCCalloc(pattern_cnt,
sizeof(SCHSCompileData));
389 cd->pattern_cnt = pattern_cnt;
391 cd->ids =
SCCalloc(pattern_cnt,
sizeof(
unsigned int));
392 if (cd->ids == NULL) {
396 cd->flags =
SCCalloc(pattern_cnt,
sizeof(
unsigned int));
397 if (cd->flags == NULL) {
401 cd->expressions =
SCCalloc(pattern_cnt,
sizeof(
char *));
402 if (cd->expressions == NULL) {
406 cd->ext =
SCCalloc(pattern_cnt,
sizeof(hs_expr_ext_t *));
407 if (cd->ext == NULL) {
425 static void SCHSFreeCompileData(SCHSCompileData *cd)
433 if (cd->expressions) {
434 for (
unsigned int i = 0; i < cd->pattern_cnt; i++) {
435 SCFree(cd->expressions[i]);
440 for (
unsigned int i = 0; i < cd->pattern_cnt; i++) {
448 typedef struct PatternDatabase_ {
450 hs_database_t *hs_db;
451 uint32_t pattern_cnt;
457 static uint32_t SCHSPatternHash(
const SCHSPattern *p, uint32_t hash)
493 static uint32_t PatternDatabaseHash(
HashTable *ht,
void *data, uint16_t
len)
495 const PatternDatabase *pd = data;
497 hash =
hashword(&pd->pattern_cnt, 1, hash);
499 for (uint32_t i = 0; i < pd->pattern_cnt; i++) {
500 hash = SCHSPatternHash(pd->parray[i], hash);
507 static char PatternDatabaseCompare(
void *data1, uint16_t len1,
void *data2,
510 const PatternDatabase *pd1 = data1;
511 const PatternDatabase *pd2 = data2;
513 if (pd1->pattern_cnt != pd2->pattern_cnt) {
517 for (uint32_t i = 0; i < pd1->pattern_cnt; i++) {
518 if (SCHSPatternCompare(pd1->parray[i], pd2->parray[i]) == 0) {
526 static void PatternDatabaseFree(PatternDatabase *pd)
530 if (pd->parray != NULL) {
531 for (uint32_t i = 0; i < pd->pattern_cnt; i++) {
542 hs_free_database(pd->hs_db);
547 static void PatternDatabaseTableFree(
void *data)
553 static PatternDatabase *PatternDatabaseAlloc(uint32_t pattern_cnt)
555 PatternDatabase *pd =
SCCalloc(1,
sizeof(PatternDatabase));
559 pd->pattern_cnt = pattern_cnt;
565 if (pd->parray == NULL) {
578 int SCHSPreparePatterns(
MpmCtx *mpm_ctx)
583 SCLogDebug(
"no patterns supplied to this mpm_ctx");
588 hs_compile_error_t *compile_err = NULL;
589 SCHSCompileData *cd = NULL;
590 PatternDatabase *pd = NULL;
603 for (uint32_t i = 0, p = 0; i < INIT_HASH_SIZE; i++) {
605 while (node != NULL) {
608 pd->parray[p++] = node;
615 ctx->init_hash = NULL;
622 if (g_db_table == NULL) {
623 g_db_table =
HashTableInit(INIT_DB_HASH_SIZE, PatternDatabaseHash,
624 PatternDatabaseCompare,
625 PatternDatabaseTableFree);
626 if (g_db_table == NULL) {
636 if (pd_cached != NULL) {
637 SCLogDebug(
"Reusing cached database %p with %" PRIu32
638 " patterns (ref_cnt=%" PRIu32
")",
639 pd_cached->hs_db, pd_cached->pattern_cnt,
641 pd_cached->ref_cnt++;
642 ctx->pattern_db = pd_cached;
644 PatternDatabaseFree(pd);
645 SCHSFreeCompileData(cd);
651 for (uint32_t i = 0; i < pd->pattern_cnt; i++) {
655 cd->
flags[i] = HS_FLAG_SINGLEMATCH;
657 cd->flags[i] |= HS_FLAG_CASELESS;
663 cd->ext[i] =
SCCalloc(1,
sizeof(hs_expr_ext_t));
664 if (cd->ext[i] == NULL) {
670 cd->ext[i]->flags |= HS_EXT_FLAG_MIN_OFFSET;
671 cd->ext[i]->min_offset = p->
offset + p->
len;
674 cd->ext[i]->flags |= HS_EXT_FLAG_MAX_OFFSET;
682 err = hs_compile_ext_multi((
const char *
const *)cd->expressions, cd->flags,
683 cd->ids, (
const hs_expr_ext_t *
const *)cd->ext,
684 cd->pattern_cnt, HS_MODE_BLOCK, NULL, &pd->hs_db,
687 if (err != HS_SUCCESS) {
688 SCLogError(
"failed to compile hyperscan database");
690 SCLogError(
"compile error: %s", compile_err->message);
692 hs_free_compile_error(compile_err);
697 ctx->pattern_db = pd;
700 err = hs_alloc_scratch(pd->hs_db, &g_scratch_proto);
702 if (err != HS_SUCCESS) {
708 err = hs_database_size(pd->hs_db, &
ctx->hs_db_size);
709 if (err != HS_SUCCESS) {
718 SCLogDebug(
"Built %" PRIu32
" patterns into a database of size %" PRIuMAX
728 SCHSFreeCompileData(cd);
733 PatternDatabaseFree(pd);
736 SCHSFreeCompileData(cd);
755 mpm_thread_ctx->
ctx =
ctx;
761 ctx->scratch_size = 0;
765 if (g_scratch_proto == NULL) {
773 hs_error_t err = hs_clone_scratch(g_scratch_proto,
774 (hs_scratch_t **)&
ctx->scratch);
778 if (err != HS_SUCCESS) {
779 FatalError(
"Unable to clone scratch prototype");
782 err = hs_scratch_size(
ctx->scratch, &
ctx->scratch_size);
783 if (err != HS_SUCCESS) {
796 void SCHSInitCtx(
MpmCtx *mpm_ctx)
798 if (mpm_ctx->
ctx != NULL)
802 if (mpm_ctx->
ctx == NULL) {
812 if (
ctx->init_hash == NULL) {
825 SCHSPrintSearchStats(mpm_thread_ctx);
827 if (mpm_thread_ctx->
ctx != NULL) {
830 if (thr_ctx->
scratch != NULL) {
831 hs_free_scratch(thr_ctx->
scratch);
837 mpm_thread_ctx->
ctx = NULL;
848 void SCHSDestroyCtx(
MpmCtx *mpm_ctx)
854 if (
ctx->init_hash != NULL) {
856 ctx->init_hash = NULL;
864 PatternDatabase *pd =
ctx->pattern_db;
868 if (pd->ref_cnt == 0) {
870 PatternDatabaseFree(pd);
881 typedef struct SCHSCallbackCtx_ {
884 uint32_t match_count;
888 static int SCHSMatchEvent(
unsigned int id,
unsigned long long from,
889 unsigned long long to,
unsigned int flags,
892 SCHSCallbackCtx *cctx =
ctx;
894 const PatternDatabase *pd = cctx->ctx->pattern_db;
897 SCLogDebug(
"Hyperscan Match %" PRIu32
": id=%" PRIu32
" @ %" PRIuMAX
898 " (pat id=%" PRIu32
")",
899 cctx->match_count, (uint32_t)
id, (uintmax_t)to, pat->
id);
925 const PatternDatabase *pd =
ctx->pattern_db;
931 SCHSCallbackCtx cctx = {.ctx =
ctx, .pmq = pmq, .match_count = 0};
934 hs_scratch_t *scratch = hs_thread_ctx->scratch;
935 BUG_ON(pd->hs_db == NULL);
938 hs_error_t err = hs_scan(pd->hs_db, (
const char *)buf, buflen, 0, scratch,
939 SCHSMatchEvent, &cctx);
940 if (err != HS_SUCCESS) {
944 SCLogError(
"Hyperscan returned error %d", err);
947 ret = cctx.match_count;
970 int SCHSAddPatternCI(
MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen,
971 uint16_t
offset, uint16_t depth, uint32_t pid,
975 return SCHSAddPattern(mpm_ctx, pat, patlen,
offset, depth, pid, sid,
flags);
995 int SCHSAddPatternCS(
MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen,
996 uint16_t
offset, uint16_t depth, uint32_t pid,
999 return SCHSAddPattern(mpm_ctx, pat, patlen,
offset, depth, pid, sid,
flags);
1002 void SCHSPrintSearchStats(
MpmThreadCtx *mpm_thread_ctx)
1006 void SCHSPrintInfo(
MpmCtx *mpm_ctx)
1010 printf(
"MPM HS Information:\n");
1011 printf(
"Memory allocs: %" PRIu32
"\n", mpm_ctx->
memory_cnt);
1012 printf(
"Memory alloced: %" PRIu32
"\n", mpm_ctx->
memory_size);
1013 printf(
" Sizeof:\n");
1014 printf(
" MpmCtx %" PRIuMAX
"\n", (uintmax_t)
sizeof(
MpmCtx));
1015 printf(
" SCHSCtx: %" PRIuMAX
"\n", (uintmax_t)
sizeof(
SCHSCtx));
1016 printf(
" SCHSPattern %" PRIuMAX
"\n", (uintmax_t)
sizeof(
SCHSPattern));
1017 printf(
"Unique Patterns: %" PRIu32
"\n", mpm_ctx->
pattern_cnt);
1018 printf(
"Smallest: %" PRIu32
"\n", mpm_ctx->
minlen);
1019 printf(
"Largest: %" PRIu32
"\n", mpm_ctx->
maxlen);
1023 PatternDatabase *pd =
ctx->pattern_db;
1024 char *db_info = NULL;
1025 if (hs_database_info(pd->hs_db, &db_info) == HS_SUCCESS) {
1026 printf(
"HS Database Info: %s\n", db_info);
1029 printf(
"HS Database Size: %" PRIuMAX
" bytes\n",
1030 (uintmax_t)
ctx->hs_db_size);
1059 SCHSSetAllocators();
1070 if (g_scratch_proto) {
1071 SCLogDebug(
"Cleaning up Hyperscan global scratch");
1072 hs_free_scratch(g_scratch_proto);
1073 g_scratch_proto = NULL;
1078 if (g_db_table != NULL) {
1079 SCLogDebug(
"Clearing Hyperscan database cache");
1091 static int SCHSTest01(
void)
1098 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1106 SCHSPreparePatterns(&mpm_ctx);
1107 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1109 const char *buf =
"abcdefghjiklmnopqrstuvwxyz";
1111 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1117 printf(
"1 != %" PRIu32
" ",
cnt);
1119 SCHSDestroyCtx(&mpm_ctx);
1120 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1125 static int SCHSTest02(
void)
1132 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1140 SCHSPreparePatterns(&mpm_ctx);
1141 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1143 const char *buf =
"abcdefghjiklmnopqrstuvwxyz";
1144 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1150 printf(
"0 != %" PRIu32
" ",
cnt);
1152 SCHSDestroyCtx(&mpm_ctx);
1153 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1158 static int SCHSTest03(
void)
1165 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1177 SCHSPreparePatterns(&mpm_ctx);
1178 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1180 const char *buf =
"abcdefghjiklmnopqrstuvwxyz";
1181 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1187 printf(
"3 != %" PRIu32
" ",
cnt);
1189 SCHSDestroyCtx(&mpm_ctx);
1190 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1195 static int SCHSTest04(
void)
1202 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1211 SCHSPreparePatterns(&mpm_ctx);
1212 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1214 const char *buf =
"abcdefghjiklmnopqrstuvwxyz";
1215 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1221 printf(
"1 != %" PRIu32
" ",
cnt);
1223 SCHSDestroyCtx(&mpm_ctx);
1224 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1229 static int SCHSTest05(
void)
1236 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1245 SCHSPreparePatterns(&mpm_ctx);
1246 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1248 const char *buf =
"abcdefghjiklmnopqrstuvwxyz";
1249 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1255 printf(
"3 != %" PRIu32
" ",
cnt);
1257 SCHSDestroyCtx(&mpm_ctx);
1258 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1263 static int SCHSTest06(
void)
1270 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1277 SCHSPreparePatterns(&mpm_ctx);
1278 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1280 const char *buf =
"abcd";
1281 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1287 printf(
"1 != %" PRIu32
" ",
cnt);
1289 SCHSDestroyCtx(&mpm_ctx);
1290 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1295 static int SCHSTest07(
void)
1302 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1315 MpmAddPatternCS(&mpm_ctx, (uint8_t *)
"AAAAAAAAAA", 10, 0, 0, 4, 0, 0);
1317 MpmAddPatternCS(&mpm_ctx, (uint8_t *)
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 30,
1321 SCHSPreparePatterns(&mpm_ctx);
1322 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1324 const char *buf =
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1325 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1331 printf(
"6 != %" PRIu32
" ",
cnt);
1333 SCHSDestroyCtx(&mpm_ctx);
1334 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1339 static int SCHSTest08(
void)
1346 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1354 SCHSPreparePatterns(&mpm_ctx);
1355 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1358 SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)
"a", 1);
1363 printf(
"0 != %" PRIu32
" ",
cnt);
1365 SCHSDestroyCtx(&mpm_ctx);
1366 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1371 static int SCHSTest09(
void)
1378 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1386 SCHSPreparePatterns(&mpm_ctx);
1387 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1390 SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)
"ab", 2);
1395 printf(
"1 != %" PRIu32
" ",
cnt);
1397 SCHSDestroyCtx(&mpm_ctx);
1398 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1403 static int SCHSTest10(
void)
1410 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1418 SCHSPreparePatterns(&mpm_ctx);
1419 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1421 const char *buf =
"01234567890123456789012345678901234567890123456789"
1422 "01234567890123456789012345678901234567890123456789"
1424 "01234567890123456789012345678901234567890123456789"
1425 "01234567890123456789012345678901234567890123456789";
1426 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1432 printf(
"1 != %" PRIu32
" ",
cnt);
1434 SCHSDestroyCtx(&mpm_ctx);
1435 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1440 static int SCHSTest11(
void)
1447 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
1451 if (
MpmAddPatternCS(&mpm_ctx, (uint8_t *)
"he", 2, 0, 0, 1, 0, 0) == -1)
1453 if (
MpmAddPatternCS(&mpm_ctx, (uint8_t *)
"she", 3, 0, 0, 2, 0, 0) == -1)
1455 if (
MpmAddPatternCS(&mpm_ctx, (uint8_t *)
"his", 3, 0, 0, 3, 0, 0) == -1)
1457 if (
MpmAddPatternCS(&mpm_ctx, (uint8_t *)
"hers", 4, 0, 0, 4, 0, 0) == -1)
1461 if (SCHSPreparePatterns(&mpm_ctx) == -1)
1464 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1468 const char *buf =
"he";
1469 result &= (SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1472 result &= (SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1475 result &= (SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1478 result &= (SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1482 SCHSDestroyCtx(&mpm_ctx);
1483 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1488 static int SCHSTest12(
void)
1495 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1505 SCHSPreparePatterns(&mpm_ctx);
1506 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1508 const char *buf =
"abcdefghijklmnopqrstuvwxyz";
1509 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1515 printf(
"2 != %" PRIu32
" ",
cnt);
1517 SCHSDestroyCtx(&mpm_ctx);
1518 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1523 static int SCHSTest13(
void)
1530 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1535 const char pat[] =
"abcdefghijklmnopqrstuvwxyzABCD";
1536 MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat,
sizeof(pat) - 1, 0, 0, 0, 0, 0);
1539 SCHSPreparePatterns(&mpm_ctx);
1540 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1542 const char *buf =
"abcdefghijklmnopqrstuvwxyzABCD";
1543 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1549 printf(
"1 != %" PRIu32
" ",
cnt);
1551 SCHSDestroyCtx(&mpm_ctx);
1552 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1557 static int SCHSTest14(
void)
1564 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1569 const char pat[] =
"abcdefghijklmnopqrstuvwxyzABCDE";
1570 MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat,
sizeof(pat) - 1, 0, 0, 0, 0, 0);
1573 SCHSPreparePatterns(&mpm_ctx);
1574 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1576 const char *buf =
"abcdefghijklmnopqrstuvwxyzABCDE";
1577 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1583 printf(
"1 != %" PRIu32
" ",
cnt);
1585 SCHSDestroyCtx(&mpm_ctx);
1586 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1591 static int SCHSTest15(
void)
1598 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1603 const char pat[] =
"abcdefghijklmnopqrstuvwxyzABCDEF";
1604 MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat,
sizeof(pat) - 1, 0, 0, 0, 0, 0);
1607 SCHSPreparePatterns(&mpm_ctx);
1608 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1610 const char *buf =
"abcdefghijklmnopqrstuvwxyzABCDEF";
1611 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1617 printf(
"1 != %" PRIu32
" ",
cnt);
1619 SCHSDestroyCtx(&mpm_ctx);
1620 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1625 static int SCHSTest16(
void)
1632 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1637 const char pat[] =
"abcdefghijklmnopqrstuvwxyzABC";
1638 MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat,
sizeof(pat) - 1, 0, 0, 0, 0, 0);
1641 SCHSPreparePatterns(&mpm_ctx);
1642 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1644 const char *buf =
"abcdefghijklmnopqrstuvwxyzABC";
1645 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1651 printf(
"1 != %" PRIu32
" ",
cnt);
1653 SCHSDestroyCtx(&mpm_ctx);
1654 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1659 static int SCHSTest17(
void)
1666 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1671 const char pat[] =
"abcdefghijklmnopqrstuvwxyzAB";
1672 MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat,
sizeof(pat) - 1, 0, 0, 0, 0, 0);
1675 SCHSPreparePatterns(&mpm_ctx);
1676 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1678 const char *buf =
"abcdefghijklmnopqrstuvwxyzAB";
1679 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1685 printf(
"1 != %" PRIu32
" ",
cnt);
1687 SCHSDestroyCtx(&mpm_ctx);
1688 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1693 static int SCHSTest18(
void)
1700 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1705 const char pat[] =
"abcde"
1711 MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat,
sizeof(pat) - 1, 0, 0, 0, 0, 0);
1714 SCHSPreparePatterns(&mpm_ctx);
1715 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1717 const char *buf =
"abcde"
1723 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1729 printf(
"1 != %" PRIu32
" ",
cnt);
1731 SCHSDestroyCtx(&mpm_ctx);
1732 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1737 static int SCHSTest19(
void)
1744 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1749 const char pat[] =
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1750 MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat,
sizeof(pat) - 1, 0, 0, 0, 0, 0);
1753 SCHSPreparePatterns(&mpm_ctx);
1754 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1756 const char *buf =
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1757 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1763 printf(
"1 != %" PRIu32
" ",
cnt);
1765 SCHSDestroyCtx(&mpm_ctx);
1766 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1771 static int SCHSTest20(
void)
1778 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1783 const char pat[] =
"AAAAA"
1790 MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat,
sizeof(pat) - 1, 0, 0, 0, 0, 0);
1793 SCHSPreparePatterns(&mpm_ctx);
1794 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1796 const char *buf =
"AAAAA"
1803 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1809 printf(
"1 != %" PRIu32
" ",
cnt);
1811 SCHSDestroyCtx(&mpm_ctx);
1812 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1817 static int SCHSTest21(
void)
1824 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1832 SCHSPreparePatterns(&mpm_ctx);
1833 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1836 SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)
"AA", 2);
1841 printf(
"1 != %" PRIu32
" ",
cnt);
1843 SCHSDestroyCtx(&mpm_ctx);
1844 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1849 static int SCHSTest22(
void)
1856 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1866 SCHSPreparePatterns(&mpm_ctx);
1867 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1869 const char *buf =
"abcdefghijklmnopqrstuvwxyz";
1870 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1876 printf(
"2 != %" PRIu32
" ",
cnt);
1878 SCHSDestroyCtx(&mpm_ctx);
1879 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1884 static int SCHSTest23(
void)
1891 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1899 SCHSPreparePatterns(&mpm_ctx);
1900 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1903 SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)
"aa", 2);
1908 printf(
"1 != %" PRIu32
" ",
cnt);
1910 SCHSDestroyCtx(&mpm_ctx);
1911 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1916 static int SCHSTest24(
void)
1923 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1931 SCHSPreparePatterns(&mpm_ctx);
1932 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1935 SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)
"aa", 2);
1940 printf(
"1 != %" PRIu32
" ",
cnt);
1942 SCHSDestroyCtx(&mpm_ctx);
1943 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1948 static int SCHSTest25(
void)
1955 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1964 SCHSPreparePatterns(&mpm_ctx);
1965 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1967 const char *buf =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1968 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1974 printf(
"3 != %" PRIu32
" ",
cnt);
1976 SCHSDestroyCtx(&mpm_ctx);
1977 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1982 static int SCHSTest26(
void)
1989 memset(&mpm_ctx, 0x00,
sizeof(
MpmCtx));
1997 SCHSPreparePatterns(&mpm_ctx);
1998 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2000 const char *buf =
"works";
2001 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
2007 printf(
"3 != %" PRIu32
" ",
cnt);
2009 SCHSDestroyCtx(&mpm_ctx);
2010 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2015 static int SCHSTest27(
void)
2022 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
2030 SCHSPreparePatterns(&mpm_ctx);
2031 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2033 const char *buf =
"tone";
2034 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
2040 printf(
"0 != %" PRIu32
" ",
cnt);
2042 SCHSDestroyCtx(&mpm_ctx);
2043 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2048 static int SCHSTest28(
void)
2055 memset(&mpm_ctx, 0,
sizeof(
MpmCtx));
2063 SCHSPreparePatterns(&mpm_ctx);
2064 SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2066 const char *buf =
"tONE";
2067 uint32_t
cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
2073 printf(
"0 != %" PRIu32
" ",
cnt);
2075 SCHSDestroyCtx(&mpm_ctx);
2076 SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2081 static int SCHSTest29(
void)
2083 uint8_t buf[] =
"onetwothreefourfivesixseveneightnine";
2084 uint16_t buflen =
sizeof(buf) - 1;
2090 memset(&th_v, 0,
sizeof(th_v));
2101 de_ctx,
"alert tcp any any -> any any "
2102 "(content:\"onetwothreefourfivesixseveneightnine\"; sid:1;)");
2107 "(content:\"onetwothreefourfivesixseveneightnine\"; "
2108 "fast_pattern:3,3; sid:2;)");
2117 printf(
"if (PacketAlertCheck(p, 1) != 1) failure\n");
2121 printf(
"if (PacketAlertCheck(p, 1) != 2) failure\n");
2139 static void SCHSRegisterTests(
void)