/src/suricata7/src/datasets-string.c
Line | Count | Source |
1 | | /* Copyright (C) 2017-2019 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 Victor Julien <victor@inliniac.net> |
22 | | */ |
23 | | |
24 | | #include "suricata-common.h" |
25 | | #include "conf.h" |
26 | | #include "datasets.h" |
27 | | #include "datasets-string.h" |
28 | | #include "util-thash.h" |
29 | | #include "util-print.h" |
30 | | #include "util-base64.h" // decode base64 |
31 | | #include "util-hash-lookup3.h" |
32 | | #include "rust.h" |
33 | | |
34 | | #if 0 |
35 | | static int StringAsAscii(const void *s, char *out, size_t out_size) |
36 | | { |
37 | | const StringType *str = s; |
38 | | uint32_t offset = 0; |
39 | | PrintRawUriBuf(out, &offset, out_size, str->ptr, str->len); |
40 | | if (out[0] == '\0') |
41 | | return 0; |
42 | | strlcat(out, "\n", out_size); |
43 | | return strlen(out); |
44 | | } |
45 | | #endif |
46 | | |
47 | | int StringAsBase64(const void *s, char *out, size_t out_size) |
48 | 0 | { |
49 | 0 | const StringType *str = s; |
50 | |
|
51 | 0 | unsigned long len = Base64EncodeBufferSize(str->len); |
52 | 0 | uint8_t encoded_data[len]; |
53 | 0 | if (Base64Encode((unsigned char *)str->ptr, str->len, |
54 | 0 | encoded_data, &len) != SC_BASE64_OK) |
55 | 0 | return 0; |
56 | | |
57 | 0 | strlcpy(out, (const char *)encoded_data, out_size); |
58 | 0 | strlcat(out, "\n", out_size); |
59 | 0 | return strlen(out); |
60 | 0 | } |
61 | | |
62 | | int StringSet(void *dst, void *src) |
63 | 28 | { |
64 | 28 | StringType *src_s = src; |
65 | 28 | StringType *dst_s = dst; |
66 | 28 | SCLogDebug("dst %p src %p, src_s->ptr %p src_s->len %u", dst, src, src_s->ptr, src_s->len); |
67 | | |
68 | 28 | dst_s->len = src_s->len; |
69 | 28 | dst_s->ptr = SCMalloc(dst_s->len); |
70 | 28 | BUG_ON(dst_s->ptr == NULL); |
71 | 28 | memcpy(dst_s->ptr, src_s->ptr, dst_s->len); |
72 | | |
73 | 28 | dst_s->rep = src_s->rep; |
74 | 28 | SCLogDebug("dst %p src %p, dst_s->ptr %p dst_s->len %u", dst, src, dst_s->ptr, dst_s->len); |
75 | 28 | return 0; |
76 | 28 | } |
77 | | |
78 | | bool StringCompare(void *a, void *b) |
79 | 1.93k | { |
80 | 1.93k | const StringType *as = a; |
81 | 1.93k | const StringType *bs = b; |
82 | | |
83 | 1.93k | if (as->len != bs->len) |
84 | 0 | return false; |
85 | | |
86 | 1.93k | return (memcmp(as->ptr, bs->ptr, as->len) == 0); |
87 | 1.93k | } |
88 | | |
89 | | uint32_t StringHash(uint32_t hash_seed, void *s) |
90 | 2.21k | { |
91 | 2.21k | StringType *str = s; |
92 | 2.21k | return hashlittle_safe(str->ptr, str->len, hash_seed); |
93 | 2.21k | } |
94 | | |
95 | | // base data stays in hash |
96 | | void StringFree(void *s) |
97 | 2.85M | { |
98 | 2.85M | StringType *str = s; |
99 | 2.85M | SCFree(str->ptr); |
100 | 2.85M | } |