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