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