summaryrefslogtreecommitdiffstats
path: root/avahi-common/strlst.c
blob: c6ceb0f7351670c6c2dc087353fa854dac9009c9 (plain)
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
/* $Id$ */

/***
  This file is part of avahi.
 
  avahi 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.
 
  avahi 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 avahi; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "strlst.h"
#include "malloc.h"
#include "defs.h"

AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) {
    AvahiStringList *n;

    if (!(n = avahi_malloc(sizeof(AvahiStringList) + size)))
        return NULL;
    
    n->next = l;
    n->size = size;

    /* NUL terminate strings, just to make sure */
    n->text[size] = 0;

    return n;
}

AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t*text, size_t size) {
    AvahiStringList *n;

    assert(text);

    if (!(n = avahi_string_list_add_anonymous(l, size)))
        return NULL;

    if (size > 0)
        memcpy(n->text, text, size);

    return n;
}

AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text) {
    assert(text);

    return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text));
}

int avahi_string_list_parse(const void* data, size_t size, AvahiStringList **ret) {
    const uint8_t *c;
    AvahiStringList *r = NULL;
    
    assert(data);
    assert(ret);

    c = data;
    while (size > 0) {
        size_t k;
        
        k = *(c++);
        size--;

        if (k > size)
            goto fail; /* Overflow */

        if (k > 0) { /* Ignore empty strings */
            AvahiStringList *n;

            if (!(n = avahi_string_list_add_arbitrary(r, c, k)))  
                goto fail; /* OOM */

            r = n;
        }
            
        c += k;
        size -= k;
    }

    *ret = r;
    
    return 0;

fail:
    avahi_string_list_free(r);
    return -1;
}

void avahi_string_list_free(AvahiStringList *l) {
    AvahiStringList *n;

    while (l) {
        n = l->next;
        avahi_free(l);
        l = n;
    }
}

AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) {
    AvahiStringList *r = NULL, *n;

    while (l) {
        n = l->next;
        l->next = r;
        r = l;
        l = n;
    }

    return r;
}

char* avahi_string_list_to_string(AvahiStringList *l) {
    AvahiStringList *n;
    size_t s = 0;
    char *t, *e;

    for (n = l; n; n = n->next) {
        if (n != l)
            s ++;

        s += n->size+2;
    }

    if (!(t = e = avahi_new(char, s+1)))
        return NULL;

    l = avahi_string_list_reverse(l);
    
    for (n = l; n; n = n->next) {
        if (n != l)
            *(e++) = ' ';

        *(e++) = '"';
        strncpy(e, (char*) n->text, n->size);
        e[n->size] = 0;
        e = strchr(e, 0);
        *(e++) = '"';

        assert(e);
    }

    l = avahi_string_list_reverse(l);
    
    *e = 0;

    return t;
}

size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) {
    size_t used = 0;

    if (data) {
        AvahiStringList *n;
        uint8_t *c;

        l = avahi_string_list_reverse(l);
        c = data;
        
        for (n = l; size > 1 && n; n = n->next) {
            size_t k;

            if ((k = n->size) == 0)
                /* Skip empty strings */
                continue;
            
            if (k > 255)
                /* Truncate strings at 255 characters */
                k = 255;
            
            if (k > size-1)
                /* Make sure this string fits in */
                k = size-1;

            *(c++) = (uint8_t) k;
            memcpy(c, n->text, k);
            c += k;
            
            used += 1 + k;
            size -= 1 + k;
        }
        
        l = avahi_string_list_reverse(l);

        if (used == 0 && size > 0) {
        
            /* Empty lists are treated specially. To comply with
             * section 6.1 of the DNS-SD spec, we return a single
             * empty string (i.e. a NUL byte)*/

            *(uint8_t*) data = 0;
            used = 1;
        }
            
    } else {
        AvahiStringList *n;

        for (n = l; n; n = n->next) {
            size_t k;
            
            if ((k = n->size) == 0)
                continue;
            
            if (k > 255)
                k = 255;
            
            used += 1+k;
        }

        if (used == 0)
            used = 1;
    }

    return used;
}

int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {

    for (;;) {
        if (!a && !b)
            return 1;

        if (!a || !b)
            return 0;

        if (a->size != b->size)
            return 0;

        if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0)
            return 0;

        a = a->next;
        b = b->next;
    }
}

AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) {
    va_list va;

    va_start(va, r);
    r = avahi_string_list_add_many_va(r, va);
    va_end(va);
    
    return r;
}

AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) {
    const char *txt;

    while ((txt = va_arg(va, const char*)))
        r = avahi_string_list_add(r, txt);

    return r;
}

AvahiStringList *avahi_string_list_new(const char *txt, ...) {
    va_list va;
    AvahiStringList *r = NULL;

    if (txt) {
        r = avahi_string_list_add(r, txt);

        va_start(va, txt);
        r = avahi_string_list_add_many_va(r, va);
        va_end(va);
    }

    return r;
}

AvahiStringList *avahi_string_list_new_va(va_list va) {
    return avahi_string_list_add_many_va(NULL, va);
}

AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) {
    AvahiStringList *r = NULL;

    for (; l; l = l->next)
        if (!(r = avahi_string_list_add_arbitrary(r, l->text, l->size))) {
            avahi_string_list_free(r);
            return NULL;
        }

    return avahi_string_list_reverse(r);
}

AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) {
    AvahiStringList *r = NULL;
    int i;

    assert(array);

    for (i = 0; length >= 0 ? i < length : !!array[i]; i++)
        r = avahi_string_list_add(r, array[i]);

    return r;
}

unsigned avahi_string_list_length(const AvahiStringList *l) {
    unsigned n = 0;

    for (; l; l = l->next)
        n++;

    return n;
}

AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va) {
    size_t len = 80;
    AvahiStringList *r;
    
    assert(format);

    if (!(r = avahi_malloc(sizeof(AvahiStringList) + len)))
        return NULL;

    for (;;) {
        int n;
        AvahiStringList *nr;
        
        n = vsnprintf((char*) r->text, len+1, format, va);

        if (n >= 0 && n < (int) len)
            break;

        if (n >= 0)
            len = n+1;
        else
            len *= 2;

        if (!(nr = avahi_realloc(r, sizeof(AvahiStringList) + len))) {
            avahi_free(r);
            return NULL;
        }

        r = nr;
    }

    
    r->next = l;
    r->size = strlen((char*) r->text); 

    return r;
}

AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) {
    va_list va;
    
    assert(format);

    va_start(va, format);
    l  = avahi_string_list_add_vprintf(l, format, va);
    va_end(va);

    return l;    
}

AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key) {
    size_t n;
    
    assert(key);
    n = strlen(key);

    for (; l; l = l->next) {
        if (strcasecmp((char*) l->text, key) == 0)
            return l;

        if (strncasecmp((char*) l->text, key, n) == 0 && l->text[n] == '=')
            return l;
    }

    return NULL;
}

AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value) {
    assert(key);

    if (value)
        return avahi_string_list_add_printf(l, "%s=%s", key, value);
    else
        return avahi_string_list_add(l, key);
}

AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size) {
    size_t n;
    assert(key);

    if (!value)
        return avahi_string_list_add(l, key);

    n = strlen(key);
    
    if (!(l = avahi_string_list_add_anonymous(l, n + 1 + size)))
        return NULL;

    memcpy(l->text, key, n);
    l->text[n] = '=';
    memcpy(l->text + n + 1, value, size);

    return l;
}

int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size) {
    char *e;
    
    assert(l);

    if (!(e = memchr(l->text, '=', l->size))) {

        if (key) 
            if (!(*key = avahi_strdup((char*) l->text)))
                return -1;

        if (value)
            *value = NULL;

        if (size)
            *size = 0;

    } else {
        size_t n;

        if (key)
            if (!(*key = avahi_strndup((char*) l->text, e - (char *) l->text)))
                return -1;

        e++; /* Advance after '=' */
        
        n = l->size - (e - (char*) l->text);
        
        if (value) {

            if (!(*value = avahi_memdup(e, n+1))) {
                if (key)
                    avahi_free(*key);
                return -1;
            }

            (*value)[n] = 0;
        }

        if (size)
            *size = n;
    }

    return 0;
}

AvahiStringList *avahi_string_list_get_next(AvahiStringList *l) {
    assert(l);
    return l->next;
}

uint8_t *avahi_string_list_get_text(AvahiStringList *l) {
    assert(l);
    return l->text;
}

size_t avahi_string_list_get_size(AvahiStringList *l) {
    assert(l);
    return l->size;
}

uint32_t avahi_string_list_get_service_cookie(AvahiStringList *l) {
    AvahiStringList *f;
    char *value = NULL, *end = NULL;
    uint32_t ret;
    
    if (!(f = avahi_string_list_find(l, AVAHI_SERVICE_COOKIE)))
        return AVAHI_SERVICE_COOKIE_INVALID;

    if (avahi_string_list_get_pair(f, NULL, &value, NULL) < 0 || !value)
        return AVAHI_SERVICE_COOKIE_INVALID;

    ret = (uint32_t) strtoll(value, &end, 0);

    if (*value && end && *end != 0) {
        avahi_free(value);
        return AVAHI_SERVICE_COOKIE_INVALID;
    }

    avahi_free(value);
    
    return ret;
}