updates.
[silc.git] / lib / silccore / silcpayload.c
1 /*
2
3   silcpayload.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 /* Implementation of generic payloads described in the protocol 
21    specification drafts. */
22 /* $Id$ */
23
24 #include "silcincludes.h"
25 #include "silcpayload.h"
26
27 /******************************************************************************
28
29                                 ID Payload
30
31 ******************************************************************************/
32
33 struct SilcIDPayloadStruct {
34   SilcIdType type;
35   uint16 len;
36   unsigned char *id;
37 };
38
39 /* Parses buffer and return ID payload into payload structure */
40
41 SilcIDPayload silc_id_payload_parse(SilcBuffer buffer)
42 {
43   SilcIDPayload new;
44   int ret;
45
46   SILC_LOG_DEBUG(("Parsing ID payload"));
47
48   new = silc_calloc(1, sizeof(*new));
49
50   ret = silc_buffer_unformat(buffer,
51                              SILC_STR_UI_SHORT(&new->type),
52                              SILC_STR_UI_SHORT(&new->len),
53                              SILC_STR_END);
54   if (ret == -1)
55     goto err;
56
57   silc_buffer_pull(buffer, 4);
58
59   if (new->len > buffer->len)
60     goto err;
61
62   ret = silc_buffer_unformat(buffer,
63                              SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
64                              SILC_STR_END);
65   if (ret == -1)
66     goto err;
67
68   silc_buffer_push(buffer, 4);
69
70   return new;
71
72  err:
73   silc_free(new);
74   return NULL;
75 }
76
77 /* Parses data and return ID payload into payload structure. */
78
79 SilcIDPayload silc_id_payload_parse_data(unsigned char *data, 
80                                          uint32 len)
81 {
82   SilcIDPayload new;
83   SilcBufferStruct buffer;
84   int ret;
85
86   SILC_LOG_DEBUG(("Parsing ID payload"));
87
88   silc_buffer_set(&buffer, data, len);
89
90   new = silc_calloc(1, sizeof(*new));
91
92   ret = silc_buffer_unformat(&buffer,
93                              SILC_STR_UI_SHORT(&new->type),
94                              SILC_STR_UI_SHORT(&new->len),
95                              SILC_STR_END);
96   if (ret == -1)
97     goto err;
98
99   silc_buffer_pull(&buffer, 4);
100
101   if (new->len > buffer.len)
102     goto err;
103
104   ret = silc_buffer_unformat(&buffer,
105                              SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
106                              SILC_STR_END);
107   if (ret == -1)
108     goto err;
109
110   return new;
111
112  err:
113   silc_free(new);
114   return NULL;
115 }
116
117 /* Return the ID directly from the raw payload data. */
118
119 void *silc_id_payload_parse_id(unsigned char *data, uint32 len)
120 {
121   SilcBufferStruct buffer;
122   SilcIdType type;
123   uint16 idlen;
124   unsigned char *id_data = NULL;
125   int ret;
126   void *id;
127
128   silc_buffer_set(&buffer, data, len);
129
130   ret = silc_buffer_unformat(&buffer,
131                              SILC_STR_UI_SHORT(&type),
132                              SILC_STR_UI_SHORT(&idlen),
133                              SILC_STR_END);
134   if (ret == -1)
135     goto err;
136
137   silc_buffer_pull(&buffer, 4);
138
139   if (idlen > buffer.len)
140     goto err;
141
142   ret = silc_buffer_unformat(&buffer,
143                              SILC_STR_UI_XNSTRING_ALLOC(&id_data, idlen),
144                              SILC_STR_END);
145   if (ret == -1)
146     goto err;
147
148   id = silc_id_str2id(id_data, idlen, type);
149   silc_free(id_data);
150   return id;
151
152  err:
153   return NULL;
154 }
155
156 /* Encodes ID Payload */
157
158 SilcBuffer silc_id_payload_encode(void *id, SilcIdType type)
159 {
160   SilcBuffer buffer;
161   unsigned char *id_data;
162   uint32 len;
163
164   id_data = silc_id_id2str(id, type);
165   len = silc_id_get_len(id, type);
166   buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
167                                        len, type);
168   silc_free(id_data);
169   return buffer;
170 }
171
172 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
173                                        uint32 id_len, SilcIdType type)
174 {
175   SilcBuffer buffer;
176
177   SILC_LOG_DEBUG(("Encoding %s ID payload",
178                   type == SILC_ID_CLIENT ? "Client" :
179                   type == SILC_ID_SERVER ? "Server" : "Channel"));
180
181   buffer = silc_buffer_alloc(4 + id_len);
182   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
183   silc_buffer_format(buffer,
184                      SILC_STR_UI_SHORT(type),
185                      SILC_STR_UI_SHORT(id_len),
186                      SILC_STR_UI_XNSTRING(id, id_len),
187                      SILC_STR_END);
188   return buffer;
189 }
190
191 /* Free ID Payload */
192
193 void silc_id_payload_free(SilcIDPayload payload)
194 {
195   if (payload) {
196     silc_free(payload->id);
197     silc_free(payload);
198   }
199 }
200
201 /* Get ID type */
202
203 SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
204 {
205   return payload ? payload->type : 0;
206 }
207
208 /* Get ID */
209
210 void *silc_id_payload_get_id(SilcIDPayload payload)
211 {
212   return payload ? silc_id_str2id(payload->id, payload->len,
213                                   payload->type) : NULL;
214 }
215
216 /* Get raw ID data. Data is duplicated. */
217
218 unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
219 {
220   unsigned char *ret;
221
222   if (!payload)
223     return NULL;
224
225   ret = silc_calloc(payload->len, sizeof(*ret));
226   memcpy(ret, payload->id, payload->len);
227   return ret;
228 }
229
230 /* Get length of ID */
231
232 uint32 silc_id_payload_get_len(SilcIDPayload payload)
233 {
234   return payload ? payload->len : 0;
235 }
236
237 /******************************************************************************
238
239                              Argument Payload
240
241 ******************************************************************************/
242
243 struct SilcArgumentPayloadStruct {
244   uint32 argc;
245   unsigned char **argv;
246   uint32 *argv_lens;
247   uint32 *argv_types;
248   uint32 pos;
249 };
250
251 /* Parses arguments and returns them into Argument Payload structure. */
252
253 SilcArgumentPayload silc_argument_payload_parse(SilcBuffer buffer,
254                                                 uint32 argc)
255 {
256   SilcArgumentPayload new;
257   uint16 payload_len = 0;
258   unsigned char arg_num = 0;
259   unsigned char arg_type = 0;
260   uint32 pull_len = 0;
261   int i = 0, ret;
262
263   SILC_LOG_DEBUG(("Parsing argument payload"));
264
265   new = silc_calloc(1, sizeof(*new));
266   new->argv = silc_calloc(argc, sizeof(unsigned char *));
267   new->argv_lens = silc_calloc(argc, sizeof(uint32));
268   new->argv_types = silc_calloc(argc, sizeof(uint32));
269     
270   /* Get arguments */
271   arg_num = 1;
272   for (i = 0; i < argc; i++) {
273     ret = silc_buffer_unformat(buffer,
274                                SILC_STR_UI_SHORT(&payload_len),
275                                SILC_STR_UI_CHAR(&arg_type),
276                                SILC_STR_END);
277     if (ret == -1)
278       goto err;
279     
280     new->argv_lens[i] = payload_len;
281     new->argv_types[i] = arg_type;
282
283     if (payload_len > buffer->len - 3)
284       break;
285     
286     /* Get argument data */
287     silc_buffer_pull(buffer, 3);
288     ret = silc_buffer_unformat(buffer,
289                                SILC_STR_UI_XNSTRING_ALLOC(&new->argv[i], 
290                                                           payload_len),
291                                SILC_STR_END);
292     if (ret == -1)
293       goto err;
294
295     silc_buffer_pull(buffer, payload_len);
296     pull_len += 3 + payload_len;
297   }
298
299   if (buffer->len != 0)
300     goto err;
301
302   new->argc = argc;
303   new->pos = 0;
304
305   silc_buffer_push(buffer, pull_len);
306
307   return new;
308
309  err:
310   if (i) {
311     int k;
312
313     for (k = 0; k < i; k++)
314       silc_free(new->argv[k]);
315   }
316
317   silc_free(new->argv);
318   silc_free(new->argv_lens);
319   silc_free(new->argv_types);
320
321   if (new)
322     silc_free(new);
323
324   return NULL;
325 }
326
327 /* Encodes arguments in to Argument Paylods returning them to SilcBuffer. */
328
329 SilcBuffer silc_argument_payload_encode(uint32 argc,
330                                         unsigned char **argv,
331                                         uint32 *argv_lens,
332                                         uint32 *argv_types)
333 {
334   SilcBuffer buffer;
335   uint32 len;
336   int i;
337
338   SILC_LOG_DEBUG(("Encoding Argument payload"));
339
340   len = 0;
341   for (i = 0; i < argc; i++)
342     len += 3 + argv_lens[i];
343
344   buffer = silc_buffer_alloc(len);
345   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
346
347   /* Put arguments */
348   for (i = 0; i < argc; i++) {
349     silc_buffer_format(buffer,
350                        SILC_STR_UI_SHORT(argv_lens[i]),
351                        SILC_STR_UI_CHAR(argv_types[i]),
352                        SILC_STR_UI_XNSTRING(argv[i], argv_lens[i]),
353                        SILC_STR_END);
354     silc_buffer_pull(buffer, 3 + argv_lens[i]);
355   }
356
357   silc_buffer_push(buffer, len);
358
359   return buffer;
360 }
361
362 /* Same as above but encode the buffer from SilcArgumentPayload structure
363    instead of raw data. */
364
365 SilcBuffer silc_argument_payload_encode_payload(SilcArgumentPayload payload)
366 {
367   SilcBuffer buffer;
368   uint32 len;
369   int i;
370
371   SILC_LOG_DEBUG(("Encoding Argument payload"));
372
373   len = 0;
374   for (i = 0; i < payload->argc; i++)
375     len += 3 + payload->argv_lens[i];
376
377   buffer = silc_buffer_alloc(len);
378   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
379
380   /* Put arguments */
381   for (i = 0; i < payload->argc; i++) {
382     silc_buffer_format(buffer,
383                        SILC_STR_UI_SHORT(payload->argv_lens[i]),
384                        SILC_STR_UI_CHAR(payload->argv_types[i]),
385                        SILC_STR_UI_XNSTRING(payload->argv[i], 
386                                             payload->argv_lens[i]),
387                        SILC_STR_END);
388     silc_buffer_pull(buffer, 3 + payload->argv_lens[i]);
389   }
390
391   silc_buffer_push(buffer, len);
392
393   return buffer;
394 }
395
396 /* Frees Argument Payload */
397
398 void silc_argument_payload_free(SilcArgumentPayload payload)
399 {
400   int i;
401
402   if (payload) {
403     for (i = 0; i < payload->argc; i++)
404       silc_free(payload->argv[i]);
405
406     silc_free(payload->argv);
407     silc_free(payload->argv_lens);
408     silc_free(payload->argv_types);
409     silc_free(payload);
410   }
411 }
412
413 /* Returns number of arguments in payload */
414
415 uint32 silc_argument_get_arg_num(SilcArgumentPayload payload)
416 {
417   return payload ? payload->argc : 0;
418 }
419
420 /* Returns first argument from payload. */
421
422 unsigned char *silc_argument_get_first_arg(SilcArgumentPayload payload,
423                                            uint32 *ret_len)
424 {
425   if (!payload)
426     return NULL;
427
428   payload->pos = 0;
429
430   if (ret_len)
431     *ret_len = payload->argv_lens[payload->pos];
432
433   return payload->argv[payload->pos++];
434 }
435
436 /* Returns next argument from payload or NULL if no more arguments. */
437
438 unsigned char *silc_argument_get_next_arg(SilcArgumentPayload payload,
439                                           uint32 *ret_len)
440 {
441   if (!payload)
442     return NULL;
443
444   if (payload->pos >= payload->argc)
445     return NULL;
446
447   if (ret_len)
448     *ret_len = payload->argv_lens[payload->pos];
449
450   return payload->argv[payload->pos++];
451 }
452
453 /* Returns argument which type is `type'. */
454
455 unsigned char *silc_argument_get_arg_type(SilcArgumentPayload payload,
456                                           uint32 type,
457                                           uint32 *ret_len)
458 {
459   int i;
460
461   if (!payload)
462     return NULL;
463
464   for (i = 0; i < payload->argc; i++)
465     if (payload->argv_types[i] == type)
466       break;
467
468   if (i >= payload->argc)
469     return NULL;
470
471   if (ret_len)
472     *ret_len = payload->argv_lens[i];
473
474   return payload->argv[i];
475 }