suricata
datasets-string.c
Go to the documentation of this file.
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-crypt.h" // encode base64
31 #include "util-base64.h" // decode base64
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 = out_size;
51  uint8_t encoded_data[str->len * 2];
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 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(void *s)
89 {
90  uint32_t hash = 5381;
91  int c;
92 
93  while ((c = *(char *)s++))
94  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
95 
96  return hash;
97 }
98 
99 // base data stays in hash
100 void StringFree(void *s)
101 {
102  StringType *str = s;
103  SCFree(str->ptr);
104 }
#define SCLogDebug(...)
Definition: util-debug.h:335
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
#define BUG_ON(x)
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
uint64_t offset
uint16_t src
#define str(s)
uint16_t dst
int StringSet(void *dst, void *src)
int StringAsBase64(const void *s, char *out, size_t out_size)
uint8_t * ptr
void PrintRawUriBuf(char *retbuf, uint32_t *offset, uint32_t retbuflen, uint8_t *buf, uint32_t buflen)
Definition: util-print.c:118
void StringFree(void *s)
#define SCMalloc(a)
Definition: util-mem.h:222
uint32_t len
#define SCFree(a)
Definition: util-mem.h:322
uint32_t StringHash(void *s)
bool StringCompare(void *a, void *b)
int Base64Encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
Definition: util-crypt.c:272
uint8_t len
DataRepType rep