2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) {
37 if (!(n = avahi_malloc(sizeof(AvahiStringList) + size)))
43 /* NUL terminate strings, just to make sure */
49 AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t*text, size_t size) {
52 assert(size == 0 || text);
54 if (!(n = avahi_string_list_add_anonymous(l, size)))
58 memcpy(n->text, text, size);
63 AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text) {
66 return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text));
69 int avahi_string_list_parse(const void* data, size_t size, AvahiStringList **ret) {
71 AvahiStringList *r = NULL;
84 goto fail; /* Overflow */
86 if (k > 0) { /* Ignore empty strings */
89 if (!(n = avahi_string_list_add_arbitrary(r, c, k)))
104 avahi_string_list_free(r);
108 void avahi_string_list_free(AvahiStringList *l) {
118 AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) {
119 AvahiStringList *r = NULL, *n;
131 char* avahi_string_list_to_string(AvahiStringList *l) {
136 for (n = l; n; n = n->next) {
143 if (!(t = e = avahi_new(char, s+1)))
146 l = avahi_string_list_reverse(l);
148 for (n = l; n; n = n->next) {
153 strncpy(e, (char*) n->text, n->size);
161 l = avahi_string_list_reverse(l);
168 size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) {
175 l = avahi_string_list_reverse(l);
178 for (n = l; size > 1 && n; n = n->next) {
181 if ((k = n->size) == 0)
182 /* Skip empty strings */
186 /* Truncate strings at 255 characters */
190 /* Make sure this string fits in */
193 *(c++) = (uint8_t) k;
194 memcpy(c, n->text, k);
201 l = avahi_string_list_reverse(l);
203 if (used == 0 && size > 0) {
205 /* Empty lists are treated specially. To comply with
206 * section 6.1 of the DNS-SD spec, we return a single
207 * empty string (i.e. a NUL byte)*/
209 *(uint8_t*) data = 0;
216 for (n = l; n; n = n->next) {
219 if ((k = n->size) == 0)
235 int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {
244 if (a->size != b->size)
247 if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0)
255 AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) {
259 r = avahi_string_list_add_many_va(r, va);
265 AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) {
268 while ((txt = va_arg(va, const char*)))
269 r = avahi_string_list_add(r, txt);
274 AvahiStringList *avahi_string_list_new(const char *txt, ...) {
276 AvahiStringList *r = NULL;
279 r = avahi_string_list_add(r, txt);
282 r = avahi_string_list_add_many_va(r, va);
289 AvahiStringList *avahi_string_list_new_va(va_list va) {
290 return avahi_string_list_add_many_va(NULL, va);
293 AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) {
294 AvahiStringList *r = NULL;
296 for (; l; l = l->next)
297 if (!(r = avahi_string_list_add_arbitrary(r, l->text, l->size))) {
298 avahi_string_list_free(r);
302 return avahi_string_list_reverse(r);
305 AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) {
306 AvahiStringList *r = NULL;
311 for (i = 0; length >= 0 ? i < length : !!array[i]; i++)
312 r = avahi_string_list_add(r, array[i]);
317 unsigned avahi_string_list_length(const AvahiStringList *l) {
320 for (; l; l = l->next)
326 AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va) {
332 if (!(r = avahi_malloc(sizeof(AvahiStringList) + len)))
341 n = vsnprintf((char*) r->text, len, format, va2);
344 if (n >= 0 && n < (int) len)
352 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;