/src/suricata8/src/util-detect.c
Line | Count | Source |
1 | | /* Copyright (C) 2017 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 Giuseppe Longo <glongo@stamus-networks.com> |
22 | | * |
23 | | * Detection engine helper functions |
24 | | */ |
25 | | |
26 | | #include "suricata-common.h" |
27 | | #include "suricata.h" |
28 | | #include "detect.h" |
29 | | #include "util-detect.h" |
30 | | |
31 | | /** |
32 | | * \brief Allocate SigString list member |
33 | | * |
34 | | * \retval Pointer to SigString |
35 | | */ |
36 | | SigString *SigStringAlloc(void) |
37 | 4.62M | { |
38 | 4.62M | SigString *sigstr = SCCalloc(1, sizeof(SigString)); |
39 | 4.62M | if (unlikely(sigstr == NULL)) |
40 | 0 | return NULL; |
41 | | |
42 | 4.62M | sigstr->line = 0; |
43 | | |
44 | 4.62M | return sigstr; |
45 | 4.62M | } |
46 | | |
47 | | /** |
48 | | * \brief Assigns the filename, signature, lineno to SigString list member |
49 | | * |
50 | | * \param sig pointer to SigString |
51 | | * \param sig_file filename that contains the signature |
52 | | * \param sig_str signature in string format |
53 | | * \param sig_error signature parsing error |
54 | | * \param line line line number |
55 | | * |
56 | | * \retval 1 on success 0 on failure |
57 | | */ |
58 | | static int SigStringAddSig(SigString *sig, const char *sig_file, |
59 | | const char *sig_str, const char *sig_error, |
60 | | int line) |
61 | 4.62M | { |
62 | 4.62M | if (sig_file == NULL || sig_str == NULL) { |
63 | 0 | return 0; |
64 | 0 | } |
65 | | |
66 | 4.62M | sig->filename = SCStrdup(sig_file); |
67 | 4.62M | if (sig->filename == NULL) { |
68 | 0 | SCLogError("Error allocating memory"); |
69 | 0 | return 0; |
70 | 0 | } |
71 | | |
72 | 4.62M | sig->sig_str = SCStrdup(sig_str); |
73 | 4.62M | if (sig->sig_str == NULL) { |
74 | 0 | SCLogError("Error allocating memory"); |
75 | 0 | SCFree(sig->filename); |
76 | 0 | return 0; |
77 | 0 | } |
78 | | |
79 | 4.62M | if (sig_error) { |
80 | 10.6k | sig->sig_error = SCStrdup(sig_error); |
81 | 10.6k | if (sig->sig_error == NULL) { |
82 | 0 | SCLogError("Error allocating memory"); |
83 | 0 | SCFree(sig->filename); |
84 | 0 | SCFree(sig->sig_str); |
85 | 0 | return 0; |
86 | 0 | } |
87 | 10.6k | } |
88 | | |
89 | 4.62M | sig->line = line; |
90 | | |
91 | 4.62M | return 1; |
92 | 4.62M | } |
93 | | |
94 | | /** |
95 | | * \brief Append a new list member to SigString list |
96 | | * |
97 | | * \param list pointer to the start of the SigString list |
98 | | * \param sig_file filename that contains the signature |
99 | | * \param sig_str signature in string format |
100 | | * \param line line line number |
101 | | * |
102 | | * \retval 1 on success 0 on failure |
103 | | */ |
104 | | int SigStringAppend(SigFileLoaderStat *sig_stats, const char *sig_file, |
105 | | const char *sig_str, const char *sig_error, int line) |
106 | 4.62M | { |
107 | 4.62M | SigString *item = SigStringAlloc(); |
108 | 4.62M | if (item == NULL) { |
109 | 0 | return 0; |
110 | 0 | } |
111 | | |
112 | 4.62M | if (!SigStringAddSig(item, sig_file, sig_str, sig_error, line)) { |
113 | 0 | SCFree(item); |
114 | 0 | return 0; |
115 | 0 | } |
116 | | |
117 | 4.62M | TAILQ_INSERT_TAIL(&sig_stats->failed_sigs, item, next); |
118 | | |
119 | 4.62M | return 1; |
120 | 4.62M | } |