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