4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 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.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) {
39 if (!(n = avahi_malloc(sizeof(AvahiStringList) + size)))
45 /* NUL terminate strings, just to make sure */
51 AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t*text, size_t size) {
56 if (!(n = avahi_string_list_add_anonymous(l, size)))
60 memcpy(n->text, text, size);
65 AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text) {
68 return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text));
71 AvahiStringList *avahi_string_list_parse(const void* data, size_t size) {
72 AvahiStringList *r = NULL;
86 if (k > 0) /* Ignore empty strings */
87 r = avahi_string_list_add_arbitrary(r, c, k);
96 void avahi_string_list_free(AvahiStringList *l) {
106 AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) {
107 AvahiStringList *r = NULL, *n;
119 char* avahi_string_list_to_string(AvahiStringList *l) {
124 for (n = l; n; n = n->next) {
131 if (!(t = e = avahi_new(char, s+1)))
134 l = avahi_string_list_reverse(l);
136 for (n = l; n; n = n->next) {
141 strncpy(e, (char*) n->text, n->size);
149 l = avahi_string_list_reverse(l);
156 size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) {
165 l = avahi_string_list_reverse(l);
168 for (n = l; n; n = n->next) {
181 memcpy(c, n->text, k);
187 l = avahi_string_list_reverse(l);
191 /* Empty lists are treated specially. To comply with
192 * section 6.1 of the DNS-SD spec, we return a single
193 * empty string (i.e. a NUL byte)*/
196 *(uint8_t*) data = 0;
209 for (n = l; n; n = n->next) {
224 int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {
233 if (a->size != b->size)
236 if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0)
244 AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) {
248 r = avahi_string_list_add_many_va(r, va);
254 AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) {
257 while ((txt = va_arg(va, const char*)))
258 r = avahi_string_list_add(r, txt);
263 AvahiStringList *avahi_string_list_new(const char *txt, ...) {
265 AvahiStringList *r = NULL;
268 r = avahi_string_list_add(r, txt);
271 r = avahi_string_list_add_many_va(r, va);
278 AvahiStringList *avahi_string_list_new_va(va_list va) {
279 return avahi_string_list_add_many_va(NULL, va);
282 AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) {
283 AvahiStringList *r = NULL;
285 for (; l; l = l->next)
286 r = avahi_string_list_add_arbitrary(r, l->text, l->size);
288 return avahi_string_list_reverse(r);
291 AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) {
292 AvahiStringList *r = NULL;
297 for (i = 0; length >= 0 ? i < length : !!array[i]; i++)
298 r = avahi_string_list_add(r, array[i]);
303 unsigned avahi_string_list_length(const AvahiStringList *l) {
306 for (; l; l = l->next)
312 AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va) {
318 if (!(r = avahi_malloc(sizeof(AvahiStringList) + len)))
325 n = vsnprintf((char*) r->text, len+1, format, va);
327 if (n >= 0 && n < (int) len)
335 if (!(nr = avahi_realloc(r, sizeof(AvahiStringList) + len))) {
345 r->size = strlen((char*) r->text);
350 AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) {
355 va_start(va, format);
356 l = avahi_string_list_add_vprintf(l, format, va);
362 AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key) {
368 for (; l; l = l->next) {
369 if (strcasecmp((char*) l->text, key) == 0)
372 if (strncasecmp((char*) l->text, key, n) == 0 && l->text[n] == '=')
379 AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value) {
383 return avahi_string_list_add_printf(l, "%s=%s", key, value);
385 return avahi_string_list_add(l, key);
388 AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size) {
393 return avahi_string_list_add(l, key);
397 if (!(l = avahi_string_list_add_anonymous(l, n + 1 + size)))
400 memcpy(l->text, key, n);
402 memcpy(l->text + n + 1, value, size);
407 int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size) {
412 if (!(e = memchr(l->text, '=', l->size))) {
415 if (!(*key = avahi_strdup((char*) l->text)))
428 if (!(*key = avahi_strndup((char*) l->text, e - (char *) l->text)))
431 e++; /* Advance after '=' */
433 n = l->size - (e - (char*) l->text);
437 if (!(*value = avahi_memdup(e, n+1))) {
453 AvahiStringList *avahi_string_list_get_next(AvahiStringList *l) {
458 uint8_t *avahi_string_list_get_text(AvahiStringList *l) {
463 size_t avahi_string_list_get_size(AvahiStringList *l) {
468 uint32_t avahi_string_list_get_service_cookie(AvahiStringList *l) {
470 char *value = NULL, *end = NULL;
473 if (!(f = avahi_string_list_find(l, AVAHI_SERVICE_COOKIE)))
474 return AVAHI_SERVICE_COOKIE_INVALID;
476 if (avahi_string_list_get_pair(f, NULL, &value, NULL) < 0 || !value)
477 return AVAHI_SERVICE_COOKIE_INVALID;
479 ret = (uint32_t) strtoll(value, &end, 0);
481 if (*value && end && *end != 0) {
483 return AVAHI_SERVICE_COOKIE_INVALID;