suricata
util-mpm-ac-ks-small.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 
23  * Included by util-mpm-ac-ks.c with different SLOAD, SINDEX and
24  * FUNC_NAME
25  *
26  */
27 
28 /* Only included into util-mpm-ac-ks.c, which defines FUNC_NAME
29  *
30  */
31 #ifdef FUNC_NAME
32 
33 /* This function handles (ctx->state_count < 32767) */
34 uint32_t FUNC_NAME(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
35  PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
36 {
37  uint32_t i = 0;
38  int matches = 0;
39 
40  uint8_t mpm_bitarray[ctx->mpm_bitarray_size];
41  memset(mpm_bitarray, 0, ctx->mpm_bitarray_size);
42 
43  const uint8_t* restrict xlate = ctx->translate_table;
44  STYPE *state_table = (STYPE*)ctx->state_table;
45  STYPE state = 0;
46  int c = xlate[buf[0]];
47  /* If buflen at least 4 bytes and buf 4-byte aligned. */
48  if (buflen >= (4 + EXTRA) && ((uintptr_t)buf & 0x3) == 0) {
49  BUF_TYPE data = *(BUF_TYPE* restrict)(&buf[0]);
50  uint64_t index = 0;
51  /* Process 4*floor(buflen/4) bytes. */
52  i = 0;
53  while ((i + EXTRA) < (buflen & ~0x3)) {
54  BUF_TYPE data1 = *(BUF_TYPE* restrict)(&buf[i + 4]);
55  index = SINDEX(index, state);
56  state = SLOAD(state_table + index + c);
57  c = xlate[BYTE1(data)];
58  if (unlikely(SCHECK(state))) {
59  matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
60  }
61  i++;
62  index = SINDEX(index, state);
63  state = SLOAD(state_table + index + c);
64  c = xlate[BYTE2(data)];
65  if (unlikely(SCHECK(state))) {
66  matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
67  }
68  i++;
69  index = SINDEX(index, state);
70  state = SLOAD(state_table + index + c);
71  c = xlate[BYTE3(data)];
72  if (unlikely(SCHECK(state))) {
73  matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
74  }
75  data = data1;
76  i++;
77  index = SINDEX(index, state);
78  state = SLOAD(state_table + index + c);
79  c = xlate[BYTE0(data)];
80  if (unlikely(SCHECK(state))) {
81  matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
82  }
83  i++;
84  }
85  }
86  /* Process buflen % 4 bytes. */
87  for (; i < buflen; i++) {
88  size_t index = 0 ;
89  index = SINDEX(index, state);
90  state = SLOAD(state_table + index + c);
91  if (likely(i+1 < buflen))
92  c = xlate[buf[i+1]];
93  if (unlikely(SCHECK(state))) {
94  matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
95  }
96  } /* for (i = 0; i < buflen; i++) */
97 
98  return matches;
99 }
100 
101 #endif /* FUNC_NAME */
SCACTileSearchCtx_
Definition: util-mpm-ac-ks.h:115
MpmThreadCtx_
Definition: util-mpm.h:46
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
PrefilterRuleStore_
structure for storing potential rule matches
Definition: util-prefilter.h:34
SLOAD
#define SLOAD(x)
Definition: util-mpm-ac-ks.c:1219
ctx
struct Thresholds ctx
BYTE0
#define BYTE0(x)
Definition: util-mpm-ac-ks.c:1110
SCHECK
#define SCHECK(x)
Definition: util-mpm-ac-ks.c:1107
BYTE3
#define BYTE3(x)
Definition: util-mpm-ac-ks.c:1113
SINDEX
#define SINDEX(y, x)
Definition: util-mpm-ac-ks.c:1317
BYTE2
#define BYTE2(x)
Definition: util-mpm-ac-ks.c:1112
BUF_TYPE
#define BUF_TYPE
Definition: util-mpm-ac-ks.c:1108
BYTE1
#define BYTE1(x)
Definition: util-mpm-ac-ks.c:1111
STYPE
#define STYPE
Definition: util-mpm-ac-ks.c:1271
likely
#define likely(expr)
Definition: util-optimize.h:32
EXTRA
#define EXTRA
Definition: util-mpm-ac-ks.c:1114
FUNC_NAME
#define FUNC_NAME
Definition: util-mpm-ac-ks.c:1315