5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2006 Pekka Riikonen
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.
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.
20 /****h* silcutil/SILC Buffer Format Interface
24 * SILC Buffer Format API provides a few functions for formatting
25 * various different data types into a buffer, and retrieving
26 * various data from buffer into specific data types. It is usefull
27 * to format for example packets and later unformat them.
36 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_format
40 * int silc_buffer_format(SilcBuffer dst, ...);
44 * Formats a buffer from a variable argument list. Returns -1 on error
45 * and the length of the formatted buffer otherwise. The buffer is
46 * enlarged automatically during formatting, if it doesn't already have
51 * SilcBufferStruct buffer;
53 * memset(&buffer, 0, sizeof(buffer));
54 * ret = silc_buffer_format(&buffer,
55 * SILC_STR_INT(intval),
56 * SILC_STR_CHAR(charval),
57 * SILC_STR_INT(intval),
58 * SILC_STR_SHORT(str_len),
59 * SILC_STR_UI_XNSTRING(str, str_len),
65 int silc_buffer_format(SilcBuffer dst, ...);
67 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sformat
71 * int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...);
75 * Same as silc_buffer_format but uses `stack' to allocate the memory.
76 * if `stack' is NULL reverts back to silc_buffer_format call.
79 int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...);
81 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_format_vp
85 * int silc_buffer_format_vp(SilcBuffer dst, va_list vp);
89 * Formats a buffer from a variable argument list indicated by the `ap'.
90 * Returns -1 on error and the length of the formatted buffer otherwise.
93 int silc_buffer_format_vp(SilcBuffer dst, va_list ap);
95 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_format_vp
99 * int silc_buffer_format_vp(SilcBuffer dst, va_list vp);
103 * Same as silc_buffer_format_vp but uses `stack' to allocate the memory.
104 * if `stack' is NULL reverts back to silc_buffer_format_vp call.
107 int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap);
109 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat
113 * int silc_buffer_unformat(SilcBuffer src, ...);
117 * Unformats a buffer from a variable argument list. Returns -1 on error
118 * and the length of the unformatted buffer otherwise.
122 * ret = silc_buffer_unformat(buffer,
123 * SILC_STR_INT(&intval),
124 * SILC_STR_CHAR(&charval),
125 * SILC_STR_OFFSET(4),
126 * SILC_STR_UI16_NSTRING_ALLOC(&str, &str_len),
132 int silc_buffer_unformat(SilcBuffer src, ...);
134 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat_vp
138 * int silc_buffer_unformat_vp(SilcBuffer src, va_list vp);
142 * Unformats a buffer from a variable argument list indicated by the `ap'.
143 * Returns -1 on error and the length of the unformatted buffer otherwise.
146 int silc_buffer_unformat_vp(SilcBuffer src, va_list ap);
148 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_strformat
152 * int silc_buffer_strformat(SilcBuffer dst, ...);
156 * Formats a buffer from variable argument list of strings. Each
157 * string must be NULL-terminated and the variable argument list must
158 * be end with SILC_STR_END argument. This allows that a string in
159 * the list can be NULL, in which case it is skipped. This automatically
160 * allocates the space for the buffer data but `dst' must be already
161 * allocated by the caller.
165 * ret = silc_buffer_strformat(buffer, "foo", "bar", SILC_STR_END);
170 int silc_buffer_strformat(SilcBuffer dst, ...);
172 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_sstrformat
176 * int silc_buffer_strformat(SilcStack stack, SilcBuffer dst, ...);
180 * Formats a buffer from variable argument list of strings. Each
181 * string must be NULL-terminated and the variable argument list must
182 * be end with SILC_STR_END argument. This allows that a string in
183 * the list can be NULL, in which case it is skipped. This automatically
184 * allocates the space for the buffer data but `dst' must be already
185 * allocated by the caller. This function is equivalent to
186 * silc_buffer_strformat but allocates memory from `stack'.
189 int silc_buffer_sstrformat(SilcStack stack, SilcBuffer dst, ...);
191 /* Macros for expanding parameters into variable function argument list.
192 These are passed to silc_buffer_format and silc_buffer_unformat
195 /* Buffer parameter types.
200 Any XXX_STRING_ALLOC types will allocate space for the data and
201 memcpy the data to the pointer sent as argument (in unformatting).
203 Any XXX_STRING will not allocate or copy any data. Instead it
204 will set the pointer to the data. Note that the data pointer
205 returned (in unformatting) must not be freed.
209 SILC_BUFFER_PARAM_SI8_CHAR,
210 SILC_BUFFER_PARAM_UI8_CHAR,
212 SILC_BUFFER_PARAM_SI16_SHORT,
213 SILC_BUFFER_PARAM_UI16_SHORT,
215 SILC_BUFFER_PARAM_SI32_INT,
216 SILC_BUFFER_PARAM_UI32_INT,
218 SILC_BUFFER_PARAM_SI64_INT,
219 SILC_BUFFER_PARAM_UI64_INT,
221 SILC_BUFFER_PARAM_UI8_STRING, /* No copy */
222 SILC_BUFFER_PARAM_UI8_STRING_ALLOC, /* Alloc + memcpy */
223 SILC_BUFFER_PARAM_UI16_STRING, /* No copy */
224 SILC_BUFFER_PARAM_UI16_STRING_ALLOC, /* Alloc + memcpy */
225 SILC_BUFFER_PARAM_UI32_STRING, /* No copy */
226 SILC_BUFFER_PARAM_UI32_STRING_ALLOC, /* Alloc + memcpy */
227 SILC_BUFFER_PARAM_UI8_NSTRING, /* No copy */
228 SILC_BUFFER_PARAM_UI8_NSTRING_ALLOC, /* Alloc + memcpy */
229 SILC_BUFFER_PARAM_UI16_NSTRING, /* No copy */
230 SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, /* Alloc + memcpy */
231 SILC_BUFFER_PARAM_UI32_NSTRING, /* No copy */
232 SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, /* Alloc + memcpy */
233 SILC_BUFFER_PARAM_UI_XNSTRING, /* No copy */
234 SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, /* Alloc + memcpy */
236 SILC_BUFFER_PARAM_OFFSET,
238 SILC_BUFFER_PARAM_END
239 } SilcBufferParamType;
241 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_CHAR
245 * #define SILC_STR_UI_CHAR() ...
246 * #define SILC_STR_SI_CHAR() ...
250 * One signed/unsigned character.
252 * Formatting: SILC_STR_SI_CHAR(char)
253 * SILC_STR_UI_CHAR(unsigned char)
254 * Unformatting: SILC_STR_SI_CHAR(char *)
255 * SILC_STR_UI_CHAR(unsigned char *)
258 #define SILC_STR_SI_CHAR(x) SILC_BUFFER_PARAM_SI8_CHAR, (x)
259 #define SILC_STR_UI_CHAR(x) SILC_BUFFER_PARAM_UI8_CHAR, (x)
261 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_SHORT
265 * #define SILC_STR_UI_SHORT() ...
266 * #define SILC_STR_SI_SHORT() ...
272 * Formatting: SILC_STR_SI_SHORT(short)
273 * SILC_STR_UI_SHORT(SilcUInt16)
274 * Unformatting: SILC_STR_SI_SHORT(short *)
275 * SILC_STR_UI_SHORT(SilcUInt16 *)
278 #define SILC_STR_SI_SHORT(x) SILC_BUFFER_PARAM_SI16_SHORT, (x)
279 #define SILC_STR_UI_SHORT(x) SILC_BUFFER_PARAM_UI16_SHORT, (x)
281 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_INT
285 * #define SILC_STR_UI_INT() ...
286 * #define SILC_STR_SI_INT() ...
292 * Formatting: SILC_STR_SI_INT(int)
293 * SILC_STR_UI_INT(SilcUInt32)
294 * Unformatting: SILC_STR_SI_INT(int *)
295 * SILC_STR_UI_INT(SilcUInt32 *)
298 #define SILC_STR_SI_INT(x) SILC_BUFFER_PARAM_SI32_INT, (x)
299 #define SILC_STR_UI_INT(x) SILC_BUFFER_PARAM_UI32_INT, (x)
301 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_INT64
305 * #define SILC_STR_UI_INT64() ...
306 * #define SILC_STR_SI_INT64() ...
312 * Formatting: SILC_STR_SI_INT64(int)
313 * SILC_STR_UI_INT64(SilcUInt32)
314 * Unformatting: SILC_STR_SI_INT64(int *)
315 * SILC_STR_UI_INT64(SilcUInt32 *)
318 #define SILC_STR_SI_INT64(x) SILC_BUFFER_PARAM_SI64_INT, (x)
319 #define SILC_STR_UI_INT64(x) SILC_BUFFER_PARAM_UI64_INT, (x)
321 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_STRING
325 * #define SILC_STR_UI8_STRING() ...
326 * #define SILC_STR_UI8_STRING_ALLOC() ...
327 * #define SILC_STR_UI16_STRING() ...
328 * #define SILC_STR_UI16_STRING_ALLOC() ...
329 * #define SILC_STR_UI32_STRING() ...
330 * #define SILC_STR_UI32_STRING_ALLOC() ...
334 * Unsigned NULL terminated string. Note that the string must be
335 * NULL terminated because strlen() will be used to get the length of
338 * Formatting: SILC_STR_UI32_STRING(unsigned char *)
339 * Unformatting: SILC_STR_UI32_STRING(unsigned char **)
341 * Unformatting procedure will check for length of the string from the
342 * buffer before trying to get the string out. Thus, one *must* format the
343 * length as UI_INT or UI_SHORT into the buffer *before* formatting the
344 * actual string to the buffer, and, in unformatting one must ignore the
345 * length of the string because unformatting procedure will take it
350 * Formatting: ..., SILC_STR_UI_INT(strlen(string)),
351 * SILC_STR_UI32_STRING(string), ...
352 * Unformatting: ..., SILC_STR_UI32_STRING(&string), ...
354 * I.e., you can ignore the formatted length field in unformatting.
356 * UI8, UI16 and UI32 means that the length is considered to be
357 * either char (8 bits), short (16 bits) or int (32 bits) in
360 * _ALLOC routines automatically allocates memory for the variable sent
361 * as argument in unformatting.
364 #define SILC_STR_UI8_STRING(x) SILC_BUFFER_PARAM_UI8_STRING, (x)
365 #define SILC_STR_UI8_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI8_STRING_ALLOC, (x)
366 #define SILC_STR_UI16_STRING(x) SILC_BUFFER_PARAM_UI16_STRING, (x)
367 #define SILC_STR_UI16_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI16_STRING_ALLOC, (x)
368 #define SILC_STR_UI32_STRING(x) SILC_BUFFER_PARAM_UI32_STRING, (x)
369 #define SILC_STR_UI32_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI32_STRING_ALLOC, (x)
371 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_NSTRING
375 * #define SILC_STR_UI8_NSTRING() ...
376 * #define SILC_STR_UI8_NSTRING_ALLOC() ...
377 * #define SILC_STR_UI16_NSTRING() ...
378 * #define SILC_STR_UI16_NSTRING_ALLOC() ...
379 * #define SILC_STR_UI32_NSTRING() ...
380 * #define SILC_STR_UI32_NSTRING_ALLOC() ...
384 * Unsigned string. Second argument is the length of the string.
386 * Formatting: SILC_STR_UI32_NSTRING(unsigned char *, SilcUInt32)
387 * Unformatting: SILC_STR_UI32_NSTRING(unsigned char **, SilcUInt32 *)
389 * Unformatting procedure will check for length of the string from the
390 * buffer before trying to get the string out. Thus, one *must* format the
391 * length as UI_INT or UI_SHORT into the buffer *before* formatting the
392 * actual string to the buffer, and, in unformatting one must ignore the
393 * length of the string because unformatting procedure will take it
398 * Formatting: ..., SILC_STR_UI_INT(strlen(string)),
399 * SILC_STR_UI32_NSTRING(string, strlen(string)), ...
400 * Unformatting: ..., SILC_STR_UI32_NSTRING(&string, &len), ...
402 * I.e., you can ignore the formatted length field in unformatting. The
403 * length taken from the buffer is returned to the pointer sent as
404 * argument (&len in above example).
406 * UI8, UI16 and UI32 means that the length is considered to be
407 * either char (8 bits), short (16 bits) or int (32 bits) in
410 * _ALLOC routines automatically allocates memory for the variable sent
411 * as argument in unformatting.
414 #define SILC_STR_UI8_NSTRING(x, l) SILC_BUFFER_PARAM_UI8_NSTRING, (x), (l)
415 #define SILC_STR_UI8_NSTRING_ALLOC(x, l) \
416 SILC_BUFFER_PARAM_UI8_NSTRING_ALLOC, (x), (l)
417 #define SILC_STR_UI16_NSTRING(x, l) SILC_BUFFER_PARAM_UI16_NSTRING, (x), (l)
418 #define SILC_STR_UI16_NSTRING_ALLOC(x, l) \
419 SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, (x), (l)
420 #define SILC_STR_UI32_NSTRING(x, l) SILC_BUFFER_PARAM_UI32_NSTRING, (x), (l)
421 #define SILC_STR_UI32_NSTRING_ALLOC(x, l) \
422 SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, (x), (l)
424 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_UI_XNSTRING
428 * #define SILC_STR_UI_XNSTRING() ...
429 * #define SILC_STR_UI_XNSTRING_ALLOC() ...
433 * Extended Unsigned string formatting. Second argument is the length of
436 * Formatting: SILC_STR_UI_XNSTRING(unsigned char *, SilcUInt32)
437 * Unformatting: SILC_STR_UI_XNSTRING(unsigned char **, SilcUInt32)
439 * This type can be used to take arbitrary length string from the buffer
440 * by sending the requested amount of bytes as argument. This differs
441 * from *_STRING and *_NSTRING so that this doesn't try to find the
442 * length of the data from the buffer but the length of the data is
443 * sent as argument. This a handy way to unformat fixed length strings
444 * from the buffer without having the length of the string formatted
447 * _ALLOC routines automatically allocates memory for the variable sent
448 * as argument in unformatting.
451 #define SILC_STR_UI_XNSTRING(x, l) SILC_BUFFER_PARAM_UI_XNSTRING, (x), (l)
452 #define SILC_STR_UI_XNSTRING_ALLOC(x, l) \
453 SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, (x), (l)
455 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_OFFSET
459 * #define SILC_STR_OFFSET() ...
463 * Offset in buffer. This can be used in formatting and unformatting to
464 * move the data pointer of the buffer either forwards (positive offset)
465 * or backwards (negative offset). It can be used to for example skip
466 * some types during unformatting.
470 * ..., SILC_STR_OFFSET(5), ...
471 * ..., SILC_STR_OFFSET(-3), ...
473 * Moves the data pointer at the point of the offset either forward
474 * or backward and then moves to the next type. Multiple SILC_STR_OFFSETs
475 * can be used in formatting and unformatting at the same time.
478 #define SILC_STR_OFFSET(x) SILC_BUFFER_PARAM_OFFSET, (x)
480 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_END
484 * #define SILC_STR_END ...
488 * Marks end of the argument list. This must be at the end of the
489 * argument list or error will occur.
492 #define SILC_STR_END SILC_BUFFER_PARAM_END
494 /****d* silcutil/SilcBufferFormatAPI/SILC_STRFMT_END
498 * #define SILC_STRFMT_END ...
502 * Marks end of the argument list in silc_buffer_strformat function.
503 * This must be at the end of the argument list or error will occur.
506 #define SILC_STRFMT_END (void *)SILC_STR_END
508 #endif /* !SILCBUFFMT_H */