Coverage Report

Created: 2025-10-13 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541/deps/mdnsd/libmdnsd/sdtxt.c
Line
Count
Source
1
#include "sdtxt.h"
2
#include <stdlib.h>
3
#include <string.h>
4
5
static size_t _sd2txt_len(const char *key, char *val)
6
20.9k
{
7
20.9k
  size_t ret = strlen(key);
8
9
20.9k
  if (!*val)
10
5.18k
    return ret;
11
12
15.7k
  ret += strlen(val);
13
15.7k
  ret++;
14
15
15.7k
  return ret;
16
20.9k
}
17
18
static void _sd2txt_count(xht_t *h, char *key, void *val, void *arg)
19
10.4k
{
20
10.4k
  int *count = (int *)arg;
21
22
10.4k
  *count += (int)_sd2txt_len(key, (char *)val) + 1;
23
10.4k
}
24
25
static void _sd2txt_write(xht_t *h, char *key, void *val, void *arg)
26
10.4k
{
27
10.4k
  unsigned char **txtp = (unsigned char **)arg;
28
10.4k
  char *cval = (char *)val;
29
30
  /* Copy in lengths, then strings */
31
10.4k
  **txtp = (unsigned char)_sd2txt_len(key, (char *)val);
32
10.4k
  (*txtp)++;
33
10.4k
  memcpy(*txtp, key, strlen(key));
34
10.4k
  *txtp += strlen(key);
35
10.4k
  if (!*cval)
36
2.59k
    return;
37
38
7.86k
  **txtp = '=';
39
7.86k
  (*txtp)++;
40
7.86k
  memcpy(*txtp, cval, strlen(cval));
41
7.86k
  *txtp += strlen(cval);
42
7.86k
}
43
44
unsigned char *sd2txt(xht_t *h, int *len)
45
651
{
46
651
  unsigned char *buf, *raw;
47
48
651
  *len = 0;
49
50
651
  xht_walk(h, _sd2txt_count, (void *)len);
51
651
  if (!*len) {
52
3
    *len = 1;
53
3
    buf = (unsigned char *)MDNSD_malloc(1);
54
3
    *buf = 0;
55
3
    return buf;
56
3
  }
57
58
648
  raw = buf = (unsigned char *)MDNSD_malloc((size_t)(*len));
59
648
  xht_walk(h, _sd2txt_write, &buf);
60
61
648
  return raw;
62
651
}
63
64
xht_t *txt2sd(const unsigned char *txt, int len)
65
371
{
66
371
  char key[256];
67
371
  xht_t *h = NULL;
68
69
371
  if (txt == 0 || len == 0 || *txt == 0)
70
3
    return NULL;
71
72
368
  h = xht_new(23);
73
74
  /* Loop through data breaking out each block, storing into hashtable */
75
46.9k
  for (; len > 0 && *txt <= len; len -= *txt, txt += *txt + 1) {
76
46.7k
    char* val;
77
46.7k
    if (*txt == 0)
78
19
      break;
79
80
46.7k
    memcpy(key, txt + 1, *txt);
81
46.7k
    key[*txt] = 0;
82
46.7k
    if ((val = strchr(key, '=')) != 0) {
83
15.2k
      *val = 0;
84
15.2k
      val++;
85
15.2k
    }
86
46.7k
    if (val != NULL)
87
15.2k
      xht_store(h, key, (int)strlen(key), val, (int)strlen(val));
88
46.7k
    if (*txt +1 > len)
89
216
        break;
90
46.7k
  }
91
92
368
  return h;
93
371
}