aboutsummaryrefslogtreecommitdiff
path: root/src/identity-attribute/plugin_identity_attribute_gnuid.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/identity-attribute/plugin_identity_attribute_gnuid.c')
-rw-r--r--src/identity-attribute/plugin_identity_attribute_gnuid.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/src/identity-attribute/plugin_identity_attribute_gnuid.c b/src/identity-attribute/plugin_identity_attribute_gnuid.c
new file mode 100644
index 000000000..006b45ea2
--- /dev/null
+++ b/src/identity-attribute/plugin_identity_attribute_gnuid.c
@@ -0,0 +1,184 @@
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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/**
22 * @file identity-attribute/plugin_identity_attribute_gnuid.c
23 * @brief identity attribute plugin to provide the API for fundamental
24 * attribute types.
25 *
26 * @author Martin Schanzenbach
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_identity_attribute_plugin.h"
31#include <inttypes.h>
32
33
34/**
35 * Convert the 'value' of an attribute to a string.
36 *
37 * @param cls closure, unused
38 * @param type type of the attribute
39 * @param data value in binary encoding
40 * @param data_size number of bytes in @a data
41 * @return NULL on error, otherwise human-readable representation of the value
42 */
43static char *
44gnuid_value_to_string (void *cls,
45 uint32_t type,
46 const void *data,
47 size_t data_size)
48{
49
50 switch (type)
51 {
52 case GNUNET_IDENTITY_ATTRIBUTE_TYPE_STRING:
53 return GNUNET_strndup (data, data_size);
54 default:
55 return NULL;
56 }
57}
58
59
60/**
61 * Convert human-readable version of a 'value' of an attribute to the binary
62 * representation.
63 *
64 * @param cls closure, unused
65 * @param type type of the attribute
66 * @param s human-readable string
67 * @param data set to value in binary encoding (will be allocated)
68 * @param data_size set to number of bytes in @a data
69 * @return #GNUNET_OK on success
70 */
71static int
72gnuid_string_to_value (void *cls,
73 uint32_t type,
74 const char *s,
75 void **data,
76 size_t *data_size)
77{
78 if (NULL == s)
79 return GNUNET_SYSERR;
80 switch (type)
81 {
82
83 case GNUNET_IDENTITY_ATTRIBUTE_TYPE_STRING:
84 *data = GNUNET_strdup (s);
85 *data_size = strlen (s);
86 return GNUNET_OK;
87 default:
88 return GNUNET_SYSERR;
89 }
90}
91
92
93/**
94 * Mapping of attribute type numbers to human-readable
95 * attribute type names.
96 */
97static struct {
98 const char *name;
99 uint32_t number;
100} gnuid_name_map[] = {
101 { "STRING", GNUNET_IDENTITY_ATTRIBUTE_TYPE_STRING },
102 { NULL, UINT32_MAX }
103};
104
105
106/**
107 * Convert a type name to the corresponding number.
108 *
109 * @param cls closure, unused
110 * @param gnuid_typename name to convert
111 * @return corresponding number, UINT32_MAX on error
112 */
113static uint32_t
114gnuid_typename_to_number (void *cls,
115 const char *gnuid_typename)
116{
117 unsigned int i;
118
119 i=0;
120 while ( (NULL != gnuid_name_map[i].name) &&
121 (0 != strcasecmp (gnuid_typename,
122 gnuid_name_map[i].name)) )
123 i++;
124 return gnuid_name_map[i].number;
125}
126
127
128/**
129 * Convert a type number (i.e. 1) to the corresponding type string
130 *
131 * @param cls closure, unused
132 * @param type number of a type to convert
133 * @return corresponding typestring, NULL on error
134 */
135static const char *
136gnuid_number_to_typename (void *cls,
137 uint32_t type)
138{
139 unsigned int i;
140
141 i=0;
142 while ( (NULL != gnuid_name_map[i].name) &&
143 (type != gnuid_name_map[i].number) )
144 i++;
145 return gnuid_name_map[i].name;
146}
147
148
149/**
150 * Entry point for the plugin.
151 *
152 * @param cls NULL
153 * @return the exported block API
154 */
155void *
156libgnunet_plugin_identity_attribute_gnuid_init (void *cls)
157{
158 struct GNUNET_IDENTITY_ATTRIBUTE_PluginFunctions *api;
159
160 api = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_PluginFunctions);
161 api->value_to_string = &gnuid_value_to_string;
162 api->string_to_value = &gnuid_string_to_value;
163 api->typename_to_number = &gnuid_typename_to_number;
164 api->number_to_typename = &gnuid_number_to_typename;
165 return api;
166}
167
168
169/**
170 * Exit point from the plugin.
171 *
172 * @param cls the return value from #libgnunet_plugin_block_test_init()
173 * @return NULL
174 */
175void *
176libgnunet_plugin_identity_attribute_gnuid_done (void *cls)
177{
178 struct GNUNET_IDENTITY_ATTRIBUTE_PluginFunctions *api = cls;
179
180 GNUNET_free (api);
181 return NULL;
182}
183
184/* end of plugin_identity_attribute_type_gnuid.c */