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