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