OpenVAS Libraries  9.0.3
arglists.c
Go to the documentation of this file.
1 /* OpenVAS
2  * $Id$
3  * Description: Arglists management.
4  *
5  * Authors:
6  * Renaud Deraison <deraison@nessus.org> (Original pre-fork development)
7  *
8  * Copyright:
9  * Based on work Copyright (C) 1998 Renaud Deraison
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
26 #include <string.h>
27 #include <stdlib.h>
28 
29 #include <glib.h>
30 
31 #include "arglists.h"
32 #include "openvas_logging.h"
33 
34 #define HASH_MAX 2713
35 
42 static int
43 mkhash (const char *name)
44 {
45  return g_str_hash (name) % HASH_MAX;
46 }
47 
56 struct name_cache
57 {
58  char *name;
60  struct name_cache *next;
61  struct name_cache *prev;
62 };
63 
64 static int cache_inited = 0;
65 static struct name_cache cache[HASH_MAX + 1];
66 
67 
68 static void
69 cache_init ()
70 {
71  int i;
72  for (i = 0; i < HASH_MAX + 1; i++)
73  {
74  bzero (&(cache[i]), sizeof (cache[i]));
75  }
76  cache_inited = 1;
77 }
78 
79 static struct name_cache *
80 cache_get_name (const char *name, int h)
81 {
82  struct name_cache *nc;
83 
84  if (cache_inited == 0)
85  cache_init ();
86 
87  if (!name)
88  return NULL;
89 
90  nc = cache[h].next;
91 
92  while (nc != NULL)
93  {
94  if (nc->name != NULL && !strcmp (nc->name, name))
95  return nc;
96  else
97  nc = nc->next;
98  }
99  return NULL;
100 }
101 
102 static struct name_cache *
103 cache_add_name (const char *name, int h)
104 {
105  struct name_cache *nc;
106 
107  if (name == NULL)
108  return NULL;
109 
110  nc = g_malloc0 (sizeof (struct name_cache));
111  nc->next = cache[h].next;
112  nc->prev = NULL;
113  nc->name = g_strdup (name);
114  nc->occurences = 1;
115  if (cache[h].next != NULL)
116  cache[h].next->prev = nc;
117 
118  cache[h].next = nc;
119 
120  return nc;
121 }
122 
123 static char *
124 cache_inc (const char *name)
125 {
126  struct name_cache *nc;
127  int h = mkhash (name);
128  nc = cache_get_name (name, h);
129  if (nc != NULL)
130  nc->occurences++;
131  else
132  nc = cache_add_name (name, h);
133  return nc->name;
134 }
135 
136 
137 static void
138 cache_dec (const char *name)
139 {
140  struct name_cache *nc;
141  int h;
142 
143  if (!name)
144  return;
145 
146  h = mkhash (name);
147  nc = cache_get_name (name, h);
148  if (nc == NULL)
149  return;
150 
151  nc->occurences--;
152  if (nc->occurences == 0)
153  {
154  int h = mkhash (name);
155  g_free (nc->name);
156  nc->name = NULL;
157  if (nc->next != NULL)
158  nc->next->prev = nc->prev;
159 
160  if (nc->prev != NULL)
161  nc->prev->next = nc->next;
162  else
163  cache[h].next = nc->next;
164 
165  g_free (nc);
166  }
167 }
168 
169 void
170 arg_add_value (arglst, name, type, value)
171  struct arglist *arglst;
172  const char *name;
173  int type;
174  void *value;
175 {
176  if (!arglst)
177  return;
178  while (arglst->next)
179  arglst = arglst->next;
180 
181  arglst->name = cache_inc (name);
182  arglst->value = value;
183  arglst->type = type;
184  arglst->next = g_malloc0 (sizeof (struct arglist));
185  arglst->hash = mkhash (arglst->name);
186 }
187 
188 void
189 arg_prepend_value (struct arglist **list, const char *name, int type,
190  void *value)
191 {
192  struct arglist *new;
193 
194  if (!list || !*list)
195  return;
196 
197  new = g_malloc0 (sizeof (struct arglist));
198  new->name = cache_inc (name);
199  new->value = value;
200  new->type = type;
201  new->next = *list;
202  new->hash = mkhash (new->name);
203  *list = new;
204 }
205 
206 static struct arglist *
207 arg_get (struct arglist *arg, const char *name)
208 {
209  int h = mkhash (name);
210  if (arg == NULL)
211  return NULL;
212 
213  while (arg->next != NULL)
214  {
215  if (arg->hash == h && strcmp (arg->name, name) == 0)
216  return arg;
217  else
218  arg = arg->next;
219  }
220  return NULL;
221 }
222 
223 
224 int
226  struct arglist *arglst;
227  const char *name;
228  void *value;
229 {
230 
231  if (name == NULL)
232  return -1;
233 
234  arglst = arg_get (arglst, name);
235 
236  if (arglst != NULL)
237  {
238  arglst->value = value;
239  return 0;
240  }
241  else
242  return -1;
243 }
244 
245 int
246 arg_get_value_int (struct arglist *args, const char *name)
247 {
248  return GPOINTER_TO_SIZE (arg_get_value (args, name));
249 }
250 
251 void *
253  struct arglist *args;
254  const char *name;
255 {
256 
257  if (args == NULL)
258  return NULL;
259 
260  args = arg_get (args, name);
261  if (args == NULL)
262  return NULL;
263  else
264  return (args->value);
265 }
266 
267 int
269  struct arglist *args;
270  const char *name;
271 {
272  args = arg_get (args, name);
273  if (args != NULL)
274  return (args->type);
275  else
276  return -1;
277 }
278 
279 void
280 arg_dump (args, level)
281  struct arglist *args;
282  int level;
283 {
284  const char *spaces = "--------------------";
285  if (!args)
286  {
287  log_legacy_write ("Error ! args == NULL");
288  return;
289  }
290 
291  if (args)
292  while (args->next)
293  {
294  switch (args->type)
295  {
296  case ARG_STRING:
297 
298  log_legacy_write ("%sargs->%s : %s\n", spaces + (20 - level),
299  args->name, (char *) args->value);
300  break;
301  case ARG_ARGLIST:
302 
303  log_legacy_write ("%sargs->%s :\n", spaces + (20 - level),
304  args->name);
305  arg_dump (args->value, level + 1);
306  break;
307  case ARG_INT:
308  log_legacy_write ("%sargs->%s : %d\n", spaces + (20 - level),
309  args->name, (int) GPOINTER_TO_SIZE (args->value));
310  break;
311  default:
312  log_legacy_write ("%sargs->%s : %d\n", spaces + (20 - level),
313  args->name, (int) GPOINTER_TO_SIZE (args->value));
314  break;
315  }
316  args = args->next;
317  }
318 }
319 
320 
321 void
322 arg_free (arg)
323  struct arglist *arg;
324 {
325  while (arg)
326  {
327  struct arglist *next = arg->next;
328  cache_dec (arg->name);
329  g_free (arg);
330  arg = next;
331  }
332 }
333 
334 
335 void
337  struct arglist *arg;
338 {
339  while (arg)
340  {
341  struct arglist *next = arg->next;
342  switch (arg->type)
343  {
344  case ARG_ARGLIST:
345  arg_free_all (arg->value);
346  break;
347  case ARG_STRING:
348  g_free (arg->value);
349  break;
350  }
351  cache_dec (arg->name);
352  g_free (arg);
353  arg = next;
354  }
355 }
356 
357 void
359  struct arglist *args;
360  const char *name;
361 {
362  int h = mkhash (name);
363  struct arglist *pivot;
364  struct arglist *element = NULL;
365  struct arglist store;
366 
367  if (args == NULL)
368  return;
369 
370  pivot = args;
371 
372  while (pivot->next != NULL)
373  {
374  if (pivot->hash == h && strcmp (pivot->name, name) == 0)
375  {
376  element = pivot;
377  break;
378  }
379  pivot = pivot->next;
380  }
381 
382  if (!element || element->hash != h || strcmp (element->name, name))
383  return;
384 
385  if (args == element)
386  {
387  element = args->next;
388  memcpy (&store, element, sizeof (struct arglist));
389  memcpy (element, args, sizeof (struct arglist));
390  memcpy (args, &store, sizeof (struct arglist));
391  }
392  else
393  {
394  pivot = args;
395  while (pivot->next != NULL && pivot->next != element)
396  pivot = pivot->next;
397  pivot->next = element->next;
398  }
399  element->next = NULL;
400 
401  arg_free (element);
402 }
#define ARG_INT
Definition: arglists.h:40
int arg_set_value(struct arglist *arglst, const char *name, void *value)
Definition: arglists.c:225
void arg_prepend_value(struct arglist **list, const char *name, int type, void *value)
Definition: arglists.c:189
void arg_free(struct arglist *arg)
Definition: arglists.c:322
Struct to cache names (keys) of arglist entries.
Definition: arglists.c:56
struct name_cache * prev
Definition: arglists.c:61
void log_legacy_write(const char *format,...)
Legacy function to write a log message.
int type
Definition: arglists.h:34
void arg_free_all(struct arglist *arg)
Definition: arglists.c:336
void arg_dump(struct arglist *args, int level)
Definition: arglists.c:280
void * value
Definition: arglists.h:32
int occurences
Definition: arglists.c:59
void arg_add_value(struct arglist *arglst, const char *name, int type, void *value)
Definition: arglists.c:170
#define HASH_MAX
Definition: arglists.c:34
struct arglist * next
Definition: arglists.h:33
struct name_cache * next
Definition: arglists.c:60
const char * name
Definition: nasl_init.c:524
char * name
Definition: arglists.c:58
#define ARG_STRING
Definition: arglists.h:38
int hash
Definition: arglists.h:35
int arg_get_value_int(struct arglist *args, const char *name)
Definition: arglists.c:246
char * name
Definition: arglists.h:31
void arg_del_value(struct arglist *args, const char *name)
Definition: arglists.c:358
#define ARG_ARGLIST
Definition: arglists.h:41
int arg_get_type(struct arglist *args, const char *name)
Definition: arglists.c:268
void * arg_get_value(struct arglist *args, const char *name)
Definition: arglists.c:252