Code auditing weekend results and fixes committing.
[silc.git] / lib / silccore / silcnotify.c
1 /*
2
3   silcnotify.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 /* $Id$ */
21
22 #include "silcincludes.h"
23 #include "silcnotify.h"
24
25 /******************************************************************************
26
27                                Notify Payload
28
29 ******************************************************************************/
30
31 struct SilcNotifyPayloadStruct {
32   SilcNotifyType type;
33   unsigned char argc;
34   SilcArgumentPayload args;
35 };
36
37 /* Parse notify payload buffer and return data into payload structure */
38
39 SilcNotifyPayload silc_notify_payload_parse(SilcBuffer buffer)
40 {
41   SilcNotifyPayload new;
42   unsigned short len;
43   int ret;
44
45   SILC_LOG_DEBUG(("Parsing Notify payload"));
46
47   new = silc_calloc(1, sizeof(*new));
48
49   ret = silc_buffer_unformat(buffer,
50                              SILC_STR_UI_SHORT(&new->type),
51                              SILC_STR_UI_SHORT(&len),
52                              SILC_STR_UI_CHAR(&new->argc),
53                              SILC_STR_END);
54   if (ret == -1)
55     goto err;
56
57   if (len > buffer->len)
58     goto err;
59
60   if (new->argc) {
61     silc_buffer_pull(buffer, 5);
62     new->args = silc_argument_payload_parse(buffer, new->argc);
63     silc_buffer_push(buffer, 5);
64   }
65
66   return new;
67
68  err:
69   silc_free(new);
70   return NULL;
71 }
72
73 /* Encode notify payload with variable argument list. If `argc' is > 0
74    argument payloads will be associated to the notify payload. Variable
75    arguments must be {usigned char *, unsigned int (len)}. */
76
77 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, unsigned int argc, 
78                                       va_list ap)
79 {
80   SilcBuffer buffer;
81   SilcBuffer args = NULL;
82   unsigned char **argv;
83   unsigned int *argv_lens = NULL, *argv_types = NULL;
84   unsigned char *x;
85   unsigned int x_len;
86   int i, len = 0;
87
88   if (argc) {
89     argv = silc_calloc(argc, sizeof(unsigned char *));
90     argv_lens = silc_calloc(argc, sizeof(unsigned int));
91     argv_types = silc_calloc(argc, sizeof(unsigned int));
92     
93     for (i = 0; i < argc; i++) {
94       x = va_arg(ap, unsigned char *);
95       x_len = va_arg(ap, unsigned int);
96       
97       argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char));
98       memcpy(argv[i], x, x_len);
99       argv_lens[i] = x_len;
100       argv_types[i] = i + 1;
101     }
102
103     args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
104     len = args->len;
105
106     for (i = 0; i < argc; i++)
107       silc_free(argv[i]);
108     silc_free(argv);
109     silc_free(argv_lens);
110     silc_free(argv_types);
111   }
112
113   len += 5;
114   buffer = silc_buffer_alloc(len);
115   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
116   silc_buffer_format(buffer,
117                      SILC_STR_UI_SHORT(type),
118                      SILC_STR_UI_SHORT(len),
119                      SILC_STR_UI_CHAR(argc),
120                      SILC_STR_END);
121
122   if (argc) {
123     silc_buffer_pull(buffer, 5);
124     silc_buffer_format(buffer,
125                        SILC_STR_UI_XNSTRING(args->data, args->len),
126                        SILC_STR_END);
127     silc_buffer_push(buffer, 5);
128     silc_buffer_free(args);
129   }
130
131   return buffer;
132 }
133
134 /* Free's notify payload */
135
136 void silc_notify_payload_free(SilcNotifyPayload payload)
137 {
138   if (payload) {
139     silc_argument_payload_free(payload->args);
140     silc_free(payload);
141   }
142 }
143
144 /* Return notify type */
145
146 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
147 {
148   return payload->type;
149 }
150
151 /* Return argument nums */
152
153 unsigned int silc_notify_get_arg_num(SilcNotifyPayload payload)
154 {
155   return payload->argc;
156 }
157
158 /* Return argument payload */
159
160 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
161 {
162   return payload->args;
163 }