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 #ifdef BUILD_HYPERSCAN
30 #include "util-hyperscan.h"
31 
32 /**
33  * \internal
34  * \brief Convert a pattern into a regex string accepted by the Hyperscan
35  * compiler.
36  *
37  * For simplicity, we just take each byte of the original pattern and render it
38  * with a hex escape (i.e. ' ' -> "\x20")/
39  */
40 char *HSRenderPattern(const uint8_t *pat, uint16_t pat_len)
41 {
42  if (pat == NULL) {
43  return NULL;
44  }
45  const size_t hex_len = (pat_len * 4) + 1;
46  char *str = SCMalloc(hex_len);
47  if (str == NULL) {
48  return NULL;
49  }
50  memset(str, 0, hex_len);
51  char *sp = str;
52  for (uint16_t i = 0; i < pat_len; i++) {
53  snprintf(sp, 5, "\\x%02x", pat[i]);
54  sp += 4;
55  }
56  *sp = '\0';
57  return str;
58 }
59 
60 #endif /* BUILD_HYPERSCAN */
char * HSRenderPattern(const uint8_t *pat, uint16_t pat_len)
#define str(s)
#define SCMalloc(a)
Definition: util-mem.h:222