suricata
datasets-string.c
Go to the documentation of this file.
1/* Copyright (C) 2017-2024 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-hash-lookup3.h"
31#include "rust.h"
32
33#if 0
34static int StringAsAscii(const void *s, char *out, size_t out_size)
35{
36 const StringType *str = s;
37 uint32_t offset = 0;
38 PrintRawUriBuf(out, &offset, out_size, str->ptr, str->len);
39 if (out[0] == '\0')
40 return 0;
41 strlcat(out, "\n", out_size);
42 return strlen(out);
43}
44#endif
45
46int StringAsBase64(const void *s, char *out, size_t out_size)
47{
48 const StringType *str = s;
49
50 unsigned long len = SCBase64EncodeBufferSize(str->len);
51 uint8_t encoded_data[len];
52 if (SCBase64Encode((unsigned char *)str->ptr, str->len, encoded_data, &len) != SC_BASE64_OK)
53 return 0;
54
55 strlcpy(out, (const char *)encoded_data, out_size);
56 strlcat(out, "\n", out_size);
57 return (int)strlen(out);
58}
59
60int StringSet(void *dst, void *src)
61{
62 StringType *src_s = src;
63 StringType *dst_s = dst;
64 SCLogDebug("dst %p src %p, src_s->ptr %p src_s->len %u", dst, src, src_s->ptr, src_s->len);
65
66 dst_s->len = src_s->len;
67 dst_s->ptr = SCMalloc(dst_s->len);
68 if (dst_s->ptr == NULL) {
69 SCLogError("Failed to allocate memory for string of length %u", dst_s->len);
70 return -1;
71 }
72 memcpy(dst_s->ptr, src_s->ptr, dst_s->len);
73
74 dst_s->rep = src_s->rep;
75 SCLogDebug("dst %p src %p, dst_s->ptr %p dst_s->len %u", dst, src, dst_s->ptr, dst_s->len);
76 return 0;
77}
78
79int StringJsonSet(void *dst, void *src)
80{
81 if (StringSet(dst, src) < 0)
82 return -1;
83
84 StringType *src_s = src;
85 StringType *dst_s = dst;
86
87 if (DatajsonCopyJson(&dst_s->json, &src_s->json) < 0) {
88 SCFree(dst_s->ptr);
89 return -1;
90 }
91
92 return 0;
93}
94
95bool StringCompare(void *a, void *b)
96{
97 const StringType *as = a;
98 const StringType *bs = b;
99
100 if (as->len != bs->len)
101 return false;
102
103 return (memcmp(as->ptr, bs->ptr, as->len) == 0);
104}
105
106uint32_t StringHash(uint32_t hash_seed, void *s)
107{
108 StringType *str = s;
109 return hashlittle_safe(str->ptr, str->len, hash_seed);
110}
111
112uint32_t StringGetLength(void *s)
113{
114 StringType *str = s;
115 return str->len;
116}
117
118// base data stays in hash
119void StringFree(void *s)
120{
121 StringType *str = s;
122 SCFree(str->ptr);
123}
124
125void StringJsonFree(void *s)
126{
127 StringType *str = s;
128 SCFree(str->ptr);
129 if (str->json.value) {
130 SCFree(str->json.value);
131 }
132}
133
134uint32_t StringJsonGetLength(void *s)
135{
136 StringType *str = s;
137 return str->json.len + str->len;
138}
uint8_t len
uint16_t dst
uint16_t src
int DatajsonCopyJson(DataJsonType *dst, DataJsonType *src)
void StringJsonFree(void *s)
int StringJsonSet(void *dst, void *src)
uint32_t StringGetLength(void *s)
uint32_t StringJsonGetLength(void *s)
bool StringCompare(void *a, void *b)
int StringAsBase64(const void *s, char *out, size_t out_size)
uint32_t StringHash(uint32_t hash_seed, void *s)
void StringFree(void *s)
int StringSet(void *dst, void *src)
DataRepType rep
DataJsonType json
uint8_t * ptr
size_t strlcat(char *, const char *src, size_t siz)
#define str(s)
size_t strlcpy(char *dst, const char *src, size_t siz)
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
uint32_t hashlittle_safe(const void *key, size_t length, uint32_t initval)
#define SCMalloc(sz)
Definition util-mem.h:47
#define SCFree(p)
Definition util-mem.h:61
void PrintRawUriBuf(char *retbuf, uint32_t *offset, uint32_t retbuflen, const uint8_t *buf, size_t buflen)
Definition util-print.c:93
uint64_t offset