basic_functions.c 25 KB

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