5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2000 - 2007 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.
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,
67 silc_buffer_len(&buffer),
71 silc_buffer_push(&buffer, 5);
81 /* Encode notify payload with variable argument list. If `argc' is > 0
82 argument payloads will be associated to the notify payload. Variable
83 arguments must be {usigned char *, SilcUInt32 (len)}. */
85 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, SilcUInt32 argc,
89 SilcBuffer args = NULL;
91 SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
93 SilcUInt32 x_len, len = 0;
97 argv = silc_calloc(argc, sizeof(unsigned char *));
100 argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
105 argv_types = silc_calloc(argc, sizeof(SilcUInt32));
107 silc_free(argv_lens);
112 for (i = 0, k = 0; i < argc; i++) {
113 x = va_arg(ap, unsigned char *);
114 x_len = va_arg(ap, SilcUInt32);
119 argv[k] = silc_memdup(x, x_len);
122 argv_lens[k] = x_len;
123 argv_types[k] = i + 1;
127 args = silc_argument_payload_encode(k, argv, argv_lens, argv_types);
128 len = silc_buffer_len(args);
130 for (i = 0; i < k; i++)
133 silc_free(argv_lens);
134 silc_free(argv_types);
138 buffer = silc_buffer_alloc_size(len);
141 silc_buffer_format(buffer,
142 SILC_STR_UI_SHORT(type),
143 SILC_STR_UI_SHORT(len),
148 silc_buffer_format(buffer,
150 SILC_STR_DATA(args->data, silc_buffer_len(args)),
152 silc_buffer_free(args);
158 /* Same as above but takes argument from the `args' Argument Payload. */
160 SilcBuffer silc_notify_payload_encode_args(SilcNotifyType type,
167 len = 5 + (args ? silc_buffer_len(args) : 0);
168 buffer = silc_buffer_alloc_size(len);
171 silc_buffer_format(buffer,
172 SILC_STR_UI_SHORT(type),
173 SILC_STR_UI_SHORT(len),
174 SILC_STR_UI_CHAR(argc),
178 silc_buffer_format(buffer,
180 SILC_STR_DATA(args->data, silc_buffer_len(args)),
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;