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_UI16_STRING, /* No copy */
48 SILC_BUFFER_PARAM_UI16_STRING_ALLOC, /* Alloc + memcpy */
49 SILC_BUFFER_PARAM_UI32_STRING, /* No copy */
50 SILC_BUFFER_PARAM_UI32_STRING_ALLOC, /* Alloc + memcpy */
51 SILC_BUFFER_PARAM_UI16_NSTRING, /* No copy */
52 SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, /* Alloc + memcpy */
53 SILC_BUFFER_PARAM_UI32_NSTRING, /* No copy */
54 SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, /* Alloc + memcpy */
55 SILC_BUFFER_PARAM_UI_XNSTRING, /* No copy */
56 SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, /* Alloc + memcpy */
59 } SilcBufferParamType;
61 /* Macros for expanding parameters into variable function argument list.
62 These are passed to silc_buffer_format and silc_buffer_unformat
65 /* One signed/unsigned character.
67 Formatting: SILC_STR_SI_CHAR(char)
68 SILC_STR_UI_CHAR(unsigned char)
69 Unformatting: SILC_STR_SI_CHAR(char *)
70 SILC_STR_UI_CHAR(unsigned char *)
73 #define SILC_STR_SI_CHAR(x) SILC_BUFFER_PARAM_SI8_CHAR, (x)
74 #define SILC_STR_UI_CHAR(x) SILC_BUFFER_PARAM_UI8_CHAR, (x)
78 Formatting: SILC_STR_SI_SHORT(short)
79 SILC_STR_UI_SHORT(uint16)
80 Unformatting: SILC_STR_SI_SHORT(short *)
81 SILC_STR_UI_SHORT(uint16 *)
84 #define SILC_STR_SI_SHORT(x) SILC_BUFFER_PARAM_SI16_SHORT, (x)
85 #define SILC_STR_UI_SHORT(x) SILC_BUFFER_PARAM_UI16_SHORT, (x)
89 Formatting: SILC_STR_SI_INT(int)
90 SILC_STR_UI_INT(uint32)
91 Unformatting: SILC_STR_SI_INT(int *)
92 SILC_STR_UI_INT(uint32 *)
95 #define SILC_STR_SI_INT(x) SILC_BUFFER_PARAM_SI32_INT, (x)
96 #define SILC_STR_UI_INT(x) SILC_BUFFER_PARAM_UI32_INT, (x)
98 /* Unsigned NULL terminated string. Note that the string must be
99 NULL terminated because strlen() will be used to get the length of
102 Formatting: SILC_STR_UI32_STRING(unsigned char *)
103 Unformatting: SILC_STR_UI32_STRING(unsigned char **)
105 Unformatting procedure will check for length of the string from the
106 buffer before trying to get the string out. Thus, one *must* format the
107 length as UI_INT or UI_SHORT into the buffer *before* formatting the
108 actual string to the buffer, and, in unformatting one must ignore the
109 length of the string because unformatting procedure will take it
114 Formatting: ..., SILC_STR_UI_INT(strlen(string)),
115 SILC_STR_UI32_STRING(string), ...
116 Unformatting: ..., SILC_STR_UI32_STRING(&string), ...
118 I.e., you ignore the formatted length field in unformatting. If you don't
119 the unformatting procedure might fail and it definitely does not unformat
122 _ALLOC routines automatically allocates memory for the variable sent
123 as argument in unformatting.
126 #define SILC_STR_UI16_STRING(x) SILC_BUFFER_PARAM_UI16_STRING, (x)
127 #define SILC_STR_UI16_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI16_STRING_ALLOC, (x)
128 #define SILC_STR_UI32_STRING(x) SILC_BUFFER_PARAM_UI32_STRING, (x)
129 #define SILC_STR_UI32_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI32_STRING_ALLOC, (x)
131 /* Unsigned string. Second argument is the length of the string.
133 Formatting: SILC_STR_UI32_NSTRING(unsigned char *, uint32)
134 Unformatting: SILC_STR_UI32_NSTRING(unsigned char **, uint32 *)
136 Unformatting procedure will check for length of the string from the
137 buffer before trying to get the string out. Thus, one *must* format the
138 length as UI_INT or UI_SHORT into the buffer *before* formatting the
139 actual string to the buffer, and, in unformatting one must ignore the
140 length of the string because unformatting procedure will take it
145 Formatting: ..., SILC_STR_UI_INT(strlen(string)),
146 SILC_STR_UI32_NSTRING(string, strlen(string)), ...
147 Unformatting: ..., SILC_STR_UI32_NSTRING(&string, &len), ...
149 I.e., you ignore the formatted length field in unformatting. If you don't
150 the unformatting procedure might fail and it definitely does not unformat
151 the data reliably. The length taken from the buffer is returned to the
152 pointer sent as argument (&len in above example).
154 UI/SI16 and UI/SI32 means that the length is considered to be either
155 short (16 bits) or int (32 bits) in unformatting.
157 _ALLOC routines automatically allocates memory for the variable sent
158 as argument in unformatting.
161 #define SILC_STR_UI16_NSTRING(x, l) SILC_BUFFER_PARAM_UI16_NSTRING, (x), (l)
162 #define SILC_STR_UI16_NSTRING_ALLOC(x, l) \
163 SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, (x), (l)
164 #define SILC_STR_UI32_NSTRING(x, l) SILC_BUFFER_PARAM_UI32_NSTRING, (x), (l)
165 #define SILC_STR_UI32_NSTRING_ALLOC(x, l) \
166 SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, (x), (l)
168 /* Extended Unsigned string formatting. Second argument is the length of
171 Formatting: This is equal to using *_NSTRING
172 Unformatting: SILC_STR_UI_XNSTRING(unsigned char **, uint32)
174 This type can be used to take arbitrary length string from the buffer
175 by sending the requested amount of bytes as argument. This differs
176 from *_STRING and *_NSTRING so that this doesn't try to find the
177 length of the data from the buffer but the length of the data is
178 sent as argument. This a handy way to unformat fixed length strings
179 from the buffer without having the length of the string formatted
182 _ALLOC routines automatically allocates memory for the variable sent
183 as argument in unformatting.
186 #define SILC_STR_UI_XNSTRING(x, l) SILC_BUFFER_PARAM_UI_XNSTRING, (x), (l)
187 #define SILC_STR_UI_XNSTRING_ALLOC(x, l) \
188 SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, (x), (l)
190 /* Marks end of the argument list. This must be at the end of the
191 argument list or error will occur. */
192 #define SILC_STR_END SILC_BUFFER_PARAM_END
195 int silc_buffer_format(SilcBuffer dst, ...);
196 int silc_buffer_unformat(SilcBuffer src, ...);