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,
42 SilcBufferStruct buffer;
43 SilcNotifyPayload new;
47 SILC_LOG_DEBUG(("Parsing Notify payload"));
49 silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
50 new = silc_calloc(1, sizeof(*new));
52 ret = silc_buffer_unformat(&buffer,
53 SILC_STR_UI_SHORT(&new->type),
54 SILC_STR_UI_SHORT(&len),
55 SILC_STR_UI_CHAR(&new->argc),
64 silc_buffer_pull(&buffer, 5);
65 new->args = silc_argument_payload_parse(buffer.data, buffer.len,
67 silc_buffer_push(&buffer, 5);
77 /* Encode notify payload with variable argument list. If `argc' is > 0
78 argument payloads will be associated to the notify payload. Variable
79 arguments must be {usigned char *, uint32 (len)}. */
81 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, uint32 argc,
85 SilcBuffer args = NULL;
87 uint32 *argv_lens = NULL, *argv_types = NULL;
90 int i, k = 0, len = 0;
93 argv = silc_calloc(argc, sizeof(unsigned char *));
94 argv_lens = silc_calloc(argc, sizeof(uint32));
95 argv_types = silc_calloc(argc, sizeof(uint32));
97 for (i = 0, k = 0; i < argc; i++) {
98 x = va_arg(ap, unsigned char *);
99 x_len = va_arg(ap, uint32);
104 argv[k] = silc_memdup(x, x_len);
105 argv_lens[k] = x_len;
106 argv_types[k] = i + 1;
110 args = silc_argument_payload_encode(k, argv, argv_lens, argv_types);
113 for (i = 0; i < k; i++)
116 silc_free(argv_lens);
117 silc_free(argv_types);
121 buffer = silc_buffer_alloc(len);
122 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
123 silc_buffer_format(buffer,
124 SILC_STR_UI_SHORT(type),
125 SILC_STR_UI_SHORT(len),
130 silc_buffer_pull(buffer, 5);
131 silc_buffer_format(buffer,
132 SILC_STR_UI_XNSTRING(args->data, args->len),
134 silc_buffer_push(buffer, 5);
135 silc_buffer_free(args);
141 /* Same as above but takes argument from the `args' Argument Payload. */
143 SilcBuffer silc_notify_payload_encode_args(SilcNotifyType type,
150 len = 5 + (args ? args->len : 0);
151 buffer = silc_buffer_alloc(len);
152 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
153 silc_buffer_format(buffer,
154 SILC_STR_UI_SHORT(type),
155 SILC_STR_UI_SHORT(len),
156 SILC_STR_UI_CHAR(argc),
160 silc_buffer_pull(buffer, 5);
161 silc_buffer_format(buffer,
162 SILC_STR_UI_XNSTRING(args->data, args->len),
164 silc_buffer_push(buffer, 5);
170 /* Frees notify payload */
172 void silc_notify_payload_free(SilcNotifyPayload payload)
175 silc_argument_payload_free(payload->args);
180 /* Return notify type */
182 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
184 return payload->type;
187 /* Return argument nums */
189 uint32 silc_notify_get_arg_num(SilcNotifyPayload payload)
191 return payload->argc;
194 /* Return argument payload */
196 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
198 return payload->args;