Coverage Report

Created: 2023-06-07 06:18

/src/avahi/avahi-common/strlst.c
Line
Count
Source (jump to first uncovered line)
1
/***
2
  This file is part of avahi.
3
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.
8
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.
13
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
17
  USA.
18
***/
19
20
#ifdef HAVE_CONFIG_H
21
#include <config.h>
22
#endif
23
24
#include <string.h>
25
#include <stdarg.h>
26
#include <assert.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
30
#include "strlst.h"
31
#include "malloc.h"
32
#include "defs.h"
33
34
0
AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) {
35
0
    AvahiStringList *n;
36
37
0
    if (!(n = avahi_malloc(sizeof(AvahiStringList) + size)))
38
0
        return NULL;
39
40
0
    n->next = l;
41
0
    n->size = size;
42
43
    /* NUL terminate strings, just to make sure */
44
0
    n->text[size] = 0;
45
46
0
    return n;
47
0
}
48
49
0
AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t*text, size_t size) {
50
0
    AvahiStringList *n;
51
52
0
    assert(size == 0 || text);
53
54
0
    if (!(n = avahi_string_list_add_anonymous(l, size)))
55
0
        return NULL;
56
57
0
    if (size > 0)
58
0
        memcpy(n->text, text, size);
59
60
0
    return n;
61
0
}
62
63
0
AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text) {
64
0
    assert(text);
65
66
0
    return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text));
67
0
}
68
69
0
int avahi_string_list_parse(const void* data, size_t size, AvahiStringList **ret) {
70
0
    const uint8_t *c;
71
0
    AvahiStringList *r = NULL;
72
73
0
    assert(data);
74
0
    assert(ret);
75
76
0
    c = data;
77
0
    while (size > 0) {
78
0
        size_t k;
79
80
0
        k = *(c++);
81
0
        size--;
82
83
0
        if (k > size)
84
0
            goto fail; /* Overflow */
85
86
0
        if (k > 0) { /* Ignore empty strings */
87
0
            AvahiStringList *n;
88
89
0
            if (!(n = avahi_string_list_add_arbitrary(r, c, k)))
90
0
                goto fail; /* OOM */
91
92
0
            r = n;
93
0
        }
94
95
0
        c += k;
96
0
        size -= k;
97
0
    }
98
99
0
    *ret = r;
100
101
0
    return 0;
102
103
0
fail:
104
0
    avahi_string_list_free(r);
105
0
    return -1;
106
0
}
107
108
0
void avahi_string_list_free(AvahiStringList *l) {
109
0
    AvahiStringList *n;
110
111
0
    while (l) {
112
0
        n = l->next;
113
0
        avahi_free(l);
114
0
        l = n;
115
0
    }
116
0
}
117
118
0
AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) {
119
0
    AvahiStringList *r = NULL, *n;
120
121
0
    while (l) {
122
0
        n = l->next;
123
0
        l->next = r;
124
0
        r = l;
125
0
        l = n;
126
0
    }
127
128
0
    return r;
129
0
}
130
131
/**
132
 * This routine is used for both human- and machine-readable output of
133
 * TXT records. As such it must cope with escaping, in order to allow
134
 * machines to reconstruct the original data.
135
 *
136
 * AFAIK no RFC specifies syntax for TXT data other than raw binary,
137
 * though presumably zonefile syntax would make sense:
138
 *
139
 *   - RFC 1035 says that TXT records contain `<character-string>`s, and section
140
 *     5 says:
141
 *
142
 *       <character-string> is expressed in one or two ways: as a contiguous set
143
 *       of characters without interior spaces, or as a string beginning with a "
144
 *       and ending with a ".  Inside a " delimited string any character can
145
 *       occur, except for a " itself, which must be quoted using \ (back slash).
146
 *
147
 *     This omits escaping of backslashes (!).
148
 *
149
 *   - RFC 1034 doesn't say anything relevant.
150
 *
151
 *   - RFC 1464 suggests a specific encoding of information within a TXT
152
 *     record but does not discuss formatting of TXT records in
153
 *     general.
154
 *
155
 * In order to also escape newlines, which interfere with line-by-line
156
 * machine processing of records, this routine:
157
 *
158
 *   - escapes >>> " <<< to >>> \" <<<
159
 *   - escapes >>> \ <<< to >>> \\ <<<
160
 *   - escapes bytes less than 32 to backslash-prefixed 3-digit DECIMAL form
161
 */
162
0
char* avahi_string_list_to_string(AvahiStringList *l) {
163
0
    AvahiStringList *n;
164
0
    size_t s = 0;
165
0
    char *p, *t, *e;
166
167
0
    for (n = l; n; n = n->next) {
168
0
        if (n != l)
169
0
            s ++; /* for the inter-string separating space */
170
171
0
        for (p = (char*) n->text; ((size_t) (p - (char*) n->text) < n->size); p++) {
172
0
            switch (*p) {
173
0
              case '"':
174
0
              case '\\':
175
0
                  s += 2;
176
0
                  break;
177
0
              default:
178
0
                  if (*p < 32) {
179
0
                      s += 4;
180
0
                  } else {
181
0
                      s ++;
182
0
                      break;
183
0
                  }
184
0
            }
185
0
        }
186
0
        s += 2; /* for the leading and trailing double-quotes */
187
0
    }
188
189
0
    if (!(t = e = avahi_new(char, s+1))) /* plus one for the trailing NUL */
190
0
        return NULL;
191
192
0
    l = avahi_string_list_reverse(l);
193
194
0
    for (n = l; n; n = n->next) {
195
0
        if (n != l)
196
0
            *(e++) = ' ';
197
198
0
        *(e++) = '"';
199
0
        for (p = (char*) n->text; ((size_t) (p - (char*) n->text) < n->size); p++) {
200
0
            switch (*p) {
201
0
              case '"':
202
0
              case '\\':
203
0
                  *(e++) = '\\';
204
                  /* FALL THROUGH */
205
0
              default:
206
0
                  if (*p < 32) {
207
0
                      *(e++) = '\\';
208
0
                      *(e++) = '0' + (char)  ((uint8_t) *p / 100);
209
0
                      *(e++) = '0' + (char) (((uint8_t) *p / 10) % 10);
210
0
                      *(e++) = '0' + (char)  ((uint8_t) *p % 10);
211
0
                  } else {
212
0
                      *(e++) = *p;
213
0
                  }
214
0
            }
215
0
        }
216
0
        *(e++) = '"';
217
218
0
        assert(e);
219
0
    }
220
221
0
    l = avahi_string_list_reverse(l);
222
223
0
    *e = 0;
224
225
0
    return t;
226
0
}
227
228
0
size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) {
229
0
    size_t used = 0;
230
231
0
    if (data) {
232
0
        AvahiStringList *n;
233
0
        uint8_t *c;
234
235
0
        l = avahi_string_list_reverse(l);
236
0
        c = data;
237
238
0
        for (n = l; size > 1 && n; n = n->next) {
239
0
            size_t k;
240
241
0
            if ((k = n->size) == 0)
242
                /* Skip empty strings */
243
0
                continue;
244
245
0
            if (k > 255)
246
                /* Truncate strings at 255 characters */
247
0
                k = 255;
248
249
0
            if (k > size-1)
250
                /* Make sure this string fits in */
251
0
                k = size-1;
252
253
0
            *(c++) = (uint8_t) k;
254
0
            memcpy(c, n->text, k);
255
0
            c += k;
256
257
0
            used += 1 + k;
258
0
            size -= 1 + k;
259
0
        }
260
261
0
        l = avahi_string_list_reverse(l);
262
263
0
        if (used == 0 && size > 0) {
264
265
            /* Empty lists are treated specially. To comply with
266
             * section 6.1 of the DNS-SD spec, we return a single
267
             * empty string (i.e. a NUL byte)*/
268
269
0
            *(uint8_t*) data = 0;
270
0
            used = 1;
271
0
        }
272
273
0
    } else {
274
0
        AvahiStringList *n;
275
276
0
        for (n = l; n; n = n->next) {
277
0
            size_t k;
278
279
0
            if ((k = n->size) == 0)
280
0
                continue;
281
282
0
            if (k > 255)
283
0
                k = 255;
284
285
0
            used += 1+k;
286
0
        }
287
288
0
        if (used == 0)
289
0
            used = 1;
290
0
    }
291
292
0
    return used;
293
0
}
294
295
0
int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {
296
297
0
    for (;;) {
298
0
        if (!a && !b)
299
0
            return 1;
300
301
0
        if (!a || !b)
302
0
            return 0;
303
304
0
        if (a->size != b->size)
305
0
            return 0;
306
307
0
        if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0)
308
0
            return 0;
309
310
0
        a = a->next;
311
0
        b = b->next;
312
0
    }
313
0
}
314
315
0
AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) {
316
0
    va_list va;
317
318
0
    va_start(va, r);
319
0
    r = avahi_string_list_add_many_va(r, va);
320
0
    va_end(va);
321
322
0
    return r;
323
0
}
324
325
0
AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) {
326
0
    const char *txt;
327
328
0
    while ((txt = va_arg(va, const char*)))
329
0
        r = avahi_string_list_add(r, txt);
330
331
0
    return r;
332
0
}
333
334
0
AvahiStringList *avahi_string_list_new(const char *txt, ...) {
335
0
    va_list va;
336
0
    AvahiStringList *r = NULL;
337
338
0
    if (txt) {
339
0
        r = avahi_string_list_add(r, txt);
340
341
0
        va_start(va, txt);
342
0
        r = avahi_string_list_add_many_va(r, va);
343
0
        va_end(va);
344
0
    }
345
346
0
    return r;
347
0
}
348
349
0
AvahiStringList *avahi_string_list_new_va(va_list va) {
350
0
    return avahi_string_list_add_many_va(NULL, va);
351
0
}
352
353
0
AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) {
354
0
    AvahiStringList *r = NULL;
355
356
0
    for (; l; l = l->next)
357
0
        if (!(r = avahi_string_list_add_arbitrary(r, l->text, l->size))) {
358
0
            avahi_string_list_free(r);
359
0
            return NULL;
360
0
        }
361
362
0
    return avahi_string_list_reverse(r);
363
0
}
364
365
0
AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) {
366
0
    AvahiStringList *r = NULL;
367
0
    int i;
368
369
0
    assert(array);
370
371
0
    for (i = 0; length >= 0 ? i < length : !!array[i]; i++)
372
0
        r = avahi_string_list_add(r, array[i]);
373
374
0
    return r;
375
0
}
376
377
0
unsigned avahi_string_list_length(const AvahiStringList *l) {
378
0
    unsigned n = 0;
379
380
0
    for (; l; l = l->next)
381
0
        n++;
382
383
0
    return n;
384
0
}
385
386
0
AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va) {
387
0
    size_t len = 80;
388
0
    AvahiStringList *r;
389
390
0
    assert(format);
391
392
0
    if (!(r = avahi_malloc(sizeof(AvahiStringList) + len)))
393
0
        return NULL;
394
395
0
    for (;;) {
396
0
        int n;
397
0
        AvahiStringList *nr;
398
0
        va_list va2;
399
400
0
        va_copy(va2, va);
401
0
        n = vsnprintf((char*) r->text, len, format, va2);
402
0
        va_end(va2);
403
404
0
        if (n >= 0 && n < (int) len)
405
0
            break;
406
407
0
        if (n >= 0)
408
0
            len = n+1;
409
0
        else
410
0
            len *= 2;
411
412
0
        if (!(nr = avahi_realloc(r, sizeof(AvahiStringList) + len))) {
413
0
            avahi_free(r);
414
0
            return NULL;
415
0
        }
416
417
0
        r = nr;
418
0
    }
419
420
0
    r->next = l;
421
0
    r->size = strlen((char*) r->text);
422
423
0
    return r;
424
0
}
425
426
0
AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) {
427
0
    va_list va;
428
429
0
    assert(format);
430
431
0
    va_start(va, format);
432
0
    l  = avahi_string_list_add_vprintf(l, format, va);
433
0
    va_end(va);
434
435
0
    return l;
436
0
}
437
438
0
AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key) {
439
0
    size_t n;
440
441
0
    assert(key);
442
0
    n = strlen(key);
443
444
0
    for (; l; l = l->next) {
445
0
        if (strcasecmp((char*) l->text, key) == 0)
446
0
            return l;
447
448
0
        if (strncasecmp((char*) l->text, key, n) == 0 && l->text[n] == '=')
449
0
            return l;
450
0
    }
451
452
0
    return NULL;
453
0
}
454
455
0
AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value) {
456
0
    assert(key);
457
458
0
    if (value)
459
0
        return avahi_string_list_add_printf(l, "%s=%s", key, value);
460
0
    else
461
0
        return avahi_string_list_add(l, key);
462
0
}
463
464
0
AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size) {
465
0
    size_t n;
466
0
    assert(key);
467
468
0
    if (!value)
469
0
        return avahi_string_list_add(l, key);
470
471
0
    n = strlen(key);
472
473
0
    if (!(l = avahi_string_list_add_anonymous(l, n + 1 + size)))
474
0
        return NULL;
475
476
0
    memcpy(l->text, key, n);
477
0
    l->text[n] = '=';
478
0
    memcpy(l->text + n + 1, value, size);
479
480
0
    return l;
481
0
}
482
483
0
int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size) {
484
0
    char *e;
485
486
0
    assert(l);
487
488
0
    if (!(e = memchr(l->text, '=', l->size))) {
489
490
0
        if (key)
491
0
            if (!(*key = avahi_strdup((char*) l->text)))
492
0
                return -1;
493
494
0
        if (value)
495
0
            *value = NULL;
496
497
0
        if (size)
498
0
            *size = 0;
499
500
0
    } else {
501
0
        size_t n;
502
503
0
        if (key)
504
0
            if (!(*key = avahi_strndup((char*) l->text, e - (char *) l->text)))
505
0
                return -1;
506
507
0
        e++; /* Advance after '=' */
508
509
0
        n = l->size - (e - (char*) l->text);
510
511
0
        if (value) {
512
513
0
            if (!(*value = avahi_memdup(e, n+1))) {
514
0
                if (key)
515
0
                    avahi_free(*key);
516
0
                return -1;
517
0
            }
518
519
0
            (*value)[n] = 0;
520
0
        }
521
522
0
        if (size)
523
0
            *size = n;
524
0
    }
525
526
0
    return 0;
527
0
}
528
529
0
AvahiStringList *avahi_string_list_get_next(AvahiStringList *l) {
530
0
    assert(l);
531
0
    return l->next;
532
0
}
533
534
0
uint8_t *avahi_string_list_get_text(AvahiStringList *l) {
535
0
    assert(l);
536
0
    return l->text;
537
0
}
538
539
0
size_t avahi_string_list_get_size(AvahiStringList *l) {
540
0
    assert(l);
541
0
    return l->size;
542
0
}
543
544
0
uint32_t avahi_string_list_get_service_cookie(AvahiStringList *l) {
545
0
    AvahiStringList *f;
546
0
    char *value = NULL, *end = NULL;
547
0
    uint32_t ret;
548
549
0
    if (!(f = avahi_string_list_find(l, AVAHI_SERVICE_COOKIE)))
550
0
        return AVAHI_SERVICE_COOKIE_INVALID;
551
552
0
    if (avahi_string_list_get_pair(f, NULL, &value, NULL) < 0 || !value)
553
0
        return AVAHI_SERVICE_COOKIE_INVALID;
554
555
0
    ret = (uint32_t) strtoll(value, &end, 0);
556
557
0
    if (*value && end && *end != 0) {
558
0
        avahi_free(value);
559
0
        return AVAHI_SERVICE_COOKIE_INVALID;
560
0
    }
561
562
0
    avahi_free(value);
563
564
0
    return ret;
565
0
}