You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

341 lines
7.5 KiB

  1. /*
  2. * This file is part of GNUnet.
  3. * Copyright (C) 2013 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. SPDX-License-Identifier: AGPL3.0-or-later
  18. */
  19. /**
  20. * @author Gabor X Toth
  21. *
  22. * @file
  23. * PSYC Environment library
  24. *
  25. * @defgroup psyc-util-env PSYC Utilities library: Environment
  26. * Environment data structure operations for PSYC and Social messages.
  27. *
  28. * Library providing operations for the @e environment of
  29. * PSYC and Social messages, and for (de)serializing variable values.
  30. *
  31. * @{
  32. */
  33. #ifndef GNUNET_PSYC_ENV_H
  34. #define GNUNET_PSYC_ENV_H
  35. #ifdef __cplusplus
  36. extern "C"
  37. {
  38. #if 0 /* keep Emacsens' auto-indent happy */
  39. }
  40. #endif
  41. #endif
  42. /**
  43. * Possible operations on PSYC state (persistent) and transient variables (per message).
  44. */
  45. enum GNUNET_PSYC_Operator
  46. {
  47. /**
  48. * Set value of a transient variable.
  49. */
  50. GNUNET_PSYC_OP_SET = ':',
  51. /**
  52. * Assign value for a persistent state variable.
  53. *
  54. * If an assigned value is NULL, the variable is deleted.
  55. */
  56. GNUNET_PSYC_OP_ASSIGN = '=',
  57. /**
  58. * Augment state variable.
  59. *
  60. * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
  61. */
  62. GNUNET_PSYC_OP_AUGMENT = '+',
  63. /**
  64. * Diminish state variable.
  65. *
  66. * Used for subtracting numbers, and removing items from a list or dictionary.
  67. */
  68. GNUNET_PSYC_OP_DIMINISH = '-',
  69. /**
  70. * Update state variable.
  71. *
  72. * Used for modifying a single item of a list or dictionary.
  73. */
  74. GNUNET_PSYC_OP_UPDATE = '@',
  75. };
  76. /**
  77. * PSYC variable types.
  78. */
  79. enum GNUNET_PSYC_Type
  80. {
  81. GNUNET_PSYC_TYPE_DATA = 0,
  82. GNUNET_PSYC_TYPE_NUMBER,
  83. GNUNET_PSYC_TYPE_LIST,
  84. GNUNET_PSYC_TYPE_DICT
  85. };
  86. /**
  87. * PSYC state modifier.
  88. */
  89. struct GNUNET_PSYC_Modifier
  90. {
  91. /**
  92. * State operation.
  93. */
  94. enum GNUNET_PSYC_Operator oper;
  95. /**
  96. * Variable name.
  97. */
  98. const char *name;
  99. /**
  100. * Size of @a value.
  101. */
  102. size_t value_size;
  103. /**
  104. * Value of variable.
  105. */
  106. const void *value;
  107. /**
  108. * Next modifier.
  109. */
  110. struct GNUNET_PSYC_Modifier *next;
  111. /**
  112. * Previous modifier.
  113. */
  114. struct GNUNET_PSYC_Modifier *prev;
  115. };
  116. /**
  117. * Environment for a message.
  118. *
  119. * Contains modifiers.
  120. */
  121. struct GNUNET_PSYC_Environment;
  122. /**
  123. * Create an environment.
  124. *
  125. * @return A newly allocated environment.
  126. */
  127. struct GNUNET_PSYC_Environment *
  128. GNUNET_PSYC_env_create ();
  129. /**
  130. * Add a modifier to the environment.
  131. *
  132. * @param env The environment.
  133. * @param oper Operation to perform.
  134. * @param name Name of the variable.
  135. * @param value Value of the variable.
  136. * @param value_size Size of @a value.
  137. */
  138. void
  139. GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
  140. enum GNUNET_PSYC_Operator oper, const char *name,
  141. const void *value, size_t value_size);
  142. /**
  143. * Get the first modifier of the environment.
  144. */
  145. struct GNUNET_PSYC_Modifier *
  146. GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env);
  147. /**
  148. * Get the last modifier of the environment.
  149. */
  150. struct GNUNET_PSYC_Modifier *
  151. GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env);
  152. /**
  153. * Remove a modifier from the environment.
  154. */
  155. void
  156. GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
  157. struct GNUNET_PSYC_Modifier *mod);
  158. /**
  159. * Remove a modifier at the beginning of the environment.
  160. */
  161. int
  162. GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
  163. enum GNUNET_PSYC_Operator *oper, const char **name,
  164. const void **value, size_t *value_size);
  165. /**
  166. * Iterator for modifiers in the environment.
  167. *
  168. * @param cls Closure.
  169. * @param mod Modifier.
  170. *
  171. * @return #GNUNET_YES to continue iterating,
  172. * #GNUNET_NO to stop.
  173. */
  174. typedef int
  175. (*GNUNET_PSYC_Iterator) (void *cls, enum GNUNET_PSYC_Operator oper,
  176. const char *name, const char *value,
  177. uint32_t value_size);
  178. /**
  179. * Iterate through all modifiers in the environment.
  180. *
  181. * @param env The environment.
  182. * @param it Iterator.
  183. * @param it_cls Closure for iterator.
  184. */
  185. void
  186. GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
  187. GNUNET_PSYC_Iterator it, void *it_cls);
  188. /**
  189. * Get the number of modifiers in the environment.
  190. *
  191. * @param env The environment.
  192. *
  193. * @return Number of modifiers.
  194. */
  195. size_t
  196. GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env);
  197. /**
  198. * Destroy an environment.
  199. *
  200. * @param env The environment to destroy.
  201. */
  202. void
  203. GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env);
  204. /**
  205. * Get the type of variable.
  206. *
  207. * @param name Name of the variable.
  208. *
  209. * @return Variable type.
  210. */
  211. enum GNUNET_PSYC_Type
  212. GNUNET_PSYC_var_get_type (char *name);
  213. /**
  214. * Perform an operation on a variable.
  215. *
  216. * @param name Name of variable.
  217. * @param current_value Current value of variable.
  218. * @param current_value_size Size of @a current_value.
  219. * @param oper Operator.
  220. * @param args Arguments for the operation.
  221. * @param args_size Size of @a args.
  222. * @param return_value Return value.
  223. * @param return_value_size Size of @a return_value.
  224. *
  225. * @return #GNUNET_OK on success, else #GNUNET_SYSERR
  226. */
  227. int
  228. GNUNET_PSYC_operation (char *name, void *current_value, size_t current_value_size,
  229. enum GNUNET_PSYC_Operator oper, void *args, size_t args_size,
  230. void **return_value, size_t *return_value_size);
  231. /**
  232. * Get the variable's value as an integer.
  233. *
  234. * @param size Size of value.
  235. * @param value Raw value of variable.
  236. * @param[out] number Value converted to a 64-bit integer.
  237. *
  238. * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
  239. */
  240. int
  241. GNUNET_PSYC_value_to_number (size_t size, const void *value, int64_t *number);
  242. /**
  243. * Get the variable's value as a dictionary.
  244. *
  245. * @param size Size of value.
  246. * @param value Raw value of variable.
  247. * @param[out] dict A newly created hashmap holding the elements of the dictionary.
  248. *
  249. * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
  250. */
  251. int
  252. GNUNET_PSYC_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);
  253. /**
  254. * Create a PSYC variable value from an integer.
  255. *
  256. * @param number The number to convert.
  257. * @param[out] value_size Size of returned value.
  258. *
  259. * @return A newly allocated value or NULL on error.
  260. */
  261. void *
  262. GNUNET_PSYC_value_from_number (int64_t number, size_t *value_size);
  263. /**
  264. * Create a PSYC variable value from a dictionary.
  265. *
  266. * @param dict The dict to convert.
  267. * @param[out] value_size Size of returned value.
  268. *
  269. * @return A newly allocated value or NULL on error.
  270. */
  271. void *
  272. GNUNET_PSYC_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);
  273. #if 0 /* keep Emacsens' auto-indent happy */
  274. {
  275. #endif
  276. #ifdef __cplusplus
  277. }
  278. #endif
  279. /* ifndef GNUNET_PSYC_ENV_H */
  280. #endif
  281. /** @} */ /* end of group */