revert
[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
19 /**
20  * @file reclaim-attribute/plugin_reclaim_attribute_gnuid.c
21  * @brief identity attribute plugin to provide the API for fundamental 
22  *                 attribute types.
23  *
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_reclaim_attribute_plugin.h"
29 #include <inttypes.h>
30
31
32 /**
33  * Convert the 'value' of an attribute to a string.
34  *
35  * @param cls closure, unused
36  * @param type type of the attribute
37  * @param data value in binary encoding
38  * @param data_size number of bytes in @a data
39  * @return NULL on error, otherwise human-readable representation of the value
40  */
41 static char *
42 gnuid_value_to_string (void *cls,
43                      uint32_t type,
44                      const void *data,
45                      size_t data_size)
46 {
47
48   switch (type)
49   {
50   case GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING:
51     return GNUNET_strndup (data, data_size);
52   default:
53     return NULL;
54   }
55 }
56
57
58 /**
59  * Convert human-readable version of a 'value' of an attribute to the binary
60  * representation.
61  *
62  * @param cls closure, unused
63  * @param type type of the attribute
64  * @param s human-readable string
65  * @param data set to value in binary encoding (will be allocated)
66  * @param data_size set to number of bytes in @a data
67  * @return #GNUNET_OK on success
68  */
69 static int
70 gnuid_string_to_value (void *cls,
71                      uint32_t type,
72                      const char *s,
73                      void **data,
74                      size_t *data_size)
75 {
76   if (NULL == s)
77     return GNUNET_SYSERR;
78   switch (type)
79   {
80
81     case GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING:
82       *data = GNUNET_strdup (s);
83       *data_size = strlen (s);
84       return GNUNET_OK;
85     default:
86       return GNUNET_SYSERR;
87   }
88 }
89
90
91 /**
92  * Mapping of attribute type numbers to human-readable
93  * attribute type names.
94  */
95 static struct {
96   const char *name;
97   uint32_t number;
98 } gnuid_name_map[] = {
99   { "STRING",  GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING },
100   { NULL, UINT32_MAX }
101 };
102
103
104 /**
105  * Convert a type name to the corresponding number.
106  *
107  * @param cls closure, unused
108  * @param gnuid_typename name to convert
109  * @return corresponding number, UINT32_MAX on error
110  */
111 static uint32_t
112 gnuid_typename_to_number (void *cls,
113                         const char *gnuid_typename)
114 {
115   unsigned int i;
116
117   i=0;
118   while ( (NULL != gnuid_name_map[i].name) &&
119           (0 != strcasecmp (gnuid_typename,
120                             gnuid_name_map[i].name)) )
121     i++;
122   return gnuid_name_map[i].number;
123 }
124
125
126 /**
127  * Convert a type number (i.e. 1) to the corresponding type string
128  *
129  * @param cls closure, unused
130  * @param type number of a type to convert
131  * @return corresponding typestring, NULL on error
132  */
133 static const char *
134 gnuid_number_to_typename (void *cls,
135                         uint32_t type)
136 {
137   unsigned int i;
138
139   i=0;
140   while ( (NULL != gnuid_name_map[i].name) &&
141           (type != gnuid_name_map[i].number) )
142     i++;
143   return gnuid_name_map[i].name;
144 }
145
146
147 /**
148  * Entry point for the plugin.
149  *
150  * @param cls NULL
151  * @return the exported block API
152  */
153 void *
154 libgnunet_plugin_reclaim_attribute_gnuid_init (void *cls)
155 {
156   struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api;
157
158   api = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions);
159   api->value_to_string = &gnuid_value_to_string;
160   api->string_to_value = &gnuid_string_to_value;
161   api->typename_to_number = &gnuid_typename_to_number;
162   api->number_to_typename = &gnuid_number_to_typename;
163   return api;
164 }
165
166
167 /**
168  * Exit point from the plugin.
169  *
170  * @param cls the return value from #libgnunet_plugin_block_test_init()
171  * @return NULL
172  */
173 void *
174 libgnunet_plugin_reclaim_attribute_gnuid_done (void *cls)
175 {
176   struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = cls;
177
178   GNUNET_free (api);
179   return NULL;
180 }
181
182 /* end of plugin_reclaim_attribute_type_gnuid.c */