/work/open62541_15/src_generated/mdnsd/sdtxt.c
Line | Count | Source |
1 | | #include "open62541/config.h" |
2 | | /* Service discovery TXT record parsing/generation |
3 | | * |
4 | | * Copyright (c) 2003 Jeremie Miller <jer@jabber.org> |
5 | | * Copyright (c) 2016-2022 Joachim Wiberg <troglobit@gmail.com> |
6 | | * All rights reserved. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions are met: |
10 | | * * Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * * Redistributions in binary form must reproduce the above copyright |
13 | | * notice, this list of conditions and the following disclaimer in the |
14 | | * documentation and/or other materials provided with the distribution. |
15 | | * * Neither the name of the copyright holders nor the names of its |
16 | | * contributors may be used to endorse or promote products derived from |
17 | | * this software without specific prior written permission. |
18 | | * |
19 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
23 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | | * POSSIBILITY OF SUCH DAMAGE. |
30 | | */ |
31 | | |
32 | | #include "sdtxt.h" |
33 | | #include <stdlib.h> |
34 | | #include <string.h> |
35 | | |
36 | | static size_t _sd2txt_len(const char *key, char *val) |
37 | 21.5k | { |
38 | 21.5k | size_t ret = strlen(key); |
39 | | |
40 | 21.5k | if (!*val) |
41 | 7.19k | return ret; |
42 | | |
43 | 14.3k | ret += strlen(val); |
44 | 14.3k | ret++; |
45 | | |
46 | 14.3k | return ret; |
47 | 21.5k | } |
48 | | |
49 | | static void _sd2txt_count(xht_t *h __attribute__((unused)), const char *key, void *val, void *arg) |
50 | 10.7k | { |
51 | 10.7k | int *const count = arg; |
52 | | |
53 | 10.7k | *count += (int)_sd2txt_len(key, val) + 1; |
54 | 10.7k | } |
55 | | |
56 | | static void _sd2txt_write(xht_t *h __attribute__((unused)), const char *key, void *val, void *arg) |
57 | 10.7k | { |
58 | 10.7k | unsigned char **txtp = arg; |
59 | 10.7k | char *const cval = val; |
60 | | |
61 | | /* Copy in lengths, then strings */ |
62 | 10.7k | **txtp = _sd2txt_len(key, val); |
63 | 10.7k | (*txtp)++; |
64 | 10.7k | memcpy(*txtp, key, strlen(key)); |
65 | 10.7k | *txtp += strlen(key); |
66 | 10.7k | if (!*cval) |
67 | 3.59k | return; |
68 | | |
69 | 7.17k | **txtp = '='; |
70 | 7.17k | (*txtp)++; |
71 | 7.17k | memcpy(*txtp, cval, strlen(cval)); |
72 | 7.17k | *txtp += strlen(cval); |
73 | 7.17k | } |
74 | | |
75 | | unsigned char *sd2txt(xht_t *h, int *len) |
76 | 1.23k | { |
77 | 1.23k | unsigned char *buf, *raw; |
78 | | |
79 | 1.23k | *len = 0; |
80 | | |
81 | 1.23k | xht_walk(h, _sd2txt_count, len); |
82 | 1.23k | if (!*len) { |
83 | 93 | *len = 1; |
84 | 93 | return (unsigned char *)strdup(""); |
85 | 93 | } |
86 | | |
87 | 1.13k | raw = buf = malloc(*len); |
88 | 1.13k | if (!buf) |
89 | 0 | return NULL; |
90 | | |
91 | 1.13k | xht_walk(h, _sd2txt_write, &buf); |
92 | | |
93 | 1.13k | return raw; |
94 | 1.13k | } |
95 | | |
96 | | xht_t *txt2sd(unsigned char *txt, int len) |
97 | 696 | { |
98 | 696 | xht_t *h = 0; |
99 | | |
100 | 696 | if (txt == 0 || len == 0 || *txt == 0) |
101 | 2 | return 0; |
102 | | |
103 | 694 | h = xht_new(23); |
104 | | |
105 | | /* Loop through data breaking out each block, storing into hashtable */ |
106 | 50.6k | for (; *txt <= len && len > 0; len -= *txt, txt += *txt + 1) { |
107 | 50.2k | char key[256], *val; |
108 | | |
109 | 50.2k | if (*txt == 0) |
110 | 345 | break; |
111 | | |
112 | 49.9k | memcpy(key, txt + 1, *txt); |
113 | 49.9k | key[*txt] = 0; |
114 | | |
115 | 49.9k | val = strchr(key, '='); |
116 | 49.9k | if (val) { |
117 | 30.4k | *val++ = 0; |
118 | 30.4k | xht_store(h, key, (int)strlen(key), val, (int)strlen(val)); |
119 | 30.4k | } |
120 | 49.9k | } |
121 | | |
122 | 694 | return h; |
123 | 696 | } |