5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
24 /* Buffer parameter types.
29 Any XXX_STRING_ALLOC types will allocate space for the data and
30 memcpy the data to the pointer sent as argument (in unformatting).
32 Any XXX_STRING will not allocate or copy any data. Instead it
33 will set the pointer to the data. Note that the data pointer
34 returned (in unformatting) must not be freed.
38 SILC_BUFFER_PARAM_SI8_CHAR,
39 SILC_BUFFER_PARAM_UI8_CHAR,
41 SILC_BUFFER_PARAM_SI16_SHORT,
42 SILC_BUFFER_PARAM_UI16_SHORT,
44 SILC_BUFFER_PARAM_SI32_INT,
45 SILC_BUFFER_PARAM_UI32_INT,
47 SILC_BUFFER_PARAM_SI64_INT,
48 SILC_BUFFER_PARAM_UI64_INT,
50 SILC_BUFFER_PARAM_UI16_STRING, /* No copy */
51 SILC_BUFFER_PARAM_UI16_STRING_ALLOC, /* Alloc + memcpy */
52 SILC_BUFFER_PARAM_UI32_STRING, /* No copy */
53 SILC_BUFFER_PARAM_UI32_STRING_ALLOC, /* Alloc + memcpy */
54 SILC_BUFFER_PARAM_UI16_NSTRING, /* No copy */
55 SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, /* Alloc + memcpy */
56 SILC_BUFFER_PARAM_UI32_NSTRING, /* No copy */
57 SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, /* Alloc + memcpy */
58 SILC_BUFFER_PARAM_UI_XNSTRING, /* No copy */
59 SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, /* Alloc + memcpy */
62 } SilcBufferParamType;
64 /* Macros for expanding parameters into variable function argument list.
65 These are passed to silc_buffer_format and silc_buffer_unformat
68 /* One signed/unsigned character.
70 Formatting: SILC_STR_SI_CHAR(char)
71 SILC_STR_UI_CHAR(unsigned char)
72 Unformatting: SILC_STR_SI_CHAR(char *)
73 SILC_STR_UI_CHAR(unsigned char *)
76 #define SILC_STR_SI_CHAR(x) SILC_BUFFER_PARAM_SI8_CHAR, (x)
77 #define SILC_STR_UI_CHAR(x) SILC_BUFFER_PARAM_UI8_CHAR, (x)
81 Formatting: SILC_STR_SI_SHORT(short)
82 SILC_STR_UI_SHORT(uint16)
83 Unformatting: SILC_STR_SI_SHORT(short *)
84 SILC_STR_UI_SHORT(uint16 *)
87 #define SILC_STR_SI_SHORT(x) SILC_BUFFER_PARAM_SI16_SHORT, (x)
88 #define SILC_STR_UI_SHORT(x) SILC_BUFFER_PARAM_UI16_SHORT, (x)
92 Formatting: SILC_STR_SI_INT(int)
93 SILC_STR_UI_INT(uint32)
94 Unformatting: SILC_STR_SI_INT(int *)
95 SILC_STR_UI_INT(uint32 *)
98 #define SILC_STR_SI_INT(x) SILC_BUFFER_PARAM_SI32_INT, (x)
99 #define SILC_STR_UI_INT(x) SILC_BUFFER_PARAM_UI32_INT, (x)
103 Formatting: SILC_STR_SI_INT64(int)
104 SILC_STR_UI_INT64(uint32)
105 Unformatting: SILC_STR_SI_INT64(int *)
106 SILC_STR_UI_INT64(uint32 *)
109 #define SILC_STR_SI_INT64(x) SILC_BUFFER_PARAM_SI64_INT, (x)
110 #define SILC_STR_UI_INT64(x) SILC_BUFFER_PARAM_UI64_INT, (x)
112 /* Unsigned NULL terminated string. Note that the string must be
113 NULL terminated because strlen() will be used to get the length of
116 Formatting: SILC_STR_UI32_STRING(unsigned char *)
117 Unformatting: SILC_STR_UI32_STRING(unsigned char **)
119 Unformatting procedure will check for length of the string from the
120 buffer before trying to get the string out. Thus, one *must* format the
121 length as UI_INT or UI_SHORT into the buffer *before* formatting the
122 actual string to the buffer, and, in unformatting one must ignore the
123 length of the string because unformatting procedure will take it
128 Formatting: ..., SILC_STR_UI_INT(strlen(string)),
129 SILC_STR_UI32_STRING(string), ...
130 Unformatting: ..., SILC_STR_UI32_STRING(&string), ...
132 I.e., you ignore the formatted length field in unformatting. If you don't
133 the unformatting procedure might fail and it definitely does not unformat
136 _ALLOC routines automatically allocates memory for the variable sent
137 as argument in unformatting.
140 #define SILC_STR_UI16_STRING(x) SILC_BUFFER_PARAM_UI16_STRING, (x)
141 #define SILC_STR_UI16_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI16_STRING_ALLOC, (x)
142 #define SILC_STR_UI32_STRING(x) SILC_BUFFER_PARAM_UI32_STRING, (x)
143 #define SILC_STR_UI32_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI32_STRING_ALLOC, (x)
145 /* Unsigned string. Second argument is the length of the string.
147 Formatting: SILC_STR_UI32_NSTRING(unsigned char *, uint32)
148 Unformatting: SILC_STR_UI32_NSTRING(unsigned char **, uint32 *)
150 Unformatting procedure will check for length of the string from the
151 buffer before trying to get the string out. Thus, one *must* format the
152 length as UI_INT or UI_SHORT into the buffer *before* formatting the
153 actual string to the buffer, and, in unformatting one must ignore the
154 length of the string because unformatting procedure will take it
159 Formatting: ..., SILC_STR_UI_INT(strlen(string)),
160 SILC_STR_UI32_NSTRING(string, strlen(string)), ...
161 Unformatting: ..., SILC_STR_UI32_NSTRING(&string, &len), ...
163 I.e., you ignore the formatted length field in unformatting. If you don't
164 the unformatting procedure might fail and it definitely does not unformat
165 the data reliably. The length taken from the buffer is returned to the
166 pointer sent as argument (&len in above example).
168 UI/SI16 and UI/SI32 means that the length is considered to be either
169 short (16 bits) or int (32 bits) in unformatting.
171 _ALLOC routines automatically allocates memory for the variable sent
172 as argument in unformatting.
175 #define SILC_STR_UI16_NSTRING(x, l) SILC_BUFFER_PARAM_UI16_NSTRING, (x), (l)
176 #define SILC_STR_UI16_NSTRING_ALLOC(x, l) \
177 SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, (x), (l)
178 #define SILC_STR_UI32_NSTRING(x, l) SILC_BUFFER_PARAM_UI32_NSTRING, (x), (l)
179 #define SILC_STR_UI32_NSTRING_ALLOC(x, l) \
180 SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, (x), (l)
182 /* Extended Unsigned string formatting. Second argument is the length of
185 Formatting: This is equal to using *_NSTRING
186 Unformatting: SILC_STR_UI_XNSTRING(unsigned char **, uint32)
188 This type can be used to take arbitrary length string from the buffer
189 by sending the requested amount of bytes as argument. This differs
190 from *_STRING and *_NSTRING so that this doesn't try to find the
191 length of the data from the buffer but the length of the data is
192 sent as argument. This a handy way to unformat fixed length strings
193 from the buffer without having the length of the string formatted
196 _ALLOC routines automatically allocates memory for the variable sent
197 as argument in unformatting.
200 #define SILC_STR_UI_XNSTRING(x, l) SILC_BUFFER_PARAM_UI_XNSTRING, (x), (l)
201 #define SILC_STR_UI_XNSTRING_ALLOC(x, l) \
202 SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, (x), (l)
204 /* Marks end of the argument list. This must be at the end of the
205 argument list or error will occur. */
206 #define SILC_STR_END SILC_BUFFER_PARAM_END
210 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_format
214 * int silc_buffer_format(SilcBuffer dst, ...);
218 * Formats a buffer from a variable argument list. Returns -1 on error
219 * and the length of the formatted buffer otherwise.
222 int silc_buffer_format(SilcBuffer dst, ...);
224 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat
228 * int silc_buffer_unformat(SilcBuffer src, ...);
232 * Formats a buffer from a variable argument list. Returns -1 on error
233 * and the length of the formatted buffer otherwise.
236 int silc_buffer_unformat(SilcBuffer src, ...);
238 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_format
242 * int silc_buffer_format_vp(SilcBuffer dst, va_list vp);
246 * Formats a buffer from a variable argument list indicated by the `ap'.
247 * Returns -1 on error and the length of the formatted buffer otherwise.
250 int silc_buffer_format_vp(SilcBuffer dst, va_list ap);
252 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat
256 * int silc_buffer_unformat_vp(SilcBuffer src, va_list vp);
260 * Formats a buffer from a variable argument list indicated by the `ap'.
261 * Returns -1 on error and the length of the formatted buffer otherwise.
264 int silc_buffer_unformat_vp(SilcBuffer src, va_list ap);