use NULL value in load_path_suffix to NOT load any files
[oweals/gnunet.git] / src / reclaim-attribute / plugin_reclaim_attribute_gnuid.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013, 2014, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      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      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file reclaim-attribute/plugin_reclaim_attribute_gnuid.c
23  * @brief reclaim-attribute-plugin-gnuid attribute plugin to provide the API for
24  *                                       fundamental
25  *                                       attribute types.
26  *
27  * @author Martin Schanzenbach
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_reclaim_attribute_plugin.h"
32 #include <inttypes.h>
33
34
35 /**
36  * Convert the 'value' of an attribute to a string.
37  *
38  * @param cls closure, unused
39  * @param type type of the attribute
40  * @param data value in binary encoding
41  * @param data_size number of bytes in @a data
42  * @return NULL on error, otherwise human-readable representation of the value
43  */
44 static char *
45 gnuid_value_to_string (void *cls,
46                        uint32_t type,
47                        const void *data,
48                        size_t data_size)
49 {
50   switch (type)
51   {
52   case GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING:
53     return GNUNET_strndup (data, data_size);
54
55   default:
56     return NULL;
57   }
58 }
59
60
61 /**
62  * Convert human-readable version of a 'value' of an attribute to the binary
63  * representation.
64  *
65  * @param cls closure, unused
66  * @param type type of the attribute
67  * @param s human-readable string
68  * @param data set to value in binary encoding (will be allocated)
69  * @param data_size set to number of bytes in @a data
70  * @return #GNUNET_OK on success
71  */
72 static int
73 gnuid_string_to_value (void *cls,
74                        uint32_t type,
75                        const char *s,
76                        void **data,
77                        size_t *data_size)
78 {
79   if (NULL == s)
80     return GNUNET_SYSERR;
81   switch (type)
82   {
83   case GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING:
84     *data = GNUNET_strdup (s);
85     *data_size = strlen (s);
86     return GNUNET_OK;
87
88   default:
89     return GNUNET_SYSERR;
90   }
91 }
92
93 /**
94    * Convert the 'value' of an attestation to a string.
95    *
96    * @param cls closure, unused
97    * @param type type of the attestation
98    * @param data value in binary encoding
99    * @param data_size number of bytes in @a data
100    * @return NULL on error, otherwise human-readable representation of the value
101    */
102 static char *
103 gnuid_value_to_string_attest (void *cls,
104                               uint32_t type,
105                               const void *data,
106                               size_t data_size)
107 {
108   switch (type)
109   {
110   case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT:
111     return GNUNET_strndup (data, data_size);
112
113   default:
114     return NULL;
115   }
116 }
117
118
119 /**
120  * Convert human-readable version of a 'value' of an attestation to the binary
121  * representation.
122  *
123  * @param cls closure, unused
124  * @param type type of the attestation
125  * @param s human-readable string
126  * @param data set to value in binary encoding (will be allocated)
127  * @param data_size set to number of bytes in @a data
128  * @return #GNUNET_OK on success
129  */
130 static int
131 gnuid_string_to_value_attest (void *cls,
132                               uint32_t type,
133                               const char *s,
134                               void **data,
135                               size_t *data_size)
136 {
137   if (NULL == s)
138     return GNUNET_SYSERR;
139   switch (type)
140   {
141   case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT:
142     *data = GNUNET_strdup (s);
143     *data_size = strlen (s);
144     return GNUNET_OK;
145
146   default:
147     return GNUNET_SYSERR;
148   }
149 }
150
151 /**
152  * Mapping of attribute type numbers to human-readable
153  * attribute type names.
154  */
155 static struct
156 {
157   const char *name;
158   uint32_t number;
159 } gnuid_name_map[] = { { "STRING", GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING },
160                        { NULL, UINT32_MAX } };
161
162 /**
163  * Mapping of attestation type numbers to human-readable
164  * attestation type names.
165  */
166 static struct
167 {
168   const char *name;
169   uint32_t number;
170 } gnuid_attest_name_map[] = { { "JWT", GNUNET_RECLAIM_ATTESTATION_TYPE_JWT },
171                               { NULL, UINT32_MAX } };
172
173 /**
174  * Convert a type name to the corresponding number.
175  *
176  * @param cls closure, unused
177  * @param gnuid_typename name to convert
178  * @return corresponding number, UINT32_MAX on error
179  */
180 static uint32_t
181 gnuid_typename_to_number (void *cls, const char *gnuid_typename)
182 {
183   unsigned int i;
184
185   i = 0;
186   while ((NULL != gnuid_name_map[i].name) &&
187          (0 != strcasecmp (gnuid_typename, gnuid_name_map[i].name)))
188     i++;
189   return gnuid_name_map[i].number;
190 }
191
192
193 /**
194  * Convert a type number (i.e. 1) to the corresponding type string
195  *
196  * @param cls closure, unused
197  * @param type number of a type to convert
198  * @return corresponding typestring, NULL on error
199  */
200 static const char *
201 gnuid_number_to_typename (void *cls, uint32_t type)
202 {
203   unsigned int i;
204
205   i = 0;
206   while ((NULL != gnuid_name_map[i].name) && (type != gnuid_name_map[i].number))
207     i++;
208   return gnuid_name_map[i].name;
209 }
210
211 /**
212    * Convert a type name to the corresponding number.
213    *
214    * @param cls closure, unused
215    * @param gnuid_typename name to convert
216    * @return corresponding number, UINT32_MAX on error
217    */
218 static uint32_t
219 gnuid_typename_to_number_attest (void *cls, const char *gnuid_typename)
220 {
221   unsigned int i;
222
223   i = 0;
224   while ((NULL != gnuid_attest_name_map[i].name) &&
225          (0 != strcasecmp (gnuid_typename, gnuid_attest_name_map[i].name)))
226     i++;
227   return gnuid_attest_name_map[i].number;
228 }
229
230 /**
231  * Convert a type number (i.e. 1) to the corresponding type string
232  *
233  * @param cls closure, unused
234  * @param type number of a type to convert
235  * @return corresponding typestring, NULL on error
236  */
237 static const char *
238 gnuid_number_to_typename_attest (void *cls, uint32_t type)
239 {
240   unsigned int i;
241
242   i = 0;
243   while ((NULL != gnuid_attest_name_map[i].name) && (type !=
244                                                      gnuid_attest_name_map[i].
245                                                      number))
246     i++;
247   return gnuid_attest_name_map[i].name;
248 }
249
250 /**
251  * Entry point for the plugin.
252  *
253  * @param cls NULL
254  * @return the exported block API
255  */
256 void *
257 libgnunet_plugin_reclaim_attribute_gnuid_init (void *cls)
258 {
259   struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api;
260
261   api = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions);
262   api->value_to_string = &gnuid_value_to_string;
263   api->string_to_value = &gnuid_string_to_value;
264   api->typename_to_number = &gnuid_typename_to_number;
265   api->number_to_typename = &gnuid_number_to_typename;
266   api->value_to_string_attest = &gnuid_value_to_string_attest;
267   api->string_to_value_attest = &gnuid_string_to_value_attest;
268   api->typename_to_number_attest = &gnuid_typename_to_number_attest;
269   api->number_to_typename_attest = &gnuid_number_to_typename_attest;
270   return api;
271 }
272
273
274 /**
275  * Exit point from the plugin.
276  *
277  * @param cls the return value from #libgnunet_plugin_block_test_init()
278  * @return NULL
279  */
280 void *
281 libgnunet_plugin_reclaim_attribute_gnuid_done (void *cls)
282 {
283   struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = cls;
284
285   GNUNET_free (api);
286   return NULL;
287 }
288
289
290 /* end of plugin_reclaim_attribute_type_gnuid.c */