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