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