4668f7f975d75d23bc4f34c6903cbaf19f2c792a
[crypto.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.  Sets silc_errno in case of error.
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.  Sets silc_errno in case of error.
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.  Sets silc_errno in case of error.
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.  Sets silc_errno
236  *    in case of error.
237  *
238  * EXAMPLE
239  *
240  *    ret = silc_buffer_unformat(buffer,
241  *                               SILC_STR_UINT32(&intval),
242  *                               SILC_STR_UINT8(&charval),
243  *                               SILC_STR_OFFSET(4),
244  *                               SILC_STR_UI16_NSTRING_ALLOC(&str, &str_len),
245  *                               SILC_STR_END);
246  *    if (ret < 0)
247  *      error;
248  *
249  ***/
250 int silc_buffer_unformat(SilcBuffer src, ...);
251
252 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sunformat
253  *
254  * SYNOPSIS
255  *
256  *    int silc_buffer_sunformat(SilcStack stack, SilcBuffer src, ...);
257  *
258  * DESCRIPTION
259  *
260  *    Same as silc_buffer_unformat but uses `stack' to allocate the memory.
261  *    if `stack' is NULL reverts back to silc_buffer_format call.
262  *
263  *    Note that this call consumes the `stack'.  The caller should push the
264  *    stack before calling the function and pop it later.
265  *
266  ***/
267 int silc_buffer_sunformat(SilcStack stack, SilcBuffer src, ...);
268
269 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat_vp
270  *
271  * SYNOPSIS
272  *
273  *    int silc_buffer_unformat_vp(SilcBuffer src, va_list vp);
274  *
275  * DESCRIPTION
276  *
277  *    Unformats a buffer from a variable argument list indicated by the `ap'.
278  *    Returns -1 on error and the length of the unformatted buffer otherwise.
279  *
280  ***/
281 int silc_buffer_unformat_vp(SilcBuffer src, va_list ap);
282
283 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sunformat_vp
284  *
285  * SYNOPSIS
286  *
287  *    int silc_buffer_sunformat_vp(SilcBuffer src, va_list vp);
288  *
289  * DESCRIPTION
290  *
291  *    Same as silc_buffer_unformat_vp but uses `stack' to allocate the
292  *    memory.  if `stack' is NULL reverts back to silc_buffer_format_vp call.
293  *
294  *    Note that this call consumes the `stack'.  The caller should push the
295  *    stack before calling the function and pop it later.
296  *
297  ***/
298 int silc_buffer_sunformat_vp(SilcStack stack, SilcBuffer src, va_list ap);
299
300 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_strformat
301  *
302  * SYNOPSIS
303  *
304  *    int silc_buffer_strformat(SilcBuffer dst, ...);
305  *
306  * DESCRIPTION
307  *
308  *    Formats a buffer from variable argument list of strings.  Each
309  *    string must be NULL-terminated and the variable argument list must
310  *    be end with SILC_STRFMT_END argument.  This allows that a string in
311  *    the list can be NULL, in which case it is skipped.  This automatically
312  *    allocates the space for the buffer data but `dst' must be already
313  *    allocated by the caller.  Returns -1 if system is out of memory and
314  *    sets silc_errno.
315  *
316  * EXAMPLE
317  *
318  *    ret = silc_buffer_strformat(buffer, "foo", "bar", SILC_STRFMT_END);
319  *    if (ret < 0)
320  *      error;
321  *
322  ***/
323 int silc_buffer_strformat(SilcBuffer dst, ...);
324
325 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sstrformat
326  *
327  * SYNOPSIS
328  *
329  *    int silc_buffer_strformat(SilcStack stack, SilcBuffer dst, ...);
330  *
331  * DESCRIPTION
332  *
333  *    Formats a buffer from variable argument list of strings.  Each
334  *    string must be NULL-terminated and the variable argument list must
335  *    be end with SILC_STRFMT_END argument.  This allows that a string in
336  *    the list can be NULL, in which case it is skipped.  This automatically
337  *    allocates the space for the buffer data but `dst' must be already
338  *    allocated by the caller.  This function is equivalent to
339  *    silc_buffer_strformat but allocates memory from `stack'.  Returns -1
340  *    if system is out of memory and sets silc_errno.
341  *
342  *    Note that this call consumes the `stack'.  The caller should push the
343  *    stack before calling the function and pop it later.
344  *
345  ***/
346 int silc_buffer_sstrformat(SilcStack stack, SilcBuffer dst, ...);
347
348 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT8
349  *
350  * NAME
351  *
352  *    #define SILC_STR_SINT8() ...
353  *
354  * DESCRIPTION
355  *
356  *    One 8-bit signed integer.
357  *
358  *    Formatting:    SILC_STR_SINT8(SilcInt8)
359  *    Unformatting:  SILC_STR_SINT8(SilcInt8 *)
360  *
361  ***/
362 #define SILC_STR_SINT8(x) SILC_PARAM_SINT8, (x)
363
364 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT8
365  *
366  * NAME
367  *
368  *    #define SILC_STR_UINT8() ...
369  *
370  * DESCRIPTION
371  *
372  *    One 8-bit unsigned integer.
373  *
374  *    Formatting:    SILC_STR_UINT8(SilcUInt8)
375  *    Unformatting:  SILC_STR_UINT8(SilcUInt8 *)
376  *
377  ***/
378 #define SILC_STR_UINT8(x) SILC_PARAM_UINT8, (x)
379
380 /* Deprecated */
381 #define SILC_STR_SI_CHAR(x) SILC_PARAM_SINT8, (x)
382 #define SILC_STR_UI_CHAR(x) SILC_PARAM_UINT8, (x)
383
384 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT16
385  *
386  * NAME
387  *
388  *    #define SILC_STR_SINT16() ...
389  *
390  * DESCRIPTION
391  *
392  *    SilcInt16.
393  *
394  *    Formatting:    SILC_STR_SINT16(SilcInt16)
395  *    Unformatting:  SILC_STR_SINT16(SilcInt16 *)
396  *
397  ***/
398 #define SILC_STR_SINT16(x) SILC_PARAM_SINT16, (x)
399
400 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT16
401  *
402  * NAME
403  *
404  *    #define SILC_STR_UINT16() ...
405  *
406  * DESCRIPTION
407  *
408  *    SilcUInt16.
409  *
410  *    Formatting:    SILC_STR_UINT16(SilcUInt16)
411  *    Unformatting:  SILC_STR_UINT16(SilcUInt16 *)
412  *
413  ***/
414 #define SILC_STR_UINT16(x) SILC_PARAM_UINT16, (x)
415
416 /* Deprecated */
417 #define SILC_STR_SI_SHORT(x) SILC_PARAM_SINT16, (x)
418 #define SILC_STR_UI_SHORT(x) SILC_PARAM_UINT16, (x)
419
420 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT32
421  *
422  * NAME
423  *
424  *    #define SILC_STR_SINT32() ...
425  *
426  * DESCRIPTION
427  *
428  *    SilcInt32.
429  *
430  *    Formatting:    SILC_STR_SINT32(SilcInt32)
431  *    Unformatting:  SILC_STR_SINT32(SilcInt32 *)
432  *
433  ***/
434 #define SILC_STR_SINT32(x) SILC_PARAM_SINT32, (x)
435
436 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT32
437  *
438  * NAME
439  *
440  *    #define SILC_STR_UINT32() ...
441  *
442  * DESCRIPTION
443  *
444  *    SilcUInt32.
445  *
446  *    Formatting:    SILC_STR_UINT32(SilcUInt32)
447  *    Unformatting:  SILC_STR_UINT32(SilcUInt32 *)
448  *
449  ***/
450 #define SILC_STR_UINT32(x) SILC_PARAM_UINT32, (x)
451
452 /* Deprecated */
453 #define SILC_STR_SI_INT(x) SILC_PARAM_SINT32, (x)
454 #define SILC_STR_UI_INT(x) SILC_PARAM_UINT32, (x)
455
456 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT64
457  *
458  * NAME
459  *
460  *    #define SILC_STR_SINT64() ...
461  *
462  * DESCRIPTION
463  *
464  *    SilcInt64.
465  *
466  *    Formatting:    SILC_STR_SINT64(SilcInt64)
467  *    Unformatting:  SILC_STR_SINT64(SilcInt64 *)
468  *
469  ***/
470 #define SILC_STR_SI_INT64(x) SILC_PARAM_SINT64, (x)
471
472 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT64
473  *
474  * NAME
475  *
476  *    #define SILC_STR_UINT64() ...
477  *
478  * DESCRIPTION
479  *
480  *    SilcUInt64.
481  *
482  *    Formatting:    SILC_STR_UINT64(SilcUInt64)
483  *    Unformatting:  SILC_STR_UINT64(SilcUInt64 *)
484  *
485  ***/
486 #define SILC_STR_UI_INT64(x) SILC_PARAM_UINT64, (x)
487
488 /* Deprecated */
489 #define SILC_STR_SI_INT64(x) SILC_PARAM_SINT64, (x)
490 #define SILC_STR_UI_INT64(x) SILC_PARAM_UINT64, (x)
491
492 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_STRING
493  *
494  * NAME
495  *
496  *    #define SILC_STR_UI8_STRING() ...
497  *    #define SILC_STR_UI8_STRING_ALLOC() ...
498  *    #define SILC_STR_UI16_STRING() ...
499  *    #define SILC_STR_UI16_STRING_ALLOC() ...
500  *    #define SILC_STR_UI32_STRING() ...
501  *    #define SILC_STR_UI32_STRING_ALLOC() ...
502  *
503  * DESCRIPTION
504  *
505  *    Unsigned NULL terminated string. Note that the string must be
506  *    NULL terminated because strlen() will be used to get the length of
507  *    the string.
508  *
509  *    Formatting:    SILC_STR_UI32_STRING(unsigned char *)
510  *    Unformatting:  SILC_STR_UI32_STRING(unsigned char **)
511  *
512  *    Unformatting procedure will check for length of the string from the
513  *    buffer before trying to get the string out. Thus, one *must* format the
514  *    length as UI_INT or UI_SHORT into the buffer *before* formatting the
515  *    actual string to the buffer, and, in unformatting one must ignore the
516  *    length of the string because unformatting procedure will take it
517  *    automatically.
518  *
519  *    Example:
520  *
521  *    Formatting:    ..., SILC_STR_UI_INT(strlen(string)),
522  *                        SILC_STR_UI32_STRING(string), ...
523  *    Unformatting:  ..., SILC_STR_UI32_STRING(&string), ...
524  *
525  *    I.e., you can ignore the formatted length field in unformatting.
526  *
527  *    UI8, UI16 and UI32 means that the length is considered to be
528  *    either char (8 bits), short (16 bits) or int (32 bits) in
529  *    unformatting.
530  *
531  *    _ALLOC routines automatically allocates memory for the variable sent
532  *    as argument in unformatting.
533  *
534  ***/
535 #define SILC_STR_UI8_STRING(x) SILC_PARAM_UI8_STRING, (x)
536 #define SILC_STR_UI8_STRING_ALLOC(x) SILC_PARAM_UI8_STRING | SILC_PARAM_ALLOC, (x)
537 #define SILC_STR_UI16_STRING(x) SILC_PARAM_UI16_STRING, (x)
538 #define SILC_STR_UI16_STRING_ALLOC(x) SILC_PARAM_UI16_STRING | SILC_PARAM_ALLOC, (x)
539 #define SILC_STR_UI32_STRING(x) SILC_PARAM_UI32_STRING, (x)
540 #define SILC_STR_UI32_STRING_ALLOC(x) SILC_PARAM_UI32_STRING | SILC_PARAM_ALLOC, (x)
541
542 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_NSTRING
543  *
544  * NAME
545  *
546  *    #define SILC_STR_UI8_NSTRING() ...
547  *    #define SILC_STR_UI8_NSTRING_ALLOC() ...
548  *    #define SILC_STR_UI16_NSTRING() ...
549  *    #define SILC_STR_UI16_NSTRING_ALLOC() ...
550  *    #define SILC_STR_UI32_NSTRING() ...
551  *    #define SILC_STR_UI32_NSTRING_ALLOC() ...
552  *
553  * DESCRIPTION
554  *
555  *    Unsigned string. Second argument is the length of the string.
556  *
557  *    Formatting:    SILC_STR_UI32_NSTRING(unsigned char *, SilcUInt32)
558  *    Unformatting:  SILC_STR_UI32_NSTRING(unsigned char **, SilcUInt32 *)
559  *
560  *    Unformatting procedure will check for length of the string from the
561  *    buffer before trying to get the string out. Thus, one *must* format the
562  *    length as UI_INT or UI_SHORT into the buffer *before* formatting the
563  *    actual string to the buffer, and, in unformatting one must ignore the
564  *    length of the string because unformatting procedure will take it
565  *    automatically.
566  *
567  *     Example:
568  *
569  *     Formatting:    ..., SILC_STR_UI_INT(strlen(string)),
570  *                         SILC_STR_UI32_NSTRING(string, strlen(string)), ...
571  *     Unformatting:  ..., SILC_STR_UI32_NSTRING(&string, &len), ...
572  *
573  *    I.e., you can ignore the formatted length field in unformatting. The
574  *    length taken from the buffer is returned to the pointer sent as
575  *    argument (&len in above example).
576  *
577  *    UI8, UI16 and UI32 means that the length is considered to be
578  *    either char (8 bits), short (16 bits) or int (32 bits) in
579  *    unformatting.
580  *
581  *    _ALLOC routines automatically allocates memory for the variable sent
582  *    as argument in unformatting.
583  *
584  ***/
585 #define SILC_STR_UI8_NSTRING(x, l) SILC_PARAM_UI8_NSTRING, (x), (l)
586 #define SILC_STR_UI8_NSTRING_ALLOC(x, l) \
587   SILC_PARAM_UI8_NSTRING | SILC_PARAM_ALLOC, (x), (l)
588 #define SILC_STR_UI16_NSTRING(x, l) SILC_PARAM_UI16_NSTRING, (x), (l)
589 #define SILC_STR_UI16_NSTRING_ALLOC(x, l) \
590   SILC_PARAM_UI16_NSTRING | SILC_PARAM_ALLOC, (x), (l)
591 #define SILC_STR_UI32_NSTRING(x, l) SILC_PARAM_UI32_NSTRING, (x), (l)
592 #define SILC_STR_UI32_NSTRING_ALLOC(x, l) \
593   SILC_PARAM_UI32_NSTRING | SILC_PARAM_ALLOC, (x), (l)
594
595 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_DATA
596  *
597  * NAME
598  *
599  *    #define SILC_STR_DATA() ...
600  *    #define SILC_STR_DATA_ALLOC() ...
601  *
602  * DESCRIPTION
603  *
604  *    Binary data formatting.  Second argument is the length of the data.
605  *
606  *    Formatting:    SILC_STR_DATA(unsigned char *, SilcUInt32)
607  *    Unformatting:  SILC_STR_DATA(unsigned char **, SilcUInt32)
608  *
609  *    This type can be used to take arbitrary size data block from the buffer
610  *    by sending the requested amount of bytes as argument.
611  *
612  *    _ALLOC routines automatically allocates memory for the variable sent
613  *    as argument in unformatting.
614  *
615  ***/
616 #define SILC_STR_DATA(x, l) SILC_PARAM_UICHAR, (x), (l)
617 #define SILC_STR_DATA_ALLOC(x, l) SILC_PARAM_UICHAR | SILC_PARAM_ALLOC, (x), (l)
618
619 /* Deprecated */
620 #define SILC_STR_UI_XNSTRING(x, l) SILC_PARAM_UICHAR, (x), (l)
621 #define SILC_STR_UI_XNSTRING_ALLOC(x, l) SILC_PARAM_UICHAR | SILC_PARAM_ALLOC, (x), (l)
622
623 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_BUFFER
624  *
625  * NAME
626  *
627  *    #define SILC_STR_BUFFER() ...
628  *    #define SILC_STR_BUFFER_ALLOC() ...
629  *
630  * DESCRIPTION
631  *
632  *    SilcBuffer formatting.
633  *
634  *    Formatting:    SILC_STR_BUFFER(SilcBuffer)
635  *    Unformatting:  SILC_STR_BUFFER(SilcBuffer)
636  *
637  *    This type can be used to format and unformat SilcBuffer.  Note that, the
638  *    length of the buffer will be automatically encoded into the buffer as
639  *    a 32-bit integer.  In unformatting the SilcBuffer context must be
640  *    pre-allocated.
641  *
642  *    _ALLOC routines automatically allocates memory inside SilcBuffer in
643  *    unformatting.
644  *
645  ***/
646 #define SILC_STR_BUFFER(x) SILC_PARAM_BUFFER, (x)
647 #define SILC_STR_BUFFER_ALLOC(x) SILC_PARAM_BUFFER | SILC_PARAM_ALLOC, (x)
648
649 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_FUNC
650  *
651  * NAME
652  *
653  *    #define SILC_STR_FUNC() ...
654  *
655  * DESCRIPTION
656  *
657  *    SilcBuffer formatting.
658  *
659  *    Formatting:    SILC_STR_FUNC(function, void *value, void *context)
660  *    Unformatting:  SILC_STR_FUNC(function, void **value, void *context)
661  *
662  *    This type can be used to call the `function' of the type
663  *    SilcBufferFormatFunc or SilcBufferUnformatFunc to encode or decode
664  *    the `value'.  In encoding the `value' will be passed to the `function'
665  *    and can be encoded into the buffer.  The buffer will be passed as
666  *    well to the `function' at the location where SILC_STR_FUNC is placed
667  *    in formatting.  The `context' delivers caller specific context to
668  *    the `function'
669  *
670  *    In unformatting the `function' will decode the encoded type and
671  *    return it to `value' pointer.  The decoding function should decide
672  *    itself whether to allocate or not the decoded value.
673  *
674  *    The `function' does not have to encode anything and passing `value'
675  *    as NULL is allowed.  The `function' could for example modify the
676  *    existing buffer.
677  *
678  * EXAMPLE
679  *
680  *    // Encode payload, encrypt and compute MAC.
681  *    silc_buffer_format(buf,
682  *                       SILC_STR_FUNC(foo_encode_id, id, ctx),
683  *                       SILC_STR_UINT16(len),
684  *                       SILC_STR_DATA(data, len),
685  *                       SILC_STR_FUNC(foo_buf_encrypt, NULL, key),
686  *                       SILC_STR_FUNC(foo_buf_hmac, NULL, hmac),
687  *                       SILC_STR_DATA(iv, iv_len);
688  *                       SILC_STR_END);
689  *
690  *    // Check MAC, decrypt and decode payload
691  *    silc_buffer_unformat(buf,
692  *                         SILC_STR_FUNC(foo_buf_hmac, NULL, hmac),
693  *                         SILC_STR_FUNC(foo_buf_decrypt, NULL, key),
694  *                         SILC_STR_FUNC(foo_decode_id, &id, ctx),
695  *                         SILC_STR_UINT16(&len),
696  *                         SILC_STR_END);
697  *
698  ***/
699 #define SILC_STR_FUNC(func, val, context) SILC_PARAM_FUNC, \
700     func, (val), (context)
701
702 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_OFFSET
703  *
704  * NAME
705  *
706  *    #define SILC_STR_OFFSET() ...
707  *
708  * DESCRIPTION
709  *
710  *    Offset in buffer.  This can be used in formatting and unformatting to
711  *    move the data pointer of the buffer either forwards (positive offset)
712  *    or backwards (negative offset).  It can be used to for example skip
713  *    some types during unformatting.
714  *
715  *    Example:
716  *
717  *    ..., SILC_STR_OFFSET(5), ...
718  *    ..., SILC_STR_OFFSET(-3), ...
719  *
720  *    Moves the data pointer at the point of the offset either forward
721  *    or backward and then moves to the next type.  Multiple SILC_STR_OFFSETs
722  *    can be used in formatting and unformatting at the same time.
723  *
724  ***/
725 #define SILC_STR_OFFSET(x) SILC_PARAM_OFFSET, (x)
726
727 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_ADVANCE
728  *
729  * NAME
730  *
731  *    #define SILC_STR_ADVANCE ...
732  *
733  * DESCRIPTION
734  *
735  *    Advance the buffer to the end of the data after the formatting is
736  *    done.  In normal operation when the formatted data is written the
737  *    buffer is positioned at the start of the data.  With SILC_STR_ADVANCE
738  *    the buffer will be positioned at the end of the data.  This makes it
739  *    easy to add new data immediately after the previously added data.
740  *    The SILC_STR_ADVANCE may also be used in unformatting.
741  *
742  * EXAMPLE
743  *
744  *    do {
745  *      len = read(fd, buf, sizeof(buf));
746  *      if (len > 0)
747  *        // Add read data to the buffer
748  *        silc_buffer_format(buffer,
749  *                           SILC_STR_ADVANCE,
750  *                           SILC_STR_DATA(buf, len),
751  *                           SILC_STR_END);
752  *    } while (len > 0);
753  *
754  *    // Move to beginning of buffer
755  *    silc_buffer_start(buffer);
756  *
757  ***/
758 #define SILC_STR_ADVANCE SILC_PARAM_ADVANCE
759
760 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_END
761  *
762  * NAME
763  *
764  *    #define SILC_STR_END ...
765  *
766  * DESCRIPTION
767  *
768  *    Marks end of the argument list. This must be at the end of the
769  *    argument list or error will occur.
770  *
771  ***/
772 #define SILC_STR_END SILC_PARAM_END
773
774 /****d* silcutil/SilcBufferFormatAPI/SILC_STRFMT_END
775  *
776  * NAME
777  *
778  *    #define SILC_STRFMT_END ...
779  *
780  * DESCRIPTION
781  *
782  *    Marks end of the argument list in silc_buffer_strformat function.
783  *    This must be at the end of the argument list or error will occur.
784  *
785  ***/
786 #define SILC_STRFMT_END (void *)SILC_STR_END
787
788 #endif  /* !SILCBUFFMT_H */