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 int avahi_string_list_parse(const void* data, size_t size, AvahiStringList **ret) {
73 AvahiStringList *r = NULL;
86 goto fail; /* Overflow */
88 if (k > 0) { /* Ignore empty strings */
91 if (!(n = avahi_string_list_add_arbitrary(r, c, k)))
106 avahi_string_list_free(r);
110 void avahi_string_list_free(AvahiStringList *l) {
120 AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) {
121 AvahiStringList *r = NULL, *n;
133 char* avahi_string_list_to_string(AvahiStringList *l) {
138 for (n = l; n; n = n->next) {
145 if (!(t = e = avahi_new(char, s+1)))
148 l = avahi_string_list_reverse(l);
150 for (n = l; n; n = n->next) {
155 strncpy(e, (char*) n->text, n->size);
163 l = avahi_string_list_reverse(l);
170 size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) {
177 l = avahi_string_list_reverse(l);
180 for (n = l; size > 1 && n; n = n->next) {
183 if ((k = n->size) == 0)
184 /* Skip empty strings */
188 /* Truncate strings at 255 characters */
192 /* Make sure this string fits in */
195 *(c++) = (uint8_t) k;
196 memcpy(c, n->text, k);
203 l = avahi_string_list_reverse(l);
205 if (used == 0 && size > 0) {
207 /* Empty lists are treated specially. To comply with
208 * section 6.1 of the DNS-SD spec, we return a single
209 * empty string (i.e. a NUL byte)*/
211 *(uint8_t*) data = 0;
218 for (n = l; n; n = n->next) {
221 if ((k = n->size) == 0)
237 int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {
246 if (a->size != b->size)
249 if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0)
257 AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) {
261 r = avahi_string_list_add_many_va(r, va);
267 AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) {
270 while ((txt = va_arg(va, const char*)))
271 r = avahi_string_list_add(r, txt);
276 AvahiStringList *avahi_string_list_new(const char *txt, ...) {
278 AvahiStringList *r = NULL;
281 r = avahi_string_list_add(r, txt);
284 r = avahi_string_list_add_many_va(r, va);
291 AvahiStringList *avahi_string_list_new_va(va_list va) {
292 return avahi_string_list_add_many_va(NULL, va);
295 AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) {
296 AvahiStringList *r = NULL;
298 for (; l; l = l->next)
299 if (!(r = avahi_string_list_add_arbitrary(r, l->text, l->size))) {
300 avahi_string_list_free(r);
304 return avahi_string_list_reverse(r);
307 AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) {
308 AvahiStringList *r = NULL;
313 for (i = 0; length >= 0 ? i < length : !!array[i]; i++)
314 r = avahi_string_list_add(r, array[i]);
319 unsigned avahi_string_list_length(const AvahiStringList *l) {
322 for (; l; l = l->next)
328 AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va) {
334 if (!(r = avahi_malloc(sizeof(AvahiStringList) + len)))
341 n = vsnprintf((char*) r->text, len+1, format, va);
343 if (n >= 0 && n < (int) len)
351 if (!(nr = avahi_realloc(r, sizeof(AvahiStringList) + len))) {
361 r->size = strlen((char*) r->text);
366 AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) {
371 va_start(va, format);
372 l = avahi_string_list_add_vprintf(l, format, va);
378 AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key) {
384 for (; l; l = l->next) {
385 if (strcasecmp((char*) l->text, key) == 0)
388 if (strncasecmp((char*) l->text, key, n) == 0 && l->text[n] == '=')
395 AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value) {
399 return avahi_string_list_add_printf(l, "%s=%s", key, value);
401 return avahi_string_list_add(l, key);
404 AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size) {
409 return avahi_string_list_add(l, key);
413 if (!(l = avahi_string_list_add_anonymous(l, n + 1 + size)))
416 memcpy(l->text, key, n);
418 memcpy(l->text + n + 1, value, size);
423 int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size) {
428 if (!(e = memchr(l->text, '=', l->size))) {
431 if (!(*key = avahi_strdup((char*) l->text)))
444 if (!(*key = avahi_strndup((char*) l->text, e - (char *) l->text)))
447 e++; /* Advance after '=' */
449 n = l->size - (e - (char*) l->text);
453 if (!(*value = avahi_memdup(e, n+1))) {
469 AvahiStringList *avahi_string_list_get_next(AvahiStringList *l) {
474 uint8_t *avahi_string_list_get_text(AvahiStringList *l) {
479 size_t avahi_string_list_get_size(AvahiStringList *l) {
484 uint32_t avahi_string_list_get_service_cookie(AvahiStringList *l) {
486 char *value = NULL, *end = NULL;
489 if (!(f = avahi_string_list_find(l, AVAHI_SERVICE_COOKIE)))
490 return AVAHI_SERVICE_COOKIE_INVALID;
492 if (avahi_string_list_get_pair(f, NULL, &value, NULL) < 0 || !value)
493 return AVAHI_SERVICE_COOKIE_INVALID;
495 ret = (uint32_t) strtoll(value, &end, 0);
497 if (*value && end && *end != 0) {
499 return AVAHI_SERVICE_COOKIE_INVALID;