Moved SILC_PARAM_* types under generic SilcParam type into the
[silc.git] / lib / silcutil / silcbuffmt.h
1 /*
2
3   silcbuffmt.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcutil/SILC Buffer Format Interface
21  *
22  * DESCRIPTION
23  *
24  * SILC Buffer Format API provides functions for formatting different data
25  * types into a buffer and retrieving different data types from a buffer
26  * into specified data types.  It is especially useful to encode packets,
27  * protocol payloads and such.
28  *
29  * As the SilcBuffer API is not thread-safe these routines may not be used
30  * in multithreaded environment with a same SilcBuffer context without
31  * concurrency control.
32  *
33  * EXAMPLE
34  *
35  * SilcBufferStruct buffer;
36  *
37  * memset(&buffer, 0, sizeof(buffer));
38  * ret = silc_buffer_format(&buffer,
39  *                          SILC_STR_UINT32(intval),
40  *                          SILC_STR_UINT8(charval),
41  *                          SILC_STR_UINT64(longintval),
42  *                          SILC_STR_UINT16(str_len),
43  *                          SILC_STR_DATA(str, str_len),
44  *                          SILC_STR_END);
45  * if (ret < 0)
46  *   error;
47  *
48  ***/
49
50 #ifndef SILCBUFFMT_H
51 #define SILCBUFFMT_H
52
53 /****f* silcutil/SilcBufferFormatAPI/SilcBufferFormatFunc
54  *
55  * SYNOPSIS
56  *
57  *    typedef int (*SilcBuffeSFormatFunc)(SilcStack stack,
58  *                                        SilcBuffer buffer,
59  *                                        void *value,
60  *                                        void *context);
61  *
62  * DESCRIPTION
63  *
64  *    Formatting function callback given with SILC_STR_FUNC type.  The
65  *    `buffer' is the buffer being formatted at the location where the
66  *    SILC_STR_FUNC was placed in formatting.  The function should call
67  *    silc_buffer_senlarge before it adds the data to the buffer to make
68  *    sure that it has enough space.  The buffer->head points to the
69  *    start of the buffer and silc_buffer_headlen() gives the length
70  *    of the currently formatted data area.  It is also possible to use
71  *    silc_buffer_sformat with `buffer' which will enlarge the buffer if
72  *    needed.
73  *
74  *    The `value' is the value given to SILC_STR_FUNC that is to be formatted
75  *    into the buffer.  It may be NULL if the function is not formatting
76  *    new data into the buffer.  The `context' is caller specific context.
77  *    Returns -1 on error and length of the formatted value otherwise, and
78  *    0 if nothing was formatted.
79  *
80  ***/
81 typedef int (*SilcBufferFormatFunc)(SilcStack stack, SilcBuffer buffer,
82                                     void *value, void *context);
83
84 /****f* silcutil/SilcBufferFormatAPI/SilcBufferUnformatFunc
85  *
86  * SYNOPSIS
87  *
88  *    typedef int (*SilcBufferUnformatFunc)(SilcBuffer buffer,
89  *                                          void **value,
90  *                                          void *context);
91  *
92  * DESCRIPTION
93  *
94  *    Unformatting function callback given with SILC_STR_FUNC type.  The
95  *    `buffer' is the buffer being unformatted and is at the location where
96  *    the SILC_STR_FUNC was placed in unformatting.  The function should
97  *    check there is enough data in the `buffer' before trying to decode
98  *    from it.
99  *
100  *    If this function unformats anything from the buffer its value is to
101  *    be returned to the `value' pointer.  The implementation should itself
102  *    decide whether the unformatted value is allocated or not.  If this
103  *    function does not unformat anything, nothing is returned to `value'
104  *
105  *    The `context' is caller specific context.  Returns -1 on error, and
106  *    length of the unformatted value otherwise, and 0 if nothing was
107  *    unformatted.
108  *
109  ***/
110 typedef int (*SilcBufferUnformatFunc)(SilcStack stack, SilcBuffer buffer,
111                                       void **value, void *context);
112
113 /* Prototypes */
114
115 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_format
116  *
117  * SYNOPSIS
118  *
119  *    int silc_buffer_format(SilcBuffer dst, ...);
120  *
121  * DESCRIPTION
122  *
123  *    Formats a buffer from a variable argument list.  Returns -1 if the
124  *    system is out of memory and the length of the formatted buffer otherwise.
125  *    The buffer is enlarged automatically during formatting, if it doesn't
126  *    already have enough space.
127  *
128  * EXAMPLE
129  *
130  *    Three basic ways of using silc_buffer_format:
131  *
132  *    // Statically allocated zero size buffer
133  *    SilcBufferStruct buffer;
134  *
135  *    memset(&buffer, 0, sizeof(buffer));
136  *    ret = silc_buffer_format(&buffer,
137  *                             SILC_STR_UINT32(intval),
138  *                             SILC_STR_UINT8(charval),
139  *                             SILC_STR_UINT32(intval),
140  *                             SILC_STR_UINT16(str_len),
141  *                             SILC_STR_DATA(str, str_len),
142  *                             SILC_STR_END);
143  *    if (ret < 0)
144  *      error;
145  *
146  *    // Free the allocated data
147  *    silc_buffer_purge(&buffer);
148  *
149  *    // Dynamically allocated zero size buffer
150  *    SilcBuffer buf;
151  *    buf = silc_buffer_alloc(0);
152  *    ret = silc_buffer_format(buf,
153  *                             SILC_STR_UINT32(intval),
154  *                             SILC_STR_UINT8(charval),
155  *                             SILC_STR_END);
156  *    if (ret < 0)
157  *      error;
158  *
159  *    // Free the allocated buffer
160  *    silc_buffer_free(buf);
161  *
162  *    // Dynamically allocated buffer with enough space
163  *    SilcBuffer buf;
164  *    buf = silc_buffer_alloc(2 + str_len);
165  *    ret = silc_buffer_format(buf,
166  *                             SILC_STR_UINT16(str_len),
167  *                             SILC_STR_DATA(str, str_len),
168  *                             SILC_STR_END);
169  *    if (ret < 0)
170  *      error;
171  *
172  ***/
173 int silc_buffer_format(SilcBuffer dst, ...);
174
175 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sformat
176  *
177  * SYNOPSIS
178  *
179  *    int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...);
180  *
181  * DESCRIPTION
182  *
183  *    Same as silc_buffer_format but uses `stack' to allocate the memory.
184  *    if `stack' is NULL reverts back to silc_buffer_format call.  Returns
185  *    -1 if system is out of memory.
186  *
187  *    Note that this call consumes the `stack'.  The caller should push the
188  *    stack before calling the function and pop it later.
189  *
190  ***/
191 int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...);
192
193 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_format_vp
194  *
195  * SYNOPSIS
196  *
197  *    int silc_buffer_format_vp(SilcBuffer dst, va_list vp);
198  *
199  * DESCRIPTION
200  *
201  *    Formats a buffer from a variable argument list indicated by the `ap'.
202  *    Returns -1 if system is out of memory and the length of the formatted
203  *    buffer otherwise.
204  *
205  ***/
206 int silc_buffer_format_vp(SilcBuffer dst, va_list ap);
207
208 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sformat_vp
209  *
210  * SYNOPSIS
211  *
212  *    int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list vp);
213  *
214  * DESCRIPTION
215  *
216  *    Same as silc_buffer_format_vp but uses `stack' to allocate the memory.
217  *    if `stack' is NULL reverts back to silc_buffer_format_vp call.  Returns
218  *    -1 if system is out of memory.
219  *
220  *    Note that this call consumes the `stack'.  The caller should push the
221  *    stack before calling the function and pop it later.
222  *
223  ***/
224 int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap);
225
226 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat
227  *
228  * SYNOPSIS
229  *
230  *    int silc_buffer_unformat(SilcBuffer src, ...);
231  *
232  * DESCRIPTION
233  *
234  *    Unformats a buffer from a variable argument list.  Returns -1 on error
235  *    and the length of the unformatted buffer otherwise.
236  *
237  * EXAMPLE
238  *
239  *    ret = silc_buffer_unformat(buffer,
240  *                               SILC_STR_UINT32(&intval),
241  *                               SILC_STR_UINT8(&charval),
242  *                               SILC_STR_OFFSET(4),
243  *                               SILC_STR_UI16_NSTRING_ALLOC(&str, &str_len),
244  *                               SILC_STR_END);
245  *    if (ret < 0)
246  *      error;
247  *
248  ***/
249 int silc_buffer_unformat(SilcBuffer src, ...);
250
251 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sunformat
252  *
253  * SYNOPSIS
254  *
255  *    int silc_buffer_sunformat(SilcStack stack, SilcBuffer src, ...);
256  *
257  * DESCRIPTION
258  *
259  *    Same as silc_buffer_unformat but uses `stack' to allocate the memory.
260  *    if `stack' is NULL reverts back to silc_buffer_format call.
261  *
262  *    Note that this call consumes the `stack'.  The caller should push the
263  *    stack before calling the function and pop it later.
264  *
265  ***/
266 int silc_buffer_sunformat(SilcStack stack, SilcBuffer src, ...);
267
268 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat_vp
269  *
270  * SYNOPSIS
271  *
272  *    int silc_buffer_unformat_vp(SilcBuffer src, va_list vp);
273  *
274  * DESCRIPTION
275  *
276  *    Unformats a buffer from a variable argument list indicated by the `ap'.
277  *    Returns -1 on error and the length of the unformatted buffer otherwise.
278  *
279  ***/
280 int silc_buffer_unformat_vp(SilcBuffer src, va_list ap);
281
282 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sunformat_vp
283  *
284  * SYNOPSIS
285  *
286  *    int silc_buffer_sunformat_vp(SilcBuffer src, va_list vp);
287  *
288  * DESCRIPTION
289  *
290  *    Same as silc_buffer_unformat_vp but uses `stack' to allocate the
291  *    memory.  if `stack' is NULL reverts back to silc_buffer_format_vp call.
292  *
293  *    Note that this call consumes the `stack'.  The caller should push the
294  *    stack before calling the function and pop it later.
295  *
296  ***/
297 int silc_buffer_sunformat_vp(SilcStack stack, SilcBuffer src, va_list ap);
298
299 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_strformat
300  *
301  * SYNOPSIS
302  *
303  *    int silc_buffer_strformat(SilcBuffer dst, ...);
304  *
305  * DESCRIPTION
306  *
307  *    Formats a buffer from variable argument list of strings.  Each
308  *    string must be NULL-terminated and the variable argument list must
309  *    be end with SILC_STRFMT_END argument.  This allows that a string in
310  *    the list can be NULL, in which case it is skipped.  This automatically
311  *    allocates the space for the buffer data but `dst' must be already
312  *    allocated by the caller.  Returns -1 if system is out of memory.
313  *
314  * EXAMPLE
315  *
316  *    ret = silc_buffer_strformat(buffer, "foo", "bar", SILC_STRFMT_END);
317  *    if (ret < 0)
318  *      error;
319  *
320  ***/
321 int silc_buffer_strformat(SilcBuffer dst, ...);
322
323 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sstrformat
324  *
325  * SYNOPSIS
326  *
327  *    int silc_buffer_strformat(SilcStack stack, SilcBuffer dst, ...);
328  *
329  * DESCRIPTION
330  *
331  *    Formats a buffer from variable argument list of strings.  Each
332  *    string must be NULL-terminated and the variable argument list must
333  *    be end with SILC_STRFMT_END argument.  This allows that a string in
334  *    the list can be NULL, in which case it is skipped.  This automatically
335  *    allocates the space for the buffer data but `dst' must be already
336  *    allocated by the caller.  This function is equivalent to
337  *    silc_buffer_strformat but allocates memory from `stack'.  Returns -1
338  *    if system is out of memory.
339  *
340  *    Note that this call consumes the `stack'.  The caller should push the
341  *    stack before calling the function and pop it later.
342  *
343  ***/
344 int silc_buffer_sstrformat(SilcStack stack, SilcBuffer dst, ...);
345
346 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT8
347  *
348  * NAME
349  *
350  *    #define SILC_STR_SINT8() ...
351  *
352  * DESCRIPTION
353  *
354  *    One 8-bit signed integer.
355  *
356  *    Formatting:    SILC_STR_SINT8(SilcInt8)
357  *    Unformatting:  SILC_STR_SINT8(SilcInt8 *)
358  *
359  ***/
360 #define SILC_STR_SINT8(x) SILC_PARAM_SINT8, (x)
361
362 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT8
363  *
364  * NAME
365  *
366  *    #define SILC_STR_UINT8() ...
367  *
368  * DESCRIPTION
369  *
370  *    One 8-bit unsigned integer.
371  *
372  *    Formatting:    SILC_STR_UINT8(SilcUInt8)
373  *    Unformatting:  SILC_STR_UINT8(SilcUInt8 *)
374  *
375  ***/
376 #define SILC_STR_UINT8(x) SILC_PARAM_UINT8, (x)
377
378 /* Deprecated */
379 #define SILC_STR_SI_CHAR(x) SILC_PARAM_SINT8, (x)
380 #define SILC_STR_UI_CHAR(x) SILC_PARAM_UINT8, (x)
381
382 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT16
383  *
384  * NAME
385  *
386  *    #define SILC_STR_SINT16() ...
387  *
388  * DESCRIPTION
389  *
390  *    SilcInt16.
391  *
392  *    Formatting:    SILC_STR_SINT16(SilcInt16)
393  *    Unformatting:  SILC_STR_SINT16(SilcInt16 *)
394  *
395  ***/
396 #define SILC_STR_SINT16(x) SILC_PARAM_SINT16, (x)
397
398 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT16
399  *
400  * NAME
401  *
402  *    #define SILC_STR_UINT16() ...
403  *
404  * DESCRIPTION
405  *
406  *    SilcUInt16.
407  *
408  *    Formatting:    SILC_STR_UINT16(SilcUInt16)
409  *    Unformatting:  SILC_STR_UINT16(SilcUInt16 *)
410  *
411  ***/
412 #define SILC_STR_UINT16(x) SILC_PARAM_UINT16, (x)
413
414 /* Deprecated */
415 #define SILC_STR_SI_SHORT(x) SILC_PARAM_SINT16, (x)
416 #define SILC_STR_UI_SHORT(x) SILC_PARAM_UINT16, (x)
417
418 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT32
419  *
420  * NAME
421  *
422  *    #define SILC_STR_SINT32() ...
423  *
424  * DESCRIPTION
425  *
426  *    SilcInt32.
427  *
428  *    Formatting:    SILC_STR_SINT32(SilcInt32)
429  *    Unformatting:  SILC_STR_SINT32(SilcInt32 *)
430  *
431  ***/
432 #define SILC_STR_SINT32(x) SILC_PARAM_SINT32, (x)
433
434 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT32
435  *
436  * NAME
437  *
438  *    #define SILC_STR_UINT32() ...
439  *
440  * DESCRIPTION
441  *
442  *    SilcUInt32.
443  *
444  *    Formatting:    SILC_STR_UINT32(SilcUInt32)
445  *    Unformatting:  SILC_STR_UINT32(SilcUInt32 *)
446  *
447  ***/
448 #define SILC_STR_UINT32(x) SILC_PARAM_UINT32, (x)
449
450 /* Deprecated */
451 #define SILC_STR_SI_INT(x) SILC_PARAM_SINT32, (x)
452 #define SILC_STR_UI_INT(x) SILC_PARAM_UINT32, (x)
453
454 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT64
455  *
456  * NAME
457  *
458  *    #define SILC_STR_SINT64() ...
459  *
460  * DESCRIPTION
461  *
462  *    SilcInt64.
463  *
464  *    Formatting:    SILC_STR_SINT64(SilcInt64)
465  *    Unformatting:  SILC_STR_SINT64(SilcInt64 *)
466  *
467  ***/
468 #define SILC_STR_SI_INT64(x) SILC_PARAM_SINT64, (x)
469
470 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT64
471  *
472  * NAME
473  *
474  *    #define SILC_STR_UINT64() ...
475  *
476  * DESCRIPTION
477  *
478  *    SilcUInt64.
479  *
480  *    Formatting:    SILC_STR_UINT64(SilcUInt64)
481  *    Unformatting:  SILC_STR_UINT64(SilcUInt64 *)
482  *
483  ***/
484 #define SILC_STR_UI_INT64(x) SILC_PARAM_UINT64, (x)
485
486 /* Deprecated */
487 #define SILC_STR_SI_INT64(x) SILC_PARAM_SINT64, (x)
488 #define SILC_STR_UI_INT64(x) SILC_PARAM_UINT64, (x)
489
490 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_STRING
491  *
492  * NAME
493  *
494  *    #define SILC_STR_UI8_STRING() ...
495  *    #define SILC_STR_UI8_STRING_ALLOC() ...
496  *    #define SILC_STR_UI16_STRING() ...
497  *    #define SILC_STR_UI16_STRING_ALLOC() ...
498  *    #define SILC_STR_UI32_STRING() ...
499  *    #define SILC_STR_UI32_STRING_ALLOC() ...
500  *
501  * DESCRIPTION
502  *
503  *    Unsigned NULL terminated string. Note that the string must be
504  *    NULL terminated because strlen() will be used to get the length of
505  *    the string.
506  *
507  *    Formatting:    SILC_STR_UI32_STRING(unsigned char *)
508  *    Unformatting:  SILC_STR_UI32_STRING(unsigned char **)
509  *
510  *    Unformatting procedure will check for length of the string from the
511  *    buffer before trying to get the string out. Thus, one *must* format the
512  *    length as UI_INT or UI_SHORT into the buffer *before* formatting the
513  *    actual string to the buffer, and, in unformatting one must ignore the
514  *    length of the string because unformatting procedure will take it
515  *    automatically.
516  *
517  *    Example:
518  *
519  *    Formatting:    ..., SILC_STR_UI_INT(strlen(string)),
520  *                        SILC_STR_UI32_STRING(string), ...
521  *    Unformatting:  ..., SILC_STR_UI32_STRING(&string), ...
522  *
523  *    I.e., you can ignore the formatted length field in unformatting.
524  *
525  *    UI8, UI16 and UI32 means that the length is considered to be
526  *    either char (8 bits), short (16 bits) or int (32 bits) in
527  *    unformatting.
528  *
529  *    _ALLOC routines automatically allocates memory for the variable sent
530  *    as argument in unformatting.
531  *
532  ***/
533 #define SILC_STR_UI8_STRING(x) SILC_PARAM_UI8_STRING, (x)
534 #define SILC_STR_UI8_STRING_ALLOC(x) SILC_PARAM_UI8_STRING | SILC_PARAM_ALLOC, (x)
535 #define SILC_STR_UI16_STRING(x) SILC_PARAM_UI16_STRING, (x)
536 #define SILC_STR_UI16_STRING_ALLOC(x) SILC_PARAM_UI16_STRING | SILC_PARAM_ALLOC, (x)
537 #define SILC_STR_UI32_STRING(x) SILC_PARAM_UI32_STRING, (x)
538 #define SILC_STR_UI32_STRING_ALLOC(x) SILC_PARAM_UI32_STRING | SILC_PARAM_ALLOC, (x)
539
540 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_NSTRING
541  *
542  * NAME
543  *
544  *    #define SILC_STR_UI8_NSTRING() ...
545  *    #define SILC_STR_UI8_NSTRING_ALLOC() ...
546  *    #define SILC_STR_UI16_NSTRING() ...
547  *    #define SILC_STR_UI16_NSTRING_ALLOC() ...
548  *    #define SILC_STR_UI32_NSTRING() ...
549  *    #define SILC_STR_UI32_NSTRING_ALLOC() ...
550  *
551  * DESCRIPTION
552  *
553  *    Unsigned string. Second argument is the length of the string.
554  *
555  *    Formatting:    SILC_STR_UI32_NSTRING(unsigned char *, SilcUInt32)
556  *    Unformatting:  SILC_STR_UI32_NSTRING(unsigned char **, SilcUInt32 *)
557  *
558  *    Unformatting procedure will check for length of the string from the
559  *    buffer before trying to get the string out. Thus, one *must* format the
560  *    length as UI_INT or UI_SHORT into the buffer *before* formatting the
561  *    actual string to the buffer, and, in unformatting one must ignore the
562  *    length of the string because unformatting procedure will take it
563  *    automatically.
564  *
565  *     Example:
566  *
567  *     Formatting:    ..., SILC_STR_UI_INT(strlen(string)),
568  *                         SILC_STR_UI32_NSTRING(string, strlen(string)), ...
569  *     Unformatting:  ..., SILC_STR_UI32_NSTRING(&string, &len), ...
570  *
571  *    I.e., you can ignore the formatted length field in unformatting. The
572  *    length taken from the buffer is returned to the pointer sent as
573  *    argument (&len in above example).
574  *
575  *    UI8, UI16 and UI32 means that the length is considered to be
576  *    either char (8 bits), short (16 bits) or int (32 bits) in
577  *    unformatting.
578  *
579  *    _ALLOC routines automatically allocates memory for the variable sent
580  *    as argument in unformatting.
581  *
582  ***/
583 #define SILC_STR_UI8_NSTRING(x, l) SILC_PARAM_UI8_NSTRING, (x), (l)
584 #define SILC_STR_UI8_NSTRING_ALLOC(x, l) \
585   SILC_PARAM_UI8_NSTRING | SILC_PARAM_ALLOC, (x), (l)
586 #define SILC_STR_UI16_NSTRING(x, l) SILC_PARAM_UI16_NSTRING, (x), (l)
587 #define SILC_STR_UI16_NSTRING_ALLOC(x, l) \
588   SILC_PARAM_UI16_NSTRING | SILC_PARAM_ALLOC, (x), (l)
589 #define SILC_STR_UI32_NSTRING(x, l) SILC_PARAM_UI32_NSTRING, (x), (l)
590 #define SILC_STR_UI32_NSTRING_ALLOC(x, l) \
591   SILC_PARAM_UI32_NSTRING | SILC_PARAM_ALLOC, (x), (l)
592
593 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_DATA
594  *
595  * NAME
596  *
597  *    #define SILC_STR_DATA() ...
598  *    #define SILC_STR_DATA_ALLOC() ...
599  *
600  * DESCRIPTION
601  *
602  *    Binary data formatting.  Second argument is the length of the data.
603  *
604  *    Formatting:    SILC_STR_DATA(unsigned char *, SilcUInt32)
605  *    Unformatting:  SILC_STR_DATA(unsigned char **, SilcUInt32)
606  *
607  *    This type can be used to take arbitrary size data block from the buffer
608  *    by sending the requested amount of bytes as argument.
609  *
610  *    _ALLOC routines automatically allocates memory for the variable sent
611  *    as argument in unformatting.
612  *
613  ***/
614 #define SILC_STR_DATA(x, l) SILC_PARAM_UICHAR, (x), (l)
615 #define SILC_STR_DATA_ALLOC(x, l) SILC_PARAM_UICHAR | SILC_PARAM_ALLOC, (x), (l)
616
617 /* Deprecated */
618 #define SILC_STR_UI_XNSTRING(x, l) SILC_PARAM_UICHAR, (x), (l)
619 #define SILC_STR_UI_XNSTRING_ALLOC(x, l) SILC_PARAM_UICHAR | SILC_PARAM_ALLOC, (x), (l)
620
621 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_BUFFER
622  *
623  * NAME
624  *
625  *    #define SILC_STR_BUFFER() ...
626  *    #define SILC_STR_BUFFER_ALLOC() ...
627  *
628  * DESCRIPTION
629  *
630  *    SilcBuffer formatting.
631  *
632  *    Formatting:    SILC_STR_BUFFER(SilcBuffer)
633  *    Unformatting:  SILC_STR_BUFFER(SilcBuffer)
634  *
635  *    This type can be used to format and unformat SilcBuffer.  Note that, the
636  *    length of the buffer will be automatically encoded into the buffer as
637  *    a 32-bit integer.  In unformatting the SilcBuffer context must be
638  *    pre-allocated.
639  *
640  *    _ALLOC routines automatically allocates memory inside SilcBuffer in
641  *    unformatting.
642  *
643  ***/
644 #define SILC_STR_BUFFER(x) SILC_PARAM_BUFFER, (x)
645 #define SILC_STR_BUFFER_ALLOC(x) SILC_PARAM_BUFFER | SILC_PARAM_ALLOC, (x)
646
647 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_FUNC
648  *
649  * NAME
650  *
651  *    #define SILC_STR_FUNC() ...
652  *
653  * DESCRIPTION
654  *
655  *    SilcBuffer formatting.
656  *
657  *    Formatting:    SILC_STR_FUNC(function, void *value, void *context)
658  *    Unformatting:  SILC_STR_FUNC(function, void **value, void *context)
659  *
660  *    This type can be used to call the `function' of the type
661  *    SilcBufferFormatFunc or SilcBufferUnformatFunc to encode or decode
662  *    the `value'.  In encoding the `value' will be passed to the `function'
663  *    and can be encoded into the buffer.  The buffer will be passed as
664  *    well to the `function' at the location where SILC_STR_FUNC is placed
665  *    in formatting.  The `context' delivers caller specific context to
666  *    the `function'
667  *
668  *    In unformatting the `function' will decode the encoded type and
669  *    return it to `value' pointer.  The decoding function should decide
670  *    itself whether to allocate or not the decoded value.
671  *
672  *    The `function' does not have to encode anything and passing `value'
673  *    as NULL is allowed.  The `function' could for example modify the
674  *    existing buffer.
675  *
676  * EXAMPLE
677  *
678  *    // Encode payload, encrypt and compute MAC.
679  *    silc_buffer_format(buf,
680  *                       SILC_STR_FUNC(foo_encode_id, id, ctx),
681  *                       SILC_STR_UINT16(len),
682  *                       SILC_STR_DATA(data, len),
683  *                       SILC_STR_FUNC(foo_buf_encrypt, NULL, key),
684  *                       SILC_STR_FUNC(foo_buf_hmac, NULL, hmac),
685  *                       SILC_STR_DATA(iv, iv_len);
686  *                       SILC_STR_END);
687  *
688  *    // Check MAC, decrypt and decode payload
689  *    silc_buffer_unformat(buf,
690  *                         SILC_STR_FUNC(foo_buf_hmac, NULL, hmac),
691  *                         SILC_STR_FUNC(foo_buf_decrypt, NULL, key),
692  *                         SILC_STR_FUNC(foo_decode_id, &id, ctx),
693  *                         SILC_STR_UINT16(&len),
694  *                         SILC_STR_END);
695  *
696  ***/
697 #define SILC_STR_FUNC(func, val, context) SILC_PARAM_FUNC, \
698     func, (val), (context)
699
700 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_OFFSET
701  *
702  * NAME
703  *
704  *    #define SILC_STR_OFFSET() ...
705  *
706  * DESCRIPTION
707  *
708  *    Offset in buffer.  This can be used in formatting and unformatting to
709  *    move the data pointer of the buffer either forwards (positive offset)
710  *    or backwards (negative offset).  It can be used to for example skip
711  *    some types during unformatting.
712  *
713  *    Example:
714  *
715  *    ..., SILC_STR_OFFSET(5), ...
716  *    ..., SILC_STR_OFFSET(-3), ...
717  *
718  *    Moves the data pointer at the point of the offset either forward
719  *    or backward and then moves to the next type.  Multiple SILC_STR_OFFSETs
720  *    can be used in formatting and unformatting at the same time.
721  *
722  ***/
723 #define SILC_STR_OFFSET(x) SILC_PARAM_OFFSET, (x)
724
725 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_ADVANCE
726  *
727  * NAME
728  *
729  *    #define SILC_STR_ADVANCE ...
730  *
731  * DESCRIPTION
732  *
733  *    Advance the buffer to the end of the data after the formatting is
734  *    done.  In normal operation when the formatted data is written the
735  *    buffer is positioned at the start of the data.  With SILC_STR_ADVANCE
736  *    the buffer will be positioned at the end of the data.  This makes it
737  *    easy to add new data immediately after the previously added data.
738  *    The SILC_STR_ADVANCE may also be used in unformatting.
739  *
740  * EXAMPLE
741  *
742  *    do {
743  *      len = read(fd, buf, sizeof(buf));
744  *      if (len > 0)
745  *        // Add read data to the buffer
746  *        silc_buffer_format(buffer,
747  *                           SILC_STR_ADVANCE,
748  *                           SILC_STR_DATA(buf, len),
749  *                           SILC_STR_END);
750  *    } while (len > 0);
751  *
752  *    // Move to beginning of buffer
753  *    silc_buffer_start(buffer);
754  *
755  ***/
756 #define SILC_STR_ADVANCE SILC_PARAM_ADVANCE
757
758 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_END
759  *
760  * NAME
761  *
762  *    #define SILC_STR_END ...
763  *
764  * DESCRIPTION
765  *
766  *    Marks end of the argument list. This must be at the end of the
767  *    argument list or error will occur.
768  *
769  ***/
770 #define SILC_STR_END SILC_PARAM_END
771
772 /****d* silcutil/SilcBufferFormatAPI/SILC_STRFMT_END
773  *
774  * NAME
775  *
776  *    #define SILC_STRFMT_END ...
777  *
778  * DESCRIPTION
779  *
780  *    Marks end of the argument list in silc_buffer_strformat function.
781  *    This must be at the end of the argument list or error will occur.
782  *
783  ***/
784 #define SILC_STRFMT_END (void *)SILC_STR_END
785
786 #endif  /* !SILCBUFFMT_H */