basic_functions.c 26 KB

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