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/* allow cppcheck to inspect this file directly */
29#ifdef CPPCHECK
30#ifndef FUNC_NAME
31#define SINDEX_INTERNAL(y, x, log_mult, width) ((1 << log_mult) * (x & ((1 << width) - 1)))
32#define STYPE int16_t
33#define SLOAD(x) *(STYPE *restrict)(x)
34#define FUNC_NAME SCACTileSearchSmall256
35#define SINDEX(y, x) SINDEX_INTERNAL(y, x, 8, 15)
36#endif
37#endif
38
39/* Only included into util-mpm-ac-ks.c, which defines FUNC_NAME
40 *
41 */
42#ifdef FUNC_NAME
43
44/* This function handles (ctx->state_count < 32767) */
45uint32_t FUNC_NAME(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thread_ctx,
46 PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
47{
48 uint32_t i = 0;
49 int matches = 0;
50
51 uint8_t mpm_bitarray[ctx->mpm_bitarray_size];
52 memset(mpm_bitarray, 0, ctx->mpm_bitarray_size);
53
54 const uint8_t* restrict xlate = ctx->translate_table;
55 STYPE *state_table = (STYPE*)ctx->state_table;
56 STYPE state = 0;
57 int c = xlate[buf[0]];
58 /* If buflen at least 4 bytes and buf 4-byte aligned. */
59 if (buflen >= (4 + EXTRA) && ((uintptr_t)buf & 0x3) == 0) {
60 BUF_TYPE data = *(BUF_TYPE* restrict)(&buf[0]);
61 uint64_t index = 0;
62 /* Process 4*floor(buflen/4) bytes. */
63 i = 0;
64 while ((i + EXTRA) < (buflen & ~0x3)) {
65 BUF_TYPE data1 = *(BUF_TYPE* restrict)(&buf[i + 4]);
66 index = SINDEX(index, state);
67 state = SLOAD(state_table + index + c);
68 c = xlate[BYTE1(data)];
69 if (unlikely(SCHECK(state))) {
70 matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
71 }
72 i++;
73 index = SINDEX(index, state);
74 state = SLOAD(state_table + index + c);
75 c = xlate[BYTE2(data)];
76 if (unlikely(SCHECK(state))) {
77 matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
78 }
79 i++;
80 index = SINDEX(index, state);
81 state = SLOAD(state_table + index + c);
82 c = xlate[BYTE3(data)];
83 if (unlikely(SCHECK(state))) {
84 matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
85 }
86 data = data1;
87 i++;
88 index = SINDEX(index, state);
89 state = SLOAD(state_table + index + c);
90 c = xlate[BYTE0(data)];
91 if (unlikely(SCHECK(state))) {
92 matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
93 }
94 i++;
95 }
96 }
97 /* Process buflen % 4 bytes. */
98 for (; i < buflen; i++) {
99 size_t index = 0 ;
100 index = SINDEX(index, state);
101 state = SLOAD(state_table + index + c);
102 if (likely(i+1 < buflen))
103 c = xlate[buf[i+1]];
104 if (unlikely(SCHECK(state))) {
105 matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray);
106 }
107 } /* for (i = 0; i < buflen; i++) */
108
109 return matches;
110}
111
112#endif /* FUNC_NAME */
struct Thresholds ctx
structure for storing potential rule matches
#define SCHECK(x)
#define STYPE
#define SLOAD(x)
#define BYTE2(x)
#define SINDEX(y, x)
#define BUF_TYPE
#define BYTE3(x)
#define BYTE0(x)
#define FUNC_NAME
#define EXTRA
#define BYTE1(x)
#define likely(expr)
#define unlikely(expr)