util-memrchr.h \
util-misc.h \
util-mpm-ac-bs.h \
+ util-mpm-ac-queue.h \
util-mpm-ac.h \
util-mpm-ac-ks.h \
util-mpm.h \
util-memrchr.c \
util-misc.c \
util-mpm-ac-bs.c \
+ util-mpm-ac-queue.c \
util-mpm-ac.c \
util-mpm-ac-ks.c \
util-mpm-ac-ks-small.c \
#include "util-memcpy.h"
#include "util-validate.h"
#include "util-mpm-ac-ks.h"
+#include "util-mpm-ac-queue.h"
#if __BYTE_ORDER == __LITTLE_ENDIAN
/* a placeholder to denote a failure transition in the goto table */
#define SC_AC_TILE_FAIL (-1)
-#define STATE_QUEUE_CONTAINER_SIZE 65536
-
-/**
- * \brief Helper structure used by AC during state table creation
- */
-typedef struct StateQueue_ {
- int32_t store[STATE_QUEUE_CONTAINER_SIZE];
- int top;
- int bot;
-} StateQueue;
-
/**
* \internal
* \brief Initialize the AC context with user specified conf parameters. We
}
}
-static inline int SCACStateQueueIsEmpty(StateQueue *q)
-{
- if (q->top == q->bot)
- return 1;
- else
- return 0;
-}
-
-static inline void SCACEnqueue(StateQueue *q, int32_t state)
-{
- int i = 0;
-
- /*if we already have this */
- for (i = q->bot; i < q->top; i++) {
- if (q->store[i] == state)
- return;
- }
-
- q->store[q->top++] = state;
-
- if (q->top == STATE_QUEUE_CONTAINER_SIZE)
- q->top = 0;
-
- if (q->top == q->bot) {
- FatalError("Just ran out of space in the queue. "
- "Fatal Error. Exiting. Please file a bug report on this");
- }
-}
-
-static inline int32_t SCACDequeue(StateQueue *q)
-{
- if (q->bot == STATE_QUEUE_CONTAINER_SIZE)
- q->bot = 0;
-
- if (q->bot == q->top) {
- FatalError("StateQueue behaving weirdly. "
- "Fatal Error. Exiting. Please file a bug report on this");
- }
-
- return q->store[q->bot++];
-}
-
/**
* \internal
* \brief Club the output data from 2 states and store it in the 1st state.
int32_t state = 0;
int32_t r_state = 0;
- StateQueue *q = SCCalloc(1, sizeof(StateQueue));
- if (q == NULL) {
- FatalError("Error allocating memory");
- }
+ StateQueue *q = SCACStateQueueAlloc();
/* Allocate space for the failure table. A failure entry in the table for
* every state(SCACTileCtx->state_count) */
mpm_ctx);
}
}
- SCFree(q);
+ SCACStateQueueFree(q);
}
/*
ctx->alphabet_storage = 256; /* Change? */
}
- StateQueue *q = SCCalloc(1, sizeof(StateQueue));
- if (q == NULL) {
- FatalError("Error allocating memory");
- }
+ StateQueue *q = SCACStateQueueAlloc();
for (aa = 0; aa < ctx->alphabet_size; aa++) {
int temp_state = ctx->goto_table[0][aa];
}
}
}
- SCFree(q);
+ SCACStateQueueFree(q);
}
static void SCACTileClubOutputStatePresenceWithDeltaTable(MpmCtx *mpm_ctx)
--- /dev/null
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "suricata-common.h"
+#include "util-debug.h"
+#include "util-mpm-ac-queue.h"
+
+StateQueue *SCACStateQueueAlloc(void)
+{
+ StateQueue *q = SCCalloc(1, sizeof(StateQueue));
+ if (q == NULL) {
+ FatalError("Error allocating memory");
+ }
+ return q;
+}
+
+void SCACStateQueueFree(StateQueue *q)
+{
+ SCFree(q);
+}
--- /dev/null
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Anoop Saldanha <anoopsaldanha@gmail.com>
+ *
+ */
+
+#ifndef SURICATA_UTIL_MPM_AC_QUEUE_H
+#define SURICATA_UTIL_MPM_AC_QUEUE_H
+
+#define STATE_QUEUE_CONTAINER_SIZE 65536
+
+/**
+ * \brief Helper structure used by AC during state table creation
+ */
+typedef struct StateQueue_ {
+ int32_t store[STATE_QUEUE_CONTAINER_SIZE];
+ int top;
+ int bot;
+} StateQueue;
+
+StateQueue *SCACStateQueueAlloc(void);
+void SCACStateQueueFree(StateQueue *q);
+
+static inline int SCACStateQueueIsEmpty(StateQueue *q)
+{
+ if (q->top == q->bot)
+ return 1;
+ else
+ return 0;
+}
+
+static inline void SCACEnqueue(StateQueue *q, int32_t state)
+{
+ int i = 0;
+
+ /*if we already have this */
+ for (i = q->bot; i < q->top; i++) {
+ if (q->store[i] == state)
+ return;
+ }
+
+ q->store[q->top++] = state;
+
+ if (q->top == STATE_QUEUE_CONTAINER_SIZE)
+ q->top = 0;
+
+ if (q->top == q->bot) {
+ FatalError("Just ran out of space in the queue. "
+ "Fatal Error. Exiting. Please file a bug report on this");
+ }
+}
+
+static inline int32_t SCACDequeue(StateQueue *q)
+{
+ if (q->bot == STATE_QUEUE_CONTAINER_SIZE)
+ q->bot = 0;
+
+ if (q->bot == q->top) {
+ FatalError("StateQueue behaving weirdly. "
+ "Fatal Error. Exiting. Please file a bug report on this");
+ }
+
+ return q->store[q->bot++];
+}
+
+#endif /* SURICATA_UTIL_MPM_AC_QUEUE_H */
#include "util-mpm-ac.h"
#include "util-memcpy.h"
#include "util-validate.h"
+#include "util-mpm-ac-queue.h"
void SCACInitCtx(MpmCtx *);
void SCACInitThreadCtx(MpmCtx *, MpmThreadCtx *);
/* a placeholder to denote a failure transition in the goto table */
#define SC_AC_FAIL (-1)
-#define STATE_QUEUE_CONTAINER_SIZE 65536
-
#define AC_CASE_MASK 0x80000000
#define AC_PID_MASK 0x7FFFFFFF
#define AC_CASE_BIT 31
static int construct_both_16_and_32_state_tables = 0;
-/**
- * \brief Helper structure used by AC during state table creation
- */
-typedef struct StateQueue_ {
- int32_t store[STATE_QUEUE_CONTAINER_SIZE];
- int top;
- int bot;
-} StateQueue;
-
/**
* \internal
* \brief Initialize the AC context with user specified conf parameters. We
return;
}
-static inline int SCACStateQueueIsEmpty(StateQueue *q)
-{
- if (q->top == q->bot)
- return 1;
- else
- return 0;
-}
-
-static inline void SCACEnqueue(StateQueue *q, int32_t state)
-{
- int i = 0;
-
- /*if we already have this */
- for (i = q->bot; i < q->top; i++) {
- if (q->store[i] == state)
- return;
- }
-
- q->store[q->top++] = state;
-
- if (q->top == STATE_QUEUE_CONTAINER_SIZE)
- q->top = 0;
-
- if (q->top == q->bot) {
- FatalError("Just ran out of space in the queue. Please file a bug report on this");
- }
-
- return;
-}
-
-static inline int32_t SCACDequeue(StateQueue *q)
-{
- if (q->bot == STATE_QUEUE_CONTAINER_SIZE)
- q->bot = 0;
-
- if (q->bot == q->top) {
- FatalError("StateQueue behaving weirdly. Please file a bug report on this");
- }
-
- return q->store[q->bot++];
-}
-
/**
* \internal
* \brief Club the output data from 2 states and store it in the 1st state.
int32_t state = 0;
int32_t r_state = 0;
- StateQueue *q = SCCalloc(1, sizeof(StateQueue));
- if (q == NULL) {
- FatalError("Error allocating memory");
- }
+ StateQueue *q = SCACStateQueueAlloc();
/* allot space for the failure table. A failure entry in the table for
* every state(SCACCtx->state_count) */
mpm_ctx);
}
}
- SCFree(q);
-
- return;
+ SCACStateQueueFree(q);
}
/**
mpm_ctx->memory_cnt++;
mpm_ctx->memory_size += (ctx->state_count * sizeof(*ctx->state_table_u16));
- StateQueue *q = SCCalloc(1, sizeof(StateQueue));
- if (q == NULL) {
- FatalError("Error allocating memory");
- }
+ StateQueue *q = SCACStateQueueAlloc();
for (ascii_code = 0; ascii_code < 256; ascii_code++) {
DEBUG_VALIDATE_BUG_ON(ctx->goto_table[0][ascii_code] > UINT16_MAX);
}
}
}
- SCFree(q);
+ SCACStateQueueFree(q);
}
if (!(ctx->state_count < 32767) || construct_both_16_and_32_state_tables) {
mpm_ctx->memory_cnt++;
mpm_ctx->memory_size += (ctx->state_count * sizeof(*ctx->state_table_u32));
- StateQueue *q = SCCalloc(1, sizeof(StateQueue));
- if (q == NULL) {
- FatalError("Error allocating memory");
- }
+ StateQueue *q = SCACStateQueueAlloc();
for (ascii_code = 0; ascii_code < 256; ascii_code++) {
SC_AC_STATE_TYPE_U32 temp_state = ctx->goto_table[0][ascii_code];
}
}
}
- SCFree(q);
+ SCACStateQueueFree(q);
}
return;