99f342cff65493b06064b25e00afcfef6340dab5
[silc.git] / lib / silccore / silcnotify.c
1 /*
2
3   silcnotify.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2000 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23 #include "silcnotify.h"
24
25 /******************************************************************************
26
27                                Notify Payload
28
29 ******************************************************************************/
30
31 struct SilcNotifyPayloadStruct {
32   SilcNotifyType type;
33   unsigned char argc;
34   SilcArgumentPayload args;
35 };
36
37 /* Parse notify payload buffer and return data into payload structure */
38
39 SilcNotifyPayload silc_notify_payload_parse(const unsigned char *payload,
40                                             uint32 payload_len)
41 {
42   SilcBufferStruct buffer;
43   SilcNotifyPayload new;
44   uint16 len;
45   int ret;
46
47   SILC_LOG_DEBUG(("Parsing Notify payload"));
48
49   silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
50   new = silc_calloc(1, sizeof(*new));
51
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),
56                              SILC_STR_END);
57   if (ret == -1)
58     goto err;
59
60   if (len > buffer.len)
61     goto err;
62
63   if (new->argc) {
64     silc_buffer_pull(&buffer, 5);
65     new->args = silc_argument_payload_parse(buffer.data, buffer.len, 
66                                             new->argc);
67     silc_buffer_push(&buffer, 5);
68   }
69
70   return new;
71
72  err:
73   silc_free(new);
74   return NULL;
75 }
76
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)}. */
80
81 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, uint32 argc, 
82                                       va_list ap)
83 {
84   SilcBuffer buffer;
85   SilcBuffer args = NULL;
86   unsigned char **argv;
87   uint32 *argv_lens = NULL, *argv_types = NULL;
88   unsigned char *x;
89   uint32 x_len;
90   int i, k = 0, len = 0;
91
92   if (argc) {
93     argv = silc_calloc(argc, sizeof(unsigned char *));
94     argv_lens = silc_calloc(argc, sizeof(uint32));
95     argv_types = silc_calloc(argc, sizeof(uint32));
96     
97     for (i = 0, k = 0; i < argc; i++) {
98       x = va_arg(ap, unsigned char *);
99       x_len = va_arg(ap, uint32);
100
101       if (!x || !x_len)
102         continue;
103       
104       argv[k] = silc_calloc(x_len + 1, sizeof(unsigned char));
105       memcpy(argv[k], x, x_len);
106       argv_lens[k] = x_len;
107       argv_types[k] = i + 1;
108       k++;
109     }
110
111     args = silc_argument_payload_encode(k, argv, argv_lens, argv_types);
112     len = args->len;
113
114     for (i = 0; i < k; i++)
115       silc_free(argv[i]);
116     silc_free(argv);
117     silc_free(argv_lens);
118     silc_free(argv_types);
119   }
120
121   len += 5;
122   buffer = silc_buffer_alloc(len);
123   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
124   silc_buffer_format(buffer,
125                      SILC_STR_UI_SHORT(type),
126                      SILC_STR_UI_SHORT(len),
127                      SILC_STR_UI_CHAR(k),
128                      SILC_STR_END);
129
130   if (k) {
131     silc_buffer_pull(buffer, 5);
132     silc_buffer_format(buffer,
133                        SILC_STR_UI_XNSTRING(args->data, args->len),
134                        SILC_STR_END);
135     silc_buffer_push(buffer, 5);
136     silc_buffer_free(args);
137   }
138
139   return buffer;
140 }
141
142 /* Same as above but takes argument from the `args' Argument Payload. */
143
144 SilcBuffer silc_notify_payload_encode_args(SilcNotifyType type, 
145                                            uint32 argc,
146                                            SilcBuffer args)
147 {
148   SilcBuffer buffer;
149   int len;
150
151   len = 5 + (args ? args->len : 0);
152   buffer = silc_buffer_alloc(len);
153   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
154   silc_buffer_format(buffer,
155                      SILC_STR_UI_SHORT(type),
156                      SILC_STR_UI_SHORT(len),
157                      SILC_STR_UI_CHAR(argc),
158                      SILC_STR_END);
159
160   if (args) {
161     silc_buffer_pull(buffer, 5);
162     silc_buffer_format(buffer,
163                        SILC_STR_UI_XNSTRING(args->data, args->len),
164                        SILC_STR_END);
165     silc_buffer_push(buffer, 5);
166   }
167
168   return buffer;
169 }
170
171 /* Frees notify payload */
172
173 void silc_notify_payload_free(SilcNotifyPayload payload)
174 {
175   if (payload) {
176     silc_argument_payload_free(payload->args);
177     silc_free(payload);
178   }
179 }
180
181 /* Return notify type */
182
183 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
184 {
185   return payload->type;
186 }
187
188 /* Return argument nums */
189
190 uint32 silc_notify_get_arg_num(SilcNotifyPayload payload)
191 {
192   return payload->argc;
193 }
194
195 /* Return argument payload */
196
197 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
198 {
199   return payload->args;
200 }