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 = Base64EncodeBufferSize(str->len);
51  uint8_t encoded_data[len];
52  if (Base64Encode((unsigned char *)str->ptr, str->len,
53  encoded_data, &len) != SC_BASE64_OK)
54  return 0;
55 
56  strlcpy(out, (const char *)encoded_data, out_size);
57  strlcat(out, "\n", out_size);
58  return (int)strlen(out);
59 }
60 
61 int StringSet(void *dst, void *src)
62 {
63  StringType *src_s = src;
64  StringType *dst_s = dst;
65  SCLogDebug("dst %p src %p, src_s->ptr %p src_s->len %u", dst, src, src_s->ptr, src_s->len);
66 
67  dst_s->len = src_s->len;
68  dst_s->ptr = SCMalloc(dst_s->len);
69  BUG_ON(dst_s->ptr == NULL);
70  memcpy(dst_s->ptr, src_s->ptr, dst_s->len);
71 
72  dst_s->rep = src_s->rep;
73  SCLogDebug("dst %p src %p, dst_s->ptr %p dst_s->len %u", dst, src, dst_s->ptr, dst_s->len);
74  return 0;
75 }
76 
77 bool StringCompare(void *a, void *b)
78 {
79  const StringType *as = a;
80  const StringType *bs = b;
81 
82  if (as->len != bs->len)
83  return false;
84 
85  return (memcmp(as->ptr, bs->ptr, as->len) == 0);
86 }
87 
88 uint32_t StringHash(uint32_t hash_seed, void *s)
89 {
90  StringType *str = s;
91  return hashlittle_safe(str->ptr, str->len, hash_seed);
92 }
93 
94 uint32_t StringGetLength(void *s)
95 {
96  StringType *str = s;
97  return str->len;
98 }
99 
100 // base data stays in hash
101 void StringFree(void *s)
102 {
103  StringType *str = s;
104  SCFree(str->ptr);
105 }
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
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:61
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:94
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
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
PrintRawUriBuf
void PrintRawUriBuf(char *retbuf, uint32_t *offset, uint32_t retbuflen, uint8_t *buf, size_t buflen)
Definition: util-print.c:93
StringType::len
uint32_t len
Definition: datasets-string.h:30
suricata-common.h
StringCompare
bool StringCompare(void *a, void *b)
Definition: datasets-string.c:77
util-hash-lookup3.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
str
#define str(s)
Definition: suricata-common.h:291
SCFree
#define SCFree(p)
Definition: util-mem.h:61
StringHash
uint32_t StringHash(uint32_t hash_seed, void *s)
Definition: datasets-string.c:88
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:101