aboutsummaryrefslogtreecommitdiff
path: root/src/include/gnunet_psyc_env.h
blob: 0d878cb961ffa49ff330b9daa96458bc3aa83755 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * This file is part of GNUnet.
 * Copyright (C) 2013 GNUnet e.V.
 *
 * GNUnet is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * GNUnet is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @author Gabor X Toth
 *
 * @file
 * PSYC Environment library
 *
 * @defgroup psyc-util-env  PSYC Utilities library: Environment
 * Environment data structure operations for PSYC and Social messages.
 *
 * Library providing operations for the @e environment of
 * PSYC and Social messages, and for (de)serializing variable values.
 *
 * @{
 */


#ifndef GNUNET_PSYC_ENV_H
#define GNUNET_PSYC_ENV_H

#ifdef __cplusplus
extern "C"
{
#if 0                           /* keep Emacsens' auto-indent happy */
}
#endif
#endif


/**
 * Possible operations on PSYC state (persistent) and transient variables (per message).
 */
enum GNUNET_PSYC_Operator
{
  /**
   * Set value of a transient variable.
   */
  GNUNET_PSYC_OP_SET = ':',

  /**
   * Assign value for a persistent state variable.
   *
   * If an assigned value is NULL, the variable is deleted.
   */
  GNUNET_PSYC_OP_ASSIGN = '=',

  /**
   * Augment state variable.
   *
   * Used for appending strings, adding numbers, and adding new items to a list or dictionary.
   */
  GNUNET_PSYC_OP_AUGMENT = '+',

  /**
   * Diminish state variable.
   *
   * Used for subtracting numbers, and removing items from a list or dictionary.
   */
  GNUNET_PSYC_OP_DIMINISH = '-',

  /**
   * Update state variable.
   *
   * Used for modifying a single item of a list or dictionary.
   */
  GNUNET_PSYC_OP_UPDATE = '@',
};


/**
 * PSYC variable types.
 */
enum GNUNET_PSYC_Type
{
  GNUNET_PSYC_TYPE_DATA = 0,
  GNUNET_PSYC_TYPE_NUMBER,
  GNUNET_PSYC_TYPE_LIST,
  GNUNET_PSYC_TYPE_DICT
};


/**
 * PSYC state modifier.
 */
struct GNUNET_PSYC_Modifier
{
  /**
   * State operation.
   */
  enum GNUNET_PSYC_Operator oper;

  /**
   * Variable name.
   */
  const char *name;

  /**
   * Size of @a value.
   */
  size_t value_size;

  /**
   * Value of variable.
   */
  const void *value;

  /**
   * Next modifier.
   */
  struct GNUNET_PSYC_Modifier *next;

  /**
   * Previous modifier.
   */
  struct GNUNET_PSYC_Modifier *prev;
};


/**
 * Environment for a message.
 *
 * Contains modifiers.
 */
struct GNUNET_PSYC_Environment;


/**
 * Create an environment.
 *
 * @return A newly allocated environment.
 */
struct GNUNET_PSYC_Environment *
GNUNET_PSYC_env_create ();


/**
 * Add a modifier to the environment.
 *
 * @param env The environment.
 * @param oper Operation to perform.
 * @param name Name of the variable.
 * @param value Value of the variable.
 * @param value_size Size of @a value.
 */
void
GNUNET_PSYC_env_add (struct GNUNET_PSYC_Environment *env,
                     enum GNUNET_PSYC_Operator oper, const char *name,
                     const void *value, size_t value_size);


/**
 * Get the first modifier of the environment.
 */
struct GNUNET_PSYC_Modifier *
GNUNET_PSYC_env_head (const struct GNUNET_PSYC_Environment *env);



/**
 * Get the last modifier of the environment.
 */
struct GNUNET_PSYC_Modifier *
GNUNET_PSYC_env_tail (const struct GNUNET_PSYC_Environment *env);


/**
 * Remove a modifier from the environment.
 */
void
GNUNET_PSYC_env_remove (struct GNUNET_PSYC_Environment *env,
                        struct GNUNET_PSYC_Modifier *mod);


/**
 * Remove a modifier at the beginning of the environment.
 */
int
GNUNET_PSYC_env_shift (struct GNUNET_PSYC_Environment *env,
                       enum GNUNET_PSYC_Operator *oper, const char **name,
                       const void **value, size_t *value_size);


/**
 * Iterator for modifiers in the environment.
 *
 * @param cls Closure.
 * @param mod Modifier.
 *
 * @return #GNUNET_YES to continue iterating,
 *         #GNUNET_NO to stop.
 */
typedef int
(*GNUNET_PSYC_Iterator) (void *cls, enum GNUNET_PSYC_Operator oper,
                         const char *name, const char *value,
                         uint32_t value_size);


/**
 * Iterate through all modifiers in the environment.
 *
 * @param env The environment.
 * @param it Iterator.
 * @param it_cls Closure for iterator.
 */
void
GNUNET_PSYC_env_iterate (const struct GNUNET_PSYC_Environment *env,
                         GNUNET_PSYC_Iterator it, void *it_cls);


/**
 * Get the number of modifiers in the environment.
 *
 * @param env The environment.
 *
 * @return Number of modifiers.
 */
size_t
GNUNET_PSYC_env_get_count (const struct GNUNET_PSYC_Environment *env);


/**
 * Destroy an environment.
 *
 * @param env The environment to destroy.
 */
void
GNUNET_PSYC_env_destroy (struct GNUNET_PSYC_Environment *env);


/**
 * Get the type of variable.
 *
 * @param name Name of the variable.
 *
 * @return Variable type.
 */
enum GNUNET_PSYC_Type
GNUNET_PSYC_var_get_type (char *name);


/**
 * Perform an operation on a variable.
 *
 * @param name Name of variable.
 * @param current_value Current value of variable.
 * @param current_value_size Size of @a current_value.
 * @param oper Operator.
 * @param args Arguments for the operation.
 * @param args_size Size of @a args.
 * @param return_value Return value.
 * @param return_value_size Size of @a return_value.
 *
 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
 */
int
GNUNET_PSYC_operation (char *name, void *current_value, size_t current_value_size,
                       enum GNUNET_PSYC_Operator oper, void *args, size_t args_size,
                       void **return_value, size_t *return_value_size);


/**
 * Get the variable's value as an integer.
 *
 * @param size Size of value.
 * @param value Raw value of variable.
 * @param[out] number Value converted to a 64-bit integer.
 *
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
 */
int
GNUNET_PSYC_value_to_number (size_t size, const void *value, int64_t *number);


/**
 * Get the variable's value as a dictionary.
 *
 * @param size Size of value.
 * @param value Raw value of variable.
 * @param[out] dict A newly created hashmap holding the elements of the dictionary.
 *
 * @return #GNUNET_OK on success, #GNUNET_SYSERR if an error occurred (e.g. the value is invalid).
 */
int
GNUNET_PSYC_value_to_dict (size_t size, const void *value, struct GNUNET_CONTAINER_MultiHashMap **dict);


/**
 * Create a PSYC variable value from an integer.
 *
 * @param number The number to convert.
 * @param[out] value_size Size of returned value.
 *
 * @return A newly allocated value or NULL on error.
 */
void *
GNUNET_PSYC_value_from_number (int64_t number, size_t *value_size);


/**
 * Create a PSYC variable value from a dictionary.
 *
 * @param dict The dict to convert.
 * @param[out] value_size Size of returned value.
 *
 * @return A newly allocated value or NULL on error.
 */
void *
GNUNET_PSYC_value_from_dict (struct GNUNET_CONTAINER_MultiHashMap *dict, size_t *value_size);


#if 0                           /* keep Emacsens' auto-indent happy */
{
#endif
#ifdef __cplusplus
}
#endif

/* ifndef GNUNET_PSYC_ENV_H */
#endif

/** @} */  /* end of group */