]> code.delx.au - pulseaudio/blob - src/pulse/proplist.c
Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
[pulseaudio] / src / pulse / proplist.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2007 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <ctype.h>
28
29 #include <pulse/xmalloc.h>
30 #include <pulse/utf8.h>
31 #include <pulse/i18n.h>
32
33 #include <pulsecore/hashmap.h>
34 #include <pulsecore/strbuf.h>
35 #include <pulsecore/core-util.h>
36
37 #include "proplist.h"
38
39 struct property {
40 char *key;
41 void *value;
42 size_t nbytes;
43 };
44
45 #define MAKE_HASHMAP(p) ((pa_hashmap*) (p))
46 #define MAKE_PROPLIST(p) ((pa_proplist*) (p))
47
48 static pa_bool_t property_name_valid(const char *key) {
49
50 if (!pa_ascii_valid(key))
51 return FALSE;
52
53 if (strlen(key) <= 0)
54 return FALSE;
55
56 return TRUE;
57 }
58
59 static void property_free(struct property *prop) {
60 pa_assert(prop);
61
62 pa_xfree(prop->key);
63 pa_xfree(prop->value);
64 pa_xfree(prop);
65 }
66
67 pa_proplist* pa_proplist_new(void) {
68 return MAKE_PROPLIST(pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func));
69 }
70
71 void pa_proplist_free(pa_proplist* p) {
72 pa_assert(p);
73
74 pa_proplist_clear(p);
75 pa_hashmap_free(MAKE_HASHMAP(p), NULL, NULL);
76 }
77
78 /** Will accept only valid UTF-8 */
79 int pa_proplist_sets(pa_proplist *p, const char *key, const char *value) {
80 struct property *prop;
81 pa_bool_t add = FALSE;
82
83 pa_assert(p);
84 pa_assert(key);
85 pa_assert(value);
86
87 if (!property_name_valid(key) || !pa_utf8_valid(value))
88 return -1;
89
90 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
91 prop = pa_xnew(struct property, 1);
92 prop->key = pa_xstrdup(key);
93 add = TRUE;
94 } else
95 pa_xfree(prop->value);
96
97 prop->value = pa_xstrdup(value);
98 prop->nbytes = strlen(value)+1;
99
100 if (add)
101 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
102
103 return 0;
104 }
105
106 /** Will accept only valid UTF-8 */
107 static int proplist_setn(pa_proplist *p, const char *key, size_t key_length, const char *value, size_t value_length) {
108 struct property *prop;
109 pa_bool_t add = FALSE;
110 char *k, *v;
111
112 pa_assert(p);
113 pa_assert(key);
114 pa_assert(value);
115
116 k = pa_xstrndup(key, key_length);
117 v = pa_xstrndup(value, value_length);
118
119 if (!property_name_valid(k) || !pa_utf8_valid(v)) {
120 pa_xfree(k);
121 pa_xfree(v);
122 return -1;
123 }
124
125 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), k))) {
126 prop = pa_xnew(struct property, 1);
127 prop->key = k;
128 add = TRUE;
129 } else {
130 pa_xfree(prop->value);
131 pa_xfree(k);
132 }
133
134 prop->value = v;
135 prop->nbytes = strlen(v)+1;
136
137 if (add)
138 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
139
140 return 0;
141 }
142
143 /** Will accept only valid UTF-8 */
144 int pa_proplist_setp(pa_proplist *p, const char *pair) {
145 const char *t;
146
147 pa_assert(p);
148 pa_assert(pair);
149
150 if (!(t = strchr(pair, '=')))
151 return -1;
152
153 return proplist_setn(p,
154 pair, t - pair,
155 t + 1, strchr(pair, 0) - t - 1);
156 }
157
158 static int proplist_sethex(pa_proplist *p, const char *key, size_t key_length, const char *value, size_t value_length) {
159 struct property *prop;
160 pa_bool_t add = FALSE;
161 char *k, *v;
162 uint8_t *d;
163 size_t dn;
164
165 pa_assert(p);
166 pa_assert(key);
167 pa_assert(value);
168
169 k = pa_xstrndup(key, key_length);
170
171 if (!property_name_valid(k)) {
172 pa_xfree(k);
173 return -1;
174 }
175
176 v = pa_xstrndup(value, value_length);
177 d = pa_xmalloc(value_length*2+1);
178
179 if ((dn = pa_parsehex(v, d, value_length*2)) == (size_t) -1) {
180 pa_xfree(k);
181 pa_xfree(v);
182 pa_xfree(d);
183 return -1;
184 }
185
186 pa_xfree(v);
187
188 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), k))) {
189 prop = pa_xnew(struct property, 1);
190 prop->key = k;
191 add = TRUE;
192 } else {
193 pa_xfree(prop->value);
194 pa_xfree(k);
195 }
196
197 d[dn] = 0;
198 prop->value = d;
199 prop->nbytes = dn;
200
201 if (add)
202 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
203
204 return 0;
205 }
206
207 /** Will accept only valid UTF-8 */
208 int pa_proplist_setf(pa_proplist *p, const char *key, const char *format, ...) {
209 struct property *prop;
210 pa_bool_t add = FALSE;
211 va_list ap;
212 char *v;
213
214 pa_assert(p);
215 pa_assert(key);
216 pa_assert(format);
217
218 if (!property_name_valid(key) || !pa_utf8_valid(format))
219 return -1;
220
221 va_start(ap, format);
222 v = pa_vsprintf_malloc(format, ap);
223 va_end(ap);
224
225 if (!pa_utf8_valid(v))
226 goto fail;
227
228 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
229 prop = pa_xnew(struct property, 1);
230 prop->key = pa_xstrdup(key);
231 add = TRUE;
232 } else
233 pa_xfree(prop->value);
234
235 prop->value = v;
236 prop->nbytes = strlen(v)+1;
237
238 if (add)
239 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
240
241 return 0;
242
243 fail:
244 pa_xfree(v);
245 return -1;
246 }
247
248 int pa_proplist_set(pa_proplist *p, const char *key, const void *data, size_t nbytes) {
249 struct property *prop;
250 pa_bool_t add = FALSE;
251
252 pa_assert(p);
253 pa_assert(key);
254 pa_assert(data);
255
256 if (!property_name_valid(key))
257 return -1;
258
259 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
260 prop = pa_xnew(struct property, 1);
261 prop->key = pa_xstrdup(key);
262 add = TRUE;
263 } else
264 pa_xfree(prop->value);
265
266 prop->value = pa_xmalloc(nbytes+1);
267 memcpy(prop->value, data, nbytes);
268 ((char*) prop->value)[nbytes] = 0;
269 prop->nbytes = nbytes;
270
271 if (add)
272 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
273
274 return 0;
275 }
276
277 const char *pa_proplist_gets(pa_proplist *p, const char *key) {
278 struct property *prop;
279
280 pa_assert(p);
281 pa_assert(key);
282
283 if (!property_name_valid(key))
284 return NULL;
285
286 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key)))
287 return NULL;
288
289 if (prop->nbytes <= 0)
290 return NULL;
291
292 if (((char*) prop->value)[prop->nbytes-1] != 0)
293 return NULL;
294
295 if (strlen((char*) prop->value) != prop->nbytes-1)
296 return NULL;
297
298 if (!pa_utf8_valid((char*) prop->value))
299 return NULL;
300
301 return (char*) prop->value;
302 }
303
304 int pa_proplist_get(pa_proplist *p, const char *key, const void **data, size_t *nbytes) {
305 struct property *prop;
306
307 pa_assert(p);
308 pa_assert(key);
309 pa_assert(data);
310 pa_assert(nbytes);
311
312 if (!property_name_valid(key))
313 return -1;
314
315 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key)))
316 return -1;
317
318 *data = prop->value;
319 *nbytes = prop->nbytes;
320
321 return 0;
322 }
323
324 void pa_proplist_update(pa_proplist *p, pa_update_mode_t mode, pa_proplist *other) {
325 struct property *prop;
326 void *state = NULL;
327
328 pa_assert(p);
329 pa_assert(mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE);
330 pa_assert(other);
331
332 if (mode == PA_UPDATE_SET)
333 pa_proplist_clear(p);
334
335 while ((prop = pa_hashmap_iterate(MAKE_HASHMAP(other), &state, NULL))) {
336
337 if (mode == PA_UPDATE_MERGE && pa_proplist_contains(p, prop->key))
338 continue;
339
340 pa_assert_se(pa_proplist_set(p, prop->key, prop->value, prop->nbytes) == 0);
341 }
342 }
343
344 int pa_proplist_unset(pa_proplist *p, const char *key) {
345 struct property *prop;
346
347 pa_assert(p);
348 pa_assert(key);
349
350 if (!property_name_valid(key))
351 return -1;
352
353 if (!(prop = pa_hashmap_remove(MAKE_HASHMAP(p), key)))
354 return -2;
355
356 property_free(prop);
357 return 0;
358 }
359
360 int pa_proplist_unset_many(pa_proplist *p, const char * const keys[]) {
361 const char * const * k;
362 int n = 0;
363
364 pa_assert(p);
365 pa_assert(keys);
366
367 for (k = keys; *k; k++)
368 if (!property_name_valid(*k))
369 return -1;
370
371 for (k = keys; *k; k++)
372 if (pa_proplist_unset(p, *k) >= 0)
373 n++;
374
375 return n;
376 }
377
378 const char *pa_proplist_iterate(pa_proplist *p, void **state) {
379 struct property *prop;
380
381 if (!(prop = pa_hashmap_iterate(MAKE_HASHMAP(p), state, NULL)))
382 return NULL;
383
384 return prop->key;
385 }
386
387 char *pa_proplist_to_string_sep(pa_proplist *p, const char *sep) {
388 const char *key;
389 void *state = NULL;
390 pa_strbuf *buf;
391
392 pa_assert(p);
393 pa_assert(sep);
394
395 buf = pa_strbuf_new();
396
397 while ((key = pa_proplist_iterate(p, &state))) {
398 const char *v;
399
400 if (!pa_strbuf_isempty(buf))
401 pa_strbuf_puts(buf, sep);
402
403 if ((v = pa_proplist_gets(p, key))) {
404 const char *t;
405
406 pa_strbuf_printf(buf, "%s = \"", key);
407
408 for (t = v;;) {
409 size_t h;
410
411 h = strcspn(t, "\"");
412
413 if (h > 0)
414 pa_strbuf_putsn(buf, t, h);
415
416 t += h;
417
418 if (*t == 0)
419 break;
420
421 pa_assert(*t == '"');
422 pa_strbuf_puts(buf, "\\\"");
423
424 t++;
425 }
426
427 pa_strbuf_puts(buf, "\"");
428 } else {
429 const void *value;
430 size_t nbytes;
431 char *c;
432
433 pa_assert_se(pa_proplist_get(p, key, &value, &nbytes) == 0);
434 c = pa_xmalloc(nbytes*2+1);
435 pa_hexstr((const uint8_t*) value, nbytes, c, nbytes*2+1);
436
437 pa_strbuf_printf(buf, "%s = hex:%s", key, c);
438 pa_xfree(c);
439 }
440 }
441
442 return pa_strbuf_tostring_free(buf);
443 }
444
445 char *pa_proplist_to_string(pa_proplist *p) {
446 char *s, *t;
447
448 s = pa_proplist_to_string_sep(p, "\n");
449 t = pa_sprintf_malloc("%s\n", s);
450 pa_xfree(s);
451
452 return t;
453 }
454
455 pa_proplist *pa_proplist_from_string(const char *s) {
456 enum {
457 WHITESPACE,
458 KEY,
459 AFTER_KEY,
460 VALUE_START,
461 VALUE_SIMPLE,
462 VALUE_DOUBLE_QUOTES,
463 VALUE_DOUBLE_QUOTES_ESCAPE,
464 VALUE_TICKS,
465 VALUE_TICKS_ESCAPED,
466 VALUE_HEX
467 } state;
468
469 pa_proplist *pl;
470 const char *p, *key = NULL, *value = NULL;
471 size_t key_len = 0, value_len = 0;
472
473 pa_assert(s);
474
475 pl = pa_proplist_new();
476
477 state = WHITESPACE;
478
479 for (p = s;; p++) {
480 switch (state) {
481
482 case WHITESPACE:
483 if (*p == 0)
484 goto success;
485 else if (*p == '=')
486 goto fail;
487 else if (!isspace(*p)) {
488 key = p;
489 state = KEY;
490 key_len = 1;
491 }
492 break;
493
494 case KEY:
495 if (*p == 0)
496 goto fail;
497 else if (*p == '=')
498 state = VALUE_START;
499 else if (isspace(*p))
500 state = AFTER_KEY;
501 else
502 key_len++;
503 break;
504
505 case AFTER_KEY:
506 if (*p == 0)
507 goto fail;
508 else if (*p == '=')
509 state = VALUE_START;
510 else if (!isspace(*p))
511 goto fail;
512 break;
513
514 case VALUE_START:
515 if (*p == 0)
516 goto fail;
517 else if (strncmp(p, "hex:", 4) == 0) {
518 state = VALUE_HEX;
519 value = p+4;
520 value_len = 0;
521 p += 3;
522 } else if (*p == '\'') {
523 state = VALUE_TICKS;
524 value = p+1;
525 value_len = 0;
526 } else if (*p == '"') {
527 state = VALUE_DOUBLE_QUOTES;
528 value = p+1;
529 value_len = 0;
530 } else if (!isspace(*p)) {
531 state = VALUE_SIMPLE;
532 value = p;
533 value_len = 1;
534 }
535 break;
536
537 case VALUE_SIMPLE:
538 if (*p == 0 || isspace(*p)) {
539 if (proplist_setn(pl, key, key_len, value, value_len) < 0)
540 goto fail;
541
542 if (*p == 0)
543 goto success;
544
545 state = WHITESPACE;
546 } else
547 value_len++;
548 break;
549
550 case VALUE_DOUBLE_QUOTES:
551 if (*p == 0)
552 goto fail;
553 else if (*p == '"') {
554 char *v;
555
556 v = pa_unescape(pa_xstrndup(value, value_len));
557
558 if (proplist_setn(pl, key, key_len, v, strlen(v)) < 0) {
559 pa_xfree(v);
560 goto fail;
561 }
562
563 pa_xfree(v);
564 state = WHITESPACE;
565 } else if (*p == '\\') {
566 state = VALUE_DOUBLE_QUOTES_ESCAPE;
567 value_len++;
568 } else
569 value_len++;
570 break;
571
572 case VALUE_DOUBLE_QUOTES_ESCAPE:
573 if (*p == 0)
574 goto fail;
575 else {
576 state = VALUE_DOUBLE_QUOTES;
577 value_len++;
578 }
579 break;
580
581 case VALUE_TICKS:
582 if (*p == 0)
583 goto fail;
584 else if (*p == '\'') {
585 char *v;
586
587 v = pa_unescape(pa_xstrndup(value, value_len));
588
589 if (proplist_setn(pl, key, key_len, v, strlen(v)) < 0) {
590 pa_xfree(v);
591 goto fail;
592 }
593
594 pa_xfree(v);
595 state = WHITESPACE;
596 } else if (*p == '\\') {
597 state = VALUE_TICKS_ESCAPED;
598 value_len++;
599 } else
600 value_len++;
601 break;
602
603 case VALUE_TICKS_ESCAPED:
604 if (*p == 0)
605 goto fail;
606 else {
607 state = VALUE_TICKS;
608 value_len++;
609 }
610 break;
611
612 case VALUE_HEX:
613 if ((*p >= '0' && *p <= '9') ||
614 (*p >= 'A' && *p <= 'F') ||
615 (*p >= 'a' && *p <= 'f')) {
616 value_len++;
617 } else if (*p == 0 || isspace(*p)) {
618
619 if (proplist_sethex(pl, key, key_len, value, value_len) < 0)
620 goto fail;
621
622 if (*p == 0)
623 goto success;
624
625 state = WHITESPACE;
626 } else
627 goto fail;
628 break;
629 }
630 }
631
632 success:
633 return MAKE_PROPLIST(pl);
634
635 fail:
636 pa_proplist_free(pl);
637 return NULL;
638 }
639
640 int pa_proplist_contains(pa_proplist *p, const char *key) {
641 pa_assert(p);
642 pa_assert(key);
643
644 if (!property_name_valid(key))
645 return -1;
646
647 if (!(pa_hashmap_get(MAKE_HASHMAP(p), key)))
648 return 0;
649
650 return 1;
651 }
652
653 void pa_proplist_clear(pa_proplist *p) {
654 struct property *prop;
655 pa_assert(p);
656
657 while ((prop = pa_hashmap_steal_first(MAKE_HASHMAP(p))))
658 property_free(prop);
659 }
660
661 pa_proplist* pa_proplist_copy(pa_proplist *template) {
662 pa_proplist *p;
663
664 pa_assert_se(p = pa_proplist_new());
665
666 if (template)
667 pa_proplist_update(p, PA_UPDATE_REPLACE, template);
668
669 return p;
670 }
671
672 unsigned pa_proplist_size(pa_proplist *p) {
673 pa_assert(p);
674
675 return pa_hashmap_size(MAKE_HASHMAP(p));
676 }
677
678 int pa_proplist_isempty(pa_proplist *p) {
679 pa_assert(p);
680
681 return pa_hashmap_isempty(MAKE_HASHMAP(p));
682 }
683
684 int pa_proplist_equal(pa_proplist *a, pa_proplist *b) {
685 const void *key = NULL;
686 struct property *a_prop = NULL;
687 struct property *b_prop = NULL;
688 void *state = NULL;
689
690 pa_assert(a);
691 pa_assert(b);
692
693 if (pa_proplist_size(a) != pa_proplist_size(b))
694 return 0;
695
696 while ((a_prop = pa_hashmap_iterate(MAKE_HASHMAP(a), &state, &key))) {
697 if (!(b_prop = pa_hashmap_get(MAKE_HASHMAP(b), key)))
698 return 0;
699
700 if (a_prop->nbytes != b_prop->nbytes)
701 return 0;
702
703 if (memcmp(a_prop->value, b_prop->value, a_prop->nbytes) != 0)
704 return 0;
705 }
706
707 return 1;
708 }