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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
|
/***
This file is part of PulseAudio.
Copyright 2007 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <ctype.h>
#include <pulse/xmalloc.h>
#include <pulse/utf8.h>
#include <pulsecore/hashmap.h>
#include <pulsecore/strbuf.h>
#include <pulsecore/core-util.h>
#include "proplist.h"
struct property {
char *key;
void *value;
size_t nbytes;
};
#define MAKE_HASHMAP(p) ((pa_hashmap*) (p))
#define MAKE_HASHMAP_CONST(p) ((const pa_hashmap*) (p))
#define MAKE_PROPLIST(p) ((pa_proplist*) (p))
int pa_proplist_key_valid(const char *key) {
if (!pa_ascii_valid(key))
return 0;
if (strlen(key) <= 0)
return 0;
return 1;
}
static void property_free(struct property *prop) {
pa_assert(prop);
pa_xfree(prop->key);
pa_xfree(prop->value);
pa_xfree(prop);
}
pa_proplist* pa_proplist_new(void) {
return MAKE_PROPLIST(pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) property_free));
}
void pa_proplist_free(pa_proplist* p) {
pa_assert(p);
pa_hashmap_free(MAKE_HASHMAP(p));
}
/** Will accept only valid UTF-8 */
int pa_proplist_sets(pa_proplist *p, const char *key, const char *value) {
struct property *prop;
bool add = false;
pa_assert(p);
pa_assert(key);
pa_assert(value);
if (!pa_proplist_key_valid(key) || !pa_utf8_valid(value))
return -1;
if (!(prop = pa_hashmap_get(MAKE_HASHMAP_CONST(p), key))) {
prop = pa_xnew(struct property, 1);
prop->key = pa_xstrdup(key);
add = true;
} else
pa_xfree(prop->value);
prop->value = pa_xstrdup(value);
prop->nbytes = strlen(value)+1;
if (add)
pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
return 0;
}
/** Will accept only valid UTF-8 */
static int proplist_setn(pa_proplist *p, const char *key, size_t key_length, const char *value, size_t value_length) {
struct property *prop;
bool add = false;
char *k, *v;
pa_assert(p);
pa_assert(key);
pa_assert(value);
k = pa_xstrndup(key, key_length);
v = pa_xstrndup(value, value_length);
if (!pa_proplist_key_valid(k) || !pa_utf8_valid(v)) {
pa_xfree(k);
pa_xfree(v);
return -1;
}
if (!(prop = pa_hashmap_get(MAKE_HASHMAP_CONST(p), k))) {
prop = pa_xnew(struct property, 1);
prop->key = k;
add = true;
} else {
pa_xfree(prop->value);
pa_xfree(k);
}
prop->value = v;
prop->nbytes = strlen(v)+1;
if (add)
pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
return 0;
}
/** Will accept only valid UTF-8 */
int pa_proplist_setp(pa_proplist *p, const char *pair) {
const char *t;
pa_assert(p);
pa_assert(pair);
if (!(t = strchr(pair, '=')))
return -1;
return proplist_setn(p,
pair, t - pair,
t + 1, strchr(pair, 0) - t - 1);
}
static int proplist_sethex(pa_proplist *p, const char *key, size_t key_length, const char *value, size_t value_length) {
struct property *prop;
bool add = false;
char *k, *v;
uint8_t *d;
size_t dn;
pa_assert(p);
pa_assert(key);
pa_assert(value);
k = pa_xstrndup(key, key_length);
if (!pa_proplist_key_valid(k)) {
pa_xfree(k);
return -1;
}
v = pa_xstrndup(value, value_length);
d = pa_xmalloc(value_length*2+1);
if ((dn = pa_parsehex(v, d, value_length*2)) == (size_t) -1) {
pa_xfree(k);
pa_xfree(v);
pa_xfree(d);
return -1;
}
pa_xfree(v);
if (!(prop = pa_hashmap_get(MAKE_HASHMAP_CONST(p), k))) {
prop = pa_xnew(struct property, 1);
prop->key = k;
add = true;
} else {
pa_xfree(prop->value);
pa_xfree(k);
}
d[dn] = 0;
prop->value = d;
prop->nbytes = dn;
if (add)
pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
return 0;
}
/** Will accept only valid UTF-8 */
int pa_proplist_setf(pa_proplist *p, const char *key, const char *format, ...) {
struct property *prop;
bool add = false;
va_list ap;
char *v;
pa_assert(p);
pa_assert(key);
pa_assert(format);
if (!pa_proplist_key_valid(key) || !pa_utf8_valid(format))
return -1;
va_start(ap, format);
v = pa_vsprintf_malloc(format, ap);
va_end(ap);
if (!pa_utf8_valid(v))
goto fail;
if (!(prop = pa_hashmap_get(MAKE_HASHMAP_CONST(p), key))) {
prop = pa_xnew(struct property, 1);
prop->key = pa_xstrdup(key);
add = true;
} else
pa_xfree(prop->value);
prop->value = v;
prop->nbytes = strlen(v)+1;
if (add)
pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
return 0;
fail:
pa_xfree(v);
return -1;
}
int pa_proplist_set(pa_proplist *p, const char *key, const void *data, size_t nbytes) {
struct property *prop;
bool add = false;
pa_assert(p);
pa_assert(key);
pa_assert(data || nbytes == 0);
if (!pa_proplist_key_valid(key))
return -1;
if (!(prop = pa_hashmap_get(MAKE_HASHMAP_CONST(p), key))) {
prop = pa_xnew(struct property, 1);
prop->key = pa_xstrdup(key);
add = true;
} else
pa_xfree(prop->value);
prop->value = pa_xmalloc(nbytes+1);
if (nbytes > 0)
memcpy(prop->value, data, nbytes);
((char*) prop->value)[nbytes] = 0;
prop->nbytes = nbytes;
if (add)
pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
return 0;
}
const char *pa_proplist_gets(const pa_proplist *p, const char *key) {
struct property *prop;
pa_assert(p);
pa_assert(key);
if (!pa_proplist_key_valid(key))
return NULL;
if (!(prop = pa_hashmap_get(MAKE_HASHMAP_CONST(p), key)))
return NULL;
if (prop->nbytes <= 0)
return NULL;
if (((char*) prop->value)[prop->nbytes-1] != 0)
return NULL;
if (strlen((char*) prop->value) != prop->nbytes-1)
return NULL;
if (!pa_utf8_valid((char*) prop->value))
return NULL;
return (char*) prop->value;
}
int pa_proplist_get(const pa_proplist *p, const char *key, const void **data, size_t *nbytes) {
struct property *prop;
pa_assert(p);
pa_assert(key);
pa_assert(data);
pa_assert(nbytes);
if (!pa_proplist_key_valid(key))
return -1;
if (!(prop = pa_hashmap_get(MAKE_HASHMAP_CONST(p), key)))
return -1;
*data = prop->value;
*nbytes = prop->nbytes;
return 0;
}
void pa_proplist_update(pa_proplist *p, pa_update_mode_t mode, const pa_proplist *other) {
struct property *prop;
void *state = NULL;
pa_assert(p);
pa_assert(mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE);
pa_assert(other);
if (mode == PA_UPDATE_SET)
pa_proplist_clear(p);
while ((prop = pa_hashmap_iterate(MAKE_HASHMAP_CONST(other), &state, NULL))) {
if (mode == PA_UPDATE_MERGE && pa_proplist_contains(p, prop->key))
continue;
pa_assert_se(pa_proplist_set(p, prop->key, prop->value, prop->nbytes) == 0);
}
}
int pa_proplist_unset(pa_proplist *p, const char *key) {
pa_assert(p);
pa_assert(key);
if (!pa_proplist_key_valid(key))
return -1;
if (pa_hashmap_remove_and_free(MAKE_HASHMAP(p), key) < 0)
return -2;
return 0;
}
int pa_proplist_unset_many(pa_proplist *p, const char * const keys[]) {
const char * const * k;
int n = 0;
pa_assert(p);
pa_assert(keys);
for (k = keys; *k; k++)
if (!pa_proplist_key_valid(*k))
return -1;
for (k = keys; *k; k++)
if (pa_proplist_unset(p, *k) >= 0)
n++;
return n;
}
const char *pa_proplist_iterate(const pa_proplist *p, void **state) {
struct property *prop;
if (!(prop = pa_hashmap_iterate(MAKE_HASHMAP_CONST(p), state, NULL)))
return NULL;
return prop->key;
}
char *pa_proplist_to_string_sep(const pa_proplist *p, const char *sep) {
const char *key;
void *state = NULL;
pa_strbuf *buf;
pa_assert(p);
pa_assert(sep);
buf = pa_strbuf_new();
while ((key = pa_proplist_iterate(p, &state))) {
const char *v;
if (!pa_strbuf_isempty(buf))
pa_strbuf_puts(buf, sep);
if ((v = pa_proplist_gets(p, key))) {
const char *t;
pa_strbuf_printf(buf, "%s = \"", key);
for (t = v;;) {
size_t h;
h = strcspn(t, "\"");
if (h > 0)
pa_strbuf_putsn(buf, t, h);
t += h;
if (*t == 0)
break;
pa_assert(*t == '"');
pa_strbuf_puts(buf, "\\\"");
t++;
}
pa_strbuf_puts(buf, "\"");
} else {
const void *value;
size_t nbytes;
char *c;
pa_assert_se(pa_proplist_get(p, key, &value, &nbytes) == 0);
c = pa_xmalloc(nbytes*2+1);
pa_hexstr((const uint8_t*) value, nbytes, c, nbytes*2+1);
pa_strbuf_printf(buf, "%s = hex:%s", key, c);
pa_xfree(c);
}
}
return pa_strbuf_to_string_free(buf);
}
char *pa_proplist_to_string(const pa_proplist *p) {
char *s, *t;
s = pa_proplist_to_string_sep(p, "\n");
t = pa_sprintf_malloc("%s\n", s);
pa_xfree(s);
return t;
}
pa_proplist *pa_proplist_from_string(const char *s) {
enum {
WHITESPACE,
KEY,
AFTER_KEY,
VALUE_START,
VALUE_SIMPLE,
VALUE_DOUBLE_QUOTES,
VALUE_DOUBLE_QUOTES_ESCAPE,
VALUE_TICKS,
VALUE_TICKS_ESCAPED,
VALUE_HEX
} state;
pa_proplist *pl;
const char *p, *key = NULL, *value = NULL;
size_t key_len = 0, value_len = 0;
pa_assert(s);
pl = pa_proplist_new();
state = WHITESPACE;
for (p = s;; p++) {
switch (state) {
case WHITESPACE:
if (*p == 0)
goto success;
else if (*p == '=')
goto fail;
else if (!isspace((unsigned char)*p)) {
key = p;
state = KEY;
key_len = 1;
}
break;
case KEY:
if (*p == 0)
goto fail;
else if (*p == '=')
state = VALUE_START;
else if (isspace((unsigned char)*p))
state = AFTER_KEY;
else
key_len++;
break;
case AFTER_KEY:
if (*p == 0)
goto fail;
else if (*p == '=')
state = VALUE_START;
else if (!isspace((unsigned char)*p))
goto fail;
break;
case VALUE_START:
if (*p == 0)
goto fail;
else if (strncmp(p, "hex:", 4) == 0) {
state = VALUE_HEX;
value = p+4;
value_len = 0;
p += 3;
} else if (*p == '\'') {
state = VALUE_TICKS;
value = p+1;
value_len = 0;
} else if (*p == '"') {
state = VALUE_DOUBLE_QUOTES;
value = p+1;
value_len = 0;
} else if (!isspace((unsigned char)*p)) {
state = VALUE_SIMPLE;
value = p;
value_len = 1;
}
break;
case VALUE_SIMPLE:
if (*p == 0 || isspace((unsigned char)*p)) {
if (proplist_setn(pl, key, key_len, value, value_len) < 0)
goto fail;
if (*p == 0)
goto success;
state = WHITESPACE;
} else
value_len++;
break;
case VALUE_DOUBLE_QUOTES:
if (*p == 0)
goto fail;
else if (*p == '"') {
char *v;
v = pa_unescape(pa_xstrndup(value, value_len));
if (proplist_setn(pl, key, key_len, v, strlen(v)) < 0) {
pa_xfree(v);
goto fail;
}
pa_xfree(v);
state = WHITESPACE;
} else if (*p == '\\') {
state = VALUE_DOUBLE_QUOTES_ESCAPE;
value_len++;
} else
value_len++;
break;
case VALUE_DOUBLE_QUOTES_ESCAPE:
if (*p == 0)
goto fail;
else {
state = VALUE_DOUBLE_QUOTES;
value_len++;
}
break;
case VALUE_TICKS:
if (*p == 0)
goto fail;
else if (*p == '\'') {
char *v;
v = pa_unescape(pa_xstrndup(value, value_len));
if (proplist_setn(pl, key, key_len, v, strlen(v)) < 0) {
pa_xfree(v);
goto fail;
}
pa_xfree(v);
state = WHITESPACE;
} else if (*p == '\\') {
state = VALUE_TICKS_ESCAPED;
value_len++;
} else
value_len++;
break;
case VALUE_TICKS_ESCAPED:
if (*p == 0)
goto fail;
else {
state = VALUE_TICKS;
value_len++;
}
break;
case VALUE_HEX:
if ((*p >= '0' && *p <= '9') ||
(*p >= 'A' && *p <= 'F') ||
(*p >= 'a' && *p <= 'f')) {
value_len++;
} else if (*p == 0 || isspace((unsigned char)*p)) {
if (proplist_sethex(pl, key, key_len, value, value_len) < 0)
goto fail;
if (*p == 0)
goto success;
state = WHITESPACE;
} else
goto fail;
break;
}
}
success:
return MAKE_PROPLIST(pl);
fail:
pa_proplist_free(pl);
return NULL;
}
int pa_proplist_contains(const pa_proplist *p, const char *key) {
pa_assert(p);
pa_assert(key);
if (!pa_proplist_key_valid(key))
return -1;
if (!(pa_hashmap_get(MAKE_HASHMAP_CONST(p), key)))
return 0;
return 1;
}
void pa_proplist_clear(pa_proplist *p) {
pa_assert(p);
pa_hashmap_remove_all(MAKE_HASHMAP(p));
}
pa_proplist* pa_proplist_copy(const pa_proplist *p) {
pa_proplist *copy;
pa_assert_se(copy = pa_proplist_new());
if (p)
pa_proplist_update(copy, PA_UPDATE_REPLACE, p);
return copy;
}
unsigned pa_proplist_size(const pa_proplist *p) {
pa_assert(p);
return pa_hashmap_size(MAKE_HASHMAP_CONST(p));
}
int pa_proplist_isempty(const pa_proplist *p) {
pa_assert(p);
return pa_hashmap_isempty(MAKE_HASHMAP_CONST(p));
}
int pa_proplist_equal(const pa_proplist *a, const pa_proplist *b) {
const void *key = NULL;
struct property *a_prop = NULL;
struct property *b_prop = NULL;
void *state = NULL;
pa_assert(a);
pa_assert(b);
if (a == b)
return 1;
if (pa_proplist_size(a) != pa_proplist_size(b))
return 0;
while ((a_prop = pa_hashmap_iterate(MAKE_HASHMAP_CONST(a), &state, &key))) {
if (!(b_prop = pa_hashmap_get(MAKE_HASHMAP_CONST(b), key)))
return 0;
if (a_prop->nbytes != b_prop->nbytes)
return 0;
if (memcmp(a_prop->value, b_prop->value, a_prop->nbytes) != 0)
return 0;
}
return 1;
}
|