BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file basic_functions.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 | |||
32 | #include <misc/expstring.h> |
||
33 | #include <misc/bsize.h> |
||
34 | #include <misc/ascii_utils.h> |
||
35 | #include <ncd/NCDValGenerator.h> |
||
36 | #include <ncd/NCDValParser.h> |
||
37 | #include <system/BTime.h> |
||
38 | |||
39 | #include <ncd/module_common.h> |
||
40 | |||
41 | #include <generated/blog_channel_ncd_basic_functions.h> |
||
42 | |||
43 | |||
44 | // Trivial functions. |
||
45 | |||
46 | static void error_eval (NCDCall call) |
||
47 | { |
||
48 | FunctionLog(&call, BLOG_ERROR, "error: failing"); |
||
49 | } |
||
50 | |||
51 | static void identity_eval (NCDCall call) |
||
52 | { |
||
53 | if (NCDCall_ArgCount(&call) != 1) { |
||
54 | return FunctionLog(&call, BLOG_ERROR, "identity: need one argument"); |
||
55 | } |
||
56 | NCDCall_SetResult(&call, NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call))); |
||
57 | } |
||
58 | |||
59 | |||
60 | // Logical functions. |
||
61 | |||
62 | static void if_eval (NCDCall call) |
||
63 | { |
||
64 | if (NCDCall_ArgCount(&call) != 3) { |
||
65 | return FunctionLog(&call, BLOG_ERROR, "if: need three arguments"); |
||
66 | } |
||
67 | NCDValRef cond = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); |
||
68 | if (NCDVal_IsInvalid(cond)) { |
||
69 | return; |
||
70 | } |
||
71 | int cond_val; |
||
72 | if (!ncd_read_boolean(cond, &cond_val)) { |
||
73 | return FunctionLog(&call, BLOG_ERROR, "if: bad condition"); |
||
74 | } |
||
75 | int eval_arg = 2 - cond_val; |
||
76 | NCDCall_SetResult(&call, NCDCall_EvalArg(&call, eval_arg, NCDCall_ResMem(&call))); |
||
77 | } |
||
78 | |||
79 | static void ifel_eval (NCDCall call) |
||
80 | { |
||
81 | size_t count = NCDCall_ArgCount(&call); |
||
82 | if (count % 2 == 0) { |
||
83 | return FunctionLog(&call, BLOG_ERROR, "ifel: need an odd number of arguments"); |
||
84 | } |
||
85 | NCDValRef value; |
||
86 | size_t j = 0; |
||
87 | while (1) { |
||
88 | NCDValRef arg = NCDCall_EvalArg(&call, j, NCDCall_ResMem(&call)); |
||
89 | if (NCDVal_IsInvalid(arg)) { |
||
90 | return; |
||
91 | } |
||
92 | if (j == count - 1) { |
||
93 | value = arg; |
||
94 | break; |
||
95 | } |
||
96 | NCDValRef arg2 = NCDCall_EvalArg(&call, j + 1, NCDCall_ResMem(&call)); |
||
97 | if (NCDVal_IsInvalid(arg2)) { |
||
98 | return; |
||
99 | } |
||
100 | int arg_val; |
||
101 | if (!ncd_read_boolean(arg, &arg_val)) { |
||
102 | return FunctionLog(&call, BLOG_ERROR, "ifel: bad condition"); |
||
103 | } |
||
104 | if (arg_val) { |
||
105 | value = arg2; |
||
106 | break; |
||
107 | } |
||
108 | j += 2; |
||
109 | } |
||
110 | NCDCall_SetResult(&call, value); |
||
111 | } |
||
112 | |||
113 | static void bool_not_eval (NCDCall call, int negate, char const *name) |
||
114 | { |
||
115 | if (NCDCall_ArgCount(&call) != 1) { |
||
116 | return FunctionLog(&call, BLOG_ERROR, "%s: need one argument", name); |
||
117 | } |
||
118 | NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); |
||
119 | if (NCDVal_IsInvalid(arg)) { |
||
120 | return; |
||
121 | } |
||
122 | int arg_val; |
||
123 | if (!ncd_read_boolean(arg, &arg_val)) { |
||
124 | return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name); |
||
125 | } |
||
126 | int res = (arg_val != negate); |
||
127 | NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); |
||
128 | } |
||
129 | |||
130 | static void bool_eval (NCDCall call) { return bool_not_eval(call, 0, "bool"); } |
||
131 | static void not_eval (NCDCall call) { return bool_not_eval(call, 1, "not"); } |
||
132 | |||
133 | static void and_or_eval (NCDCall call, int is_and, char const *name) |
||
134 | { |
||
135 | size_t count = NCDCall_ArgCount(&call); |
||
136 | int res = is_and; |
||
137 | for (size_t i = 0; i < count; i++) { |
||
138 | NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); |
||
139 | if (NCDVal_IsInvalid(arg)) { |
||
140 | return; |
||
141 | } |
||
142 | int arg_val; |
||
143 | if (!ncd_read_boolean(arg, &arg_val)) { |
||
144 | return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name); |
||
145 | } |
||
146 | if (arg_val != is_and) { |
||
147 | res = !is_and; |
||
148 | break; |
||
149 | } |
||
150 | } |
||
151 | NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); |
||
152 | } |
||
153 | |||
154 | static void and_eval (NCDCall call) { return and_or_eval(call, 1, "and"); } |
||
155 | static void or_eval (NCDCall call) { return and_or_eval(call, 0, "or"); } |
||
156 | |||
157 | static void imp_eval (NCDCall call) |
||
158 | { |
||
159 | if (NCDCall_ArgCount(&call) != 2) { |
||
160 | return FunctionLog(&call, BLOG_ERROR, "imp: need two arguments"); |
||
161 | } |
||
162 | int res = 0; |
||
163 | for (size_t i = 0; i < 2; i++) { |
||
164 | NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); |
||
165 | if (NCDVal_IsInvalid(arg)) { |
||
166 | return; |
||
167 | } |
||
168 | int arg_val; |
||
169 | if (!ncd_read_boolean(arg, &arg_val)) { |
||
170 | return FunctionLog(&call, BLOG_ERROR, "imp: bad argument"); |
||
171 | } |
||
172 | if (arg_val == i) { |
||
173 | res = 1; |
||
174 | break; |
||
175 | } |
||
176 | } |
||
177 | NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); |
||
178 | } |
||
179 | |||
180 | |||
181 | // Value comparison functions. |
||
182 | |||
183 | typedef int (*value_compare_func) (int cmp); |
||
184 | |||
185 | static void value_compare_eval (NCDCall call, value_compare_func func) |
||
186 | { |
||
187 | if (NCDCall_ArgCount(&call) != 2) { |
||
188 | return FunctionLog(&call, BLOG_ERROR, "value_compare: need two arguments"); |
||
189 | } |
||
190 | NCDValRef vals[2]; |
||
191 | for (int i = 0; i < 2; i++) { |
||
192 | vals[i] = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); |
||
193 | if (NCDVal_IsInvalid(vals[i])) { |
||
194 | return; |
||
195 | } |
||
196 | } |
||
197 | int res = func(NCDVal_Compare(vals[0], vals[1])); |
||
198 | NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); |
||
199 | } |
||
200 | |||
201 | #define DEFINE_VALUE_COMPARE(name, expr) \ |
||
202 | static int value_compare_##name##_func (int cmp) { return expr; } \ |
||
203 | static void value_compare_##name##_eval (NCDCall call) { return value_compare_eval(call, value_compare_##name##_func); } |
||
204 | |||
205 | DEFINE_VALUE_COMPARE(lesser, (cmp < 0)) |
||
206 | DEFINE_VALUE_COMPARE(greater, (cmp > 0)) |
||
207 | DEFINE_VALUE_COMPARE(lesser_equal, (cmp <= 0)) |
||
208 | DEFINE_VALUE_COMPARE(greater_equal, (cmp >= 0)) |
||
209 | DEFINE_VALUE_COMPARE(equal, (cmp == 0)) |
||
210 | DEFINE_VALUE_COMPARE(different, (cmp != 0)) |
||
211 | |||
212 | |||
213 | // Concatenation functions. |
||
214 | |||
215 | static int concat_recurser (ExpString *estr, NCDValRef arg, NCDCall const *call) |
||
216 | { |
||
217 | if (NCDVal_IsString(arg)) { |
||
218 | if (!ExpString_AppendBinaryMr(estr, NCDVal_StringMemRef(arg))) { |
||
219 | FunctionLog(call, BLOG_ERROR, "ExpString_AppendBinaryMr failed"); |
||
220 | return 0; |
||
221 | } |
||
222 | } else if (NCDVal_IsList(arg)) { |
||
223 | size_t count = NCDVal_ListCount(arg); |
||
224 | for (size_t i = 0; i < count; i++) { |
||
225 | if (!concat_recurser(estr, NCDVal_ListGet(arg, i), call)) { |
||
226 | return 0; |
||
227 | } |
||
228 | } |
||
229 | } else { |
||
230 | FunctionLog(call, BLOG_ERROR, "concat: value is not a string or list"); |
||
231 | return 0; |
||
232 | } |
||
233 | return 1; |
||
234 | } |
||
235 | |||
236 | static void concat_eval (NCDCall call) |
||
237 | { |
||
238 | ExpString estr; |
||
239 | if (!ExpString_Init(&estr)) { |
||
240 | FunctionLog(&call, BLOG_ERROR, "ExpString_Init failed"); |
||
241 | goto fail0; |
||
242 | } |
||
243 | size_t count = NCDCall_ArgCount(&call); |
||
244 | for (size_t i = 0; i < count; i++) { |
||
245 | NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); |
||
246 | if (NCDVal_IsInvalid(arg)) { |
||
247 | goto fail1; |
||
248 | } |
||
249 | if (!concat_recurser(&estr, arg, &call)) { |
||
250 | goto fail1; |
||
251 | } |
||
252 | } |
||
253 | NCDCall_SetResult(&call, NCDVal_NewStringBinMr(NCDCall_ResMem(&call), ExpString_GetMr(&estr))); |
||
254 | fail1: |
||
255 | ExpString_Free(&estr); |
||
256 | fail0: |
||
257 | return; |
||
258 | } |
||
259 | |||
260 | static void concatlist_eval (NCDCall call) |
||
261 | { |
||
262 | NCDValRef args_list; |
||
263 | if (!ncd_eval_func_args(&call, NCDCall_ResMem(&call), &args_list)) { |
||
264 | return; |
||
265 | } |
||
266 | size_t arg_count = NCDVal_ListCount(args_list); |
||
267 | bsize_t elem_count = bsize_fromsize(0); |
||
268 | for (size_t i = 0; i < arg_count; i++) { |
||
269 | NCDValRef arg = NCDVal_ListGet(args_list, i); |
||
270 | if (!NCDVal_IsList(arg)) { |
||
271 | return FunctionLog(&call, BLOG_ERROR, "concatlist: argument is not a list"); |
||
272 | } |
||
273 | elem_count = bsize_add(elem_count, bsize_fromsize(NCDVal_ListCount(arg))); |
||
274 | } |
||
275 | if (elem_count.is_overflow) { |
||
276 | return FunctionLog(&call, BLOG_ERROR, "concatlist: count overflow"); |
||
277 | } |
||
278 | NCDValRef res = NCDVal_NewList(NCDCall_ResMem(&call), elem_count.value); |
||
279 | if (NCDVal_IsInvalid(res)) { |
||
280 | return; |
||
281 | } |
||
282 | for (size_t i = 0; i < arg_count; i++) { |
||
283 | NCDValRef arg = NCDVal_ListGet(args_list, i); |
||
284 | size_t arg_list_count = NCDVal_ListCount(arg); |
||
285 | for (size_t j = 0; j < arg_list_count; j++) { |
||
286 | NCDValRef copy = NCDVal_NewCopy(NCDCall_ResMem(&call), NCDVal_ListGet(arg, j)); |
||
287 | if (NCDVal_IsInvalid(copy)) { |
||
288 | return; |
||
289 | } |
||
290 | if (!NCDVal_ListAppend(res, copy)) { |
||
291 | return; |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | NCDCall_SetResult(&call, res); |
||
296 | } |
||
297 | |||
298 | |||
299 | // Integer comparison functions. |
||
300 | |||
301 | typedef int (*integer_compare_func) (uintmax_t n1, uintmax_t n2); |
||
302 | |||
303 | static void integer_compare_eval (NCDCall call, integer_compare_func func) |
||
304 | { |
||
305 | if (NCDCall_ArgCount(&call) != 2) { |
||
306 | return FunctionLog(&call, BLOG_ERROR, "integer_compare: need two arguments"); |
||
307 | } |
||
308 | uintmax_t ints[2]; |
||
309 | for (int i = 0; i < 2; i++) { |
||
310 | NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); |
||
311 | if (NCDVal_IsInvalid(arg)) { |
||
312 | return; |
||
313 | } |
||
314 | if (!ncd_read_uintmax(arg, &ints[i])) { |
||
315 | return FunctionLog(&call, BLOG_ERROR, "integer_compare: wrong value"); |
||
316 | } |
||
317 | } |
||
318 | int res = func(ints[0], ints[1]); |
||
319 | NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); |
||
320 | } |
||
321 | |||
322 | #define DEFINE_INT_COMPARE(name, expr) \ |
||
323 | static int integer_compare_##name##_func (uintmax_t n1, uintmax_t n2) { return expr; } \ |
||
324 | static void integer_compare_##name##_eval (NCDCall call) { return integer_compare_eval(call, integer_compare_##name##_func); } |
||
325 | |||
326 | DEFINE_INT_COMPARE(lesser, (n1 < n2)) |
||
327 | DEFINE_INT_COMPARE(greater, (n1 > n2)) |
||
328 | DEFINE_INT_COMPARE(lesser_equal, (n1 <= n2)) |
||
329 | DEFINE_INT_COMPARE(greater_equal, (n1 >= n2)) |
||
330 | DEFINE_INT_COMPARE(equal, (n1 == n2)) |
||
331 | DEFINE_INT_COMPARE(different, (n1 != n2)) |
||
332 | |||
333 | |||
334 | // Integer operators. |
||
335 | |||
336 | typedef int (*integer_operator_func) (uintmax_t n1, uintmax_t n2, uintmax_t *out, NCDCall const *call); |
||
337 | |||
338 | static void integer_operator_eval (NCDCall call, integer_operator_func func) |
||
339 | { |
||
340 | if (NCDCall_ArgCount(&call) != 2) { |
||
341 | return FunctionLog(&call, BLOG_ERROR, "integer_operator: need two arguments"); |
||
342 | } |
||
343 | uintmax_t ints[2]; |
||
344 | for (int i = 0; i < 2; i++) { |
||
345 | NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); |
||
346 | if (NCDVal_IsInvalid(arg)) { |
||
347 | return; |
||
348 | } |
||
349 | if (!ncd_read_uintmax(arg, &ints[i])) { |
||
350 | return FunctionLog(&call, BLOG_ERROR, "integer_operator: wrong value"); |
||
351 | } |
||
352 | } |
||
353 | uintmax_t res; |
||
354 | if (!func(ints[0], ints[1], &res, &call)) { |
||
355 | return; |
||
356 | } |
||
357 | NCDCall_SetResult(&call, ncd_make_uintmax(NCDCall_ResMem(&call), res)); |
||
358 | } |
||
359 | |||
360 | #define DEFINE_INT_OPERATOR(name, expr, check_expr, check_err_str) \ |
||
361 | static int integer_operator_##name##_func (uintmax_t n1, uintmax_t n2, uintmax_t *out, NCDCall const *call) \ |
||
362 | { \ |
||
363 | if (check_expr) { \ |
||
364 | FunctionLog(call, BLOG_ERROR, check_err_str); \ |
||
365 | return 0; \ |
||
366 | } \ |
||
367 | *out = expr; \ |
||
368 | return 1; \ |
||
369 | } \ |
||
370 | static void integer_operator_##name##_eval (NCDCall call) { return integer_operator_eval(call, integer_operator_##name##_func); } |
||
371 | |||
372 | DEFINE_INT_OPERATOR(add, (n1 + n2), (n1 > UINTMAX_MAX - n2), "addition overflow") |
||
373 | DEFINE_INT_OPERATOR(subtract, (n1 - n2), (n1 < n2), "subtraction underflow") |
||
374 | DEFINE_INT_OPERATOR(multiply, (n1 * n2), (n2 != 0 && n1 > UINTMAX_MAX / n2), "multiplication overflow") |
||
375 | DEFINE_INT_OPERATOR(divide, (n1 / n2), (n2 == 0), "division quotient is zero") |
||
376 | DEFINE_INT_OPERATOR(modulo, (n1 % n2), (n2 == 0), "modulo modulus is zero") |
||
377 | DEFINE_INT_OPERATOR(min, (n1 < n2 ? n1 : n2), (0), "") |
||
378 | DEFINE_INT_OPERATOR(max, (n1 > n2 ? n1 : n2), (0), "") |
||
379 | |||
380 | |||
381 | // Encode and decode value. |
||
382 | |||
383 | static void encode_value_eval (NCDCall call) |
||
384 | { |
||
385 | if (NCDCall_ArgCount(&call) != 1) { |
||
386 | return FunctionLog(&call, BLOG_ERROR, "encode_value: need one argument"); |
||
387 | } |
||
388 | NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); |
||
389 | if (NCDVal_IsInvalid(arg)) { |
||
390 | return; |
||
391 | } |
||
392 | char *str = NCDValGenerator_Generate(arg); |
||
393 | if (!str) { |
||
394 | return FunctionLog(&call, BLOG_ERROR, "encode_value: NCDValGenerator_Generate failed"); |
||
395 | } |
||
396 | NCDCall_SetResult(&call, NCDVal_NewString(NCDCall_ResMem(&call), str)); |
||
397 | free(str); |
||
398 | } |
||
399 | |||
400 | static void decode_value_eval (NCDCall call) |
||
401 | { |
||
402 | if (NCDCall_ArgCount(&call) != 1) { |
||
403 | return FunctionLog(&call, BLOG_ERROR, "decode_value: need one argument"); |
||
404 | } |
||
405 | // Evaluate the string to a temporary mem, not ResMem. |
||
406 | // Otherwise the ResMem could get resized while we're |
||
407 | // parsing a string within it, and boom. |
||
408 | NCDValMem temp_mem; |
||
409 | NCDValMem_Init(&temp_mem, NCDCall_Iparams(&call)->string_index); |
||
410 | NCDValRef arg = NCDCall_EvalArg(&call, 0, &temp_mem); |
||
411 | if (NCDVal_IsInvalid(arg)) { |
||
412 | goto fail1; |
||
413 | } |
||
414 | if (!NCDVal_IsString(arg)) { |
||
415 | FunctionLog(&call, BLOG_ERROR, "decode_value: argument not a string"); |
||
416 | goto fail1; |
||
417 | } |
||
418 | NCDValRef value; |
||
419 | int res = NCDValParser_Parse(NCDVal_StringMemRef(arg), NCDCall_ResMem(&call), &value); |
||
420 | if (!res) { |
||
421 | FunctionLog(&call, BLOG_ERROR, "decode_value: NCDValParser_Parse failed"); |
||
422 | goto fail1; |
||
423 | } |
||
424 | NCDCall_SetResult(&call, value); |
||
425 | fail1: |
||
426 | NCDValMem_Free(&temp_mem); |
||
427 | } |
||
428 | |||
429 | |||
430 | // ASCII case conversion |
||
431 | |||
432 | typedef char (*perchar_func) (char ch); |
||
433 | |||
434 | static void perchar_eval (NCDCall call, perchar_func func) |
||
435 | { |
||
436 | if (NCDCall_ArgCount(&call) != 1) { |
||
437 | return FunctionLog(&call, BLOG_ERROR, "tolower: need one argument"); |
||
438 | } |
||
439 | NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); |
||
440 | if (NCDVal_IsInvalid(arg)) { |
||
441 | return; |
||
442 | } |
||
443 | if (!NCDVal_IsString(arg)) { |
||
444 | return FunctionLog(&call, BLOG_ERROR, "tolower: argument not a string"); |
||
445 | } |
||
446 | NCDValRef value = NCDVal_NewStringUninitialized(NCDCall_ResMem(&call), NCDVal_StringLength(arg)); |
||
447 | if (NCDVal_IsInvalid(value)) { |
||
448 | return; |
||
449 | } |
||
450 | char *out_data = (char *)NCDVal_StringData(value); |
||
451 | MEMREF_LOOP_CHARS(NCDVal_StringMemRef(arg), i, ch, { |
||
452 | out_data[i] = func(ch); |
||
453 | }) |
||
454 | NCDCall_SetResult(&call, value); |
||
455 | } |
||
456 | |||
457 | #define DEFINE_PERCHAR(name, expr) \ |
||
458 | static char perchar_##name##_func (char ch) { return expr; } \ |
||
459 | static void perchar_##name##_eval (NCDCall call) { return perchar_eval(call, perchar_##name##_func); } |
||
460 | |||
461 | DEFINE_PERCHAR(tolower, b_ascii_tolower(ch)) |
||
462 | DEFINE_PERCHAR(toupper, b_ascii_toupper(ch)) |
||
463 | |||
464 | |||
465 | // struct_encode, struct_decode |
||
466 | |||
467 | static int read_integer_encoding (NCDValRef encoding, int *out_big, int *out_size) |
||
468 | { |
||
469 | if (!NCDVal_IsString(encoding)) { |
||
470 | return 0; |
||
471 | } |
||
472 | int big; |
||
473 | int size; |
||
474 | if (NCDVal_StringEquals(encoding, "u8")) { |
||
475 | big = 0; |
||
476 | size = 1; |
||
477 | } else if ((big = NCDVal_StringEquals(encoding, "u16b")) || NCDVal_StringEquals(encoding, "u16l")) { |
||
478 | size = 2; |
||
479 | } else if ((big = NCDVal_StringEquals(encoding, "u32b")) || NCDVal_StringEquals(encoding, "u32l")) { |
||
480 | size = 4; |
||
481 | } else if ((big = NCDVal_StringEquals(encoding, "u64b")) || NCDVal_StringEquals(encoding, "u64l")) { |
||
482 | size = 8; |
||
483 | } else { |
||
484 | return 0; |
||
485 | } |
||
486 | *out_big = big; |
||
487 | *out_size = size; |
||
488 | return 1; |
||
489 | } |
||
490 | |||
491 | static int struct_encode_single (NCDCall call, ExpString *estr, NCDValRef encoding, NCDValRef value) |
||
492 | { |
||
493 | uintmax_t val_int; |
||
494 | if (!ncd_read_uintmax(value, &val_int)) { |
||
495 | FunctionLog(&call, BLOG_ERROR, "struct_encode: value must be an integer"); |
||
496 | return 0; |
||
497 | } |
||
498 | int big; |
||
499 | int size; |
||
500 | if (!read_integer_encoding(encoding, &big, &size)) { |
||
501 | FunctionLog(&call, BLOG_ERROR, "struct_encode: invalid encoding specified"); |
||
502 | return 0; |
||
503 | } |
||
504 | uint8_t results[8]; |
||
505 | for (int i = 0; i < size; i++) { |
||
506 | results[big ? (size - 1 - i) : i] = val_int; |
||
507 | val_int >>= 8; |
||
508 | } |
||
509 | if (val_int > 0) { |
||
510 | FunctionLog(&call, BLOG_ERROR, "struct_encode: value is out of range"); |
||
511 | return 0; |
||
512 | } |
||
513 | if (!ExpString_AppendBinaryMr(estr, MemRef_Make((char const *)results, size))) { |
||
514 | FunctionLog(&call, BLOG_ERROR, "ExpString_AppendBinaryMr failed"); |
||
515 | return 0; |
||
516 | } |
||
517 | return 1; |
||
518 | } |
||
519 | |||
520 | static void struct_encode_eval (NCDCall call) |
||
521 | { |
||
522 | if (NCDCall_ArgCount(&call) != 1) { |
||
523 | FunctionLog(&call, BLOG_ERROR, "struct_encode: need one argument"); |
||
524 | goto fail0; |
||
525 | } |
||
526 | NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); |
||
527 | if (NCDVal_IsInvalid(arg)) { |
||
528 | goto fail0; |
||
529 | } |
||
530 | if (!NCDVal_IsList(arg)) { |
||
531 | FunctionLog(&call, BLOG_ERROR, "struct_encode: argument must be a list"); |
||
532 | goto fail0; |
||
533 | } |
||
534 | ExpString estr; |
||
535 | if (!ExpString_Init(&estr)) { |
||
536 | FunctionLog(&call, BLOG_ERROR, "ExpString_Init failed"); |
||
537 | goto fail0; |
||
538 | } |
||
539 | size_t count = NCDVal_ListCount(arg); |
||
540 | for (size_t i = 0; i < count; i++) { |
||
541 | NCDValRef elem = NCDVal_ListGet(arg, i); |
||
542 | if (!NCDVal_IsList(elem)) { |
||
543 | FunctionLog(&call, BLOG_ERROR, "struct_encode: element must be a list"); |
||
544 | goto fail1; |
||
545 | } |
||
546 | NCDValRef encoding; |
||
547 | NCDValRef value; |
||
548 | if (!NCDVal_ListRead(elem, 2, &encoding, &value)) { |
||
549 | FunctionLog(&call, BLOG_ERROR, "struct_encode: element list must have two elements"); |
||
550 | goto fail1; |
||
551 | } |
||
552 | if (!struct_encode_single(call, &estr, encoding, value)) { |
||
553 | goto fail1; |
||
554 | } |
||
555 | } |
||
556 | NCDCall_SetResult(&call, NCDVal_NewStringBinMr(NCDCall_ResMem(&call), ExpString_GetMr(&estr))); |
||
557 | fail1: |
||
558 | ExpString_Free(&estr); |
||
559 | fail0: |
||
560 | return; |
||
561 | } |
||
562 | |||
563 | static int struct_decode_single (NCDCall call, MemRef *data, NCDValRef encoding, NCDValRef result_list) |
||
564 | { |
||
565 | int big; |
||
566 | int size; |
||
567 | if (!read_integer_encoding(encoding, &big, &size)) { |
||
568 | FunctionLog(&call, BLOG_ERROR, "struct_decode: invalid encoding specified"); |
||
569 | return 0; |
||
570 | } |
||
571 | if (data->len < size) { |
||
572 | FunctionLog(&call, BLOG_ERROR, "struct_decode: insufficient data available"); |
||
573 | return 0; |
||
574 | } |
||
575 | uintmax_t val_int = 0; |
||
576 | for (int i = 0; i < size; i++) { |
||
577 | val_int <<= 8; |
||
578 | val_int |= *(uint8_t const *)(data->ptr + (big ? i : (size - 1 - i))); |
||
579 | } |
||
580 | *data = MemRef_SubFrom(*data, size); |
||
581 | NCDValRef value = ncd_make_uintmax(NCDCall_ResMem(&call), val_int); |
||
582 | if (NCDVal_IsInvalid(value)) { |
||
583 | return 0; |
||
584 | } |
||
585 | if (!NCDVal_ListAppend(result_list, value)) { |
||
586 | return 0; |
||
587 | } |
||
588 | return 1; |
||
589 | } |
||
590 | |||
591 | static void struct_decode_eval (NCDCall call) |
||
592 | { |
||
593 | if (NCDCall_ArgCount(&call) != 2) { |
||
594 | FunctionLog(&call, BLOG_ERROR, "struct_decode: need two arguments"); |
||
595 | goto fail0; |
||
596 | } |
||
597 | NCDValRef format_arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); |
||
598 | if (NCDVal_IsInvalid(format_arg)) { |
||
599 | goto fail0; |
||
600 | } |
||
601 | if (!NCDVal_IsList(format_arg)) { |
||
602 | FunctionLog(&call, BLOG_ERROR, "struct_decode: format argument must be a list"); |
||
603 | goto fail0; |
||
604 | } |
||
605 | // Evaluate the data string to temp mem, so the pointer doesn't change. |
||
606 | NCDValMem temp_mem; |
||
607 | NCDValMem_Init(&temp_mem, NCDCall_Iparams(&call)->string_index); |
||
608 | NCDValRef data_arg = NCDCall_EvalArg(&call, 1, &temp_mem); |
||
609 | if (NCDVal_IsInvalid(data_arg)) { |
||
610 | goto fail1; |
||
611 | } |
||
612 | if (!NCDVal_IsString(data_arg)) { |
||
613 | FunctionLog(&call, BLOG_ERROR, "struct_decode: data argument must be a string"); |
||
614 | goto fail1; |
||
615 | } |
||
616 | size_t count = NCDVal_ListCount(format_arg); |
||
617 | NCDValRef result_list = NCDVal_NewList(NCDCall_ResMem(&call), count); |
||
618 | if (NCDVal_IsInvalid(result_list)) { |
||
619 | goto fail1; |
||
620 | } |
||
621 | MemRef data = NCDVal_StringMemRef(data_arg); |
||
622 | for (size_t i = 0; i < count; i++) { |
||
623 | NCDValRef encoding = NCDVal_ListGet(format_arg, i); |
||
624 | if (!struct_decode_single(call, &data, encoding, result_list)) { |
||
625 | goto fail1; |
||
626 | } |
||
627 | } |
||
628 | if (data.len > 0) { |
||
629 | FunctionLog(&call, BLOG_ERROR, "struct_decode: not all data was consumed"); |
||
630 | goto fail1; |
||
631 | } |
||
632 | NCDCall_SetResult(&call, result_list); |
||
633 | fail1: |
||
634 | NCDValMem_Free(&temp_mem); |
||
635 | fail0: |
||
636 | return; |
||
637 | } |
||
638 | |||
639 | |||
640 | // checksum |
||
641 | |||
642 | static void checksum_eval (NCDCall call) |
||
643 | { |
||
644 | if (NCDCall_ArgCount(&call) != 2) { |
||
645 | FunctionLog(&call, BLOG_ERROR, "checksum: need two arguments"); |
||
646 | return; |
||
647 | } |
||
648 | NCDValRef algorithm_arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); |
||
649 | if (NCDVal_IsInvalid(algorithm_arg)) { |
||
650 | return; |
||
651 | } |
||
652 | if (!NCDVal_IsString(algorithm_arg)) { |
||
653 | FunctionLog(&call, BLOG_ERROR, "checksum: algorithm argument must be a string"); |
||
654 | return; |
||
655 | } |
||
656 | NCDValRef data_arg = NCDCall_EvalArg(&call, 1, NCDCall_ResMem(&call)); |
||
657 | if (NCDVal_IsInvalid(data_arg)) { |
||
658 | return; |
||
659 | } |
||
660 | if (!NCDVal_IsString(data_arg)) { |
||
661 | FunctionLog(&call, BLOG_ERROR, "checksum: data argument must be a string"); |
||
662 | return; |
||
663 | } |
||
664 | MemRef data = NCDVal_StringMemRef(data_arg); |
||
665 | uintmax_t result; |
||
666 | if (NCDVal_StringEquals(algorithm_arg, "inverted_sum_bytes")) { |
||
667 | uint8_t s = 0; |
||
668 | for (size_t i = 0; i < data.len; i++) { |
||
669 | s += *(uint8_t const *)(data.ptr + i); |
||
670 | } |
||
671 | result = (uint8_t)~s; |
||
672 | } else { |
||
673 | FunctionLog(&call, BLOG_ERROR, "checksum: unknown algorithm"); |
||
674 | return; |
||
675 | } |
||
676 | NCDCall_SetResult(&call, ncd_make_uintmax(NCDCall_ResMem(&call), result)); |
||
677 | } |
||
678 | |||
679 | |||
680 | // clock_get_ms |
||
681 | |||
682 | static void clock_get_ms_eval (NCDCall call) |
||
683 | { |
||
684 | if (NCDCall_ArgCount(&call) != 0) { |
||
685 | FunctionLog(&call, BLOG_ERROR, "clock_get_ms: need zero arguments"); |
||
686 | return; |
||
687 | } |
||
688 | // Convert to unsigned. The time should be non-negative so this is OK. |
||
689 | uintmax_t the_time = btime_gettime(); |
||
690 | NCDCall_SetResult(&call, ncd_make_uintmax(NCDCall_ResMem(&call), the_time)); |
||
691 | } |
||
692 | |||
693 | |||
694 | static struct NCDModuleFunction const functions[] = { |
||
695 | { |
||
696 | .func_name = "error", |
||
697 | .func_eval = error_eval |
||
698 | }, { |
||
699 | .func_name = "identity", |
||
700 | .func_eval = identity_eval |
||
701 | }, { |
||
702 | .func_name = "if", |
||
703 | .func_eval = if_eval |
||
704 | }, { |
||
705 | .func_name = "ifel", |
||
706 | .func_eval = ifel_eval |
||
707 | }, { |
||
708 | .func_name = "bool", |
||
709 | .func_eval = bool_eval |
||
710 | }, { |
||
711 | .func_name = "not", |
||
712 | .func_eval = not_eval |
||
713 | }, { |
||
714 | .func_name = "and", |
||
715 | .func_eval = and_eval |
||
716 | }, { |
||
717 | .func_name = "or", |
||
718 | .func_eval = or_eval |
||
719 | }, { |
||
720 | .func_name = "imp", |
||
721 | .func_eval = imp_eval |
||
722 | }, { |
||
723 | .func_name = "val_lesser", |
||
724 | .func_eval = value_compare_lesser_eval |
||
725 | }, { |
||
726 | .func_name = "val_greater", |
||
727 | .func_eval = value_compare_greater_eval |
||
728 | }, { |
||
729 | .func_name = "val_lesser_equal", |
||
730 | .func_eval = value_compare_lesser_equal_eval |
||
731 | }, { |
||
732 | .func_name = "val_greater_equal", |
||
733 | .func_eval = value_compare_greater_equal_eval |
||
734 | }, { |
||
735 | .func_name = "val_equal", |
||
736 | .func_eval = value_compare_equal_eval |
||
737 | }, { |
||
738 | .func_name = "val_different", |
||
739 | .func_eval = value_compare_different_eval |
||
740 | }, { |
||
741 | .func_name = "concat", |
||
742 | .func_eval = concat_eval |
||
743 | }, { |
||
744 | .func_name = "concatlist", |
||
745 | .func_eval = concatlist_eval |
||
746 | }, { |
||
747 | .func_name = "num_lesser", |
||
748 | .func_eval = integer_compare_lesser_eval |
||
749 | }, { |
||
750 | .func_name = "num_greater", |
||
751 | .func_eval = integer_compare_greater_eval |
||
752 | }, { |
||
753 | .func_name = "num_lesser_equal", |
||
754 | .func_eval = integer_compare_lesser_equal_eval |
||
755 | }, { |
||
756 | .func_name = "num_greater_equal", |
||
757 | .func_eval = integer_compare_greater_equal_eval |
||
758 | }, { |
||
759 | .func_name = "num_equal", |
||
760 | .func_eval = integer_compare_equal_eval |
||
761 | }, { |
||
762 | .func_name = "num_different", |
||
763 | .func_eval = integer_compare_different_eval |
||
764 | }, { |
||
765 | .func_name = "num_add", |
||
766 | .func_eval = integer_operator_add_eval |
||
767 | }, { |
||
768 | .func_name = "num_subtract", |
||
769 | .func_eval = integer_operator_subtract_eval |
||
770 | }, { |
||
771 | .func_name = "num_multiply", |
||
772 | .func_eval = integer_operator_multiply_eval |
||
773 | }, { |
||
774 | .func_name = "num_divide", |
||
775 | .func_eval = integer_operator_divide_eval |
||
776 | }, { |
||
777 | .func_name = "num_modulo", |
||
778 | .func_eval = integer_operator_modulo_eval |
||
779 | }, { |
||
780 | .func_name = "num_min", |
||
781 | .func_eval = integer_operator_min_eval |
||
782 | }, { |
||
783 | .func_name = "num_max", |
||
784 | .func_eval = integer_operator_max_eval |
||
785 | }, { |
||
786 | .func_name = "encode_value", |
||
787 | .func_eval = encode_value_eval |
||
788 | }, { |
||
789 | .func_name = "decode_value", |
||
790 | .func_eval = decode_value_eval |
||
791 | }, { |
||
792 | .func_name = "tolower", |
||
793 | .func_eval = perchar_tolower_eval |
||
794 | }, { |
||
795 | .func_name = "toupper", |
||
796 | .func_eval = perchar_toupper_eval |
||
797 | }, { |
||
798 | .func_name = "struct_encode", |
||
799 | .func_eval = struct_encode_eval |
||
800 | }, { |
||
801 | .func_name = "struct_decode", |
||
802 | .func_eval = struct_decode_eval |
||
803 | }, { |
||
804 | .func_name = "checksum", |
||
805 | .func_eval = checksum_eval |
||
806 | }, { |
||
807 | .func_name = "clock_get_ms", |
||
808 | .func_eval = clock_get_ms_eval |
||
809 | }, { |
||
810 | .func_name = NULL |
||
811 | } |
||
812 | }; |
||
813 | |||
814 | const struct NCDModuleGroup ncdmodule_basic_functions = { |
||
815 | .functions = functions |
||
816 | }; |