1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/*======================================================================
FILE: icalderivedparameters.{c,h}
CREATOR: eric 09 May 1999
(C) COPYRIGHT 2000, Eric Busboom <eric@civicknowledge.com>
This library is free software; you can redistribute it and/or modify
it under the terms of either:
The LGPL as published by the Free Software Foundation, version
2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html
Or:
The Mozilla Public License Version 2.0. You may obtain a copy of
the License at https://www.mozilla.org/MPL/
The original code is icalderivedparameters.{c,h}
Contributions from:
Graham Davison <g.m.davison@computer.org>
======================================================================*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "icalparameter.h"
#include "icalparameterimpl.h"
#include "icalerror.h"
#include "icalmemory.h"
#include <errno.h>
#include <stdlib.h>
LIBICAL_ICAL_EXPORT struct icalparameter_impl *icalparameter_new_impl(icalparameter_kind kind)
{
struct icalparameter_impl *v;
if ((v = (struct icalparameter_impl *)icalmemory_new_buffer(sizeof(struct icalparameter_impl))) == 0) {
icalerror_set_errno(ICAL_NEWFAILED_ERROR);
return 0;
}
memset(v, 0, sizeof(struct icalparameter_impl));
strcpy(v->id, "para");
v->kind = kind;
return v;
}
icalparameter *icalparameter_new(icalparameter_kind kind)
{
struct icalparameter_impl *v = icalparameter_new_impl(kind);
return (icalparameter *) v;
}
void icalparameter_free(icalparameter *param)
{
/* Comment out the following as it always triggers, even when parameter is non-zero
icalerror_check_arg_rv((parameter==0),"parameter");*/
if (param->parent != 0) {
return;
}
if (param->string != 0) {
icalmemory_free_buffer((void *)param->string);
}
if (param->x_name != 0) {
icalmemory_free_buffer((void *)param->x_name);
}
memset(param, 0, sizeof(icalparameter));
param->parent = 0;
param->id[0] = 'X';
icalmemory_free_buffer(param);
}
icalparameter *icalparameter_clone(const icalparameter *old)
{
struct icalparameter_impl *new;
icalerror_check_arg_rz((old != 0), "param");
new = icalparameter_new_impl(old->kind);
if (new == 0) {
return 0;
}
memcpy(new, old, sizeof(struct icalparameter_impl));
if (old->string != 0) {
new->string = icalmemory_strdup(old->string);
if (new->string == 0) {
new->parent = 0;
icalparameter_free(new);
return 0;
}
}
if (old->x_name != 0) {
new->x_name = icalmemory_strdup(old->x_name);
if (new->x_name == 0) {
new->parent = 0;
icalparameter_free(new);
return 0;
}
}
return new;
}
icalparameter *icalparameter_new_clone(icalparameter *old)
{
return icalparameter_clone(old);
}
icalparameter *icalparameter_new_from_string(const char *str)
{
char *eq;
char *cpy;
icalparameter_kind kind;
icalparameter *param;
icalerror_check_arg_rz(str != 0, "str");
cpy = icalmemory_strdup(str);
if (cpy == 0) {
icalerror_set_errno(ICAL_NEWFAILED_ERROR);
return 0;
}
eq = strchr(cpy, '=');
if (eq == 0) {
icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
icalmemory_free_buffer(cpy);
return 0;
}
*eq = '\0';
eq++;
kind = icalparameter_string_to_kind(cpy);
if (kind == ICAL_NO_PARAMETER) {
icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
icalmemory_free_buffer(cpy);
return 0;
}
param = icalparameter_new_from_value_string(kind, eq);
if (kind == ICAL_X_PARAMETER) {
icalparameter_set_xname(param, cpy);
} else if (kind == ICAL_IANA_PARAMETER) {
icalparameter_set_iana_name(param, cpy);
}
icalmemory_free_buffer(cpy);
return param;
}
char *icalparameter_as_ical_string(icalparameter *param)
{
char *buf;
buf = icalparameter_as_ical_string_r(param);
icalmemory_add_tmp_buffer(buf);
return buf;
}
/*
* Checks whether this character is allowed in a (Q)SAFE-CHAR
*
* QSAFE-CHAR = WSP / %x21 / %x23-7E / NON-US-ASCII
* ; any character except CTLs and DQUOTE
* SAFE-CHAR = WSP / %x21 / %x23-2B / %x2D-39 / %x3C-7E / NON-US-ASCII
* ; any character except CTLs, DQUOTE. ";", ":", ","
* WSP = SPACE / HTAB
* NON-US-ASCII = %x80-F8
* ; Use restricted by charset parameter
* ; on outer MIME object (UTF-8 preferred)
*/
static int icalparameter_is_safe_char(unsigned char character, int quoted)
{
if (character == ' ' || character == '\t' || character == '!' ||
(character >= 0x80 && character <= 0xF8)) {
return 1;
}
if (quoted && character >= 0x23 && character <= 0x7e) {
return 1;
}
else if (!quoted &&
((character >= 0x23 && character <= 0x2b) ||
(character >= 0x2d && character <= 0x39) ||
(character >= 0x3c && character <= 0x7e))) {
return 1;
}
return 0;
}
/**
* Appends the parameter value to the buffer, encoding per RFC 6868
* and filtering out those characters not permitted by the specifications
*
* paramtext = *SAFE-CHAR
* quoted-string= DQUOTE *QSAFE-CHAR DQUOTE
*/
static void icalparameter_append_encoded_value(char **buf, char **buf_ptr,
size_t *buf_size, const char *value)
{
int qm = 0;
const char *p;
/* Encapsulate the property in quotes if necessary */
if (!*value || strpbrk(value, ";:,") != 0) {
icalmemory_append_char(buf, buf_ptr, buf_size, '"');
qm = 1;
}
/* Copy the parameter value */
for (p = value; *p; p++) {
if (icalparameter_is_safe_char((unsigned char)*p, qm)) {
icalmemory_append_char(buf, buf_ptr, buf_size, *p);
} else {
/* Encode unsafe characters per RFC6868, otherwise replace with SP */
switch (*p) {
case '\n':
icalmemory_append_string(buf, buf_ptr, buf_size, "^n");
break;
case '^':
icalmemory_append_string(buf, buf_ptr, buf_size, "^^");
break;
case '"':
icalmemory_append_string(buf, buf_ptr, buf_size, "^'");
break;
default:
icalmemory_append_char(buf, buf_ptr, buf_size, ' ');
break;
}
}
}
if (qm == 1) {
icalmemory_append_char(buf, buf_ptr, buf_size, '"');
}
}
/*
* - param = param-name "=" param-value
* - param-name = iana-token / x-token
* - param-value = paramtext /quoted-string
* - paramtext = *SAFE-CHAR
* - quoted-string= DQUOTE *QSAFE-CHAR DQUOTE
* - QSAFE-CHAR = any character except CTLs and DQUOTE
* - SAFE-CHAR = any character except CTLs, DQUOTE. ";", ":", ","
*/
char *icalparameter_as_ical_string_r(icalparameter *param)
{
size_t buf_size = 1024;
char *buf;
char *buf_ptr;
const char *kind_string;
icalerror_check_arg_rz((param != 0), "parameter");
/* Create new buffer that we can append names, parameters and a
* value to, and reallocate as needed.
*/
buf = icalmemory_new_buffer(buf_size);
buf_ptr = buf;
if (param->kind == ICAL_X_PARAMETER) {
icalmemory_append_string(&buf, &buf_ptr, &buf_size, icalparameter_get_xname(param));
} else if (param->kind == ICAL_IANA_PARAMETER) {
icalmemory_append_string(&buf, &buf_ptr, &buf_size, icalparameter_get_iana_name(param));
} else {
kind_string = icalparameter_kind_to_string(param->kind);
if (param->kind == ICAL_NO_PARAMETER ||
param->kind == ICAL_ANY_PARAMETER || kind_string == 0) {
icalerror_set_errno(ICAL_BADARG_ERROR);
icalmemory_free_buffer(buf);
return 0;
}
/* Put the parameter name into the string */
icalmemory_append_string(&buf, &buf_ptr, &buf_size, kind_string);
}
icalmemory_append_string(&buf, &buf_ptr, &buf_size, "=");
if (param->string != 0) {
icalparameter_append_encoded_value(&buf, &buf_ptr, &buf_size, param->string);
} else if (param->data != 0) {
const char *str = icalparameter_enum_to_string(param->data);
icalmemory_append_string(&buf, &buf_ptr, &buf_size, str);
} else {
icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
icalmemory_free_buffer(buf);
return 0;
}
return buf;
}
icalparameter_kind icalparameter_isa(icalparameter *parameter)
{
if (parameter == 0) {
return ICAL_NO_PARAMETER;
}
return parameter->kind;
}
int icalparameter_isa_parameter(void *parameter)
{
struct icalparameter_impl *impl = (struct icalparameter_impl *)parameter;
if (parameter == 0) {
return 0;
}
if (strcmp(impl->id, "para") == 0) {
return 1;
} else {
return 0;
}
}
void icalparameter_set_xname(icalparameter *param, const char *v)
{
icalerror_check_arg_rv((param != 0), "param");
icalerror_check_arg_rv((v != 0), "v");
if (param->x_name != 0) {
icalmemory_free_buffer((void *)param->x_name);
}
param->x_name = icalmemory_strdup(v);
if (param->x_name == 0) {
errno = ENOMEM;
}
}
const char *icalparameter_get_xname(icalparameter *param)
{
icalerror_check_arg_rz((param != 0), "param");
return param->x_name;
}
void icalparameter_set_xvalue(icalparameter *param, const char *v)
{
icalerror_check_arg_rv((param != 0), "param");
icalerror_check_arg_rv((v != 0), "v");
if (param->string != 0) {
icalmemory_free_buffer((void *)param->string);
}
param->string = icalmemory_strdup(v);
if (param->string == 0) {
errno = ENOMEM;
}
}
const char *icalparameter_get_xvalue(icalparameter *param)
{
icalerror_check_arg_rz((param != 0), "param");
return param->string;
}
void icalparameter_set_iana_value(icalparameter *param, const char *v)
{
icalparameter_set_xvalue(param, v);
}
const char *icalparameter_get_iana_value(icalparameter *param)
{
return icalparameter_get_xvalue(param);
}
void icalparameter_set_iana_name(icalparameter *param, const char *v)
{
icalparameter_set_xname(param, v);
}
const char *icalparameter_get_iana_name(icalparameter *param)
{
return icalparameter_get_xname(param);
}
void icalparameter_set_parent(icalparameter *param, icalproperty *property)
{
icalerror_check_arg_rv((param != 0), "param");
param->parent = property;
}
icalproperty *icalparameter_get_parent(icalparameter *param)
{
icalerror_check_arg_rz((param != 0), "param");
return param->parent;
}
int icalparameter_has_same_name(icalparameter *param1, icalparameter *param2)
{
icalparameter_kind kind1;
icalparameter_kind kind2;
const char *name1;
const char *name2;
icalerror_check_arg_rz((param1 != 0), "param1");
icalerror_check_arg_rz((param2 != 0), "param2");
kind1 = icalparameter_isa(param1);
kind2 = icalparameter_isa(param2);
if (kind1 != kind2)
return 0;
if (kind1 == ICAL_X_PARAMETER) {
name1 = icalparameter_get_xname(param1);
name2 = icalparameter_get_xname(param2);
if (strcasecmp(name1, name2) != 0) {
return 0;
}
} else if (kind1 == ICAL_IANA_PARAMETER) {
name1 = icalparameter_get_iana_name(param1);
name2 = icalparameter_get_iana_name(param2);
if (strcasecmp(name1, name2) != 0) {
return 0;
}
}
return 1;
}
/* Everything below this line is machine generated. Do not edit. */
/* ALTREP */
|