suricata
util-mpm-hs-core.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2024 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 Jim Xu <jim.xu@windriver.com>
22  * \author Justin Viiret <justin.viiret@intel.com>
23  * \author Lukas Sismis <lsismis@oisf.net>
24  *
25  * MPM pattern matcher core function for the Hyperscan regex matcher.
26  */
27 
28 #include "suricata-common.h"
29 #include "suricata.h"
30 #include "util-mpm-hs-core.h"
31 
32 #ifdef BUILD_HYPERSCAN
33 
34 #include <hs.h>
35 
36 // Encode major, minor, and patch into a single 32-bit integer.
37 #define HS_VERSION_ENCODE(major, minor, patch) (((major) << 24) | ((minor) << 16) | ((patch) << 8))
38 #define HS_VERSION_AT_LEAST(major, minor, patch) \
39  (HS_VERSION_32BIT >= HS_VERSION_ENCODE(major, minor, patch))
40 
41 /**
42  * Translates Hyperscan error codes to human-readable messages.
43  *
44  * \param error_code
45  * The error code returned by a Hyperscan function.
46  * \return
47  * A string describing the error.
48  */
49 const char *HSErrorToStr(hs_error_t error_code)
50 {
51  switch (error_code) {
52  case HS_SUCCESS:
53  return "HS_SUCCESS: The engine completed normally";
54  case HS_INVALID:
55  return "HS_INVALID: A parameter passed to this function was invalid";
56  case HS_NOMEM:
57  return "HS_NOMEM: A memory allocation failed";
58  case HS_SCAN_TERMINATED:
59  return "HS_SCAN_TERMINATED: The engine was terminated by callback";
60  case HS_COMPILER_ERROR:
61  return "HS_COMPILER_ERROR: The pattern compiler failed";
62  case HS_DB_VERSION_ERROR:
63  return "HS_DB_VERSION_ERROR: The given database was built for a different version of "
64  "Hyperscan";
65  case HS_DB_PLATFORM_ERROR:
66  return "HS_DB_PLATFORM_ERROR: The given database was built for a different platform "
67  "(i.e., CPU type)";
68  case HS_DB_MODE_ERROR:
69  return "HS_DB_MODE_ERROR: The given database was built for a different mode of "
70  "operation";
71  case HS_BAD_ALIGN:
72  return "HS_BAD_ALIGN: A parameter passed to this function was not correctly aligned";
73  case HS_BAD_ALLOC:
74  return "HS_BAD_ALLOC: The memory allocator did not return correctly aligned memory";
75  case HS_SCRATCH_IN_USE:
76  return "HS_SCRATCH_IN_USE: The scratch region was already in use";
77 #if HS_VERSION_AT_LEAST(4, 4, 0)
78  case HS_ARCH_ERROR:
79  return "HS_ARCH_ERROR: Unsupported CPU architecture";
80 #endif // HS_VERSION_AT_LEAST(4, 4, 0)
81 #if HS_VERSION_AT_LEAST(4, 6, 0)
82  case HS_INSUFFICIENT_SPACE:
83  return "HS_INSUFFICIENT_SPACE: Provided buffer was too small";
84 #endif // HS_VERSION_AT_LEAST(4, 6, 0)
85 #if HS_VERSION_AT_LEAST(5, 1, 1)
86  case HS_UNKNOWN_ERROR:
87  return "HS_UNKNOWN_ERROR: Unexpected internal error";
88 #endif // HS_VERSION_AT_LEAST(5, 1, 1)
89  default:
90  return "Unknown error code";
91  }
92 }
93 
94 #endif /* BUILD_HYPERSCAN */
suricata-common.h
util-mpm-hs-core.h
suricata.h