5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 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.
22 #include "silcincludes.h"
23 #include "silcnotify.h"
25 /******************************************************************************
29 ******************************************************************************/
31 struct SilcNotifyPayloadStruct {
34 SilcArgumentPayload args;
37 /* Parse notify payload buffer and return data into payload structure */
39 SilcNotifyPayload silc_notify_payload_parse(const unsigned char *payload,
40 SilcUInt32 payload_len)
42 SilcBufferStruct buffer;
43 SilcNotifyPayload newp;
47 SILC_LOG_DEBUG(("Parsing Notify payload"));
49 silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
50 newp = silc_calloc(1, sizeof(*newp));
54 ret = silc_buffer_unformat(&buffer,
55 SILC_STR_UI_SHORT(&newp->type),
56 SILC_STR_UI_SHORT(&len),
57 SILC_STR_UI_CHAR(&newp->argc),
66 silc_buffer_pull(&buffer, 5);
67 newp->args = silc_argument_payload_parse(buffer.data, buffer.len,
69 silc_buffer_push(&buffer, 5);
79 /* Encode notify payload with variable argument list. If `argc' is > 0
80 argument payloads will be associated to the notify payload. Variable
81 arguments must be {usigned char *, SilcUInt32 (len)}. */
83 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, SilcUInt32 argc,
87 SilcBuffer args = NULL;
89 SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
92 int i, k = 0, len = 0;
95 argv = silc_calloc(argc, sizeof(unsigned char *));
98 argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
103 argv_types = silc_calloc(argc, sizeof(SilcUInt32));
105 silc_free(argv_lens);
110 for (i = 0, k = 0; i < argc; i++) {
111 x = va_arg(ap, unsigned char *);
112 x_len = va_arg(ap, SilcUInt32);
117 argv[k] = silc_memdup(x, x_len);
120 argv_lens[k] = x_len;
121 argv_types[k] = i + 1;
125 args = silc_argument_payload_encode(k, argv, argv_lens, argv_types);
128 for (i = 0; i < k; i++)
131 silc_free(argv_lens);
132 silc_free(argv_types);
136 buffer = silc_buffer_alloc_size(len);
139 silc_buffer_format(buffer,
140 SILC_STR_UI_SHORT(type),
141 SILC_STR_UI_SHORT(len),
146 silc_buffer_pull(buffer, 5);
147 silc_buffer_format(buffer,
148 SILC_STR_UI_XNSTRING(args->data, args->len),
150 silc_buffer_push(buffer, 5);
151 silc_buffer_free(args);
157 /* Same as above but takes argument from the `args' Argument Payload. */
159 SilcBuffer silc_notify_payload_encode_args(SilcNotifyType type,
166 len = 5 + (args ? args->len : 0);
167 buffer = silc_buffer_alloc_size(len);
170 silc_buffer_format(buffer,
171 SILC_STR_UI_SHORT(type),
172 SILC_STR_UI_SHORT(len),
173 SILC_STR_UI_CHAR(argc),
177 silc_buffer_pull(buffer, 5);
178 silc_buffer_format(buffer,
179 SILC_STR_UI_XNSTRING(args->data, args->len),
181 silc_buffer_push(buffer, 5);
187 /* Frees notify payload */
189 void silc_notify_payload_free(SilcNotifyPayload payload)
192 silc_argument_payload_free(payload->args);
197 /* Return notify type */
199 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
201 return payload->type;
204 /* Return argument nums */
206 SilcUInt32 silc_notify_get_arg_num(SilcNotifyPayload payload)
208 return payload->argc;
211 /* Return argument payload */
213 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
215 return payload->args;