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