consolidate reclaim attribute lib
[oweals/gnunet.git] / src / reclaim / plugin_reclaim_attribute_basic.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_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 basic_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 basic_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  * Mapping of attribute type numbers to human-readable
95  * attribute type names.
96  */
97 static struct
98 {
99   const char *name;
100   uint32_t number;
101 } basic_name_map[] = { { "STRING", GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING },
102                        { NULL, UINT32_MAX } };
103
104
105 /**
106  * Convert a type name to the corresponding number.
107  *
108  * @param cls closure, unused
109  * @param basic_typename name to convert
110  * @return corresponding number, UINT32_MAX on error
111  */
112 static uint32_t
113 basic_typename_to_number (void *cls, const char *basic_typename)
114 {
115   unsigned int i;
116
117   i = 0;
118   while ((NULL != basic_name_map[i].name) &&
119          (0 != strcasecmp (basic_typename, basic_name_map[i].name)))
120     i++;
121   return basic_name_map[i].number;
122 }
123
124
125 /**
126  * Convert a type number (i.e. 1) to the corresponding type string
127  *
128  * @param cls closure, unused
129  * @param type number of a type to convert
130  * @return corresponding typestring, NULL on error
131  */
132 static const char *
133 basic_number_to_typename (void *cls, uint32_t type)
134 {
135   unsigned int i;
136
137   i = 0;
138   while ((NULL != basic_name_map[i].name) && (type != basic_name_map[i].number))
139     i++;
140   return basic_name_map[i].name;
141 }
142
143
144 /**
145  * Entry point for the plugin.
146  *
147  * @param cls NULL
148  * @return the exported block API
149  */
150 void *
151 libgnunet_plugin_reclaim_attribute_basic_init (void *cls)
152 {
153   struct GNUNET_RECLAIM_AttributePluginFunctions *api;
154
155   api = GNUNET_new (struct GNUNET_RECLAIM_AttributePluginFunctions);
156   api->value_to_string = &basic_value_to_string;
157   api->string_to_value = &basic_string_to_value;
158   api->typename_to_number = &basic_typename_to_number;
159   api->number_to_typename = &basic_number_to_typename;
160   return api;
161 }
162
163
164 /**
165  * Exit point from the plugin.
166  *
167  * @param cls the return value from #libgnunet_plugin_block_test_init()
168  * @return NULL
169  */
170 void *
171 libgnunet_plugin_reclaim_attribute_basic_done (void *cls)
172 {
173   struct GNUNET_RECLAIM_AttributePluginFunctions *api = cls;
174
175   GNUNET_free (api);
176   return NULL;
177 }
178
179
180 /* end of plugin_reclaim_attribute_type_gnuid.c */