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.
31 SILC_BUFFER_PARAM_SI8_CHAR,
32 SILC_BUFFER_PARAM_UI8_CHAR,
34 SILC_BUFFER_PARAM_SI16_SHORT,
35 SILC_BUFFER_PARAM_UI16_SHORT,
37 SILC_BUFFER_PARAM_SI32_INT,
38 SILC_BUFFER_PARAM_UI32_INT,
40 SILC_BUFFER_PARAM_UI16_STRING,
41 SILC_BUFFER_PARAM_UI16_STRING_ALLOC,
42 SILC_BUFFER_PARAM_UI32_STRING,
43 SILC_BUFFER_PARAM_UI32_STRING_ALLOC,
44 SILC_BUFFER_PARAM_UI16_NSTRING,
45 SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC,
46 SILC_BUFFER_PARAM_UI32_NSTRING,
47 SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC,
48 SILC_BUFFER_PARAM_UI_XNSTRING,
49 SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC,
52 } SilcBufferParamType;
54 /* Macros for expanding parameters into variable function argument list.
55 These are passed to silc_buffer_format and silc_buffer_unformat
58 /* One signed/unsigned character.
60 Formatting: SILC_STR_SI_CHAR(char)
61 SILC_STR_UI_CHAR(unsigned char)
62 Unformatting: SILC_STR_SI_CHAR(char *)
63 SILC_STR_UI_CHAR(unsigned char *)
66 #define SILC_STR_SI_CHAR(x) SILC_BUFFER_PARAM_SI8_CHAR, (x)
67 #define SILC_STR_UI_CHAR(x) SILC_BUFFER_PARAM_UI8_CHAR, (x)
69 /* Signed/unsigned short.
71 Formatting: SILC_STR_SI_SHORT(short)
72 SILC_STR_UI_SHORT(unsigned short)
73 Unformatting: SILC_STR_SI_SHORT(short *)
74 SILC_STR_UI_SHORT(unsigned short *)
77 #define SILC_STR_SI_SHORT(x) SILC_BUFFER_PARAM_SI16_SHORT, (x)
78 #define SILC_STR_UI_SHORT(x) SILC_BUFFER_PARAM_UI16_SHORT, (x)
80 /* Signed/unsigned int.
82 Formatting: SILC_STR_SI_INT(int)
83 SILC_STR_UI_INT(unsigned int)
84 Unformatting: SILC_STR_SI_INT(int *)
85 SILC_STR_UI_INT(unsigned int *)
88 #define SILC_STR_SI_INT(x) SILC_BUFFER_PARAM_SI32_INT, (x)
89 #define SILC_STR_UI_INT(x) SILC_BUFFER_PARAM_UI32_INT, (x)
91 /* Unsigned NULL terminated string. Note that the string must be
92 NULL terminated because strlen() will be used to get the length of
95 Formatting: SILC_STR_UI32_STRING(unsigned char *)
96 Unformatting: SILC_STR_UI32_STRING(unsigned char **)
98 Unformatting procedure will check for length of the string from the
99 buffer before trying to get the string out. Thus, one *must* format the
100 length as UI_INT or UI_SHORT into the buffer *before* formatting the
101 actual string to the buffer, and, in unformatting one must ignore the
102 length of the string because unformatting procedure will take it
107 Formatting: ..., SILC_STR_UI_INT(strlen(string)),
108 SILC_STR_UI32_STRING(string), ...
109 Unformatting: ..., SILC_STR_UI32_STRING(&string), ...
111 I.e., you ignore the formatted length field in unformatting. If you don't
112 the unformatting procedure might fail and it definitely does not unformat
115 _ALLOC routines automatically allocates memory for the variable sent
116 as argument in unformatting.
119 #define SILC_STR_UI16_STRING(x) SILC_BUFFER_PARAM_UI16_STRING, (x)
120 #define SILC_STR_UI16_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI16_STRING_ALLOC, (x)
121 #define SILC_STR_UI32_STRING(x) SILC_BUFFER_PARAM_UI32_STRING, (x)
122 #define SILC_STR_UI32_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI32_STRING_ALLOC, (x)
124 /* Unsigned string. Second argument is the length of the string.
126 Formatting: SILC_STR_UI32_NSTRING(unsigned char *, unsigned int)
127 Unformatting: SILC_STR_UI32_NSTRING(unsigned char **, unsigned int *)
129 Unformatting procedure will check for length of the string from the
130 buffer before trying to get the string out. Thus, one *must* format the
131 length as UI_INT or UI_SHORT into the buffer *before* formatting the
132 actual string to the buffer, and, in unformatting one must ignore the
133 length of the string because unformatting procedure will take it
138 Formatting: ..., SILC_STR_UI_INT(strlen(string)),
139 SILC_STR_UI32_NSTRING(string, strlen(string)), ...
140 Unformatting: ..., SILC_STR_UI32_NSTRING(&string, &len), ...
142 I.e., you ignore the formatted length field in unformatting. If you don't
143 the unformatting procedure might fail and it definitely does not unformat
144 the data reliably. The length taken from the buffer is returned to the
145 pointer sent as argument (&len in above example).
147 UI/SI16 and UI/SI32 means that the length is considered to be either
148 short (16 bits) or int (32 bits) in unformatting.
150 _ALLOC routines automatically allocates memory for the variable sent
151 as argument in unformatting.
154 #define SILC_STR_UI16_NSTRING(x, l) SILC_BUFFER_PARAM_UI16_NSTRING, (x), (l)
155 #define SILC_STR_UI16_NSTRING_ALLOC(x, l) \
156 SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, (x), (l)
157 #define SILC_STR_UI32_NSTRING(x, l) SILC_BUFFER_PARAM_UI32_NSTRING, (x), (l)
158 #define SILC_STR_UI32_NSTRING_ALLOC(x, l) \
159 SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, (x), (l)
161 /* Extended Unsigned string formatting. Second argument is the length of
164 Formatting: This is equal to using *_NSTRING
165 Unformatting: SILC_STR_UI_XNSTRING(unsigned char **, unsigned int)
167 This type can be used to take arbitrary length string from the buffer
168 by sending the requested amount of bytes as argument. This differs
169 from *_STRING and *_NSTRING so that this doesn't try to find the
170 length of the data from the buffer but the length of the data is
171 sent as argument. This a handy way to unformat fixed length strings
172 from the buffer without having the length of the string formatted
175 _ALLOC routines automatically allocates memory for the variable sent
176 as argument in unformatting.
179 #define SILC_STR_UI_XNSTRING(x, l) SILC_BUFFER_PARAM_UI_XNSTRING, (x), (l)
180 #define SILC_STR_UI_XNSTRING_ALLOC(x, l) \
181 SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, (x), (l)
183 /* Marks end of the argument list. This must the at the end of the
184 argument list or error will occur. */
185 #define SILC_STR_END SILC_BUFFER_PARAM_END
188 int silc_buffer_format(SilcBuffer dst, ...);
189 int silc_buffer_unformat(SilcBuffer src, ...);