1 /* Copyright (C) 2019-2020 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: AGPL-3.0-or-later
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as
7  * published by the Free Software Foundation, either version 3 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /**
20  * @file
21  * @brief GVM manage layer: Preference utils.
22  */
23 
24 #include "manage_preferences.h"
25 
26 #include <stdlib.h>
27 
28 /**
29  * @brief Create a new preference.
30  *
31  * @param[in]  id        ID of preference.
32  * @param[in]  name      Name of preference.
33  * @param[in]  type      Type of preference.
34  * @param[in]  value     Value of preference.
35  * @param[in]  nvt_name  Name of NVT of preference.
36  * @param[in]  nvt_oid   OID of NVT of preference.
37  * @param[in]  alts      Array of gchar's.  Alternative values for type radio.
38  * @param[in]  default_value   Default value of preference.
39  * @param[in]  hr_name   Extended, more human-readable name of the preference.
40  * @param[in]  free_strings Whether string fields are freed by preference_free.
41  *
42  * @return Newly allocated preference.
43  */
44 gpointer
preference_new(char * id,char * name,char * type,char * value,char * nvt_name,char * nvt_oid,array_t * alts,char * default_value,char * hr_name,int free_strings)45 preference_new (char *id, char *name, char *type, char *value, char *nvt_name,
46                 char *nvt_oid, array_t *alts, char* default_value,
47                 char *hr_name, int free_strings)
48 {
49   preference_t *preference;
50 
51   preference = (preference_t*) g_malloc0 (sizeof (preference_t));
52   preference->id = id;
53   preference->name = name;
54   preference->type = type;
55   preference->value = value;
56   preference->nvt_name = nvt_name;
57   preference->nvt_oid = nvt_oid;
58   preference->alts = alts;
59   preference->default_value = default_value;
60   preference->hr_name = hr_name;
61   preference->free_strings = free_strings;
62 
63   return preference;
64 }
65 
66 /**
67  * @brief Frees a preference including its assigned values.
68  *
69  * @param[in]  preference  The preference to free.
70  */
71 void
preference_free(preference_t * preference)72 preference_free (preference_t *preference)
73 {
74   if (preference == NULL)
75     return;
76 
77   if (preference->alts)
78     g_ptr_array_free (preference->alts, TRUE);
79   if (preference->free_strings)
80     {
81       free (preference->id);
82       free (preference->name);
83       free (preference->type);
84       free (preference->value);
85       free (preference->nvt_name);
86       free (preference->nvt_oid);
87       free (preference->default_value);
88       free (preference->hr_name);
89     }
90 
91   g_free (preference);
92 }
93 
94 /**
95  * @brief Cleanup preferences array.
96  *
97  * @param[in]  import_preferences  Import preferences.
98  */
99 void
cleanup_import_preferences(array_t * import_preferences)100 cleanup_import_preferences (array_t *import_preferences)
101 {
102   if (import_preferences)
103     {
104       guint index;
105 
106       for (index = 0; index < import_preferences->len; index++)
107         {
108           preference_t *pref;
109           pref = (preference_t*) g_ptr_array_index (import_preferences,
110                                                     index);
111           if (pref)
112             preference_free (pref);
113         }
114       g_ptr_array_free (import_preferences, TRUE);
115     }
116 }
117