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
34 static 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 
46 int 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 
60 int 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  BUG_ON(dst_s->ptr == NULL);
69  memcpy(dst_s->ptr, src_s->ptr, dst_s->len);
70 
71  dst_s->rep = src_s->rep;
72  SCLogDebug("dst %p src %p, dst_s->ptr %p dst_s->len %u", dst, src, dst_s->ptr, dst_s->len);
73  return 0;
74 }
75 
76 bool StringCompare(void *a, void *b)
77 {
78  const StringType *as = a;
79  const StringType *bs = b;
80 
81  if (as->len != bs->len)
82  return false;
83 
84  return (memcmp(as->ptr, bs->ptr, as->len) == 0);
85 }
86 
87 uint32_t StringHash(uint32_t hash_seed, void *s)
88 {
89  StringType *str = s;
90  return hashlittle_safe(str->ptr, str->len, hash_seed);
91 }
92 
93 uint32_t StringGetLength(void *s)
94 {
95  StringType *str = s;
96  return str->len;
97 }
98 
99 // base data stays in hash
100 void StringFree(void *s)
101 {
102  StringType *str = s;
103  SCFree(str->ptr);
104 }
StringType::rep
DataRepType rep
Definition: datasets-string.h:31
len
uint8_t len
Definition: app-layer-dnp3.h:2
datasets-string.h
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
PrintRawUriBuf
void PrintRawUriBuf(char *retbuf, uint32_t *offset, uint32_t retbuflen, const uint8_t *buf, size_t buflen)
Definition: util-print.c:93
rust.h
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
StringSet
int StringSet(void *dst, void *src)
Definition: datasets-string.c:60
datasets.h
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
StringAsBase64
int StringAsBase64(const void *s, char *out, size_t out_size)
Definition: datasets-string.c:46
util-print.h
StringGetLength
uint32_t StringGetLength(void *s)
Definition: datasets-string.c:93
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:309
hashlittle_safe
uint32_t hashlittle_safe(const void *key, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:482
StringType
Definition: datasets-string.h:29
conf.h
StringType::ptr
uint8_t * ptr
Definition: datasets-string.h:32
StringType::len
uint32_t len
Definition: datasets-string.h:30
suricata-common.h
StringCompare
bool StringCompare(void *a, void *b)
Definition: datasets-string.c:76
util-hash-lookup3.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
str
#define str(s)
Definition: suricata-common.h:300
SCFree
#define SCFree(p)
Definition: util-mem.h:61
StringHash
uint32_t StringHash(uint32_t hash_seed, void *s)
Definition: datasets-string.c:87
src
uint16_t src
Definition: app-layer-dnp3.h:5
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
util-thash.h
StringFree
void StringFree(void *s)
Definition: datasets-string.c:100