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