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 /**
652  * \internal
653  * \brief Compute the size in bytes of the delta table.
654  * \retval size table size in bytes, or 0 if the multiplication overflows
655  */
656 static inline size_t SCACTileStateTableSize(
657  uint32_t state_count, uint8_t bytes_per_state, uint16_t alphabet_storage)
658 {
659  size_t size = MpmCheckSafeSizetMult((size_t)state_count, (size_t)bytes_per_state);
660  if (size == 0) {
661  return 0;
662  }
663  return MpmCheckSafeSizetMult(size, (size_t)alphabet_storage);
664 }
665 
666 static void SCACTileClubOutputStatePresenceWithDeltaTable(MpmCtx *mpm_ctx)
667 {
668  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
669  SCACTileCtx *ctx = search_ctx->init_ctx;
670 
671  int aa = 0;
672  uint32_t state = 0;
673 
674  /* Allocate next-state table. */
675  size_t size =
676  SCACTileStateTableSize(ctx->state_count, ctx->bytes_per_state, ctx->alphabet_storage);
677  if (unlikely(size == 0)) {
678  FatalError("ac-ks state table size overflow");
679  }
680  void *state_table = SCCalloc(1, size);
681  if (unlikely(state_table == NULL)) {
682  FatalError("Error allocating memory");
683  }
684  ctx->state_table = state_table;
685 
686  mpm_ctx->memory_cnt++;
687  mpm_ctx->memory_size += size;
688 
689  SCLogDebug("Delta Table size %" PRIuMAX ", alphabet: %d, %d-byte states: %d", (uintmax_t)size,
690  ctx->alphabet_size, ctx->bytes_per_state, ctx->state_count);
691 
692  /* Copy next state from Goto table, which is 32 bits and encode it into the next
693  * state table, which can be 1, 2 or 4 bytes each and include if there is an
694  * output.
695  */
696  for (state = 0; state < ctx->state_count; state++) {
697  for (aa = 0; aa < ctx->alphabet_size; aa++) {
698  int next_state = ctx->goto_table[state][aa];
699  int next_state_outputs = ctx->output_table[next_state].no_of_entries;
700  ctx->SetNextState(ctx, state, aa, next_state, next_state_outputs);
701  }
702  }
703 }
704 
705 static inline void SCACTileInsertCaseSensitiveEntriesForPatterns(MpmCtx *mpm_ctx)
706 {
707  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
708  SCACTileCtx *ctx = search_ctx->init_ctx;
709 
710  uint32_t state = 0;
711  uint32_t k = 0;
712 
713  for (state = 0; state < ctx->state_count; state++) {
714  if (ctx->output_table[state].no_of_entries == 0)
715  continue;
716 
717  for (k = 0; k < ctx->output_table[state].no_of_entries; k++) {
718  if (ctx->pattern_list[ctx->output_table[state].patterns[k]].cs != NULL) {
719  /* TODO - Find better way to store this. */
720  ctx->output_table[state].patterns[k] &= 0x0FFFFFFF;
721  ctx->output_table[state].patterns[k] |= (uint32_t)1 << 31;
722  }
723  }
724  }
725 }
726 
727 #if 0
728 static void SCACTilePrintDeltaTable(MpmCtx *mpm_ctx)
729 {
730  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
731  SCACTileCtx *ctx = search_ctx->init_ctx;
732 
733  int i = 0, j = 0;
734 
735  printf("##############Delta Table##############\n");
736  for (i = 0; i < ctx->state_count; i++) {
737  printf("%d: \n", i);
738  for (j = 0; j < ctx->alphabet_size; j++) {
739  if (SCACTileGetDelta(i, j, mpm_ctx) != 0) {
740  printf(" %c -> %d\n", j, SCACTileGetDelta(i, j, mpm_ctx));
741  }
742  }
743  }
744 }
745 #endif
746 
747 /**
748  * \brief Process the patterns and prepare the state table.
749  *
750  * \param mpm_ctx Pointer to the mpm context.
751  */
752 static void SCACTilePrepareStateTable(MpmCtx *mpm_ctx)
753 {
754  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
755  SCACTileCtx *ctx = search_ctx->init_ctx;
756 
757  /* Create Alphabet compression and Lower Case translation table. */
758  SCACTileInitTranslateTable(ctx);
759 
760  /* create the 0th state in the goto table and output_table */
761  SCACTileInitNewState(mpm_ctx);
762 
763  /* create the goto table */
764  SCACTileCreateGotoTable(mpm_ctx);
765  /* create the failure table */
766  SCACTileCreateFailureTable(mpm_ctx);
767  /* create the final state(delta) table */
768  SCACTileCreateDeltaTable(mpm_ctx);
769  /* club the output state presence with delta transition entries */
770  SCACTileClubOutputStatePresenceWithDeltaTable(mpm_ctx);
771 
772  /* club nocase entries */
773  SCACTileInsertCaseSensitiveEntriesForPatterns(mpm_ctx);
774 
775 #if 0
776  SCACTilePrintDeltaTable(mpm_ctx);
777 #endif
778 
779  /* we don't need these anymore */
780  SCFree(ctx->goto_table);
781  ctx->goto_table = NULL;
782  SCFree(ctx->failure_table);
783  ctx->failure_table = NULL;
784 }
785 
786 
787 /**
788  * \brief Process Internal AC MPM tables to create the Search Context
789  *
790  * The search context is only the data needed to search the MPM.
791  *
792  * \param mpm_ctx Pointer to the mpm context.
793  */
794 static void SCACTilePrepareSearch(MpmCtx *mpm_ctx)
795 {
796  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
797  SCACTileCtx *ctx = search_ctx->init_ctx;
798 
799  /* Resize the output table to be only as big as its final size. */
800  SCACTileReallocOutputTable(ctx, ctx->state_count);
801 
802  search_ctx->Search = ctx->Search;
803  memcpy(search_ctx->translate_table, ctx->translate_table, sizeof(ctx->translate_table));
804 
805  /* Move the state table from the Init context */
806  search_ctx->state_table = ctx->state_table;
807  ctx->state_table = NULL; /* So that it won't get freed twice. */
808 
809  /* Move the output_table from the Init context to the Search Context */
810  /* TODO: Could be made more compact */
811  search_ctx->output_table = ctx->output_table;
812  ctx->output_table = NULL;
813  search_ctx->state_count = ctx->state_count;
814 
815  search_ctx->pattern_list = ctx->pattern_list;
816  ctx->pattern_list = NULL;
817  search_ctx->pattern_cnt = mpm_ctx->pattern_cnt;
818 
819  /* One bit per pattern, rounded up to the next byte size. */
820  search_ctx->mpm_bitarray_size = (mpm_ctx->pattern_cnt + 7) / 8;
821 
822  /* Can now free the Initialization data */
823  SCACTileDestroyInitCtx(mpm_ctx);
824 }
825 
826 /**
827  * \brief Process the patterns added to the mpm, and create the internal tables.
828  *
829  * \param mpm_conf Pointer to the generic MPM matcher configuration
830  * \param mpm_ctx Pointer to the mpm context.
831  */
832 int SCACTilePreparePatterns(MpmConfig *mpm_conf, MpmCtx *mpm_ctx)
833 {
834  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
835 
836  if (mpm_ctx->pattern_cnt == 0 || search_ctx->init_ctx == NULL) {
837  SCLogDebug("no patterns supplied to this mpm_ctx");
838  return 0;
839  }
840  SCACTileCtx *ctx = search_ctx->init_ctx;
841  if (mpm_ctx->init_hash == NULL) {
842  SCLogDebug("no patterns supplied to this mpm_ctx");
843  return 0;
844  }
845 
846  /* alloc the pattern array */
847  ctx->parray = (MpmPattern **)SCCalloc(mpm_ctx->pattern_cnt, sizeof(MpmPattern *));
848  if (ctx->parray == NULL)
849  goto error;
850 
851  /* populate it with the patterns in the hash */
852  uint32_t i = 0, p = 0;
853  for (i = 0; i < MPM_INIT_HASH_SIZE; i++) {
854  MpmPattern *node = mpm_ctx->init_hash[i], *nnode = NULL;
855  while(node != NULL) {
856  nnode = node->next;
857  node->next = NULL;
858  ctx->parray[p++] = node;
859  SCACTileHistogramAlphabet(ctx, node);
860  node = nnode;
861  }
862  }
863 
864  /* we no longer need the hash, so free it's memory */
865  SCFree(mpm_ctx->init_hash);
866  mpm_ctx->init_hash = NULL;
867 
868  /* Handle case patterns by storing a copy of the pattern to compare
869  * to each possible match (no-case).
870  *
871  * Allocate the memory for the array and each of the strings as one block.
872  */
873  size_t string_space_needed = 0;
874  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
875  if (!(ctx->parray[i]->flags & MPM_PATTERN_FLAG_NOCASE)) {
876  /* Round up to next 8 byte aligned length */
877  uint32_t space = ((ctx->parray[i]->len + 7) / 8) * 8;
878  string_space_needed += space;
879  }
880  }
881 
882  size_t pattern_list_size = mpm_ctx->pattern_cnt * sizeof(SCACTilePatternList);
883  size_t mem_size = string_space_needed + pattern_list_size;
884  void *mem_block = SCCalloc(1, mem_size);
885  if (mem_block == NULL) {
886  FatalError("Error allocating memory");
887  }
888  mpm_ctx->memory_cnt++;
889  mpm_ctx->memory_size += mem_size;
890  /* Split the allocated block into pattern list array and string space. */
891  ctx->pattern_list = mem_block;
892  uint8_t *string_space = mem_block + pattern_list_size;
893 
894  /* Now make the copies of the no-case strings. */
895  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
896  if (!(ctx->parray[i]->flags & MPM_PATTERN_FLAG_NOCASE)) {
897  uint16_t len = ctx->parray[i]->len;
898  uint32_t space = ((len + 7) / 8) * 8;
899  memcpy(string_space, ctx->parray[i]->original_pat, len);
900  ctx->pattern_list[i].cs = string_space;
901  ctx->pattern_list[i].patlen = len;
902  string_space += space;
903  }
904  ctx->pattern_list[i].offset = ctx->parray[i]->offset;
905  ctx->pattern_list[i].depth = ctx->parray[i]->depth;
906  ctx->pattern_list[i].pid = ctx->parray[i]->id;
907 
908  /* ACPatternList now owns this memory */
909  ctx->pattern_list[i].sids_size = ctx->parray[i]->sids_size;
910  ctx->pattern_list[i].sids = ctx->parray[i]->sids;
911  ctx->parray[i]->sids = NULL;
912  ctx->parray[i]->sids_size = 0;
913  }
914 
915  /* prepare the state table required by AC */
916  SCACTilePrepareStateTable(mpm_ctx);
917 
918  /* Convert to the Search Context structure */
919  SCACTilePrepareSearch(mpm_ctx);
920 
921  return 0;
922 
923 error:
924  return -1;
925 }
926 
927 /**
928  * \brief Initialize the AC context.
929  *
930  * \param mpm_ctx Mpm context.
931  */
932 void SCACTileInitCtx(MpmCtx *mpm_ctx)
933 {
934  if (mpm_ctx->ctx != NULL)
935  return;
936 
937  /* Search Context */
938  mpm_ctx->ctx = SCCalloc(1, sizeof(SCACTileSearchCtx));
939  if (mpm_ctx->ctx == NULL) {
940  exit(EXIT_FAILURE);
941  }
942 
943  mpm_ctx->memory_cnt++;
944  mpm_ctx->memory_size += sizeof(SCACTileSearchCtx);
945 
946  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
947 
948  /* MPM Creation context */
949  search_ctx->init_ctx = SCCalloc(1, sizeof(SCACTileCtx));
950  if (search_ctx->init_ctx == NULL) {
951  exit(EXIT_FAILURE);
952  }
953 
954  mpm_ctx->memory_cnt++;
955  mpm_ctx->memory_size += sizeof(SCACTileCtx);
956 
957  /* initialize the hash we use to speed up pattern insertions */
958  mpm_ctx->init_hash = SCCalloc(MPM_INIT_HASH_SIZE, sizeof(MpmPattern *));
959  if (mpm_ctx->init_hash == NULL) {
960  exit(EXIT_FAILURE);
961  }
962 
963  /* get conf values for AC from our yaml file. We have no conf values for
964  * now. We will certainly need this, as we develop the algo */
965  SCACTileGetConfig();
966 }
967 
968 static void SCACTileDestroyInitCtx(MpmCtx *mpm_ctx)
969 {
970  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
971  SCACTileCtx *ctx = search_ctx->init_ctx;
972 
973  if (ctx == NULL)
974  return;
975 
976  if (mpm_ctx->init_hash != NULL) {
977  SCFree(mpm_ctx->init_hash);
978  mpm_ctx->init_hash = NULL;
979  }
980 
981  if (ctx->parray != NULL) {
982  uint32_t i;
983  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
984  if (ctx->parray[i] != NULL) {
985  MpmFreePattern(mpm_ctx, ctx->parray[i]);
986  }
987  }
988 
989  SCFree(ctx->parray);
990  ctx->parray = NULL;
991  }
992 
993  if (ctx->state_table != NULL) {
994  SCFree(ctx->state_table);
995 
996  mpm_ctx->memory_cnt--;
997  mpm_ctx->memory_size -= SCACTileStateTableSize(
998  ctx->state_count, ctx->bytes_per_state, ctx->alphabet_storage);
999  }
1000 
1001  if (ctx->output_table != NULL) {
1002  uint32_t state;
1003  for (state = 0; state < ctx->state_count; state++) {
1004  if (ctx->output_table[state].patterns != NULL) {
1005  SCFree(ctx->output_table[state].patterns);
1006  }
1007  }
1008  SCFree(ctx->output_table);
1009  }
1010 
1011  if (ctx->pattern_list != NULL) {
1012  uint32_t i;
1013  for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
1014  if (ctx->pattern_list[i].cs != NULL)
1015  SCFree(ctx->pattern_list[i].cs);
1016  if (ctx->pattern_list[i].sids != NULL)
1017  SCFree(ctx->pattern_list[i].sids);
1018  }
1019  SCFree(ctx->pattern_list);
1020  }
1021 
1022  SCFree(ctx);
1023  search_ctx->init_ctx = NULL;
1024  mpm_ctx->memory_cnt--;
1025  mpm_ctx->memory_size -= sizeof(SCACTileCtx);
1026 }
1027 
1028 /**
1029  * \brief Destroy the mpm context.
1030  *
1031  * \param mpm_ctx Pointer to the mpm context.
1032  */
1034 {
1035  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
1036  if (search_ctx == NULL)
1037  return;
1038 
1039  /* Destroy Initialization data */
1040  SCACTileDestroyInitCtx(mpm_ctx);
1041 
1042  /* Free Search tables */
1043  SCFree(search_ctx->state_table);
1044 
1045  if (search_ctx->pattern_list != NULL) {
1046  uint32_t i;
1047  for (i = 0; i < search_ctx->pattern_cnt; i++) {
1048  if (search_ctx->pattern_list[i].sids != NULL)
1049  SCFree(search_ctx->pattern_list[i].sids);
1050  }
1051  SCFree(search_ctx->pattern_list);
1052  }
1053 
1054  if (search_ctx->output_table != NULL) {
1055  uint32_t state;
1056  for (state = 0; state < search_ctx->state_count; state++) {
1057  if (search_ctx->output_table[state].patterns != NULL) {
1058  SCFree(search_ctx->output_table[state].patterns);
1059  }
1060  }
1061  SCFree(search_ctx->output_table);
1062  }
1063 
1064  SCFree(search_ctx);
1065  mpm_ctx->ctx = NULL;
1066 
1067  mpm_ctx->memory_cnt--;
1068  mpm_ctx->memory_size -= sizeof(SCACTileSearchCtx);
1069 }
1070 
1071 /*
1072  * Heavily optimized pattern matching routine for TILE-Gx.
1073  */
1074 
1075 #define SCHECK(x) ((x) > 0)
1076 #define BUF_TYPE int32_t
1077 // Extract byte N=0,1,2,3 from x
1078 #define BYTE0(x) (((x) & 0x000000ff) >> 0)
1079 #define BYTE1(x) (((x) & 0x0000ff00) >> 8)
1080 #define BYTE2(x) (((x) & 0x00ff0000) >> 16)
1081 #define BYTE3(x) (((x) & 0xff000000) >> 24)
1082 #define EXTRA 4 // need 4 extra bytes to avoid OOB reads
1084 static int CheckMatch(const SCACTileSearchCtx *ctx, PrefilterRuleStore *pmq, const uint8_t *buf,
1085  uint32_t buflen, uint32_t state, int i, int matches, uint8_t *mpm_bitarray)
1086 {
1087  const SCACTilePatternList *pattern_list = ctx->pattern_list;
1088  const uint8_t *buf_offset = buf + i + 1; // Lift out of loop
1089  uint32_t no_of_entries = ctx->output_table[state].no_of_entries;
1090  MpmPatternIndex *patterns = ctx->output_table[state].patterns;
1091  uint32_t k;
1092 
1093  for (k = 0; k < no_of_entries; k++) {
1094  MpmPatternIndex pindex = patterns[k] & 0x0FFFFFFF;
1095  if (mpm_bitarray[pindex / 8] & (1 << (pindex % 8))) {
1096  /* Pattern already seen by this MPM. */
1097  continue;
1098  }
1099  const SCACTilePatternList *pat = &pattern_list[pindex];
1100  const int offset = i - pat->patlen + 1;
1101  if (offset < (int)pat->offset || (pat->depth && i > pat->depth))
1102  continue;
1103 
1104  /* Double check case-sensitive match now. */
1105  if (patterns[k] >> 31) {
1106  const uint16_t patlen = pat->patlen;
1107  if (SCMemcmp(pat->cs, buf_offset - patlen, patlen) != 0) {
1108  /* Case-sensitive match failed. */
1109  continue;
1110  }
1111  }
1112  /* New match found */
1113  mpm_bitarray[pindex / 8] |= (1 << (pindex % 8));
1114 
1115  /* Always add the Signature IDs, since they could be different in the current MPM
1116  * than in a previous MPM on the same PMQ when finding the same pattern.
1117  */
1118  PrefilterAddSids(pmq, pattern_list[pindex].sids,
1119  pattern_list[pindex].sids_size);
1120  matches++;
1121  }
1122 
1123  return matches;
1124 }
1125 
1126 /**
1127  * \brief The aho corasick search function.
1128  *
1129  * \param mpm_ctx Pointer to the mpm context.
1130  * \param mpm_thread_ctx Pointer to the mpm thread context.
1131  * \param pmq Pointer to the Pattern Matcher Queue to hold
1132  * search matches.
1133  * \param buf Buffer to be searched.
1134  * \param buflen Buffer length.
1135  *
1136  * \retval matches Match count.
1137  */
1138 uint32_t SCACTileSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
1139  PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
1140 {
1141  const SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
1142 
1143  if (buflen == 0)
1144  return 0;
1145 
1146  /* Context specific matching function. */
1147  return search_ctx->Search(search_ctx, mpm_thread_ctx, pmq, buf, buflen);
1148 }
1149 
1150 /* This function handles (ctx->state_count >= 32767) */
1151 uint32_t SCACTileSearchLarge(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
1152  PrefilterRuleStore *pmq,
1153  const uint8_t *buf, uint32_t buflen)
1154 {
1155  uint32_t i = 0;
1156  int matches = 0;
1157 
1158  uint8_t *mpm_bitarray = (uint8_t *)mpm_thread_ctx->ctx;
1159  memset(mpm_bitarray, 0, mpm_thread_ctx->memory_size);
1160 
1161  const uint8_t* restrict xlate = ctx->translate_table;
1162  register int state = 0;
1163  int32_t (*state_table_u32)[256] = ctx->state_table;
1164  for (i = 0; i < buflen; i++) {
1165  state = state_table_u32[state & 0x00FFFFFF][xlate[buf[i]]];
1166  if (SCHECK(state)) {
1167  matches = CheckMatch(ctx, pmq, buf, buflen, (uint32_t)(state & 0x00FFFFFF), i, matches,
1168  mpm_bitarray);
1169  }
1170  } /* for (i = 0; i < buflen; i++) */
1171 
1172  return matches;
1173 }
1174 
1175 /*
1176  * Search with Alphabet size of 256 and 16-bit next-state entries.
1177  * Next state entry has MSB as "match" and 15 LSB bits as next-state index.
1178  */
1179 // y = 1<<log_mult * (x & (1<<width -1))
1180 #define SINDEX_INTERNAL(y, x, log_mult, width) \
1181  ((1<<log_mult) * (x & ((1<<width) - 1)))
1182 
1183 /* Type of next_state */
1184 #define STYPE int16_t
1185 #define SLOAD(x) *(STYPE * restrict)(x)
1187 #define FUNC_NAME SCACTileSearchSmall256
1188 // y = 256 * (x & 0x7FFF)
1189 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 8, 15)
1190 #include "util-mpm-ac-ks-small.c"
1191 
1192 /* Search with Alphabet size of 128 */
1193 #undef FUNC_NAME
1194 #undef SINDEX
1195 #define FUNC_NAME SCACTileSearchSmall128
1196 // y = 128 * (x & 0x7FFF)
1197 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 7, 15)
1198 #include "util-mpm-ac-ks-small.c"
1199 
1200 /* Search with Alphabet size of 64 */
1201 #undef FUNC_NAME
1202 #undef SINDEX
1203 #define FUNC_NAME SCACTileSearchSmall64
1204 // y = 64 * (x & 0x7FFF)
1205 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 6, 15)
1206 #include "util-mpm-ac-ks-small.c"
1207 
1208 /* Search with Alphabet size of 32 */
1209 #undef FUNC_NAME
1210 #undef SINDEX
1211 #define FUNC_NAME SCACTileSearchSmall32
1212 // y = 32 * (x & 0x7FFF)
1213 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 5, 15)
1214 #include "util-mpm-ac-ks-small.c"
1215 
1216 /* Search with Alphabet size of 16 */
1217 #undef FUNC_NAME
1218 #undef SINDEX
1219 #define FUNC_NAME SCACTileSearchSmall16
1220 // y = 16 * (x & 0x7FFF)
1221 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 4, 15)
1222 #include "util-mpm-ac-ks-small.c"
1223 
1224 /* Search with Alphabet size of 8 */
1225 #undef FUNC_NAME
1226 #undef SINDEX
1227 #define FUNC_NAME SCACTileSearchSmall8
1228 // y = 8 * (x & 0x7FFF)
1229 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 3, 15)
1230 #include "util-mpm-ac-ks-small.c"
1231 
1232 /*
1233  * Search with Alphabet size of 256 and 8-bit next-state entries.
1234  * Next state entry has MSB as "match" and 15 LSB bits as next-state index.
1235  */
1236 #undef STYPE
1237 #define STYPE int8_t
1239 #undef FUNC_NAME
1240 #undef SINDEX
1241 #define FUNC_NAME SCACTileSearchTiny256
1242 // y = 256 * (x & 0x7F)
1243 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 8, 7)
1244 #include "util-mpm-ac-ks-small.c"
1245 
1246 /* Search with Alphabet size of 128 */
1247 #undef FUNC_NAME
1248 #undef SINDEX
1249 #define FUNC_NAME SCACTileSearchTiny128
1250 // y = 128 * (x & 0x7F)
1251 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 7, 7)
1252 #include "util-mpm-ac-ks-small.c"
1253 
1254 /* Search with Alphabet size of 64 */
1255 #undef FUNC_NAME
1256 #undef SINDEX
1257 #define FUNC_NAME SCACTileSearchTiny64
1258 // y = 64 * (x & 0x7F)
1259 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 6, 7)
1260 #include "util-mpm-ac-ks-small.c"
1261 
1262 /* Search with Alphabet size of 32 */
1263 #undef FUNC_NAME
1264 #undef SINDEX
1265 #define FUNC_NAME SCACTileSearchTiny32
1266 // y = 32 * (x & 0x7F)
1267 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 5, 7)
1268 #include "util-mpm-ac-ks-small.c"
1269 
1270 /* Search with Alphabet size of 16 */
1271 #undef FUNC_NAME
1272 #undef SINDEX
1273 #define FUNC_NAME SCACTileSearchTiny16
1274 // y = 16 * (x & 0x7F)
1275 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 4, 7)
1276 #include "util-mpm-ac-ks-small.c"
1277 
1278 /* Search with Alphabet size of 8 */
1279 #undef FUNC_NAME
1280 #undef SINDEX
1281 #define FUNC_NAME SCACTileSearchTiny8
1282 // y = 8 * (x & 0x7F)
1283 #define SINDEX(y,x) SINDEX_INTERNAL(y, x, 3, 7)
1285 
1286 
1287 /**
1288  * \brief Add a case insensitive pattern. Although we have different calls for
1289  * adding case sensitive and insensitive patterns, we make a single call
1290  * for either case. No special treatment for either case.
1291  *
1292  * \param mpm_ctx Pointer to the mpm context.
1293  * \param pat The pattern to add.
1294  * \param patnen The pattern length.
1295  * \param offset Ignored.
1296  * \param depth Ignored.
1297  * \param pid The pattern id.
1298  * \param sid Ignored.
1299  * \param flags Flags associated with this pattern.
1300  *
1301  * \retval 0 On success.
1302  * \retval -1 On failure.
1303  */
1304 int SCACTileAddPatternCI(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset,
1305  uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
1306 {
1308  return MpmAddPattern(mpm_ctx, pat, patlen, offset, depth,
1309  pid, sid, flags);
1310 }
1311 
1312 /**
1313  * \brief Add a case sensitive pattern. Although we have different calls for
1314  * adding case sensitive and insensitive patterns, we make a single call
1315  * for either case. No special treatment for either case.
1316  *
1317  * \param mpm_ctx Pointer to the mpm context.
1318  * \param pat The pattern to add.
1319  * \param patnen The pattern length.
1320  * \param offset Ignored.
1321  * \param depth Ignored.
1322  * \param pid The pattern id.
1323  * \param sid Ignored.
1324  * \param flags Flags associated with this pattern.
1325  *
1326  * \retval 0 On success.
1327  * \retval -1 On failure.
1328  */
1329 int SCACTileAddPatternCS(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen,
1330  uint16_t offset, uint16_t depth, uint32_t pid,
1331  SigIntId sid, uint8_t flags)
1332 {
1333  return MpmAddPattern(mpm_ctx, pat, patlen, offset, depth,
1334  pid, sid, flags);
1335 }
1336 
1338 {
1339  SCACTileSearchCtx *search_ctx = (SCACTileSearchCtx *)mpm_ctx->ctx;
1340  SCACTileCtx *ctx = search_ctx->init_ctx;
1341 
1342  printf("MPM AC Information:\n");
1343  printf("Memory allocs: %" PRIu32 "\n", mpm_ctx->memory_cnt);
1344  printf("Memory alloced: %" PRIu32 "\n", mpm_ctx->memory_size);
1345  printf(" Sizeof:\n");
1346  printf(" MpmCtx %" PRIuMAX "\n", (uintmax_t)sizeof(MpmCtx));
1347  printf(" SCACTileCtx: %" PRIuMAX "\n", (uintmax_t)sizeof(SCACTileCtx));
1348  printf(" MpmPattern %" PRIuMAX "\n", (uintmax_t)sizeof(MpmPattern));
1349  printf(" MpmPattern %" PRIuMAX "\n", (uintmax_t)sizeof(MpmPattern));
1350  printf("Unique Patterns: %" PRIu32 "\n", mpm_ctx->pattern_cnt);
1351  printf("Smallest: %" PRIu32 "\n", mpm_ctx->minlen);
1352  printf("Largest: %" PRIu32 "\n", mpm_ctx->maxlen);
1353  printf("Total states in the state table: %u\n", ctx->state_count);
1354  printf("\n");
1355 }
1356 
1357 /**
1358  * \brief Init the mpm thread context.
1359  *
1360  * \param mpm_ctx Pointer to the mpm context.
1361  * \param mpm_thread_ctx Pointer to the mpm thread context.
1362  */
1363 static void SCACTileInitThreadCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx)
1364 {
1365  uint32_t size = (mpm_ctx->pattern_cnt + 7) / 8;
1366 
1367  uint8_t *bitarray = SCCalloc(size, sizeof(uint8_t));
1368  if (bitarray == NULL) {
1369  exit(EXIT_FAILURE);
1370  }
1371  mpm_thread_ctx->ctx = bitarray;
1372  mpm_thread_ctx->memory_cnt = 1;
1373  mpm_thread_ctx->memory_size = size;
1374 }
1375 
1376 static void SCACTileDestroyThreadCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx)
1377 {
1378  mpm_thread_ctx->memory_cnt = 0;
1379  mpm_thread_ctx->memory_size = 0;
1380  SCFree(mpm_thread_ctx->ctx);
1381  mpm_thread_ctx->ctx = NULL;
1382 }
1383 
1384 /************************** Mpm Registration ***************************/
1385 
1386 /**
1387  * \brief Register the aho-corasick mpm 'ks' originally developed by
1388  * Ken Steele for Tilera Tile-Gx processor.
1389  */
1391 {
1392  mpm_table[MPM_AC_KS].name = "ac-ks";
1395  mpm_table[MPM_AC_KS].ConfigInit = NULL;
1404  mpm_table[MPM_AC_KS].InitThreadCtx = SCACTileInitThreadCtx;
1405  mpm_table[MPM_AC_KS].DestroyThreadCtx = SCACTileDestroyThreadCtx;
1406 #ifdef UNITTESTS
1407  mpm_table[MPM_AC_KS].RegisterUnittests = SCACTileRegisterTests;
1408 #endif
1410 }
1411 
1412 
1413 /*************************************Unittests********************************/
1414 
1415 #ifdef UNITTESTS
1416 #include "detect-engine-alert.h"
1417 
1418 static int SCACTileTest01(void)
1419 {
1420  int result = 0;
1421  MpmCtx mpm_ctx;
1422  MpmThreadCtx mpm_thread_ctx;
1423  PrefilterRuleStore pmq;
1424 
1425  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1426  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1427  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1428 
1429  /* 1 match */
1430  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1431  PmqSetup(&pmq);
1432 
1433  SCACTilePreparePatterns(NULL, &mpm_ctx);
1434  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1435 
1436  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1437 
1438  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1439  (uint8_t *)buf, strlen(buf));
1440 
1441  if (cnt == 1)
1442  result = 1;
1443  else
1444  printf("1 != %" PRIu32 " ",cnt);
1445 
1446  SCACTileDestroyCtx(&mpm_ctx);
1447  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1448  PmqFree(&pmq);
1449  return result;
1450 }
1451 
1452 static int SCACTileTest02(void)
1453 {
1454  int result = 0;
1455  MpmCtx mpm_ctx;
1456  MpmThreadCtx mpm_thread_ctx;
1457  PrefilterRuleStore pmq;
1458 
1459  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1460  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1461  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1462 
1463  /* 1 match */
1464  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0, 0);
1465  PmqSetup(&pmq);
1466 
1467  SCACTilePreparePatterns(NULL, &mpm_ctx);
1468  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1469 
1470  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1471  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1472  (uint8_t *)buf, strlen(buf));
1473 
1474  if (cnt == 0)
1475  result = 1;
1476  else
1477  printf("0 != %" PRIu32 " ",cnt);
1478 
1479  SCACTileDestroyCtx(&mpm_ctx);
1480  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1481  PmqFree(&pmq);
1482  return result;
1483 }
1484 
1485 static int SCACTileTest03(void)
1486 {
1487  int result = 0;
1488  MpmCtx mpm_ctx;
1489  MpmThreadCtx mpm_thread_ctx;
1490  PrefilterRuleStore pmq;
1491 
1492  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1493  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1494  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1495 
1496  /* 1 match */
1497  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1498  /* 1 match */
1499  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"bcde", 4, 0, 0, 1, 0, 0);
1500  /* 1 match */
1501  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"fghj", 4, 0, 0, 2, 0, 0);
1502  PmqSetup(&pmq);
1503 
1504  SCACTilePreparePatterns(NULL, &mpm_ctx);
1505  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1506 
1507  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1508  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1509  (uint8_t *)buf, strlen(buf));
1510 
1511  if (cnt == 3)
1512  result = 1;
1513  else
1514  printf("3 != %" PRIu32 " ",cnt);
1515 
1516  SCACTileDestroyCtx(&mpm_ctx);
1517  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1518  PmqFree(&pmq);
1519  return result;
1520 }
1521 
1522 static int SCACTileTest04(void)
1523 {
1524  int result = 0;
1525  MpmCtx mpm_ctx;
1526  MpmThreadCtx mpm_thread_ctx;
1527  PrefilterRuleStore pmq;
1528 
1529  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1530  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1531  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1532 
1533  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1534  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"bcdegh", 6, 0, 0, 1, 0, 0);
1535  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"fghjxyz", 7, 0, 0, 2, 0, 0);
1536  PmqSetup(&pmq);
1537 
1538  SCACTilePreparePatterns(NULL, &mpm_ctx);
1539  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1540 
1541  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1542  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1543  (uint8_t *)buf, strlen(buf));
1544 
1545  if (cnt == 1)
1546  result = 1;
1547  else
1548  printf("1 != %" PRIu32 " ",cnt);
1549 
1550  SCACTileDestroyCtx(&mpm_ctx);
1551  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1552  PmqFree(&pmq);
1553  return result;
1554 }
1555 
1556 static int SCACTileTest05(void)
1557 {
1558  int result = 0;
1559  MpmCtx mpm_ctx;
1560  MpmThreadCtx mpm_thread_ctx;
1561  PrefilterRuleStore pmq;
1562 
1563  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1564  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1565  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1566 
1567  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0);
1568  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"bCdEfG", 6, 0, 0, 1, 0, 0);
1569  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"fghJikl", 7, 0, 0, 2, 0, 0);
1570  PmqSetup(&pmq);
1571 
1572  SCACTilePreparePatterns(NULL, &mpm_ctx);
1573  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1574 
1575  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1576  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1577  (uint8_t *)buf, strlen(buf));
1578 
1579  if (cnt == 3)
1580  result = 1;
1581  else
1582  printf("3 != %" PRIu32 " ",cnt);
1583 
1584  SCACTileDestroyCtx(&mpm_ctx);
1585  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1586  PmqFree(&pmq);
1587  return result;
1588 }
1589 
1590 static int SCACTileTest06(void)
1591 {
1592  int result = 0;
1593  MpmCtx mpm_ctx;
1594  MpmThreadCtx mpm_thread_ctx;
1595  PrefilterRuleStore pmq;
1596 
1597  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1598  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1599  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1600 
1601  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1602  PmqSetup(&pmq);
1603 
1604  SCACTilePreparePatterns(NULL, &mpm_ctx);
1605  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1606 
1607  const char *buf = "abcd";
1608  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1609  (uint8_t *)buf, strlen(buf));
1610 
1611  if (cnt == 1)
1612  result = 1;
1613  else
1614  printf("1 != %" PRIu32 " ",cnt);
1615 
1616  SCACTileDestroyCtx(&mpm_ctx);
1617  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1618  PmqFree(&pmq);
1619  return result;
1620 }
1621 
1622 static int SCACTileTest07(void)
1623 {
1624  MpmCtx mpm_ctx;
1625  MpmThreadCtx mpm_thread_ctx;
1626  PrefilterRuleStore pmq;
1627 
1628  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1629  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1630  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1631 
1632  /* should match 30 times */
1633  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0, 0);
1634  /* should match 29 times */
1635  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 1, 0, 0);
1636  /* should match 28 times */
1637  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAA", 3, 0, 0, 2, 0, 0);
1638  /* 26 */
1639  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAA", 5, 0, 0, 3, 0, 0);
1640  /* 21 */
1641  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAA", 10, 0, 0, 4, 0, 0);
1642  /* 1 */
1643  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1644  30, 0, 0, 5, 0, 0);
1645  PmqSetup(&pmq);
1646  /* total matches: 135: 6 unique */
1647 
1648  SCACTilePreparePatterns(NULL, &mpm_ctx);
1649  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1650 
1651  const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1652  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1653  (uint8_t *)buf, strlen(buf));
1654  FAIL_IF_NOT(cnt == 6);
1655 
1656  SCACTileDestroyCtx(&mpm_ctx);
1657  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1658  PmqFree(&pmq);
1659  PASS;
1660 }
1661 
1662 static int SCACTileTest08(void)
1663 {
1664  int result = 0;
1665  MpmCtx mpm_ctx;
1666  MpmThreadCtx mpm_thread_ctx;
1667  PrefilterRuleStore pmq;
1668 
1669  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1670  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1671  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1672 
1673  /* 1 match */
1674  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1675  PmqSetup(&pmq);
1676 
1677  SCACTilePreparePatterns(NULL, &mpm_ctx);
1678  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1679 
1680  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1681  (uint8_t *)"a", 1);
1682 
1683  if (cnt == 0)
1684  result = 1;
1685  else
1686  printf("0 != %" PRIu32 " ",cnt);
1687 
1688  SCACTileDestroyCtx(&mpm_ctx);
1689  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1690  PmqFree(&pmq);
1691  return result;
1692 }
1693 
1694 static int SCACTileTest09(void)
1695 {
1696  int result = 0;
1697  MpmCtx mpm_ctx;
1698  MpmThreadCtx mpm_thread_ctx;
1699  PrefilterRuleStore pmq;
1700 
1701  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1702  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1703  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1704 
1705  /* 1 match */
1706  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0, 0);
1707  PmqSetup(&pmq);
1708 
1709  SCACTilePreparePatterns(NULL, &mpm_ctx);
1710  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1711 
1712  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1713  (uint8_t *)"ab", 2);
1714 
1715  if (cnt == 1)
1716  result = 1;
1717  else
1718  printf("1 != %" PRIu32 " ",cnt);
1719 
1720  SCACTileDestroyCtx(&mpm_ctx);
1721  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1722  PmqFree(&pmq);
1723  return result;
1724 }
1725 
1726 static int SCACTileTest10(void)
1727 {
1728  int result = 0;
1729  MpmCtx mpm_ctx;
1730  MpmThreadCtx mpm_thread_ctx;
1731  PrefilterRuleStore pmq;
1732 
1733  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1734  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1735  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1736 
1737  /* 1 match */
1738  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0, 0);
1739  PmqSetup(&pmq);
1740 
1741  SCACTilePreparePatterns(NULL, &mpm_ctx);
1742  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1743 
1744  const char *buf = "01234567890123456789012345678901234567890123456789"
1745  "01234567890123456789012345678901234567890123456789"
1746  "abcdefgh"
1747  "01234567890123456789012345678901234567890123456789"
1748  "01234567890123456789012345678901234567890123456789";
1749  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1750  (uint8_t *)buf, strlen(buf));
1751 
1752  if (cnt == 1)
1753  result = 1;
1754  else
1755  printf("1 != %" PRIu32 " ",cnt);
1756 
1757  SCACTileDestroyCtx(&mpm_ctx);
1758  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1759  PmqFree(&pmq);
1760  return result;
1761 }
1762 
1763 static int SCACTileTest11(void)
1764 {
1765  int result = 0;
1766  MpmCtx mpm_ctx;
1767  MpmThreadCtx mpm_thread_ctx;
1768  PrefilterRuleStore pmq;
1769 
1770  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1771  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1772  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1773 
1774  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"he", 2, 0, 0, 1, 0, 0) == -1)
1775  goto end;
1776  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"she", 3, 0, 0, 2, 0, 0) == -1)
1777  goto end;
1778  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"his", 3, 0, 0, 3, 0, 0) == -1)
1779  goto end;
1780  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"hers", 4, 0, 0, 4, 0, 0) == -1)
1781  goto end;
1782  PmqSetup(&pmq);
1783 
1784  if (SCACTilePreparePatterns(NULL, &mpm_ctx) == -1)
1785  goto end;
1786  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1787 
1788  result = 1;
1789 
1790  const char *buf = "he";
1791  result &= (SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1792  strlen(buf)) == 1);
1793  buf = "she";
1794  result &= (SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1795  strlen(buf)) == 2);
1796  buf = "his";
1797  result &= (SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1798  strlen(buf)) == 1);
1799  buf = "hers";
1800  result &= (SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1801  strlen(buf)) == 2);
1802 
1803  end:
1804  SCACTileDestroyCtx(&mpm_ctx);
1805  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1806  PmqFree(&pmq);
1807  return result;
1808 }
1809 
1810 static int SCACTileTest12(void)
1811 {
1812  int result = 0;
1813  MpmCtx mpm_ctx;
1814  MpmThreadCtx mpm_thread_ctx;
1815  PrefilterRuleStore pmq;
1816 
1817  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1818  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1819  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1820 
1821  /* 1 match */
1822  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0, 0);
1823  /* 1 match */
1824  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"vwxyz", 5, 0, 0, 1, 0, 0);
1825  PmqSetup(&pmq);
1826 
1827  SCACTilePreparePatterns(NULL, &mpm_ctx);
1828  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1829 
1830  const char *buf = "abcdefghijklmnopqrstuvwxyz";
1831  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1832  (uint8_t *)buf, strlen(buf));
1833 
1834  if (cnt == 2)
1835  result = 1;
1836  else
1837  printf("2 != %" PRIu32 " ",cnt);
1838 
1839  SCACTileDestroyCtx(&mpm_ctx);
1840  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1841  PmqFree(&pmq);
1842  return result;
1843 }
1844 
1845 static int SCACTileTest13(void)
1846 {
1847  int result = 0;
1848  MpmCtx mpm_ctx;
1849  MpmThreadCtx mpm_thread_ctx;
1850  PrefilterRuleStore pmq;
1851 
1852  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1853  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1854  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1855 
1856  /* 1 match */
1857  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCD";
1858  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1859  PmqSetup(&pmq);
1860 
1861  SCACTilePreparePatterns(NULL, &mpm_ctx);
1862  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1863 
1864  const char *buf = "abcdefghijklmnopqrstuvwxyzABCD";
1865  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1866  (uint8_t *)buf, strlen(buf));
1867 
1868  if (cnt == 1)
1869  result = 1;
1870  else
1871  printf("1 != %" PRIu32 " ",cnt);
1872 
1873  SCACTileDestroyCtx(&mpm_ctx);
1874  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1875  PmqFree(&pmq);
1876  return result;
1877 }
1878 
1879 static int SCACTileTest14(void)
1880 {
1881  int result = 0;
1882  MpmCtx mpm_ctx;
1883  MpmThreadCtx mpm_thread_ctx;
1884  PrefilterRuleStore pmq;
1885 
1886  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1887  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1888  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1889 
1890  /* 1 match */
1891  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCDE";
1892  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1893  PmqSetup(&pmq);
1894 
1895  SCACTilePreparePatterns(NULL, &mpm_ctx);
1896  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1897 
1898  const char *buf = "abcdefghijklmnopqrstuvwxyzABCDE";
1899  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1900  (uint8_t *)buf, strlen(buf));
1901 
1902  if (cnt == 1)
1903  result = 1;
1904  else
1905  printf("1 != %" PRIu32 " ",cnt);
1906 
1907  SCACTileDestroyCtx(&mpm_ctx);
1908  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1909  PmqFree(&pmq);
1910  return result;
1911 }
1912 
1913 static int SCACTileTest15(void)
1914 {
1915  int result = 0;
1916  MpmCtx mpm_ctx;
1917  MpmThreadCtx mpm_thread_ctx;
1918  PrefilterRuleStore pmq;
1919 
1920  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1921  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1922  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1923 
1924  /* 1 match */
1925  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCDEF";
1926  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1927  PmqSetup(&pmq);
1928 
1929  SCACTilePreparePatterns(NULL, &mpm_ctx);
1930  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1931 
1932  const char *buf = "abcdefghijklmnopqrstuvwxyzABCDEF";
1933  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1934  (uint8_t *)buf, strlen(buf));
1935 
1936  if (cnt == 1)
1937  result = 1;
1938  else
1939  printf("1 != %" PRIu32 " ",cnt);
1940 
1941  SCACTileDestroyCtx(&mpm_ctx);
1942  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1943  PmqFree(&pmq);
1944  return result;
1945 }
1946 
1947 static int SCACTileTest16(void)
1948 {
1949  int result = 0;
1950  MpmCtx mpm_ctx;
1951  MpmThreadCtx mpm_thread_ctx;
1952  PrefilterRuleStore pmq;
1953 
1954  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1955  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1956  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1957 
1958  /* 1 match */
1959  const char pat[] = "abcdefghijklmnopqrstuvwxyzABC";
1960  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1961  PmqSetup(&pmq);
1962 
1963  SCACTilePreparePatterns(NULL, &mpm_ctx);
1964  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1965 
1966  const char *buf = "abcdefghijklmnopqrstuvwxyzABC";
1967  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
1968  (uint8_t *)buf, strlen(buf));
1969 
1970  if (cnt == 1)
1971  result = 1;
1972  else
1973  printf("1 != %" PRIu32 " ",cnt);
1974 
1975  SCACTileDestroyCtx(&mpm_ctx);
1976  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1977  PmqFree(&pmq);
1978  return result;
1979 }
1980 
1981 static int SCACTileTest17(void)
1982 {
1983  int result = 0;
1984  MpmCtx mpm_ctx;
1985  MpmThreadCtx mpm_thread_ctx;
1986  PrefilterRuleStore pmq;
1987 
1988  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1989  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1990  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
1991 
1992  /* 1 match */
1993  const char pat[] = "abcdefghijklmnopqrstuvwxyzAB";
1994  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1995  PmqSetup(&pmq);
1996 
1997  SCACTilePreparePatterns(NULL, &mpm_ctx);
1998  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1999 
2000  const char *buf = "abcdefghijklmnopqrstuvwxyzAB";
2001  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2002  (uint8_t *)buf, strlen(buf));
2003 
2004  if (cnt == 1)
2005  result = 1;
2006  else
2007  printf("1 != %" PRIu32 " ",cnt);
2008 
2009  SCACTileDestroyCtx(&mpm_ctx);
2010  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2011  PmqFree(&pmq);
2012  return result;
2013 }
2014 
2015 static int SCACTileTest18(void)
2016 {
2017  int result = 0;
2018  MpmCtx mpm_ctx;
2019  MpmThreadCtx mpm_thread_ctx;
2020  PrefilterRuleStore pmq;
2021 
2022  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2023  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2024  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2025 
2026  /* 1 match */
2027  const char pat[] = "abcde"
2028  "fghij"
2029  "klmno"
2030  "pqrst"
2031  "uvwxy"
2032  "z";
2033  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
2034  PmqSetup(&pmq);
2035 
2036  SCACTilePreparePatterns(NULL, &mpm_ctx);
2037  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2038 
2039  const char *buf = "abcde""fghij""klmno""pqrst""uvwxy""z";
2040  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2041  (uint8_t *)buf, strlen(buf));
2042 
2043  if (cnt == 1)
2044  result = 1;
2045  else
2046  printf("1 != %" PRIu32 " ",cnt);
2047 
2048  SCACTileDestroyCtx(&mpm_ctx);
2049  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2050  PmqFree(&pmq);
2051  return result;
2052 }
2053 
2054 static int SCACTileTest19(void)
2055 {
2056  int result = 0;
2057  MpmCtx mpm_ctx;
2058  MpmThreadCtx mpm_thread_ctx;
2059  PrefilterRuleStore pmq;
2060 
2061  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2062  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2063  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2064 
2065  /* 1 */
2066  const char pat[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
2067  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
2068  PmqSetup(&pmq);
2069 
2070  SCACTilePreparePatterns(NULL, &mpm_ctx);
2071  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2072 
2073  const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
2074  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2075  (uint8_t *)buf, strlen(buf));
2076 
2077  if (cnt == 1)
2078  result = 1;
2079  else
2080  printf("1 != %" PRIu32 " ",cnt);
2081 
2082  SCACTileDestroyCtx(&mpm_ctx);
2083  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2084  PmqFree(&pmq);
2085  return result;
2086 }
2087 
2088 static int SCACTileTest20(void)
2089 {
2090  int result = 0;
2091  MpmCtx mpm_ctx;
2092  MpmThreadCtx mpm_thread_ctx;
2093  PrefilterRuleStore pmq;
2094 
2095  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2096  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2097  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2098 
2099  /* 1 */
2100  const char pat[] = "AAAAA"
2101  "AAAAA"
2102  "AAAAA"
2103  "AAAAA"
2104  "AAAAA"
2105  "AAAAA"
2106  "AA";
2107  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
2108  PmqSetup(&pmq);
2109 
2110  SCACTilePreparePatterns(NULL, &mpm_ctx);
2111  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2112 
2113  const char *buf = "AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AA";
2114  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2115  (uint8_t *)buf, strlen(buf));
2116 
2117  if (cnt == 1)
2118  result = 1;
2119  else
2120  printf("1 != %" PRIu32 " ",cnt);
2121 
2122  SCACTileDestroyCtx(&mpm_ctx);
2123  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2124  PmqFree(&pmq);
2125  return result;
2126 }
2127 
2128 static int SCACTileTest21(void)
2129 {
2130  int result = 0;
2131  MpmCtx mpm_ctx;
2132  MpmThreadCtx mpm_thread_ctx;
2133  PrefilterRuleStore pmq;
2134 
2135  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2136  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2137  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2138 
2139  /* 1 */
2140  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2141  PmqSetup(&pmq);
2142 
2143  SCACTilePreparePatterns(NULL, &mpm_ctx);
2144  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2145 
2146  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2147  (uint8_t *)"AA", 2);
2148 
2149  if (cnt == 1)
2150  result = 1;
2151  else
2152  printf("1 != %" PRIu32 " ",cnt);
2153 
2154  SCACTileDestroyCtx(&mpm_ctx);
2155  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2156  PmqFree(&pmq);
2157  return result;
2158 }
2159 
2160 static int SCACTileTest22(void)
2161 {
2162  int result = 0;
2163  MpmCtx mpm_ctx;
2164  MpmThreadCtx mpm_thread_ctx;
2165  PrefilterRuleStore pmq;
2166 
2167  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2168  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2169  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2170 
2171  /* 1 match */
2172  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
2173  /* 1 match */
2174  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcde", 5, 0, 0, 1, 0, 0);
2175  PmqSetup(&pmq);
2176 
2177  SCACTilePreparePatterns(NULL, &mpm_ctx);
2178  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2179 
2180  const char *buf = "abcdefghijklmnopqrstuvwxyz";
2181  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2182  (uint8_t *)buf, strlen(buf));
2183 
2184  if (cnt == 2)
2185  result = 1;
2186  else
2187  printf("2 != %" PRIu32 " ",cnt);
2188 
2189  SCACTileDestroyCtx(&mpm_ctx);
2190  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2191  PmqFree(&pmq);
2192  return result;
2193 }
2194 
2195 static int SCACTileTest23(void)
2196 {
2197  int result = 0;
2198  MpmCtx mpm_ctx;
2199  MpmThreadCtx mpm_thread_ctx;
2200  PrefilterRuleStore pmq;
2201 
2202  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2203  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2204  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2205 
2206  /* 1 */
2207  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2208  PmqSetup(&pmq);
2209 
2210  SCACTilePreparePatterns(NULL, &mpm_ctx);
2211  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2212 
2213  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2214  (uint8_t *)"aa", 2);
2215 
2216  if (cnt == 0)
2217  result = 1;
2218  else
2219  printf("1 != %" PRIu32 " ",cnt);
2220 
2221  SCACTileDestroyCtx(&mpm_ctx);
2222  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2223  PmqFree(&pmq);
2224  return result;
2225 }
2226 
2227 static int SCACTileTest24(void)
2228 {
2229  int result = 0;
2230  MpmCtx mpm_ctx;
2231  MpmThreadCtx mpm_thread_ctx;
2232  PrefilterRuleStore pmq;
2233 
2234  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2235  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2236  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2237 
2238  /* 1 */
2239  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2240  PmqSetup(&pmq);
2241 
2242  SCACTilePreparePatterns(NULL, &mpm_ctx);
2243  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2244 
2245  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2246  (uint8_t *)"aa", 2);
2247 
2248  if (cnt == 1)
2249  result = 1;
2250  else
2251  printf("1 != %" PRIu32 " ",cnt);
2252 
2253  SCACTileDestroyCtx(&mpm_ctx);
2254  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2255  PmqFree(&pmq);
2256  return result;
2257 }
2258 
2259 static int SCACTileTest25(void)
2260 {
2261  int result = 0;
2262  MpmCtx mpm_ctx;
2263  MpmThreadCtx mpm_thread_ctx;
2264  PrefilterRuleStore pmq;
2265 
2266  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2267  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2268  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2269 
2270  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0);
2271  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"bCdEfG", 6, 0, 0, 1, 0, 0);
2272  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"fghiJkl", 7, 0, 0, 2, 0, 0);
2273  PmqSetup(&pmq);
2274 
2275  SCACTilePreparePatterns(NULL, &mpm_ctx);
2276  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2277 
2278  const char *buf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2279  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2280  (uint8_t *)buf, strlen(buf));
2281 
2282  if (cnt == 3)
2283  result = 1;
2284  else
2285  printf("3 != %" PRIu32 " ",cnt);
2286 
2287  SCACTileDestroyCtx(&mpm_ctx);
2288  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2289  PmqFree(&pmq);
2290  return result;
2291 }
2292 
2293 static int SCACTileTest26(void)
2294 {
2295  int result = 0;
2296  MpmCtx mpm_ctx;
2297  MpmThreadCtx mpm_thread_ctx;
2298  PrefilterRuleStore pmq;
2299 
2300  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2301  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2302  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2303 
2304  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"Works", 5, 0, 0, 0, 0, 0);
2305  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"Works", 5, 0, 0, 1, 0, 0);
2306  PmqSetup(&pmq);
2307 
2308  SCACTilePreparePatterns(NULL, &mpm_ctx);
2309  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2310 
2311  const char *buf = "works";
2312  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2313  (uint8_t *)buf, strlen(buf));
2314 
2315  if (cnt == 1)
2316  result = 1;
2317  else
2318  printf("3 != %" PRIu32 " ",cnt);
2319 
2320  SCACTileDestroyCtx(&mpm_ctx);
2321  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2322  PmqFree(&pmq);
2323  return result;
2324 }
2325 
2326 static int SCACTileTest27(void)
2327 {
2328  int result = 0;
2329  MpmCtx mpm_ctx;
2330  MpmThreadCtx mpm_thread_ctx;
2331  PrefilterRuleStore pmq;
2332 
2333  memset(&mpm_ctx, 0, sizeof(MpmCtx));
2334  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2335  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2336 
2337  /* 0 match */
2338  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"ONE", 3, 0, 0, 0, 0, 0);
2339  PmqSetup(&pmq);
2340 
2341  SCACTilePreparePatterns(NULL, &mpm_ctx);
2342  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2343 
2344  const char *buf = "tone";
2345  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2346  (uint8_t *)buf, strlen(buf));
2347 
2348  if (cnt == 0)
2349  result = 1;
2350  else
2351  printf("0 != %" PRIu32 " ",cnt);
2352 
2353  SCACTileDestroyCtx(&mpm_ctx);
2354  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2355  PmqFree(&pmq);
2356  return result;
2357 }
2358 
2359 static int SCACTileTest28(void)
2360 {
2361  int result = 0;
2362  MpmCtx mpm_ctx;
2363  MpmThreadCtx mpm_thread_ctx;
2364  PrefilterRuleStore pmq;
2365 
2366  memset(&mpm_ctx, 0, sizeof(MpmCtx));
2367  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2368  MpmInitCtx(&mpm_ctx, MPM_AC_KS);
2369 
2370  /* 0 match */
2371  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"one", 3, 0, 0, 0, 0, 0);
2372  PmqSetup(&pmq);
2373 
2374  SCACTilePreparePatterns(NULL, &mpm_ctx);
2375  SCACTileInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2376 
2377  const char *buf = "tONE";
2378  uint32_t cnt = SCACTileSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
2379  (uint8_t *)buf, strlen(buf));
2380 
2381  if (cnt == 0)
2382  result = 1;
2383  else
2384  printf("0 != %" PRIu32 " ",cnt);
2385 
2386  SCACTileDestroyCtx(&mpm_ctx);
2387  SCACTileDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2388  PmqFree(&pmq);
2389  return result;
2390 }
2391 
2392 static int SCACTileTest29(void)
2393 {
2394  uint8_t buf[] = "onetwothreefourfivesixseveneightnine";
2395  uint16_t buflen = sizeof(buf) - 1;
2396  Packet *p = NULL;
2397  ThreadVars th_v;
2398  DetectEngineThreadCtx *det_ctx = NULL;
2399  int result = 0;
2400 
2401  memset(&th_v, 0, sizeof(th_v));
2403  p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
2404 
2406  if (de_ctx == NULL)
2407  goto end;
2408 
2409  de_ctx->flags |= DE_QUIET;
2410 
2411  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
2412  "(content:\"onetwothreefourfivesixseveneightnine\"; sid:1;)");
2413  if (de_ctx->sig_list == NULL)
2414  goto end;
2415  de_ctx->sig_list->next = SigInit(de_ctx, "alert tcp any any -> any any "
2416  "(content:\"onetwothreefourfivesixseveneightnine\"; fast_pattern:3,3; sid:2;)");
2417  if (de_ctx->sig_list->next == NULL)
2418  goto end;
2419 
2421  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
2422 
2423  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
2424  if (PacketAlertCheck(p, 1) != 1) {
2425  printf("if (PacketAlertCheck(p, 1) != 1) failure\n");
2426  goto end;
2427  }
2428  if (PacketAlertCheck(p, 2) != 1) {
2429  printf("if (PacketAlertCheck(p, 1) != 2) failure\n");
2430  goto end;
2431  }
2432 
2433  result = 1;
2434 end:
2435  UTHFreePackets(&p, 1);
2436  if (de_ctx != NULL) {
2437  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
2439  }
2441  return result;
2442 }
2443 
2444 void SCACTileRegisterTests(void)
2445 {
2446  UtRegisterTest("SCACTileTest01", SCACTileTest01);
2447  UtRegisterTest("SCACTileTest02", SCACTileTest02);
2448  UtRegisterTest("SCACTileTest03", SCACTileTest03);
2449  UtRegisterTest("SCACTileTest04", SCACTileTest04);
2450  UtRegisterTest("SCACTileTest05", SCACTileTest05);
2451  UtRegisterTest("SCACTileTest06", SCACTileTest06);
2452  UtRegisterTest("SCACTileTest07", SCACTileTest07);
2453  UtRegisterTest("SCACTileTest08", SCACTileTest08);
2454  UtRegisterTest("SCACTileTest09", SCACTileTest09);
2455  UtRegisterTest("SCACTileTest10", SCACTileTest10);
2456  UtRegisterTest("SCACTileTest11", SCACTileTest11);
2457  UtRegisterTest("SCACTileTest12", SCACTileTest12);
2458  UtRegisterTest("SCACTileTest13", SCACTileTest13);
2459  UtRegisterTest("SCACTileTest14", SCACTileTest14);
2460  UtRegisterTest("SCACTileTest15", SCACTileTest15);
2461  UtRegisterTest("SCACTileTest16", SCACTileTest16);
2462  UtRegisterTest("SCACTileTest17", SCACTileTest17);
2463  UtRegisterTest("SCACTileTest18", SCACTileTest18);
2464  UtRegisterTest("SCACTileTest19", SCACTileTest19);
2465  UtRegisterTest("SCACTileTest20", SCACTileTest20);
2466  UtRegisterTest("SCACTileTest21", SCACTileTest21);
2467  UtRegisterTest("SCACTileTest22", SCACTileTest22);
2468  UtRegisterTest("SCACTileTest23", SCACTileTest23);
2469  UtRegisterTest("SCACTileTest24", SCACTileTest24);
2470  UtRegisterTest("SCACTileTest25", SCACTileTest25);
2471  UtRegisterTest("SCACTileTest26", SCACTileTest26);
2472  UtRegisterTest("SCACTileTest27", SCACTileTest27);
2473  UtRegisterTest("SCACTileTest28", SCACTileTest28);
2474  UtRegisterTest("SCACTileTest29", SCACTileTest29);
2475 }
2476 #endif
2477 
2478 #else /* we're big endian */
2479 
2480 void MpmACTileRegister(void)
2481 {
2482  /* no-op on big endian */
2483 }
2484 
2485 #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:60
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
MpmTableElmt_::InitThreadCtx
void(* InitThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:171
MpmThreadCtx_
Definition: util-mpm.h:62
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:169
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:65
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:987
th_v
ThreadVars * th_v
Definition: fuzz_iprep.c:20
MPM_FEATURE_FLAG_OFFSET
#define MPM_FEATURE_FLAG_OFFSET
Definition: util-mpm.h:165
util-memcpy.h
MpmCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:126
MemcmpTest18Tests::result
int result
Definition: util-memcmp.c:313
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2878
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:333
MpmCtx_::maxlen
uint16_t maxlen
Definition: util-mpm.h:123
MpmTableElmt_::AddPattern
int(* AddPattern)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:190
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:206
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:3064
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:170
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:1304
util-unittest.h
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:1075
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:770
MpmTableElmt_::PrintCtx
void(* PrintCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:201
SCACTileOutputTable_
Definition: util-mpm-ac-ks.h:44
SCACStateQueueFree
void SCACStateQueueFree(StateQueue *q)
Definition: util-mpm-ac-queue.c:36
MPM_AC_KS
@ MPM_AC_KS
Definition: util-mpm.h:53
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:95
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:1306
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:66
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:3626
MPM_FEATURE_FLAG_DEPTH
#define MPM_FEATURE_FLAG_DEPTH
Definition: util-mpm.h:164
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:1138
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:3512
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:122
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:516
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:197
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:832
SCACTilePatternList_::sids
SigIntId * sids
Definition: util-mpm-ac-ks.h:41
MpmPattern_
Definition: util-mpm.h:70
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:200
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:1337
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:1390
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:2300
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:154
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:120
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:3871
MpmTableElmt_::AddPatternNocase
int(* AddPatternNocase)(struct MpmCtx_ *, const uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:191
FatalError
#define FatalError(...)
Definition: util-debug.h:517
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:997
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:193
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:172
SCFree
#define SCFree(p)
Definition: util-mem.h:61
MpmConfig_
Definition: util-mpm.h:105
detect-parse.h
MpmCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:125
MpmCtx_::init_hash
MpmPattern ** init_hash
Definition: util-mpm.h:131
SCACTileInitCtx
void SCACTileInitCtx(MpmCtx *)
Initialize the AC context.
Definition: util-mpm-ac-ks.c:932
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2839
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:1151
SCACTileDestroyCtx
void SCACTileDestroyCtx(MpmCtx *)
Destroy the mpm context.
Definition: util-mpm-ac-ks.c:1033
MpmTableElmt_::ConfigDeinit
void(* ConfigDeinit)(MpmConfig **)
Definition: util-mpm.h:176
suricata.h
MpmTableElmt_::ConfigCacheDirSet
void(* ConfigCacheDirSet)(MpmConfig *, const char *dir_path)
Definition: util-mpm.h:177
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:175
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:989
MpmTableElmt_::DestroyThreadCtx
void(* DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:173
MpmCtx_
Definition: util-mpm.h:111
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:112
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:1329
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:63
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
MpmTableElmt_::RegisterUnittests
void(* RegisterUnittests)(void)
Definition: util-mpm.h:204
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