suricata
util-mpm-ac-ks.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2014 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Ken Steele <suricata@tilera.com>
22  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23  *
24  * Aho-corasick MPM optimized for the Tilera Tile-Gx architecture.
25  *
26  * Efficient String Matching: An Aid to Bibliographic Search
27  * Alfred V. Aho and Margaret J. Corasick
28  *
29  * - Started with util-mpm-ac.c:
30  * - Uses the delta table for calculating transitions,
31  * instead of having separate goto and failure
32  * transitions.
33  * - If we cross 2 ** 16 states, we use 4 bytes in the
34  * transition table to hold each state, otherwise we use
35  * 2 bytes.
36  * - This version of the MPM is heavy on memory, but it
37  * performs well. If you can fit the ruleset with this
38  * mpm on your box without hitting swap, this is the MPM
39  * to go for.
40  *
41  * - Added these optimizations:
42  * - Compress the input alphabet from 256 characters down
43  * to the actual characters used in the patterns, plus
44  * one character for all the unused characters.
45  * - Reduce the size of the delta table so that each state
46  * is the smallest power of two that is larger than the
47  * size of the compressed alphabet.
48  * - Specialized the search function based on state count
49  * (small for 8-bit large for 16-bit) and the size of
50  * the alphabet, so that it is constant inside the
51  * function for better optimization.
52  *
53  * \todo - Do a proper analysis of our existing MPMs and suggest a good
54  * one based on the pattern distribution and the expected
55  * traffic(say http).
56 
57  * - Irrespective of whether we cross 2 ** 16 states or
58  * not,shift to using uint32_t for state type, so that we can
59  * integrate it's status as a final state or not in the
60  * topmost byte. We are already doing it if state_count is >
61  * 2 ** 16.
62  * - Test case-sensitive patterns if they have any ascii chars.
63  * If they don't treat them as nocase.
64  * - Reorder the compressed alphabet to put the most common characters
65  * first.
66  */
67 
68 #include "suricata-common.h"
69 #include "suricata.h"
70 
71 #include "detect.h"
72 #include "detect-parse.h"
73 #include "detect-engine.h"
74 #include "detect-engine-build.h"
75 
76 #include "conf.h"
77 #include "util-debug.h"
78 #include "util-unittest.h"
79 #include "util-unittest-helper.h"
80 #include "util-memcmp.h"
81 #include "util-memcpy.h"
82 #include "util-validate.h"
83 #include "util-mpm-ac-ks.h"
84 #include "util-mpm-ac-queue.h"
85 
86 #if __BYTE_ORDER == __LITTLE_ENDIAN
87 
88 void SCACTileInitCtx(MpmCtx *);
91  MpmCtx *, const uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t);
92 int SCACTileAddPatternCS(MpmCtx *, uint8_t *, uint16_t, uint16_t, uint16_t,
93  uint32_t, SigIntId, uint8_t);
94 int SCACTilePreparePatterns(MpmConfig *mpm_conf, MpmCtx *mpm_ctx);
95 uint32_t SCACTileSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
96  PrefilterRuleStore *pmq, const uint8_t *buf,
97  uint32_t buflen);
98 void SCACTilePrintInfo(MpmCtx *mpm_ctx);
99 #ifdef UNITTESTS
100 static void SCACTileRegisterTests(void);
101 #endif
102 
103 uint32_t SCACTileSearchLarge(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
104  PrefilterRuleStore *pmq,
105  const uint8_t *buf, uint32_t buflen);
106 uint32_t SCACTileSearchSmall256(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
107  PrefilterRuleStore *pmq,
108  const uint8_t *buf, uint32_t buflen);
109 uint32_t SCACTileSearchSmall128(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
110  PrefilterRuleStore *pmq,
111  const uint8_t *buf, uint32_t buflen);
112 uint32_t SCACTileSearchSmall64(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
113  PrefilterRuleStore *pmq,
114  const uint8_t *buf, uint32_t buflen);
115 uint32_t SCACTileSearchSmall32(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
116  PrefilterRuleStore *pmq,
117  const uint8_t *buf, uint32_t buflen);
118 uint32_t SCACTileSearchSmall16(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
119  PrefilterRuleStore *pmq,
120  const uint8_t *buf, uint32_t buflen);
121 uint32_t SCACTileSearchSmall8(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
122  PrefilterRuleStore *pmq,
123  const uint8_t *buf, uint32_t buflen);
124 
125 uint32_t SCACTileSearchTiny256(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
126  PrefilterRuleStore *pmq,
127  const uint8_t *buf, uint32_t buflen);
128 uint32_t SCACTileSearchTiny128(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
129  PrefilterRuleStore *pmq,
130  const uint8_t *buf, uint32_t buflen);
131 uint32_t SCACTileSearchTiny64(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
132  PrefilterRuleStore *pmq,
133  const uint8_t *buf, uint32_t buflen);
134 uint32_t SCACTileSearchTiny32(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
135  PrefilterRuleStore *pmq,
136  const uint8_t *buf, uint32_t buflen);
137 uint32_t SCACTileSearchTiny16(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
138  PrefilterRuleStore *pmq,
139  const uint8_t *buf, uint32_t buflen);
140 uint32_t SCACTileSearchTiny8(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
141  PrefilterRuleStore *pmq,
142  const uint8_t *buf, uint32_t buflen);
143 
144 
145 static void SCACTileDestroyInitCtx(MpmCtx *mpm_ctx);
146 
147 
148 /* a placeholder to denote a failure transition in the goto table */
149 #define SC_AC_TILE_FAIL (-1)
150 
151 /**
152  * \internal
153  * \brief Initialize the AC context with user specified conf parameters. We
154  * aren't retrieving anything for AC conf now, but we will certainly
155  * need it, when we customize AC.
156  */
157 static void SCACTileGetConfig(void)
158 {
159 }
160 
161 /**
162  * \internal
163  * \brief Count the occurrences of each character in the pattern and
164  * accumulate into a histogram. Really only used to detect unused
165  * characters, so could just set to 1 instead of counting.
166  */
167 static inline void SCACTileHistogramAlphabet(SCACTileCtx *ctx,
168  MpmPattern *p)
169 {
170  for (int i = 0; i < p->len; i++) {
171  ctx->alpha_hist[p->ci[i]]++;
172  }
173 }
174 
175 /* Use Alphabet Histogram to create compressed alphabet.
176  */
177 static void SCACTileInitTranslateTable(SCACTileCtx *ctx)
178 {
179  /* Count the number of ASCII values actually appearing in any
180  * pattern. Create compressed mapping table with unused
181  * characters mapping to zero.
182  */
183  for (int i = 0; i < 256; i++) {
184  /* Move all upper case counts to lower case */
185  if (i >= 'A' && i <= 'Z') {
186  ctx->alpha_hist[i - 'A' + 'a'] += ctx->alpha_hist[i];
187  ctx->alpha_hist[i] = 0;
188  }
189  if (ctx->alpha_hist[i]) {
190  ctx->alphabet_size++;
191  DEBUG_VALIDATE_BUG_ON(ctx->alphabet_size > UINT8_MAX);
192  ctx->translate_table[i] = (uint8_t)ctx->alphabet_size;
193  } else
194  ctx->translate_table[i] = 0;
195  }
196  /* Fix up translation table for uppercase */
197  for (int i = 'A'; i <= 'Z'; i++)
198  ctx->translate_table[i] = ctx->translate_table[i - 'A' + 'a'];
199 
200  SCLogDebug(" Alphabet size %d", ctx->alphabet_size);
201 
202  /* Round alphabet size up to next power-of-two Leave one extra
203  * space For the unused-characters = 0 mapping.
204  */
205  ctx->alphabet_size += 1; /* Extra space for unused-character */
206  if (ctx->alphabet_size <= 8) {
207  ctx->alphabet_storage = 8;
208  } else if (ctx->alphabet_size <= 16) {
209  ctx->alphabet_storage = 16;
210  } else if (ctx->alphabet_size <= 32) {
211  ctx->alphabet_storage = 32;
212  } else if (ctx->alphabet_size <= 64) {
213  ctx->alphabet_storage = 64;
214  } else if (ctx->alphabet_size <= 128) {
215  ctx->alphabet_storage = 128;
216  } else
217  ctx->alphabet_storage = 256;
218 }
219 
220 static void SCACTileReallocOutputTable(SCACTileCtx *ctx, int new_state_count)
221 {
222 
223  /* reallocate space in the output table for the new state */
224  size_t size = ctx->allocated_state_count * sizeof(SCACTileOutputTable);
225  void *ptmp = SCRealloc(ctx->output_table, size);
226  if (ptmp == NULL) {
227  SCFree(ctx->output_table);
228  ctx->output_table = NULL;
229  FatalError("Error allocating memory");
230  }
231  ctx->output_table = ptmp;
232 }
233 
234 static void SCACTileReallocState(SCACTileCtx *ctx, int new_state_count)
235 {
236  /* reallocate space in the goto table to include a new state */
237  size_t size = ctx->allocated_state_count * sizeof(int32_t) * 256;
238  void *ptmp = SCRealloc(ctx->goto_table, size);
239  if (ptmp == NULL) {
240  SCFree(ctx->goto_table);
241  ctx->goto_table = NULL;
242  FatalError("Error allocating memory");
243  }
244  ctx->goto_table = ptmp;
245 
246  SCACTileReallocOutputTable(ctx, new_state_count);
247 }
248 
249 /**
250  * \internal
251  * \brief Initialize a new state in the goto and output tables.
252  *
253  * \param mpm_ctx Pointer to the mpm context.
254  *
255  * \retval The state id, of the newly created state.
256  */
257 static inline int SCACTileInitNewState(MpmCtx *mpm_ctx)
258 {
259  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
260  SCACTileCtx *ctx = search_ctx->init_ctx;
261  int aa = 0;
262 
263  /* Exponentially increase the allocated space when needed. */
264  if (ctx->allocated_state_count < ctx->state_count + 1) {
265  if (ctx->allocated_state_count == 0)
266  ctx->allocated_state_count = 256;
267  else
268  ctx->allocated_state_count *= 2;
269 
270  SCACTileReallocState(ctx, ctx->allocated_state_count);
271  }
272 
273  /* set all transitions for the newly assigned state as FAIL transitions */
274  for (aa = 0; aa < ctx->alphabet_size; aa++) {
275  ctx->goto_table[ctx->state_count][aa] = SC_AC_TILE_FAIL;
276  }
277 
278  memset(ctx->output_table + ctx->state_count, 0,
279  sizeof(SCACTileOutputTable));
280 
281  return ctx->state_count++;
282 }
283 
284 /**
285  * \internal
286  * \brief Adds a pid to the output table for a state.
287  *
288  * \param state The state to whose output table we should add the pid.
289  * \param pid The pattern id to add.
290  * \param mpm_ctx Pointer to the mpm context.
291  */
292 static void SCACTileSetOutputState(int32_t state, MpmPatternIndex pindex, MpmCtx *mpm_ctx)
293 {
294  void *ptmp;
295  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
296  SCACTileCtx *ctx = search_ctx->init_ctx;
297 
298  SCACTileOutputTable *output_state = &ctx->output_table[state];
299  uint32_t i = 0;
300 
301  /* Don't add the pattern more than once to the same state. */
302  for (i = 0; i < output_state->no_of_entries; i++) {
303  if (output_state->patterns[i] == pindex)
304  return;
305  }
306 
307  /* Increase the size of the array of pids for this state and add
308  * the new pid. */
309  output_state->no_of_entries++;
310  ptmp = SCRealloc(output_state->patterns,
311  output_state->no_of_entries * sizeof(MpmPatternIndex));
312  if (ptmp == NULL) {
313  SCFree(output_state->patterns);
314  output_state->patterns = NULL;
315  FatalError("Error allocating memory");
316  }
317  output_state->patterns = ptmp;
318 
319  output_state->patterns[output_state->no_of_entries - 1] = pindex;
320 }
321 
322 /**
323  * \brief Helper function used by SCACTileCreateGotoTable. Adds a
324  * pattern to the goto table.
325  *
326  * \param pattern Pointer to the pattern.
327  * \param pattern_len Pattern length.
328  * \param pid The pattern id, that corresponds to this pattern. We
329  * need it to updated the output table for this pattern.
330  * \param mpm_ctx Pointer to the mpm context.
331  */
332 static void SCACTileEnter(uint8_t *pattern, uint16_t pattern_len,
333  MpmPatternIndex pindex, MpmCtx *mpm_ctx)
334 {
335  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
336  SCACTileCtx *ctx = search_ctx->init_ctx;
337 
338  int32_t state = 0;
339  int32_t newstate = 0;
340  int i = 0;
341  int p = 0;
342  int tc;
343 
344  /* Walk down the trie till we have a match for the pattern prefix */
345  state = 0;
346  for (i = 0; i < pattern_len; i++) {
347  tc = ctx->translate_table[pattern[i]];
348  if (ctx->goto_table[state][tc] == SC_AC_TILE_FAIL)
349  break;
350  state = ctx->goto_table[state][tc];
351  }
352 
353  /* Add the non-matching pattern suffix to the trie, from the last state
354  * we left off */
355  for (p = i; p < pattern_len; p++) {
356  newstate = SCACTileInitNewState(mpm_ctx);
357  tc = ctx->translate_table[pattern[p]];
358  ctx->goto_table[state][tc] = newstate;
359  state = newstate;
360  }
361 
362  /* Add this pattern id, to the output table of the last state, where the
363  * pattern ends in the trie */
364  SCACTileSetOutputState(state, pindex, mpm_ctx);
365 }
366 
367 /**
368  * \internal
369  * \brief Create the goto table.
370  *
371  * \param mpm_ctx Pointer to the mpm context.
372  */
373 static void SCACTileCreateGotoTable(MpmCtx *mpm_ctx)
374 {
375  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
376  SCACTileCtx *ctx = search_ctx->init_ctx;
377 
378  uint32_t i = 0;
379 
380  /* add each pattern to create the goto table */
381  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
382  SCACTileEnter(ctx->parray[i]->ci, ctx->parray[i]->len,
383  i, mpm_ctx);
384  }
385 
386  int aa = 0;
387  for (aa = 0; aa < ctx->alphabet_size; aa++) {
388  if (ctx->goto_table[0][aa] == SC_AC_TILE_FAIL) {
389  ctx->goto_table[0][aa] = 0;
390  }
391  }
392 }
393 
394 /**
395  * \internal
396  * \brief Club the output data from 2 states and store it in the 1st state.
397  * dst_state_data = {dst_state_data} UNION {src_state_data}
398  *
399  * \param dst_state First state(also the destination) for the union operation.
400  * \param src_state Second state for the union operation.
401  * \param mpm_ctx Pointer to the mpm context.
402  */
403 static void SCACTileClubOutputStates(int32_t dst_state,
404  int32_t src_state,
405  MpmCtx *mpm_ctx)
406 {
407  void *ptmp;
408  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
409  SCACTileCtx *ctx = search_ctx->init_ctx;
410 
411  uint32_t i = 0;
412  uint32_t j = 0;
413 
414  SCACTileOutputTable *output_dst_state = &ctx->output_table[dst_state];
415  SCACTileOutputTable *output_src_state = &ctx->output_table[src_state];
416 
417  for (i = 0; i < output_src_state->no_of_entries; i++) {
418  for (j = 0; j < output_dst_state->no_of_entries; j++) {
419  if (output_src_state->patterns[i] == output_dst_state->patterns[j]) {
420  break;
421  }
422  }
423  if (j == output_dst_state->no_of_entries) {
424  output_dst_state->no_of_entries++;
425 
426  ptmp = SCRealloc(output_dst_state->patterns,
427  (output_dst_state->no_of_entries * sizeof(uint32_t)));
428  if (ptmp == NULL) {
429  SCFree(output_dst_state->patterns);
430  output_dst_state->patterns = NULL;
431  FatalError("Error allocating memory");
432  }
433  output_dst_state->patterns = ptmp;
434 
435  output_dst_state->patterns[output_dst_state->no_of_entries - 1] =
436  output_src_state->patterns[i];
437  }
438  }
439 }
440 
441 /**
442  * \internal
443  * \brief Create the failure table.
444  *
445  * \param mpm_ctx Pointer to the mpm context.
446  */
447 static void SCACTileCreateFailureTable(MpmCtx *mpm_ctx)
448 {
449  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
450  SCACTileCtx *ctx = search_ctx->init_ctx;
451 
452  int aa = 0;
453  int32_t state = 0;
454  int32_t r_state = 0;
455 
457 
458  /* Allocate space for the failure table. A failure entry in the table for
459  * every state(SCACTileCtx->state_count) */
460  ctx->failure_table = SCCalloc(ctx->state_count, sizeof(int32_t));
461  if (ctx->failure_table == NULL) {
462  FatalError("Error allocating memory");
463  }
464 
465  /* Add the failure transitions for the 0th state, and add every non-fail
466  * transition from the 0th state to the queue for further processing
467  * of failure states */
468  for (aa = 0; aa < ctx->alphabet_size; aa++) {
469  int32_t temp_state = ctx->goto_table[0][aa];
470  if (temp_state != 0) {
471  SCACEnqueue(q, temp_state);
472  ctx->failure_table[temp_state] = 0;
473  }
474  }
475 
476  while (!SCACStateQueueIsEmpty(q)) {
477  /* pick up every state from the queue and add failure transitions */
478  r_state = SCACDequeue(q);
479  for (aa = 0; aa < ctx->alphabet_size; aa++) {
480  int32_t temp_state = ctx->goto_table[r_state][aa];
481  if (temp_state == SC_AC_TILE_FAIL)
482  continue;
483  SCACEnqueue(q, temp_state);
484  state = ctx->failure_table[r_state];
485 
486  while(ctx->goto_table[state][aa] == SC_AC_TILE_FAIL)
487  state = ctx->failure_table[state];
488  ctx->failure_table[temp_state] = ctx->goto_table[state][aa];
489  SCACTileClubOutputStates(temp_state, ctx->failure_table[temp_state],
490  mpm_ctx);
491  }
492  }
494 }
495 
496 /*
497  * Set the next state for 1 byte next-state.
498  */
499 static void SCACTileSetState1Byte(SCACTileCtx *ctx, int state, int aa,
500  int next_state, int outputs)
501 {
502  uint8_t *state_table = (uint8_t*)ctx->state_table;
503  DEBUG_VALIDATE_BUG_ON(next_state < 0 || next_state > UINT8_MAX);
504  uint8_t encoded_next_state = (uint8_t)next_state;
505 
506  if (next_state == SC_AC_TILE_FAIL) {
507  FatalError("Error FAIL state in output");
508  }
509 
510  if (outputs == 0)
511  encoded_next_state |= (1 << 7);
512 
513  state_table[state * ctx->alphabet_storage + aa] = encoded_next_state;
514 }
515 
516 /*
517  * Set the next state for 2 byte next-state.
518  */
519 static void SCACTileSetState2Bytes(SCACTileCtx *ctx, int state, int aa,
520  int next_state, int outputs)
521 {
522  uint16_t *state_table = (uint16_t*)ctx->state_table;
523  DEBUG_VALIDATE_BUG_ON(next_state < 0 || next_state > UINT16_MAX);
524  uint16_t encoded_next_state = (uint16_t)next_state;
525 
526  if (next_state == SC_AC_TILE_FAIL) {
527  FatalError("Error FAIL state in output");
528  }
529 
530  if (outputs == 0)
531  encoded_next_state |= (1 << 15);
532 
533  state_table[state * ctx->alphabet_storage + aa] = encoded_next_state;
534 }
535 
536 /*
537  * Set the next state for 4 byte next-state.
538  */
539 static void SCACTileSetState4Bytes(SCACTileCtx *ctx, int state, int aa,
540  int next_state, int outputs)
541 {
542  uint32_t *state_table = (uint32_t*)ctx->state_table;
543  uint32_t encoded_next_state = next_state;
544 
545  if (next_state == SC_AC_TILE_FAIL) {
546  FatalError("Error FAIL state in output");
547  }
548 
549  if (outputs == 0)
550  encoded_next_state |= (1UL << 31);
551 
552  state_table[state * ctx->alphabet_storage + aa] = encoded_next_state;
553 }
554 
555 /**
556  * \internal
557  * \brief Create the delta table.
558  *
559  * \param mpm_ctx Pointer to the mpm context.
560  */
561 static inline void SCACTileCreateDeltaTable(MpmCtx *mpm_ctx)
562 {
563  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
564  SCACTileCtx *ctx = search_ctx->init_ctx;
565 
566  int aa = 0;
567  int32_t r_state = 0;
568 
569  if (ctx->state_count < 32767) {
570  if (ctx->state_count < 128) {
571  ctx->bytes_per_state = 1;
572  ctx->SetNextState = SCACTileSetState1Byte;
573 
574  switch(ctx->alphabet_storage) {
575  case 8:
576  ctx->Search = SCACTileSearchTiny8;
577  break;
578  case 16:
579  ctx->Search = SCACTileSearchTiny16;
580  break;
581  case 32:
582  ctx->Search = SCACTileSearchTiny32;
583  break;
584  case 64:
585  ctx->Search = SCACTileSearchTiny64;
586  break;
587  case 128:
588  ctx->Search = SCACTileSearchTiny128;
589  break;
590  default:
591  ctx->Search = SCACTileSearchTiny256;
592  }
593  } else {
594  /* 16-bit state needed */
595  ctx->bytes_per_state = 2;
596  ctx->SetNextState = SCACTileSetState2Bytes;
597 
598  switch(ctx->alphabet_storage) {
599  case 8:
600  ctx->Search = SCACTileSearchSmall8;
601  break;
602  case 16:
603  ctx->Search = SCACTileSearchSmall16;
604  break;
605  case 32:
606  ctx->Search = SCACTileSearchSmall32;
607  break;
608  case 64:
609  ctx->Search = SCACTileSearchSmall64;
610  break;
611  case 128:
612  ctx->Search = SCACTileSearchSmall128;
613  break;
614  default:
615  ctx->Search = SCACTileSearchSmall256;
616  }
617  }
618  } else {
619  /* 32-bit next state */
620  ctx->Search = SCACTileSearchLarge;
621  ctx->bytes_per_state = 4;
622  ctx->SetNextState = SCACTileSetState4Bytes;
623 
624  ctx->alphabet_storage = 256; /* Change? */
625  }
626 
628 
629  for (aa = 0; aa < ctx->alphabet_size; aa++) {
630  int temp_state = ctx->goto_table[0][aa];
631  if (temp_state != 0)
632  SCACEnqueue(q, temp_state);
633  }
634 
635  while (!SCACStateQueueIsEmpty(q)) {
636  r_state = SCACDequeue(q);
637 
638  for (aa = 0; aa < ctx->alphabet_size; aa++) {
639  int temp_state = ctx->goto_table[r_state][aa];
640  if (temp_state != SC_AC_TILE_FAIL) {
641  SCACEnqueue(q, temp_state);
642  } else {
643  int f_state = ctx->failure_table[r_state];
644  ctx->goto_table[r_state][aa] = ctx->goto_table[f_state][aa];
645  }
646  }
647  }
649 }
650 
651 static void SCACTileClubOutputStatePresenceWithDeltaTable(MpmCtx *mpm_ctx)
652 {
653  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
654  SCACTileCtx *ctx = search_ctx->init_ctx;
655 
656  int aa = 0;
657  uint32_t state = 0;
658 
659  /* Allocate next-state table. */
660  int size = ctx->state_count * ctx->bytes_per_state * ctx->alphabet_storage;
661  void *state_table = SCCalloc(1, size);
662  if (unlikely(state_table == NULL)) {
663  FatalError("Error allocating memory");
664  }
665  ctx->state_table = state_table;
666 
667  mpm_ctx->memory_cnt++;
668  mpm_ctx->memory_size += size;
669 
670  SCLogDebug("Delta Table size %d, alphabet: %d, %d-byte states: %d",
671  size, ctx->alphabet_size, ctx->bytes_per_state, ctx->state_count);
672 
673  /* Copy next state from Goto table, which is 32 bits and encode it into the next
674  * state table, which can be 1, 2 or 4 bytes each and include if there is an
675  * output.
676  */
677  for (state = 0; state < ctx->state_count; state++) {
678  for (aa = 0; aa < ctx->alphabet_size; aa++) {
679  int next_state = ctx->goto_table[state][aa];
680  int next_state_outputs = ctx->output_table[next_state].no_of_entries;
681  ctx->SetNextState(ctx, state, aa, next_state, next_state_outputs);
682  }
683  }
684 }
685 
686 static inline void SCACTileInsertCaseSensitiveEntriesForPatterns(MpmCtx *mpm_ctx)
687 {
688  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
689  SCACTileCtx *ctx = search_ctx->init_ctx;
690 
691  uint32_t state = 0;
692  uint32_t k = 0;
693 
694  for (state = 0; state < ctx->state_count; state++) {
695  if (ctx->output_table[state].no_of_entries == 0)
696  continue;
697 
698  for (k = 0; k < ctx->output_table[state].no_of_entries; k++) {
699  if (ctx->pattern_list[ctx->output_table[state].patterns[k]].cs != NULL) {
700  /* TODO - Find better way to store this. */
701  ctx->output_table[state].patterns[k] &= 0x0FFFFFFF;
702  ctx->output_table[state].patterns[k] |= (uint32_t)1 << 31;
703  }
704  }
705  }
706 }
707 
708 #if 0
709 static void SCACTilePrintDeltaTable(MpmCtx *mpm_ctx)
710 {
711  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
712  SCACTileCtx *ctx = search_ctx->init_ctx;
713 
714  int i = 0, j = 0;
715 
716  printf("##############Delta Table##############\n");
717  for (i = 0; i < ctx->state_count; i++) {
718  printf("%d: \n", i);
719  for (j = 0; j < ctx->alphabet_size; j++) {
720  if (SCACTileGetDelta(i, j, mpm_ctx) != 0) {
721  printf(" %c -> %d\n", j, SCACTileGetDelta(i, j, mpm_ctx));
722  }
723  }
724  }
725 }
726 #endif
727 
728 /**
729  * \brief Process the patterns and prepare the state table.
730  *
731  * \param mpm_ctx Pointer to the mpm context.
732  */
733 static void SCACTilePrepareStateTable(MpmCtx *mpm_ctx)
734 {
735  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
736  SCACTileCtx *ctx = search_ctx->init_ctx;
737 
738  /* Create Alphabet compression and Lower Case translation table. */
739  SCACTileInitTranslateTable(ctx);
740 
741  /* create the 0th state in the goto table and output_table */
742  SCACTileInitNewState(mpm_ctx);
743 
744  /* create the goto table */
745  SCACTileCreateGotoTable(mpm_ctx);
746  /* create the failure table */
747  SCACTileCreateFailureTable(mpm_ctx);
748  /* create the final state(delta) table */
749  SCACTileCreateDeltaTable(mpm_ctx);
750  /* club the output state presence with delta transition entries */
751  SCACTileClubOutputStatePresenceWithDeltaTable(mpm_ctx);
752 
753  /* club nocase entries */
754  SCACTileInsertCaseSensitiveEntriesForPatterns(mpm_ctx);
755 
756 #if 0
757  SCACTilePrintDeltaTable(mpm_ctx);
758 #endif
759 
760  /* we don't need these anymore */
761  SCFree(ctx->goto_table);
762  ctx->goto_table = NULL;
763  SCFree(ctx->failure_table);
764  ctx->failure_table = NULL;
765 }
766 
767 
768 /**
769  * \brief Process Internal AC MPM tables to create the Search Context
770  *
771  * The search context is only the data needed to search the MPM.
772  *
773  * \param mpm_ctx Pointer to the mpm context.
774  */
775 static void SCACTilePrepareSearch(MpmCtx *mpm_ctx)
776 {
777  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
778  SCACTileCtx *ctx = search_ctx->init_ctx;
779 
780  /* Resize the output table to be only as big as its final size. */
781  SCACTileReallocOutputTable(ctx, ctx->state_count);
782 
783  search_ctx->Search = ctx->Search;
784  memcpy(search_ctx->translate_table, ctx->translate_table, sizeof(ctx->translate_table));
785 
786  /* Move the state table from the Init context */
787  search_ctx->state_table = ctx->state_table;
788  ctx->state_table = NULL; /* So that it won't get freed twice. */
789 
790  /* Move the output_table from the Init context to the Search Context */
791  /* TODO: Could be made more compact */
792  search_ctx->output_table = ctx->output_table;
793  ctx->output_table = NULL;
794  search_ctx->state_count = ctx->state_count;
795 
796  search_ctx->pattern_list = ctx->pattern_list;
797  ctx->pattern_list = NULL;
798  search_ctx->pattern_cnt = mpm_ctx->pattern_cnt;
799 
800  /* One bit per pattern, rounded up to the next byte size. */
801  search_ctx->mpm_bitarray_size = (mpm_ctx->pattern_cnt + 7) / 8;
802 
803  /* Can now free the Initialization data */
804  SCACTileDestroyInitCtx(mpm_ctx);
805 }
806 
807 /**
808  * \brief Process the patterns added to the mpm, and create the internal tables.
809  *
810  * \param mpm_conf Pointer to the generic MPM matcher configuration
811  * \param mpm_ctx Pointer to the mpm context.
812  */
813 int SCACTilePreparePatterns(MpmConfig *mpm_conf, MpmCtx *mpm_ctx)
814 {
815  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
816 
817  if (mpm_ctx->pattern_cnt == 0 || search_ctx->init_ctx == NULL) {
818  SCLogDebug("no patterns supplied to this mpm_ctx");
819  return 0;
820  }
821  SCACTileCtx *ctx = search_ctx->init_ctx;
822  if (mpm_ctx->init_hash == NULL) {
823  SCLogDebug("no patterns supplied to this mpm_ctx");
824  return 0;
825  }
826 
827  /* alloc the pattern array */
828  ctx->parray = (MpmPattern **)SCCalloc(mpm_ctx->pattern_cnt, sizeof(MpmPattern *));
829  if (ctx->parray == NULL)
830  goto error;
831 
832  /* populate it with the patterns in the hash */
833  uint32_t i = 0, p = 0;
834  for (i = 0; i < MPM_INIT_HASH_SIZE; i++) {
835  MpmPattern *node = mpm_ctx->init_hash[i], *nnode = NULL;
836  while(node != NULL) {
837  nnode = node->next;
838  node->next = NULL;
839  ctx->parray[p++] = node;
840  SCACTileHistogramAlphabet(ctx, node);
841  node = nnode;
842  }
843  }
844 
845  /* we no longer need the hash, so free it's memory */
846  SCFree(mpm_ctx->init_hash);
847  mpm_ctx->init_hash = NULL;
848 
849  /* Handle case patterns by storing a copy of the pattern to compare
850  * to each possible match (no-case).
851  *
852  * Allocate the memory for the array and each of the strings as one block.
853  */
854  size_t string_space_needed = 0;
855  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
856  if (!(ctx->parray[i]->flags & MPM_PATTERN_FLAG_NOCASE)) {
857  /* Round up to next 8 byte aligned length */
858  uint32_t space = ((ctx->parray[i]->len + 7) / 8) * 8;
859  string_space_needed += space;
860  }
861  }
862 
863  size_t pattern_list_size = mpm_ctx->pattern_cnt * sizeof(SCACTilePatternList);
864  size_t mem_size = string_space_needed + pattern_list_size;
865  void *mem_block = SCCalloc(1, mem_size);
866  if (mem_block == NULL) {
867  FatalError("Error allocating memory");
868  }
869  mpm_ctx->memory_cnt++;
870  mpm_ctx->memory_size += mem_size;
871  /* Split the allocated block into pattern list array and string space. */
872  ctx->pattern_list = mem_block;
873  uint8_t *string_space = mem_block + pattern_list_size;
874 
875  /* Now make the copies of the no-case strings. */
876  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
877  if (!(ctx->parray[i]->flags & MPM_PATTERN_FLAG_NOCASE)) {
878  uint16_t len = ctx->parray[i]->len;
879  uint32_t space = ((len + 7) / 8) * 8;
880  memcpy(string_space, ctx->parray[i]->original_pat, len);
881  ctx->pattern_list[i].cs = string_space;
882  ctx->pattern_list[i].patlen = len;
883  string_space += space;
884  }
885  ctx->pattern_list[i].offset = ctx->parray[i]->offset;
886  ctx->pattern_list[i].depth = ctx->parray[i]->depth;
887  ctx->pattern_list[i].pid = ctx->parray[i]->id;
888 
889  /* ACPatternList now owns this memory */
890  ctx->pattern_list[i].sids_size = ctx->parray[i]->sids_size;
891  ctx->pattern_list[i].sids = ctx->parray[i]->sids;
892  ctx->parray[i]->sids = NULL;
893  ctx->parray[i]->sids_size = 0;
894  }
895 
896  /* prepare the state table required by AC */
897  SCACTilePrepareStateTable(mpm_ctx);
898 
899  /* Convert to the Search Context structure */
900  SCACTilePrepareSearch(mpm_ctx);
901 
902  return 0;
903 
904 error:
905  return -1;
906 }
907 
908 /**
909  * \brief Initialize the AC context.
910  *
911  * \param mpm_ctx Mpm context.
912  */
913 void SCACTileInitCtx(MpmCtx *mpm_ctx)
914 {
915  if (mpm_ctx->ctx != NULL)
916  return;
917 
918  /* Search Context */
919  mpm_ctx->ctx = SCCalloc(1, sizeof(SCACTileSearchCtx));
920  if (mpm_ctx->ctx == NULL) {
921  exit(EXIT_FAILURE);
922  }
923 
924  mpm_ctx->memory_cnt++;
925  mpm_ctx->memory_size += sizeof(SCACTileSearchCtx);
926 
927  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
928 
929  /* MPM Creation context */
930  search_ctx->init_ctx = SCCalloc(1, sizeof(SCACTileCtx));
931  if (search_ctx->init_ctx == NULL) {
932  exit(EXIT_FAILURE);
933  }
934 
935  mpm_ctx->memory_cnt++;
936  mpm_ctx->memory_size += sizeof(SCACTileCtx);
937 
938  /* initialize the hash we use to speed up pattern insertions */
939  mpm_ctx->init_hash = SCCalloc(MPM_INIT_HASH_SIZE, sizeof(MpmPattern *));
940  if (mpm_ctx->init_hash == NULL) {
941  exit(EXIT_FAILURE);
942  }
943 
944  /* get conf values for AC from our yaml file. We have no conf values for
945  * now. We will certainly need this, as we develop the algo */
946  SCACTileGetConfig();
947 }
948 
949 static void SCACTileDestroyInitCtx(MpmCtx *mpm_ctx)
950 {
951  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
952  SCACTileCtx *ctx = search_ctx->init_ctx;
953 
954  if (ctx == NULL)
955  return;
956 
957  if (mpm_ctx->init_hash != NULL) {
958  SCFree(mpm_ctx->init_hash);
959  mpm_ctx->init_hash = NULL;
960  }
961 
962  if (ctx->parray != NULL) {
963  uint32_t i;
964  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
965  if (ctx->parray[i] != NULL) {
966  MpmFreePattern(mpm_ctx, ctx->parray[i]);
967  }
968  }
969 
970  SCFree(ctx->parray);
971  ctx->parray = NULL;
972  }
973 
974  if (ctx->state_table != NULL) {
975  SCFree(ctx->state_table);
976 
977  mpm_ctx->memory_cnt--;
978  mpm_ctx->memory_size -= (ctx->state_count *
979  ctx->bytes_per_state * ctx->alphabet_storage);
980  }
981 
982  if (ctx->output_table != NULL) {
983  uint32_t state;
984  for (state = 0; state < ctx->state_count; state++) {
985  if (ctx->output_table[state].patterns != NULL) {
986  SCFree(ctx->output_table[state].patterns);
987  }
988  }
989  SCFree(ctx->output_table);
990  }
991 
992  if (ctx->pattern_list != NULL) {
993  uint32_t i;
994  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
995  if (ctx->pattern_list[i].cs != NULL)
996  SCFree(ctx->pattern_list[i].cs);
997  if (ctx->pattern_list[i].sids != NULL)
998  SCFree(ctx->pattern_list[i].sids);
999  }
1000  SCFree(ctx->pattern_list);
1001  }
1002 
1003  SCFree(ctx);
1004  search_ctx->init_ctx = NULL;
1005  mpm_ctx->memory_cnt--;
1006  mpm_ctx->memory_size -= sizeof(SCACTileCtx);
1007 }
1008 
1009 /**
1010  * \brief Destroy the mpm context.
1011  *
1012  * \param mpm_ctx Pointer to the mpm context.
1013  */
1015 {
1016  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
1017  if (search_ctx == NULL)
1018  return;
1019 
1020  /* Destroy Initialization data */
1021  SCACTileDestroyInitCtx(mpm_ctx);
1022 
1023  /* Free Search tables */
1024  SCFree(search_ctx->state_table);
1025 
1026  if (search_ctx->pattern_list != NULL) {
1027  uint32_t i;
1028  for (i = 0; i < search_ctx->pattern_cnt; i++) {
1029  if (search_ctx->pattern_list[i].sids != NULL)
1030  SCFree(search_ctx->pattern_list[i].sids);
1031  }
1032  SCFree(search_ctx->pattern_list);
1033  }
1034 
1035  if (search_ctx->output_table != NULL) {
1036  uint32_t state;
1037  for (state = 0; state < search_ctx->state_count; state++) {
1038  if (search_ctx->output_table[state].patterns != NULL) {
1039  SCFree(search_ctx->output_table[state].patterns);
1040  }
1041  }
1042  SCFree(search_ctx->output_table);
1043  }
1044 
1045  SCFree(search_ctx);
1046  mpm_ctx->ctx = NULL;
1047 
1048  mpm_ctx->memory_cnt--;
1049  mpm_ctx->memory_size -= sizeof(SCACTileSearchCtx);
1050 }
1051 
1052 /*
1053  * Heavily optimized pattern matching routine for TILE-Gx.
1054  */
1055 
1056 #define SCHECK(x) ((x) > 0)
1057 #define BUF_TYPE int32_t
1058 // Extract byte N=0,1,2,3 from x
1059 #define BYTE0(x) (((x) & 0x000000ff) >> 0)
1060 #define BYTE1(x) (((x) & 0x0000ff00) >> 8)
1061 #define BYTE2(x) (((x) & 0x00ff0000) >> 16)
1062 #define BYTE3(x) (((x) & 0xff000000) >> 24)
1063 #define EXTRA 4 // need 4 extra bytes to avoid OOB reads
1065 static int CheckMatch(const SCACTileSearchCtx *ctx, PrefilterRuleStore *pmq,
1066  const uint8_t *buf, uint32_t buflen,
1067  uint16_t state, int i, int matches,
1068  uint8_t *mpm_bitarray)
1069 {
1070  const SCACTilePatternList *pattern_list = ctx->pattern_list;
1071  const uint8_t *buf_offset = buf + i + 1; // Lift out of loop
1072  uint32_t no_of_entries = ctx->output_table[state].no_of_entries;
1073  MpmPatternIndex *patterns = ctx->output_table[state].patterns;
1074  uint32_t k;
1075 
1076  for (k = 0; k < no_of_entries; k++) {
1077  MpmPatternIndex pindex = patterns[k] & 0x0FFFFFFF;
1078  if (mpm_bitarray[pindex / 8] & (1 << (pindex % 8))) {
1079  /* Pattern already seen by this MPM. */
1080  continue;
1081  }
1082  const SCACTilePatternList *pat = &pattern_list[pindex];
1083  const int offset = i - pat->patlen + 1;
1084  if (offset < (int)pat->offset || (pat->depth && i > pat->depth))
1085  continue;
1086 
1087  /* Double check case-sensitive match now. */
1088  if (patterns[k] >> 31) {
1089  const uint16_t patlen = pat->patlen;
1090  if (SCMemcmp(pat->cs, buf_offset - patlen, patlen) != 0) {
1091  /* Case-sensitive match failed. */
1092  continue;
1093  }
1094  }
1095  /* New match found */
1096  mpm_bitarray[pindex / 8] |= (1 << (pindex % 8));
1097 
1098  /* Always add the Signature IDs, since they could be different in the current MPM
1099  * than in a previous MPM on the same PMQ when finding the same pattern.
1100  */
1101  PrefilterAddSids(pmq, pattern_list[pindex].sids,
1102  pattern_list[pindex].sids_size);
1103  matches++;
1104  }
1105 
1106  return matches;
1107 }
1108 
1109 /**
1110  * \brief The aho corasick search function.
1111  *
1112  * \param mpm_ctx Pointer to the mpm context.
1113  * \param mpm_thread_ctx Pointer to the mpm thread context.
1114  * \param pmq Pointer to the Pattern Matcher Queue to hold
1115  * search matches.
1116  * \param buf Buffer to be searched.
1117  * \param buflen Buffer length.
1118  *
1119  * \retval matches Match count.
1120  */
1121 uint32_t SCACTileSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
1122  PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
1123 {
1124  const SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
1125 
1126  if (buflen == 0)
1127  return 0;
1128 
1129  /* Context specific matching function. */
1130  return search_ctx->Search(search_ctx, mpm_thread_ctx, pmq, buf, buflen);
1131 }
1132 
1133 /* This function handles (ctx->state_count >= 32767) */
1134 uint32_t SCACTileSearchLarge(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
1135  PrefilterRuleStore *pmq,
1136  const uint8_t *buf, uint32_t buflen)
1137 {
1138  uint32_t i = 0;
1139  int matches = 0;
1140 
1141  uint8_t *mpm_bitarray = (uint8_t *)mpm_thread_ctx->ctx;
1142  memset(mpm_bitarray, 0, mpm_thread_ctx->memory_size);
1143 
1144  const uint8_t* restrict xlate = ctx->translate_table;
1145  register int state = 0;
1146  int32_t (*state_table_u32)[256] = ctx->state_table;
1147  for (i = 0; i < buflen; i++) {
1148  state = state_table_u32[state & 0x00FFFFFF][xlate[buf[i]]];
1149  if (SCHECK(state)) {
1150  DEBUG_VALIDATE_BUG_ON(state < 0 || state > UINT16_MAX);
1151  matches = CheckMatch(ctx, pmq, buf, buflen, (uint16_t)state, i, matches, mpm_bitarray);
1152  }
1153  } /* for (i = 0; i < buflen; i++) */
1154 
1155  return matches;
1156 }
1157 
1158 /*
1159  * Search with Alphabet size of 256 and 16-bit next-state entries.
1160  * Next state entry has MSB as "match" and 15 LSB bits as next-state index.
1161  */
1162 // y = 1<<log_mult * (x & (1<<width -1))
1163 #define SINDEX_INTERNAL(y, x, log_mult, width) \
1164  ((1<<log_mult) * (x & ((1<<width) - 1)))
1165 
1166 /* Type of next_state */
1167 #define STYPE int16_t
1168 #define SLOAD(x) *(STYPE * restrict)(x)
1170 #define FUNC_NAME SCACTileSearchSmall256
1171 // y = 256 * (x & 0x7FFF)
1172 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 8, 15)
1173 #include "util-mpm-ac-ks-small.c"
1174 
1175 /* Search with Alphabet size of 128 */
1176 #undef FUNC_NAME
1177 #undef SINDEX
1178 #define FUNC_NAME SCACTileSearchSmall128
1179 // y = 128 * (x & 0x7FFF)
1180 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 7, 15)
1181 #include "util-mpm-ac-ks-small.c"
1182 
1183 /* Search with Alphabet size of 64 */
1184 #undef FUNC_NAME
1185 #undef SINDEX
1186 #define FUNC_NAME SCACTileSearchSmall64
1187 // y = 64 * (x & 0x7FFF)
1188 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 6, 15)
1189 #include "util-mpm-ac-ks-small.c"
1190 
1191 /* Search with Alphabet size of 32 */
1192 #undef FUNC_NAME
1193 #undef SINDEX
1194 #define FUNC_NAME SCACTileSearchSmall32
1195 // y = 32 * (x & 0x7FFF)
1196 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 5, 15)
1197 #include "util-mpm-ac-ks-small.c"
1198 
1199 /* Search with Alphabet size of 16 */
1200 #undef FUNC_NAME
1201 #undef SINDEX
1202 #define FUNC_NAME SCACTileSearchSmall16
1203 // y = 16 * (x & 0x7FFF)
1204 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 4, 15)
1205 #include "util-mpm-ac-ks-small.c"
1206 
1207 /* Search with Alphabet size of 8 */
1208 #undef FUNC_NAME
1209 #undef SINDEX
1210 #define FUNC_NAME SCACTileSearchSmall8
1211 // y = 8 * (x & 0x7FFF)
1212 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 3, 15)
1213 #include "util-mpm-ac-ks-small.c"
1214 
1215 /*
1216  * Search with Alphabet size of 256 and 8-bit next-state entries.
1217  * Next state entry has MSB as "match" and 15 LSB bits as next-state index.
1218  */
1219 #undef STYPE
1220 #define STYPE int8_t
1222 #undef FUNC_NAME
1223 #undef SINDEX
1224 #define FUNC_NAME SCACTileSearchTiny256
1225 // y = 256 * (x & 0x7F)
1226 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 8, 7)
1227 #include "util-mpm-ac-ks-small.c"
1228 
1229 /* Search with Alphabet size of 128 */
1230 #undef FUNC_NAME
1231 #undef SINDEX
1232 #define FUNC_NAME SCACTileSearchTiny128
1233 // y = 128 * (x & 0x7F)
1234 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 7, 7)
1235 #include "util-mpm-ac-ks-small.c"
1236 
1237 /* Search with Alphabet size of 64 */
1238 #undef FUNC_NAME
1239 #undef SINDEX
1240 #define FUNC_NAME SCACTileSearchTiny64
1241 // y = 64 * (x & 0x7F)
1242 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 6, 7)
1243 #include "util-mpm-ac-ks-small.c"
1244 
1245 /* Search with Alphabet size of 32 */
1246 #undef FUNC_NAME
1247 #undef SINDEX
1248 #define FUNC_NAME SCACTileSearchTiny32
1249 // y = 32 * (x & 0x7F)
1250 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 5, 7)
1251 #include "util-mpm-ac-ks-small.c"
1252 
1253 /* Search with Alphabet size of 16 */
1254 #undef FUNC_NAME
1255 #undef SINDEX
1256 #define FUNC_NAME SCACTileSearchTiny16
1257 // y = 16 * (x & 0x7F)
1258 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 4, 7)
1259 #include "util-mpm-ac-ks-small.c"
1260 
1261 /* Search with Alphabet size of 8 */
1262 #undef FUNC_NAME
1263 #undef SINDEX
1264 #define FUNC_NAME SCACTileSearchTiny8
1265 // y = 8 * (x & 0x7F)
1266 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 3, 7)
1268 
1269 
1270 /**
1271  * \brief Add a case insensitive pattern. Although we have different calls for
1272  * adding case sensitive and insensitive patterns, we make a single call
1273  * for either case. No special treatment for either case.
1274  *
1275  * \param mpm_ctx Pointer to the mpm context.
1276  * \param pat The pattern to add.
1277  * \param patnen The pattern length.
1278  * \param offset Ignored.
1279  * \param depth Ignored.
1280  * \param pid The pattern id.
1281  * \param sid Ignored.
1282  * \param flags Flags associated with this pattern.
1283  *
1284  * \retval 0 On success.
1285  * \retval -1 On failure.
1286  */
1287 int SCACTileAddPatternCI(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset,
1288  uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
1289 {
1291  return MpmAddPattern(mpm_ctx, pat, patlen, offset, depth,
1292  pid, sid, flags);
1293 }
1294 
1295 /**
1296  * \brief Add a case sensitive pattern. Although we have different calls for
1297  * adding case sensitive and insensitive patterns, we make a single call
1298  * for either case. No special treatment for either case.
1299  *
1300  * \param mpm_ctx Pointer to the mpm context.
1301  * \param pat The pattern to add.
1302  * \param patnen The pattern length.
1303  * \param offset Ignored.
1304  * \param depth Ignored.
1305  * \param pid The pattern id.
1306  * \param sid Ignored.
1307  * \param flags Flags associated with this pattern.
1308  *
1309  * \retval 0 On success.
1310  * \retval -1 On failure.
1311  */
1312 int SCACTileAddPatternCS(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen,
1313  uint16_t offset, uint16_t depth, uint32_t pid,
1314  SigIntId sid, uint8_t flags)
1315 {
1316  return MpmAddPattern(mpm_ctx, pat, patlen, offset, depth,
1317  pid, sid, flags);
1318 }
1319 
1321 {
1322  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
1323  SCACTileCtx *ctx = search_ctx->init_ctx;
1324 
1325  printf("MPM AC Information:\n");
1326  printf("Memory allocs: %" PRIu32 "\n", mpm_ctx->memory_cnt);
1327  printf("Memory alloced: %" PRIu32 "\n", mpm_ctx->memory_size);
1328  printf(" Sizeof:\n");
1329  printf(" MpmCtx %" PRIuMAX "\n", (uintmax_t)sizeof(MpmCtx));
1330  printf(" SCACTileCtx: %" PRIuMAX "\n", (uintmax_t)sizeof(SCACTileCtx));
1331  printf(" MpmPattern %" PRIuMAX "\n", (uintmax_t)sizeof(MpmPattern));
1332  printf(" MpmPattern %" PRIuMAX "\n", (uintmax_t)sizeof(MpmPattern));
1333  printf("Unique Patterns: %" PRIu32 "\n", mpm_ctx->pattern_cnt);
1334  printf("Smallest: %" PRIu32 "\n", mpm_ctx->minlen);
1335  printf("Largest: %" PRIu32 "\n", mpm_ctx->maxlen);
1336  printf("Total states in the state table: %u\n", ctx->state_count);
1337  printf("\n");
1338 }
1339 
1340 /**
1341  * \brief Init the mpm thread context.
1342  *
1343  * \param mpm_ctx Pointer to the mpm context.
1344  * \param mpm_thread_ctx Pointer to the mpm thread context.
1345  */
1346 static void SCACTileInitThreadCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx)
1347 {
1348  uint32_t size = (mpm_ctx->pattern_cnt + 7) / 8;
1349 
1350  uint8_t *bitarray = SCCalloc(size, sizeof(uint8_t));
1351  if (bitarray == NULL) {
1352  exit(EXIT_FAILURE);
1353  }
1354  mpm_thread_ctx->ctx = bitarray;
1355  mpm_thread_ctx->memory_cnt = 1;
1356  mpm_thread_ctx->memory_size = size;
1357 }
1358 
1359 static void SCACTileDestroyThreadCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx)
1360 {
1361  mpm_thread_ctx->memory_cnt = 0;
1362  mpm_thread_ctx->memory_size = 0;
1363  SCFree(mpm_thread_ctx->ctx);
1364  mpm_thread_ctx->ctx = NULL;
1365 }
1366 
1367 /************************** Mpm Registration ***************************/
1368 
1369 /**
1370  * \brief Register the aho-corasick mpm 'ks' originally developed by
1371  * Ken Steele for Tilera Tile-Gx processor.
1372  */
1374 {
1375  mpm_table[MPM_AC_KS].name = "ac-ks";
1378  mpm_table[MPM_AC_KS].ConfigInit = NULL;
1387  mpm_table[MPM_AC_KS].InitThreadCtx = SCACTileInitThreadCtx;
1388  mpm_table[MPM_AC_KS].DestroyThreadCtx = SCACTileDestroyThreadCtx;
1389 #ifdef UNITTESTS
1390  mpm_table[MPM_AC_KS].RegisterUnittests = SCACTileRegisterTests;
1391 #endif
1393 }
1394 
1395 
1396 /*************************************Unittests********************************/
1397 
1398 #ifdef UNITTESTS
1399 #include "detect-engine-alert.h"
1400 
1401 static int SCACTileTest01(void)
1402 {
1403  int result = 0;
1404  MpmCtx mpm_ctx;
1405  MpmThreadCtx mpm_thread_ctx;
1406  PrefilterRuleStore pmq;
1407 
1408  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1409  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1410  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1411 
1412  /* 1 match */
1413  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1414  PmqSetup(&pmq);
1415 
1416  SCACTilePreparePatterns(NULL, &mpm_ctx);
1417  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1418 
1419  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1420 
1421  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1422  (uint8_t *)buf, strlen(buf));
1423 
1424  if (cnt == 1)
1425  result = 1;
1426  else
1427  printf("1 != %" PRIu32 " ",cnt);
1428 
1429  SCACTileDestroyCtx(&mpm_ctx);
1430  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1431  PmqFree(&pmq);
1432  return result;
1433 }
1434 
1435 static int SCACTileTest02(void)
1436 {
1437  int result = 0;
1438  MpmCtx mpm_ctx;
1439  MpmThreadCtx mpm_thread_ctx;
1440  PrefilterRuleStore pmq;
1441 
1442  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1443  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1444  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1445 
1446  /* 1 match */
1447  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0, 0);
1448  PmqSetup(&pmq);
1449 
1450  SCACTilePreparePatterns(NULL, &mpm_ctx);
1451  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1452 
1453  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1454  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1455  (uint8_t *)buf, strlen(buf));
1456 
1457  if (cnt == 0)
1458  result = 1;
1459  else
1460  printf("0 != %" PRIu32 " ",cnt);
1461 
1462  SCACTileDestroyCtx(&mpm_ctx);
1463  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1464  PmqFree(&pmq);
1465  return result;
1466 }
1467 
1468 static int SCACTileTest03(void)
1469 {
1470  int result = 0;
1471  MpmCtx mpm_ctx;
1472  MpmThreadCtx mpm_thread_ctx;
1473  PrefilterRuleStore pmq;
1474 
1475  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1476  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1477  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1478 
1479  /* 1 match */
1480  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1481  /* 1 match */
1482  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"bcde", 4, 0, 0, 1, 0, 0);
1483  /* 1 match */
1484  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"fghj", 4, 0, 0, 2, 0, 0);
1485  PmqSetup(&pmq);
1486 
1487  SCACTilePreparePatterns(NULL, &mpm_ctx);
1488  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1489 
1490  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1491  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1492  (uint8_t *)buf, strlen(buf));
1493 
1494  if (cnt == 3)
1495  result = 1;
1496  else
1497  printf("3 != %" PRIu32 " ",cnt);
1498 
1499  SCACTileDestroyCtx(&mpm_ctx);
1500  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1501  PmqFree(&pmq);
1502  return result;
1503 }
1504 
1505 static int SCACTileTest04(void)
1506 {
1507  int result = 0;
1508  MpmCtx mpm_ctx;
1509  MpmThreadCtx mpm_thread_ctx;
1510  PrefilterRuleStore pmq;
1511 
1512  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1513  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1514  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1515 
1516  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1517  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"bcdegh", 6, 0, 0, 1, 0, 0);
1518  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"fghjxyz", 7, 0, 0, 2, 0, 0);
1519  PmqSetup(&pmq);
1520 
1521  SCACTilePreparePatterns(NULL, &mpm_ctx);
1522  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1523 
1524  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1525  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1526  (uint8_t *)buf, strlen(buf));
1527 
1528  if (cnt == 1)
1529  result = 1;
1530  else
1531  printf("1 != %" PRIu32 " ",cnt);
1532 
1533  SCACTileDestroyCtx(&mpm_ctx);
1534  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1535  PmqFree(&pmq);
1536  return result;
1537 }
1538 
1539 static int SCACTileTest05(void)
1540 {
1541  int result = 0;
1542  MpmCtx mpm_ctx;
1543  MpmThreadCtx mpm_thread_ctx;
1544  PrefilterRuleStore pmq;
1545 
1546  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1547  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1548  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1549 
1550  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0);
1551  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"bCdEfG", 6, 0, 0, 1, 0, 0);
1552  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"fghJikl", 7, 0, 0, 2, 0, 0);
1553  PmqSetup(&pmq);
1554 
1555  SCACTilePreparePatterns(NULL, &mpm_ctx);
1556  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1557 
1558  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1559  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1560  (uint8_t *)buf, strlen(buf));
1561 
1562  if (cnt == 3)
1563  result = 1;
1564  else
1565  printf("3 != %" PRIu32 " ",cnt);
1566 
1567  SCACTileDestroyCtx(&mpm_ctx);
1568  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1569  PmqFree(&pmq);
1570  return result;
1571 }
1572 
1573 static int SCACTileTest06(void)
1574 {
1575  int result = 0;
1576  MpmCtx mpm_ctx;
1577  MpmThreadCtx mpm_thread_ctx;
1578  PrefilterRuleStore pmq;
1579 
1580  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1581  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1582  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1583 
1584  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1585  PmqSetup(&pmq);
1586 
1587  SCACTilePreparePatterns(NULL, &mpm_ctx);
1588  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1589 
1590  const char *buf = "abcd";
1591  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1592  (uint8_t *)buf, strlen(buf));
1593 
1594  if (cnt == 1)
1595  result = 1;
1596  else
1597  printf("1 != %" PRIu32 " ",cnt);
1598 
1599  SCACTileDestroyCtx(&mpm_ctx);
1600  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1601  PmqFree(&pmq);
1602  return result;
1603 }
1604 
1605 static int SCACTileTest07(void)
1606 {
1607  MpmCtx mpm_ctx;
1608  MpmThreadCtx mpm_thread_ctx;
1609  PrefilterRuleStore pmq;
1610 
1611  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1612  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1613  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1614 
1615  /* should match 30 times */
1616  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0, 0);
1617  /* should match 29 times */
1618  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 1, 0, 0);
1619  /* should match 28 times */
1620  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAA", 3, 0, 0, 2, 0, 0);
1621  /* 26 */
1622  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAA", 5, 0, 0, 3, 0, 0);
1623  /* 21 */
1624  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAA", 10, 0, 0, 4, 0, 0);
1625  /* 1 */
1626  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1627  30, 0, 0, 5, 0, 0);
1628  PmqSetup(&pmq);
1629  /* total matches: 135: 6 unique */
1630 
1631  SCACTilePreparePatterns(NULL, &mpm_ctx);
1632  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1633 
1634  const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1635  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1636  (uint8_t *)buf, strlen(buf));
1637  FAIL_IF_NOT(cnt == 6);
1638 
1639  SCACTileDestroyCtx(&mpm_ctx);
1640  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1641  PmqFree(&pmq);
1642  PASS;
1643 }
1644 
1645 static int SCACTileTest08(void)
1646 {
1647  int result = 0;
1648  MpmCtx mpm_ctx;
1649  MpmThreadCtx mpm_thread_ctx;
1650  PrefilterRuleStore pmq;
1651 
1652  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1653  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1654  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1655 
1656  /* 1 match */
1657  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1658  PmqSetup(&pmq);
1659 
1660  SCACTilePreparePatterns(NULL, &mpm_ctx);
1661  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1662 
1663  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1664  (uint8_t *)"a", 1);
1665 
1666  if (cnt == 0)
1667  result = 1;
1668  else
1669  printf("0 != %" PRIu32 " ",cnt);
1670 
1671  SCACTileDestroyCtx(&mpm_ctx);
1672  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1673  PmqFree(&pmq);
1674  return result;
1675 }
1676 
1677 static int SCACTileTest09(void)
1678 {
1679  int result = 0;
1680  MpmCtx mpm_ctx;
1681  MpmThreadCtx mpm_thread_ctx;
1682  PrefilterRuleStore pmq;
1683 
1684  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1685  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1686  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1687 
1688  /* 1 match */
1689  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0, 0);
1690  PmqSetup(&pmq);
1691 
1692  SCACTilePreparePatterns(NULL, &mpm_ctx);
1693  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1694 
1695  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1696  (uint8_t *)"ab", 2);
1697 
1698  if (cnt == 1)
1699  result = 1;
1700  else
1701  printf("1 != %" PRIu32 " ",cnt);
1702 
1703  SCACTileDestroyCtx(&mpm_ctx);
1704  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1705  PmqFree(&pmq);
1706  return result;
1707 }
1708 
1709 static int SCACTileTest10(void)
1710 {
1711  int result = 0;
1712  MpmCtx mpm_ctx;
1713  MpmThreadCtx mpm_thread_ctx;
1714  PrefilterRuleStore pmq;
1715 
1716  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1717  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1718  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1719 
1720  /* 1 match */
1721  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0, 0);
1722  PmqSetup(&pmq);
1723 
1724  SCACTilePreparePatterns(NULL, &mpm_ctx);
1725  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1726 
1727  const char *buf = "01234567890123456789012345678901234567890123456789"
1728  "01234567890123456789012345678901234567890123456789"
1729  "abcdefgh"
1730  "01234567890123456789012345678901234567890123456789"
1731  "01234567890123456789012345678901234567890123456789";
1732  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1733  (uint8_t *)buf, strlen(buf));
1734 
1735  if (cnt == 1)
1736  result = 1;
1737  else
1738  printf("1 != %" PRIu32 " ",cnt);
1739 
1740  SCACTileDestroyCtx(&mpm_ctx);
1741  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1742  PmqFree(&pmq);
1743  return result;
1744 }
1745 
1746 static int SCACTileTest11(void)
1747 {
1748  int result = 0;
1749  MpmCtx mpm_ctx;
1750  MpmThreadCtx mpm_thread_ctx;
1751  PrefilterRuleStore pmq;
1752 
1753  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1754  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1755  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1756 
1757  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"he", 2, 0, 0, 1, 0, 0) == -1)
1758  goto end;
1759  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"she", 3, 0, 0, 2, 0, 0) == -1)
1760  goto end;
1761  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"his", 3, 0, 0, 3, 0, 0) == -1)
1762  goto end;
1763  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"hers", 4, 0, 0, 4, 0, 0) == -1)
1764  goto end;
1765  PmqSetup(&pmq);
1766 
1767  if (SCACTilePreparePatterns(NULL, &mpm_ctx) == -1)
1768  goto end;
1769  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1770 
1771  result = 1;
1772 
1773  const char *buf = "he";
1774  result &= (SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1775  strlen(buf)) == 1);
1776  buf = "she";
1777  result &= (SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1778  strlen(buf)) == 2);
1779  buf = "his";
1780  result &= (SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1781  strlen(buf)) == 1);
1782  buf = "hers";
1783  result &= (SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1784  strlen(buf)) == 2);
1785 
1786  end:
1787  SCACTileDestroyCtx(&mpm_ctx);
1788  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1789  PmqFree(&pmq);
1790  return result;
1791 }
1792 
1793 static int SCACTileTest12(void)
1794 {
1795  int result = 0;
1796  MpmCtx mpm_ctx;
1797  MpmThreadCtx mpm_thread_ctx;
1798  PrefilterRuleStore pmq;
1799 
1800  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1801  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1802  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1803 
1804  /* 1 match */
1805  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0, 0);
1806  /* 1 match */
1807  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"vwxyz", 5, 0, 0, 1, 0, 0);
1808  PmqSetup(&pmq);
1809 
1810  SCACTilePreparePatterns(NULL, &mpm_ctx);
1811  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1812 
1813  const char *buf = "abcdefghijklmnopqrstuvwxyz";
1814  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1815  (uint8_t *)buf, strlen(buf));
1816 
1817  if (cnt == 2)
1818  result = 1;
1819  else
1820  printf("2 != %" PRIu32 " ",cnt);
1821 
1822  SCACTileDestroyCtx(&mpm_ctx);
1823  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1824  PmqFree(&pmq);
1825  return result;
1826 }
1827 
1828 static int SCACTileTest13(void)
1829 {
1830  int result = 0;
1831  MpmCtx mpm_ctx;
1832  MpmThreadCtx mpm_thread_ctx;
1833  PrefilterRuleStore pmq;
1834 
1835  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1836  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1837  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1838 
1839  /* 1 match */
1840  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCD";
1841  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1842  PmqSetup(&pmq);
1843 
1844  SCACTilePreparePatterns(NULL, &mpm_ctx);
1845  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1846 
1847  const char *buf = "abcdefghijklmnopqrstuvwxyzABCD";
1848  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1849  (uint8_t *)buf, strlen(buf));
1850 
1851  if (cnt == 1)
1852  result = 1;
1853  else
1854  printf("1 != %" PRIu32 " ",cnt);
1855 
1856  SCACTileDestroyCtx(&mpm_ctx);
1857  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1858  PmqFree(&pmq);
1859  return result;
1860 }
1861 
1862 static int SCACTileTest14(void)
1863 {
1864  int result = 0;
1865  MpmCtx mpm_ctx;
1866  MpmThreadCtx mpm_thread_ctx;
1867  PrefilterRuleStore pmq;
1868 
1869  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1870  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1871  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1872 
1873  /* 1 match */
1874  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCDE";
1875  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1876  PmqSetup(&pmq);
1877 
1878  SCACTilePreparePatterns(NULL, &mpm_ctx);
1879  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1880 
1881  const char *buf = "abcdefghijklmnopqrstuvwxyzABCDE";
1882  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1883  (uint8_t *)buf, strlen(buf));
1884 
1885  if (cnt == 1)
1886  result = 1;
1887  else
1888  printf("1 != %" PRIu32 " ",cnt);
1889 
1890  SCACTileDestroyCtx(&mpm_ctx);
1891  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1892  PmqFree(&pmq);
1893  return result;
1894 }
1895 
1896 static int SCACTileTest15(void)
1897 {
1898  int result = 0;
1899  MpmCtx mpm_ctx;
1900  MpmThreadCtx mpm_thread_ctx;
1901  PrefilterRuleStore pmq;
1902 
1903  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1904  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1905  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1906 
1907  /* 1 match */
1908  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCDEF";
1909  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1910  PmqSetup(&pmq);
1911 
1912  SCACTilePreparePatterns(NULL, &mpm_ctx);
1913  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1914 
1915  const char *buf = "abcdefghijklmnopqrstuvwxyzABCDEF";
1916  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1917  (uint8_t *)buf, strlen(buf));
1918 
1919  if (cnt == 1)
1920  result = 1;
1921  else
1922  printf("1 != %" PRIu32 " ",cnt);
1923 
1924  SCACTileDestroyCtx(&mpm_ctx);
1925  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1926  PmqFree(&pmq);
1927  return result;
1928 }
1929 
1930 static int SCACTileTest16(void)
1931 {
1932  int result = 0;
1933  MpmCtx mpm_ctx;
1934  MpmThreadCtx mpm_thread_ctx;
1935  PrefilterRuleStore pmq;
1936 
1937  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1938  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1939  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1940 
1941  /* 1 match */
1942  const char pat[] = "abcdefghijklmnopqrstuvwxyzABC";
1943  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1944  PmqSetup(&pmq);
1945 
1946  SCACTilePreparePatterns(NULL, &mpm_ctx);
1947  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1948 
1949  const char *buf = "abcdefghijklmnopqrstuvwxyzABC";
1950  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1951  (uint8_t *)buf, strlen(buf));
1952 
1953  if (cnt == 1)
1954  result = 1;
1955  else
1956  printf("1 != %" PRIu32 " ",cnt);
1957 
1958  SCACTileDestroyCtx(&mpm_ctx);
1959  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1960  PmqFree(&pmq);
1961  return result;
1962 }
1963 
1964 static int SCACTileTest17(void)
1965 {
1966  int result = 0;
1967  MpmCtx mpm_ctx;
1968  MpmThreadCtx mpm_thread_ctx;
1969  PrefilterRuleStore pmq;
1970 
1971  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1972  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1973  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1974 
1975  /* 1 match */
1976  const char pat[] = "abcdefghijklmnopqrstuvwxyzAB";
1977  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1978  PmqSetup(&pmq);
1979 
1980  SCACTilePreparePatterns(NULL, &mpm_ctx);
1981  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1982 
1983  const char *buf = "abcdefghijklmnopqrstuvwxyzAB";
1984  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1985  (uint8_t *)buf, strlen(buf));
1986 
1987  if (cnt == 1)
1988  result = 1;
1989  else
1990  printf("1 != %" PRIu32 " ",cnt);
1991 
1992  SCACTileDestroyCtx(&mpm_ctx);
1993  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1994  PmqFree(&pmq);
1995  return result;
1996 }
1997 
1998 static int SCACTileTest18(void)
1999 {
2000  int result = 0;
2001  MpmCtx mpm_ctx;
2002  MpmThreadCtx mpm_thread_ctx;
2003  PrefilterRuleStore pmq;
2004 
2005  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2006  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2007  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2008 
2009  /* 1 match */
2010  const char pat[] = "abcde"
2011  "fghij"
2012  "klmno"
2013  "pqrst"
2014  "uvwxy"
2015  "z";
2016  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
2017  PmqSetup(&pmq);
2018 
2019  SCACTilePreparePatterns(NULL, &mpm_ctx);
2020  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2021 
2022  const char *buf = "abcde""fghij""klmno""pqrst""uvwxy""z";
2023  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2024  (uint8_t *)buf, strlen(buf));
2025 
2026  if (cnt == 1)
2027  result = 1;
2028  else
2029  printf("1 != %" PRIu32 " ",cnt);
2030 
2031  SCACTileDestroyCtx(&mpm_ctx);
2032  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2033  PmqFree(&pmq);
2034  return result;
2035 }
2036 
2037 static int SCACTileTest19(void)
2038 {
2039  int result = 0;
2040  MpmCtx mpm_ctx;
2041  MpmThreadCtx mpm_thread_ctx;
2042  PrefilterRuleStore pmq;
2043 
2044  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2045  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2046  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2047 
2048  /* 1 */
2049  const char pat[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
2050  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
2051  PmqSetup(&pmq);
2052 
2053  SCACTilePreparePatterns(NULL, &mpm_ctx);
2054  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2055 
2056  const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
2057  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2058  (uint8_t *)buf, strlen(buf));
2059 
2060  if (cnt == 1)
2061  result = 1;
2062  else
2063  printf("1 != %" PRIu32 " ",cnt);
2064 
2065  SCACTileDestroyCtx(&mpm_ctx);
2066  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2067  PmqFree(&pmq);
2068  return result;
2069 }
2070 
2071 static int SCACTileTest20(void)
2072 {
2073  int result = 0;
2074  MpmCtx mpm_ctx;
2075  MpmThreadCtx mpm_thread_ctx;
2076  PrefilterRuleStore pmq;
2077 
2078  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2079  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2080  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2081 
2082  /* 1 */
2083  const char pat[] = "AAAAA"
2084  "AAAAA"
2085  "AAAAA"
2086  "AAAAA"
2087  "AAAAA"
2088  "AAAAA"
2089  "AA";
2090  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
2091  PmqSetup(&pmq);
2092 
2093  SCACTilePreparePatterns(NULL, &mpm_ctx);
2094  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2095 
2096  const char *buf = "AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AA";
2097  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2098  (uint8_t *)buf, strlen(buf));
2099 
2100  if (cnt == 1)
2101  result = 1;
2102  else
2103  printf("1 != %" PRIu32 " ",cnt);
2104 
2105  SCACTileDestroyCtx(&mpm_ctx);
2106  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2107  PmqFree(&pmq);
2108  return result;
2109 }
2110 
2111 static int SCACTileTest21(void)
2112 {
2113  int result = 0;
2114  MpmCtx mpm_ctx;
2115  MpmThreadCtx mpm_thread_ctx;
2116  PrefilterRuleStore pmq;
2117 
2118  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2119  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2120  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2121 
2122  /* 1 */
2123  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2124  PmqSetup(&pmq);
2125 
2126  SCACTilePreparePatterns(NULL, &mpm_ctx);
2127  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2128 
2129  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2130  (uint8_t *)"AA", 2);
2131 
2132  if (cnt == 1)
2133  result = 1;
2134  else
2135  printf("1 != %" PRIu32 " ",cnt);
2136 
2137  SCACTileDestroyCtx(&mpm_ctx);
2138  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2139  PmqFree(&pmq);
2140  return result;
2141 }
2142 
2143 static int SCACTileTest22(void)
2144 {
2145  int result = 0;
2146  MpmCtx mpm_ctx;
2147  MpmThreadCtx mpm_thread_ctx;
2148  PrefilterRuleStore pmq;
2149 
2150  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2151  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2152  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2153 
2154  /* 1 match */
2155  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
2156  /* 1 match */
2157  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcde", 5, 0, 0, 1, 0, 0);
2158  PmqSetup(&pmq);
2159 
2160  SCACTilePreparePatterns(NULL, &mpm_ctx);
2161  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2162 
2163  const char *buf = "abcdefghijklmnopqrstuvwxyz";
2164  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2165  (uint8_t *)buf, strlen(buf));
2166 
2167  if (cnt == 2)
2168  result = 1;
2169  else
2170  printf("2 != %" PRIu32 " ",cnt);
2171 
2172  SCACTileDestroyCtx(&mpm_ctx);
2173  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2174  PmqFree(&pmq);
2175  return result;
2176 }
2177 
2178 static int SCACTileTest23(void)
2179 {
2180  int result = 0;
2181  MpmCtx mpm_ctx;
2182  MpmThreadCtx mpm_thread_ctx;
2183  PrefilterRuleStore pmq;
2184 
2185  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2186  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2187  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2188 
2189  /* 1 */
2190  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2191  PmqSetup(&pmq);
2192 
2193  SCACTilePreparePatterns(NULL, &mpm_ctx);
2194  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2195 
2196  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2197  (uint8_t *)"aa", 2);
2198 
2199  if (cnt == 0)
2200  result = 1;
2201  else
2202  printf("1 != %" PRIu32 " ",cnt);
2203 
2204  SCACTileDestroyCtx(&mpm_ctx);
2205  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2206  PmqFree(&pmq);
2207  return result;
2208 }
2209 
2210 static int SCACTileTest24(void)
2211 {
2212  int result = 0;
2213  MpmCtx mpm_ctx;
2214  MpmThreadCtx mpm_thread_ctx;
2215  PrefilterRuleStore pmq;
2216 
2217  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2218  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2219  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2220 
2221  /* 1 */
2222  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2223  PmqSetup(&pmq);
2224 
2225  SCACTilePreparePatterns(NULL, &mpm_ctx);
2226  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2227 
2228  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2229  (uint8_t *)"aa", 2);
2230 
2231  if (cnt == 1)
2232  result = 1;
2233  else
2234  printf("1 != %" PRIu32 " ",cnt);
2235 
2236  SCACTileDestroyCtx(&mpm_ctx);
2237  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2238  PmqFree(&pmq);
2239  return result;
2240 }
2241 
2242 static int SCACTileTest25(void)
2243 {
2244  int result = 0;
2245  MpmCtx mpm_ctx;
2246  MpmThreadCtx mpm_thread_ctx;
2247  PrefilterRuleStore pmq;
2248 
2249  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2250  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2251  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2252 
2253  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0);
2254  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"bCdEfG", 6, 0, 0, 1, 0, 0);
2255  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"fghiJkl", 7, 0, 0, 2, 0, 0);
2256  PmqSetup(&pmq);
2257 
2258  SCACTilePreparePatterns(NULL, &mpm_ctx);
2259  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2260 
2261  const char *buf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2262  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2263  (uint8_t *)buf, strlen(buf));
2264 
2265  if (cnt == 3)
2266  result = 1;
2267  else
2268  printf("3 != %" PRIu32 " ",cnt);
2269 
2270  SCACTileDestroyCtx(&mpm_ctx);
2271  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2272  PmqFree(&pmq);
2273  return result;
2274 }
2275 
2276 static int SCACTileTest26(void)
2277 {
2278  int result = 0;
2279  MpmCtx mpm_ctx;
2280  MpmThreadCtx mpm_thread_ctx;
2281  PrefilterRuleStore pmq;
2282 
2283  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2284  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2285  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2286 
2287  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"Works", 5, 0, 0, 0, 0, 0);
2288  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"Works", 5, 0, 0, 1, 0, 0);
2289  PmqSetup(&pmq);
2290 
2291  SCACTilePreparePatterns(NULL, &mpm_ctx);
2292  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2293 
2294  const char *buf = "works";
2295  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2296  (uint8_t *)buf, strlen(buf));
2297 
2298  if (cnt == 1)
2299  result = 1;
2300  else
2301  printf("3 != %" PRIu32 " ",cnt);
2302 
2303  SCACTileDestroyCtx(&mpm_ctx);
2304  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2305  PmqFree(&pmq);
2306  return result;
2307 }
2308 
2309 static int SCACTileTest27(void)
2310 {
2311  int result = 0;
2312  MpmCtx mpm_ctx;
2313  MpmThreadCtx mpm_thread_ctx;
2314  PrefilterRuleStore pmq;
2315 
2316  memset(&mpm_ctx, 0, sizeof(MpmCtx));
2317  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2318  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2319 
2320  /* 0 match */
2321  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"ONE", 3, 0, 0, 0, 0, 0);
2322  PmqSetup(&pmq);
2323 
2324  SCACTilePreparePatterns(NULL, &mpm_ctx);
2325  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2326 
2327  const char *buf = "tone";
2328  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2329  (uint8_t *)buf, strlen(buf));
2330 
2331  if (cnt == 0)
2332  result = 1;
2333  else
2334  printf("0 != %" PRIu32 " ",cnt);
2335 
2336  SCACTileDestroyCtx(&mpm_ctx);
2337  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2338  PmqFree(&pmq);
2339  return result;
2340 }
2341 
2342 static int SCACTileTest28(void)
2343 {
2344  int result = 0;
2345  MpmCtx mpm_ctx;
2346  MpmThreadCtx mpm_thread_ctx;
2347  PrefilterRuleStore pmq;
2348 
2349  memset(&mpm_ctx, 0, sizeof(MpmCtx));
2350  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2351  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2352 
2353  /* 0 match */
2354  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"one", 3, 0, 0, 0, 0, 0);
2355  PmqSetup(&pmq);
2356 
2357  SCACTilePreparePatterns(NULL, &mpm_ctx);
2358  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2359 
2360  const char *buf = "tONE";
2361  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2362  (uint8_t *)buf, strlen(buf));
2363 
2364  if (cnt == 0)
2365  result = 1;
2366  else
2367  printf("0 != %" PRIu32 " ",cnt);
2368 
2369  SCACTileDestroyCtx(&mpm_ctx);
2370  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2371  PmqFree(&pmq);
2372  return result;
2373 }
2374 
2375 static int SCACTileTest29(void)
2376 {
2377  uint8_t buf[] = "onetwothreefourfivesixseveneightnine";
2378  uint16_t buflen = sizeof(buf) - 1;
2379  Packet *p = NULL;
2380  ThreadVars th_v;
2381  DetectEngineThreadCtx *det_ctx = NULL;
2382  int result = 0;
2383 
2384  memset(&th_v, 0, sizeof(th_v));
2386  p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
2387 
2389  if (de_ctx == NULL)
2390  goto end;
2391 
2392  de_ctx->flags |= DE_QUIET;
2393 
2394  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2395  "(content:\"onetwothreefourfivesixseveneightnine\"; sid:1;)");
2396  if (de_ctx->sig_list == NULL)
2397  goto end;
2398  de_ctx->sig_list->next = SigInit(de_ctx, "alert tcp any any -> any any "
2399  "(content:\"onetwothreefourfivesixseveneightnine\"; fast_pattern:3,3; sid:2;)");
2400  if (de_ctx->sig_list->next == NULL)
2401  goto end;
2402 
2404  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
2405 
2406  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
2407  if (PacketAlertCheck(p, 1) != 1) {
2408  printf("if (PacketAlertCheck(p, 1) != 1) failure\n");
2409  goto end;
2410  }
2411  if (PacketAlertCheck(p, 2) != 1) {
2412  printf("if (PacketAlertCheck(p, 1) != 2) failure\n");
2413  goto end;
2414  }
2415 
2416  result = 1;
2417 end:
2418  UTHFreePackets(&p, 1);
2419  if (de_ctx != NULL) {
2420  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
2422  }
2424  return result;
2425 }
2426 
2427 void SCACTileRegisterTests(void)
2428 {
2429  UtRegisterTest("SCACTileTest01", SCACTileTest01);
2430  UtRegisterTest("SCACTileTest02", SCACTileTest02);
2431  UtRegisterTest("SCACTileTest03", SCACTileTest03);
2432  UtRegisterTest("SCACTileTest04", SCACTileTest04);
2433  UtRegisterTest("SCACTileTest05", SCACTileTest05);
2434  UtRegisterTest("SCACTileTest06", SCACTileTest06);
2435  UtRegisterTest("SCACTileTest07", SCACTileTest07);
2436  UtRegisterTest("SCACTileTest08", SCACTileTest08);
2437  UtRegisterTest("SCACTileTest09", SCACTileTest09);
2438  UtRegisterTest("SCACTileTest10", SCACTileTest10);
2439  UtRegisterTest("SCACTileTest11", SCACTileTest11);
2440  UtRegisterTest("SCACTileTest12", SCACTileTest12);
2441  UtRegisterTest("SCACTileTest13", SCACTileTest13);
2442  UtRegisterTest("SCACTileTest14", SCACTileTest14);
2443  UtRegisterTest("SCACTileTest15", SCACTileTest15);
2444  UtRegisterTest("SCACTileTest16", SCACTileTest16);
2445  UtRegisterTest("SCACTileTest17", SCACTileTest17);
2446  UtRegisterTest("SCACTileTest18", SCACTileTest18);
2447  UtRegisterTest("SCACTileTest19", SCACTileTest19);
2448  UtRegisterTest("SCACTileTest20", SCACTileTest20);
2449  UtRegisterTest("SCACTileTest21", SCACTileTest21);
2450  UtRegisterTest("SCACTileTest22", SCACTileTest22);
2451  UtRegisterTest("SCACTileTest23", SCACTileTest23);
2452  UtRegisterTest("SCACTileTest24", SCACTileTest24);
2453  UtRegisterTest("SCACTileTest25", SCACTileTest25);
2454  UtRegisterTest("SCACTileTest26", SCACTileTest26);
2455  UtRegisterTest("SCACTileTest27", SCACTileTest27);
2456  UtRegisterTest("SCACTileTest28", SCACTileTest28);
2457  UtRegisterTest("SCACTileTest29", SCACTileTest29);
2458 }
2459 #endif
2460 
2461 #else /* we're big endian */
2462 
2463 void MpmACTileRegister(void)
2464 {
2465  /* no-op on big endian */
2466 }
2467 
2468 #endif /* little endian check */
SCACTileSearchCtx
struct SCACTileSearchCtx_ SCACTileSearchCtx
len
uint8_t len
Definition: app-layer-dnp3.h:2
detect-engine.h
SCACTileSearchCtx_
Definition: util-mpm-ac-ks.h:115
MpmPatternIndex
uint32_t MpmPatternIndex
Definition: util-mpm.h:46
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
MpmTableElmt_::InitThreadCtx
void(* InitThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:157
MpmThreadCtx_
Definition: util-mpm.h:48
SCACTileSearchSmall256
uint32_t SCACTileSearchSmall256(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
MpmFreePattern
void MpmFreePattern(MpmCtx *mpm_ctx, MpmPattern *p)
Definition: util-mpm.c:353
SCACTileSearchCtx_::Search
uint32_t(* Search)(const struct SCACTileSearchCtx_ *ctx, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm-ac-ks.h:122
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
MpmTableElmt_::name
const char * name
Definition: util-mpm.h:155
PrefilterRuleStore_
structure for storing potential rule matches
Definition: util-prefilter.h:34
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
util-mpm-ac-ks.h
SCACTileSearchSmall32
uint32_t SCACTileSearchSmall32(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:144
MpmThreadCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:51
StateQueue_
Helper structure used by AC during state table creation.
Definition: util-mpm-ac-queue.h:33
SC_AC_TILE_FAIL
#define SC_AC_TILE_FAIL
Definition: util-mpm-ac-ks.c:149
ctx
struct Thresholds ctx
util-mpm-ac-queue.h
SCACTilePatternList_::offset
uint16_t offset
Definition: util-mpm-ac-ks.h:33
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
th_v
ThreadVars * th_v
Definition: fuzz_iprep.c:20
MPM_FEATURE_FLAG_OFFSET
#define MPM_FEATURE_FLAG_OFFSET
Definition: util-mpm.h:151
util-memcpy.h
MpmCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:112
MemcmpTest18Tests::result
int result
Definition: util-memcmp.c:313
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2715
SCACTileSearchTiny8
uint32_t SCACTileSearchTiny8(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
SCACTileOutputTable
struct SCACTileOutputTable_ SCACTileOutputTable
SCACTileSearchCtx_::output_table
SCACTileOutputTable * output_table
Definition: util-mpm-ac-ks.h:132
SCACTileSearchSmall128
uint32_t SCACTileSearchSmall128(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
MpmCtx_::maxlen
uint16_t maxlen
Definition: util-mpm.h:109
MpmTableElmt_::AddPattern
int(* AddPattern)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:176
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:243
MpmTableElmt_::feature_flags
uint8_t feature_flags
Definition: util-mpm.h:192
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2971
p
Packet * p
Definition: fuzz_iprep.c:21
SCACTileSearchCtx_::pattern_cnt
uint32_t pattern_cnt
Definition: util-mpm-ac-ks.h:141
SCACTileSearchCtx_::state_table
void * state_table
Definition: util-mpm-ac-ks.h:129
MpmTableElmt_::InitCtx
void(* InitCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:156
SCACTileAddPatternCI
int SCACTileAddPatternCI(MpmCtx *, const uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Add a case insensitive pattern. Although we have different calls for adding case sensitive and insens...
Definition: util-mpm-ac-ks.c:1287
util-unittest.h
MPM_AC_KS
@ MPM_AC_KS
Definition: util-mpm.h:39
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
SCHECK
#define SCHECK(x)
Definition: util-mpm-ac-ks.c:1056
util-memcmp.h
SCACTileSearchCtx_::state_count
uint32_t state_count
Definition: util-mpm-ac-ks.h:139
MpmInitCtx
void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher)
Definition: util-mpm.c:209
Signature_::next
struct Signature_ * next
Definition: detect.h:757
MpmTableElmt_::PrintCtx
void(* PrintCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:187
SCACTileOutputTable_
Definition: util-mpm-ac-ks.h:44
SCACStateQueueFree
void SCACStateQueueFree(StateQueue *q)
Definition: util-mpm-ac-queue.c:36
util-debug.h
SCACTileSearchTiny16
uint32_t SCACTileSearchTiny16(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
MpmPattern_::next
struct MpmPattern_ * next
Definition: util-mpm.h:81
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
SCACStateQueueAlloc
StateQueue * SCACStateQueueAlloc(void)
Definition: util-mpm-ac-queue.c:22
DetectEngineThreadCtx_
Definition: detect.h:1291
SCACTileSearchCtx_::mpm_bitarray_size
uint32_t mpm_bitarray_size
Definition: util-mpm-ac-ks.h:136
SCACTileSearchSmall8
uint32_t SCACTileSearchSmall8(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
MpmThreadCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:52
MpmAddPattern
int MpmAddPattern(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:435
SCMpmAddPatternCI
int SCMpmAddPatternCI(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:258
SCACTileOutputTable_::patterns
MpmPatternIndex * patterns
Definition: util-mpm-ac-ks.h:46
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SCACTilePatternList
struct SCACTilePatternList_ SCACTilePatternList
util-mpm-ac-ks-small.c
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3461
MPM_FEATURE_FLAG_DEPTH
#define MPM_FEATURE_FLAG_DEPTH
Definition: util-mpm.h:150
SCACTileSearch
uint32_t SCACTileSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
The aho corasick search function.
Definition: util-mpm-ac-ks.c:1121
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:3238
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:108
SCACTileSearchCtx_::init_ctx
SCACTileCtx * init_ctx
Definition: util-mpm-ac-ks.h:144
SigIntId
#define SigIntId
Definition: detect-engine-state.h:38
Packet_
Definition: decode.h:514
detect-engine-build.h
detect-engine-alert.h
conf.h
MPM_INIT_HASH_SIZE
#define MPM_INIT_HASH_SIZE
Definition: util-mpm.h:32
SCACTileSearchTiny128
uint32_t SCACTileSearchTiny128(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
MpmTableElmt_::CacheRuleset
int(* CacheRuleset)(MpmConfig *)
Definition: util-mpm.h:183
SCACTilePreparePatterns
int SCACTilePreparePatterns(MpmConfig *mpm_conf, MpmCtx *mpm_ctx)
Process the patterns added to the mpm, and create the internal tables.
Definition: util-mpm-ac-ks.c:813
SCACTilePatternList_::sids
SigIntId * sids
Definition: util-mpm-ac-ks.h:41
MpmPattern_
Definition: util-mpm.h:56
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:186
MpmAddPatternCS
int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:249
SCACTilePrintInfo
void SCACTilePrintInfo(MpmCtx *mpm_ctx)
Definition: util-mpm-ac-ks.c:1320
SCACTileSearchCtx_::pattern_list
SCACTilePatternList * pattern_list
Definition: util-mpm-ac-ks.h:133
SCACTileSearchTiny64
uint32_t SCACTileSearchTiny64(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
MpmACTileRegister
void MpmACTileRegister(void)
Register the aho-corasick mpm 'ks' originally developed by Ken Steele for Tilera Tile-Gx processor.
Definition: util-mpm-ac-ks.c:1373
SCACTilePatternList_::cs
uint8_t * cs
Definition: util-mpm-ac-ks.h:30
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2286
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1333
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
MPM_PATTERN_FLAG_NOCASE
#define MPM_PATTERN_FLAG_NOCASE
Definition: util-mpm.h:140
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
flags
uint8_t flags
Definition: decode-gre.h:0
SCACTilePatternList_::depth
uint16_t depth
Definition: util-mpm-ac-ks.h:34
suricata-common.h
MpmCtx_::pattern_cnt
uint32_t pattern_cnt
Definition: util-mpm.h:106
SCACTilePatternList_::patlen
uint16_t patlen
Definition: util-mpm-ac-ks.h:31
SCACTileSearchTiny256
uint32_t SCACTileSearchTiny256(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3706
MpmTableElmt_::AddPatternNocase
int(* AddPatternNocase)(struct MpmCtx_ *, const uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:177
FatalError
#define FatalError(...)
Definition: util-debug.h:517
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:982
util-validate.h
SCACTileCtx
struct SCACTileCtx_ SCACTileCtx
SCACTileCtx_
Definition: util-mpm-ac-ks.h:54
SCACTilePatternList_
Definition: util-mpm-ac-ks.h:29
MpmTableElmt_::Prepare
int(* Prepare)(MpmConfig *, struct MpmCtx_ *)
Definition: util-mpm.h:179
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:158
SCFree
#define SCFree(p)
Definition: util-mem.h:61
MpmConfig_
Definition: util-mpm.h:91
detect-parse.h
MpmCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:111
MpmCtx_::init_hash
MpmPattern ** init_hash
Definition: util-mpm.h:117
SCACTileInitCtx
void SCACTileInitCtx(MpmCtx *)
Initialize the AC context.
Definition: util-mpm-ac-ks.c:913
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2676
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
SCACTileSearchLarge
uint32_t SCACTileSearchLarge(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
Definition: util-mpm-ac-ks.c:1134
SCACTileDestroyCtx
void SCACTileDestroyCtx(MpmCtx *)
Destroy the mpm context.
Definition: util-mpm-ac-ks.c:1014
MpmTableElmt_::ConfigDeinit
void(* ConfigDeinit)(MpmConfig **)
Definition: util-mpm.h:162
suricata.h
MpmTableElmt_::ConfigCacheDirSet
void(* ConfigCacheDirSet)(MpmConfig *, const char *dir_path)
Definition: util-mpm.h:163
PmqFree
void PmqFree(PrefilterRuleStore *pmq)
Cleanup and free a Pmq.
Definition: util-prefilter.c:126
SCACTileSearchTiny32
uint32_t SCACTileSearchTiny32(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
MpmTableElmt_::ConfigInit
MpmConfig *(* ConfigInit)(void)
Definition: util-mpm.h:161
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:975
MpmTableElmt_::DestroyThreadCtx
void(* DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:159
MpmCtx_
Definition: util-mpm.h:97
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
SCACTileSearchCtx_::translate_table
uint8_t translate_table[256]
Definition: util-mpm-ac-ks.h:126
MpmCtx_::ctx
void * ctx
Definition: util-mpm.h:98
SCACTileAddPatternCS
int SCACTileAddPatternCS(MpmCtx *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Add a case sensitive pattern. Although we have different calls for adding case sensitive and insensit...
Definition: util-mpm-ac-ks.c:1312
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1429
SCMemcmp
#define SCMemcmp(a, b, c)
Definition: util-memcmp.h:290
MpmThreadCtx_::ctx
void * ctx
Definition: util-mpm.h:49
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
MpmTableElmt_::RegisterUnittests
void(* RegisterUnittests)(void)
Definition: util-mpm.h:190
PmqSetup
int PmqSetup(PrefilterRuleStore *pmq)
Setup a pmq.
Definition: util-prefilter.c:37
SCACTileSearchSmall16
uint32_t SCACTileSearchSmall16(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
SCACTileSearchSmall64
uint32_t SCACTileSearchSmall64(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx, PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:455