BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file NCDValGenerator.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <stdlib.h>
31 #include <string.h>
32 #include <inttypes.h>
33  
34 #include <misc/debug.h>
35 #include <misc/expstring.h>
36 #include <base/BLog.h>
37  
38 #include "NCDValGenerator.h"
39  
40 #include <generated/blog_channel_NCDValGenerator.h>
41  
42 static int generate_val (NCDValRef value, ExpString *out_str)
43 {
44 ASSERT(!NCDVal_IsInvalid(value))
45  
46 switch (NCDVal_Type(value)) {
47 case NCDVAL_STRING: {
48 if (!ExpString_AppendChar(out_str, '"')) {
49 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
50 goto fail;
51 }
52  
53 MEMREF_LOOP_CHARS(NCDVal_StringMemRef(value), char_pos, ch, {
54 if (ch == '\0') {
55 char buf[5];
56 snprintf(buf, sizeof(buf), "\\x%02"PRIx8, (uint8_t)ch);
57  
58 if (!ExpString_Append(out_str, buf)) {
59 BLog(BLOG_ERROR, "ExpString_Append failed");
60 goto fail;
61 }
62  
63 goto next_char;
64 }
65  
66 if (ch == '"' || ch == '\\') {
67 if (!ExpString_AppendChar(out_str, '\\')) {
68 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
69 goto fail;
70 }
71 }
72  
73 if (!ExpString_AppendChar(out_str, ch)) {
74 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
75 goto fail;
76 }
77 next_char:;
78 })
79  
80 if (!ExpString_AppendChar(out_str, '"')) {
81 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
82 goto fail;
83 }
84 } break;
85  
86 case NCDVAL_LIST: {
87 size_t count = NCDVal_ListCount(value);
88  
89 if (!ExpString_AppendChar(out_str, '{')) {
90 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
91 goto fail;
92 }
93  
94 for (size_t i = 0; i < count; i++) {
95 if (i > 0) {
96 if (!ExpString_Append(out_str, ", ")) {
97 BLog(BLOG_ERROR, "ExpString_Append failed");
98 goto fail;
99 }
100 }
101  
102 if (!generate_val(NCDVal_ListGet(value, i), out_str)) {
103 goto fail;
104 }
105 }
106  
107 if (!ExpString_AppendChar(out_str, '}')) {
108 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
109 goto fail;
110 }
111 } break;
112  
113 case NCDVAL_MAP: {
114 if (!ExpString_AppendChar(out_str, '[')) {
115 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
116 goto fail;
117 }
118  
119 int is_first = 1;
120  
121 for (NCDValMapElem e = NCDVal_MapOrderedFirst(value); !NCDVal_MapElemInvalid(e); e = NCDVal_MapOrderedNext(value, e)) {
122 NCDValRef ekey = NCDVal_MapElemKey(value, e);
123 NCDValRef eval = NCDVal_MapElemVal(value, e);
124  
125 if (!is_first) {
126 if (!ExpString_Append(out_str, ", ")) {
127 BLog(BLOG_ERROR, "ExpString_Append failed");
128 goto fail;
129 }
130 }
131  
132 if (!generate_val(ekey, out_str)) {
133 goto fail;
134 }
135  
136 if (!ExpString_AppendChar(out_str, ':')) {
137 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
138 goto fail;
139 }
140  
141 if (!generate_val(eval, out_str)) {
142 goto fail;
143 }
144  
145 is_first = 0;
146 }
147  
148 if (!ExpString_AppendChar(out_str, ']')) {
149 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
150 goto fail;
151 }
152 } break;
153  
154 default: ASSERT(0);
155 }
156  
157 return 1;
158  
159 fail:
160 return 0;
161 }
162  
163 char * NCDValGenerator_Generate (NCDValRef value)
164 {
165 ASSERT(!NCDVal_IsInvalid(value))
166  
167 ExpString str;
168 if (!ExpString_Init(&str)) {
169 BLog(BLOG_ERROR, "ExpString_Init failed");
170 goto fail0;
171 }
172  
173 if (!generate_val(value, &str)) {
174 goto fail1;
175 }
176  
177 return ExpString_Get(&str);
178  
179 fail1:
180 ExpString_Free(&str);
181 fail0:
182 return NULL;
183 }
184  
185 int NCDValGenerator_AppendGenerate (NCDValRef value, ExpString *str)
186 {
187 ASSERT(!NCDVal_IsInvalid(value))
188 ASSERT(str)
189  
190 return generate_val(value, str);
191 }