1 /****************************************************************************
2 *
3 * Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
4 * Copyright (C) 2009-2013 Sourcefire, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 as
8 * published by the Free Software Foundation. You may not use, modify or
9 * distribute this program under any other version of the GNU General
10 * Public License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 ****************************************************************************/
22
23 #include <stdlib.h>
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "strvec.h"
29 #include "util.h"
30
31 typedef struct {
32 char** v;
33 unsigned n;
34 } StringVector;
35
StringVector_New(void)36 void* StringVector_New (void)
37 {
38 StringVector* sv = SnortAlloc(sizeof(*sv));
39 sv->v = SnortAlloc(sizeof(*sv->v));
40 sv->n = 0;
41 return sv;
42 }
43
StringVector_Delete(void * pv)44 void StringVector_Delete (void* pv)
45 {
46 unsigned i;
47 StringVector* sv = (StringVector*)pv;
48
49 if ( !sv )
50 return;
51
52 for ( i = 0; i < sv->n; i++ )
53 free(sv->v[i]);
54
55 free(sv->v);
56 free(sv);
57 }
58
StringVector_Add(void * pv,const char * s)59 int StringVector_Add (void* pv, const char* s)
60 {
61 StringVector* sv = (StringVector*)pv;
62 char** v;
63
64 if ( !sv || !s )
65 return 0;
66
67 v = realloc(sv->v, (sv->n+2) * sizeof(char*));
68
69 if ( !v )
70 return 0;
71
72 sv->v = v;
73 sv->v[sv->n++] = SnortStrdup(s);
74 sv->v[sv->n] = NULL;
75
76 return 1;
77 }
78
StringVector_Get(void * pv,unsigned index)79 char* StringVector_Get (void* pv, unsigned index)
80 {
81 StringVector* sv = (StringVector*)pv;
82
83 if ( !sv || index >= sv->n )
84 return NULL;
85
86 return sv->v[index];
87 }
88
StringVector_AddVector(void * pd,void * ps)89 int StringVector_AddVector (void* pd, void* ps)
90 {
91 unsigned i = 0;
92 const char* s = StringVector_Get(ps, i++);
93
94 while ( s )
95 {
96 if ( !StringVector_Add(pd, s) )
97 return 0;
98
99 s = StringVector_Get(ps, i++);
100 }
101 return 1;
102 }
103
StringVector_GetVector(void * pv)104 const char** StringVector_GetVector (void* pv)
105 {
106 StringVector* sv = (StringVector*)pv;
107
108 if ( !sv )
109 return NULL;
110
111 return (const char**)sv->v;
112 }
113
114