5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2000 - 2005 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.
21 #include "silcincludes.h"
22 #include "silcnotify.h"
24 /******************************************************************************
28 ******************************************************************************/
30 struct SilcNotifyPayloadStruct {
33 SilcArgumentPayload args;
36 /* Parse notify payload buffer and return data into payload structure */
38 SilcNotifyPayload silc_notify_payload_parse(const unsigned char *payload,
39 SilcUInt32 payload_len)
41 SilcBufferStruct buffer;
42 SilcNotifyPayload newp;
46 SILC_LOG_DEBUG(("Parsing Notify payload"));
48 silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
49 newp = silc_calloc(1, sizeof(*newp));
53 ret = silc_buffer_unformat(&buffer,
54 SILC_STR_UI_SHORT(&newp->type),
55 SILC_STR_UI_SHORT(&len),
56 SILC_STR_UI_CHAR(&newp->argc),
61 if (len > silc_buffer_len(&buffer))
65 silc_buffer_pull(&buffer, 5);
66 newp->args = silc_argument_payload_parse(buffer.data, silc_buffer_len(&buffer),
68 silc_buffer_push(&buffer, 5);
78 /* Encode notify payload with variable argument list. If `argc' is > 0
79 argument payloads will be associated to the notify payload. Variable
80 arguments must be {usigned char *, SilcUInt32 (len)}. */
82 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, SilcUInt32 argc,
86 SilcBuffer args = NULL;
88 SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
90 SilcUInt32 x_len, len = 0;
94 argv = silc_calloc(argc, sizeof(unsigned char *));
97 argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
102 argv_types = silc_calloc(argc, sizeof(SilcUInt32));
104 silc_free(argv_lens);
109 for (i = 0, k = 0; i < argc; i++) {
110 x = va_arg(ap, unsigned char *);
111 x_len = va_arg(ap, SilcUInt32);
116 argv[k] = silc_memdup(x, x_len);
119 argv_lens[k] = x_len;
120 argv_types[k] = i + 1;
124 args = silc_argument_payload_encode(k, argv, argv_lens, argv_types);
125 len = silc_buffer_len(args);
127 for (i = 0; i < k; i++)
130 silc_free(argv_lens);
131 silc_free(argv_types);
135 buffer = silc_buffer_alloc_size(len);
138 silc_buffer_format(buffer,
139 SILC_STR_UI_SHORT(type),
140 SILC_STR_UI_SHORT(len),
145 silc_buffer_pull(buffer, 5);
146 silc_buffer_format(buffer,
147 SILC_STR_UI_XNSTRING(args->data, silc_buffer_len(args)),
149 silc_buffer_push(buffer, 5);
150 silc_buffer_free(args);
156 /* Same as above but takes argument from the `args' Argument Payload. */
158 SilcBuffer silc_notify_payload_encode_args(SilcNotifyType type,
165 len = 5 + (args ? silc_buffer_len(args) : 0);
166 buffer = silc_buffer_alloc_size(len);
169 silc_buffer_format(buffer,
170 SILC_STR_UI_SHORT(type),
171 SILC_STR_UI_SHORT(len),
172 SILC_STR_UI_CHAR(argc),
176 silc_buffer_pull(buffer, 5);
177 silc_buffer_format(buffer,
178 SILC_STR_UI_XNSTRING(args->data, silc_buffer_len(args)),
180 silc_buffer_push(buffer, 5);
186 /* Frees notify payload */
188 void silc_notify_payload_free(SilcNotifyPayload payload)
191 silc_argument_payload_free(payload->args);
196 /* Return notify type */
198 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
200 return payload->type;
203 /* Return argument nums */
205 SilcUInt32 silc_notify_get_arg_num(SilcNotifyPayload payload)
207 return payload->argc;
210 /* Return argument payload */
212 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
214 return payload->args;