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