suricata
util-hyperscan.c
Go to the documentation of this file.
1 /* Copyright (C) 2016 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 Justin Viiret <justin.viiret@intel.com>
22  *
23  * Support functions for Hyperscan library integration.
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 
29 #include "util-debug.h"
30 
31 #ifdef BUILD_HYPERSCAN
32 #include "util-hyperscan.h"
33 
34 /**
35  * \internal
36  * \brief Convert a pattern into a regex string accepted by the Hyperscan
37  * compiler.
38  *
39  * For simplicity, we just take each byte of the original pattern and render it
40  * with a hex escape (i.e. ' ' -> "\x20")/
41  */
42 char *HSRenderPattern(const uint8_t *pat, uint16_t pat_len)
43 {
44  if (pat == NULL) {
45  return NULL;
46  }
47  const size_t hex_len = (pat_len * 4) + 1;
48  char *str = SCCalloc(1, hex_len);
49  if (str == NULL) {
50  return NULL;
51  }
52  char *sp = str;
53  for (uint16_t i = 0; i < pat_len; i++) {
54  snprintf(sp, 5, "\\x%02x", pat[i]);
55  sp += 4;
56  }
57  *sp = '\0';
58  return str;
59 }
60 
61 void HSLogCompileError(const char *expr, hs_compile_error_t *compile_err, hs_error_t err)
62 {
63  if (compile_err) {
64  SCLogError("Unable to compile '%s' with Hyperscan, "
65  "returned %d."
66  "Hyperscan compile error: %s",
67  expr, err, compile_err->message);
68  hs_free_compile_error(compile_err);
69  } else {
70  SCLogError("Unable to compile '%s' with Hyperscan, "
71  "returned %d.",
72  expr, err);
73  }
74 }
75 
76 #endif /* BUILD_HYPERSCAN */
util-hyperscan.h
util-debug.h
suricata-common.h
str
#define str(s)
Definition: suricata-common.h:313
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
suricata.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53