plugin_reclaim_attribute_gnuid.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013, 2014, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file reclaim-attribute/plugin_reclaim_attribute_gnuid.c
  18. * @brief reclaim-attribute-plugin-gnuid attribute plugin to provide the API for
  19. * fundamental
  20. * attribute types.
  21. *
  22. * @author Martin Schanzenbach
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_reclaim_attribute_plugin.h"
  27. #include <inttypes.h>
  28. /**
  29. * Convert the 'value' of an attribute to a string.
  30. *
  31. * @param cls closure, unused
  32. * @param type type of the attribute
  33. * @param data value in binary encoding
  34. * @param data_size number of bytes in @a data
  35. * @return NULL on error, otherwise human-readable representation of the value
  36. */
  37. static char *
  38. gnuid_value_to_string (void *cls,
  39. uint32_t type,
  40. const void *data,
  41. size_t data_size)
  42. {
  43. switch (type)
  44. {
  45. case GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING:
  46. return GNUNET_strndup (data, data_size);
  47. default:
  48. return NULL;
  49. }
  50. }
  51. /**
  52. * Convert human-readable version of a 'value' of an attribute to the binary
  53. * representation.
  54. *
  55. * @param cls closure, unused
  56. * @param type type of the attribute
  57. * @param s human-readable string
  58. * @param data set to value in binary encoding (will be allocated)
  59. * @param data_size set to number of bytes in @a data
  60. * @return #GNUNET_OK on success
  61. */
  62. static int
  63. gnuid_string_to_value (void *cls,
  64. uint32_t type,
  65. const char *s,
  66. void **data,
  67. size_t *data_size)
  68. {
  69. if (NULL == s)
  70. return GNUNET_SYSERR;
  71. switch (type)
  72. {
  73. case GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING:
  74. *data = GNUNET_strdup (s);
  75. *data_size = strlen (s);
  76. return GNUNET_OK;
  77. default:
  78. return GNUNET_SYSERR;
  79. }
  80. }
  81. /**
  82. * Mapping of attribute type numbers to human-readable
  83. * attribute type names.
  84. */
  85. static struct
  86. {
  87. const char *name;
  88. uint32_t number;
  89. } gnuid_name_map[] = { { "STRING", GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING },
  90. { NULL, UINT32_MAX } };
  91. /**
  92. * Convert a type name to the corresponding number.
  93. *
  94. * @param cls closure, unused
  95. * @param gnuid_typename name to convert
  96. * @return corresponding number, UINT32_MAX on error
  97. */
  98. static uint32_t
  99. gnuid_typename_to_number (void *cls, const char *gnuid_typename)
  100. {
  101. unsigned int i;
  102. i = 0;
  103. while ((NULL != gnuid_name_map[i].name) &&
  104. (0 != strcasecmp (gnuid_typename, gnuid_name_map[i].name)))
  105. i++;
  106. return gnuid_name_map[i].number;
  107. }
  108. /**
  109. * Convert a type number (i.e. 1) to the corresponding type string
  110. *
  111. * @param cls closure, unused
  112. * @param type number of a type to convert
  113. * @return corresponding typestring, NULL on error
  114. */
  115. static const char *
  116. gnuid_number_to_typename (void *cls, uint32_t type)
  117. {
  118. unsigned int i;
  119. i = 0;
  120. while ((NULL != gnuid_name_map[i].name) && (type != gnuid_name_map[i].number))
  121. i++;
  122. return gnuid_name_map[i].name;
  123. }
  124. /**
  125. * Entry point for the plugin.
  126. *
  127. * @param cls NULL
  128. * @return the exported block API
  129. */
  130. void *
  131. libgnunet_plugin_reclaim_attribute_gnuid_init (void *cls)
  132. {
  133. struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api;
  134. api = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions);
  135. api->value_to_string = &gnuid_value_to_string;
  136. api->string_to_value = &gnuid_string_to_value;
  137. api->typename_to_number = &gnuid_typename_to_number;
  138. api->number_to_typename = &gnuid_number_to_typename;
  139. return api;
  140. }
  141. /**
  142. * Exit point from the plugin.
  143. *
  144. * @param cls the return value from #libgnunet_plugin_block_test_init()
  145. * @return NULL
  146. */
  147. void *
  148. libgnunet_plugin_reclaim_attribute_gnuid_done (void *cls)
  149. {
  150. struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = cls;
  151. GNUNET_free (api);
  152. return NULL;
  153. }
  154. /* end of plugin_reclaim_attribute_type_gnuid.c */