basic_functions.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 bool_not_eval (NCDCall call, int negate, char const *name)
  67. {
  68. if (NCDCall_ArgCount(&call) != 1) {
  69. return FunctionLog(&call, BLOG_ERROR, "%s: need one argument", name);
  70. }
  71. NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call));
  72. if (NCDVal_IsInvalid(arg)) {
  73. return;
  74. }
  75. int arg_val;
  76. if (!ncd_read_boolean(arg, &arg_val)) {
  77. return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name);
  78. }
  79. int res = (arg_val != negate);
  80. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  81. }
  82. static void bool_eval (NCDCall call) { return bool_not_eval(call, 0, "bool"); }
  83. static void not_eval (NCDCall call) { return bool_not_eval(call, 1, "not"); }
  84. static void and_or_eval (NCDCall call, int is_and, char const *name)
  85. {
  86. size_t count = NCDCall_ArgCount(&call);
  87. int res = is_and;
  88. for (size_t i = 0; i < count; i++) {
  89. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  90. if (NCDVal_IsInvalid(arg)) {
  91. return;
  92. }
  93. int arg_val;
  94. if (!ncd_read_boolean(arg, &arg_val)) {
  95. return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name);
  96. }
  97. if (arg_val != is_and) {
  98. res = !is_and;
  99. break;
  100. }
  101. }
  102. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  103. }
  104. static void and_eval (NCDCall call) { return and_or_eval(call, 1, "and"); }
  105. static void or_eval (NCDCall call) { return and_or_eval(call, 0, "or"); }
  106. static void imp_eval (NCDCall call)
  107. {
  108. if (NCDCall_ArgCount(&call) != 2) {
  109. return FunctionLog(&call, BLOG_ERROR, "imp: need two arguments");
  110. }
  111. int res = 0;
  112. for (size_t i = 0; i < 2; i++) {
  113. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  114. if (NCDVal_IsInvalid(arg)) {
  115. return;
  116. }
  117. int arg_val;
  118. if (!ncd_read_boolean(arg, &arg_val)) {
  119. return FunctionLog(&call, BLOG_ERROR, "imp: bad argument");
  120. }
  121. if (arg_val == i) {
  122. res = 1;
  123. break;
  124. }
  125. }
  126. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  127. }
  128. // Value comparison functions.
  129. typedef int (*value_compare_func) (int cmp);
  130. static void value_compare_eval (NCDCall call, value_compare_func func)
  131. {
  132. if (NCDCall_ArgCount(&call) != 2) {
  133. return FunctionLog(&call, BLOG_ERROR, "value_compare: need two arguments");
  134. }
  135. NCDValRef vals[2];
  136. for (int i = 0; i < 2; i++) {
  137. vals[i] = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  138. if (NCDVal_IsInvalid(vals[i])) {
  139. return;
  140. }
  141. }
  142. int res = func(NCDVal_Compare(vals[0], vals[1]));
  143. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  144. }
  145. #define DEFINE_VALUE_COMPARE(name, expr) \
  146. static int value_compare_##name##_func (int cmp) { return expr; } \
  147. static void value_compare_##name##_eval (NCDCall call) { return value_compare_eval(call, value_compare_##name##_func); }
  148. DEFINE_VALUE_COMPARE(lesser, (cmp < 0))
  149. DEFINE_VALUE_COMPARE(greater, (cmp > 0))
  150. DEFINE_VALUE_COMPARE(lesser_equal, (cmp <= 0))
  151. DEFINE_VALUE_COMPARE(greater_equal, (cmp >= 0))
  152. DEFINE_VALUE_COMPARE(equal, (cmp == 0))
  153. DEFINE_VALUE_COMPARE(different, (cmp != 0))
  154. // Concatenation functions.
  155. static int concat_recurser (ExpString *estr, NCDValRef arg, NCDCall const *call)
  156. {
  157. if (NCDVal_IsString(arg)) {
  158. MemRef mr = NCDVal_StringMemRef(arg);
  159. if (!ExpString_AppendBinary(estr, (uint8_t const *)mr.ptr, mr.len)) {
  160. FunctionLog(call, BLOG_ERROR, "ExpString_AppendBinary failed");
  161. return 0;
  162. }
  163. } else if (NCDVal_IsList(arg)) {
  164. size_t count = NCDVal_ListCount(arg);
  165. for (size_t i = 0; i < count; i++) {
  166. if (!concat_recurser(estr, NCDVal_ListGet(arg, i), call)) {
  167. return 0;
  168. }
  169. }
  170. } else {
  171. FunctionLog(call, BLOG_ERROR, "concat: value is not a string or list");
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. static void concat_eval (NCDCall call)
  177. {
  178. ExpString estr;
  179. if (!ExpString_Init(&estr)) {
  180. FunctionLog(&call, BLOG_ERROR, "ExpString_Init failed");
  181. goto fail0;
  182. }
  183. size_t count = NCDCall_ArgCount(&call);
  184. for (size_t i = 0; i < count; i++) {
  185. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  186. if (NCDVal_IsInvalid(arg)) {
  187. goto fail1;
  188. }
  189. if (!concat_recurser(&estr, arg, &call)) {
  190. goto fail1;
  191. }
  192. }
  193. NCDCall_SetResult(&call, NCDVal_NewStringBin(NCDCall_ResMem(&call), (uint8_t const *)ExpString_Get(&estr), ExpString_Length(&estr)));
  194. fail1:
  195. ExpString_Free(&estr);
  196. fail0:
  197. return;
  198. }
  199. static void concatlist_eval (NCDCall call)
  200. {
  201. NCDValRef args_list;
  202. if (!ncd_eval_func_args(&call, NCDCall_ResMem(&call), &args_list)) {
  203. return;
  204. }
  205. size_t arg_count = NCDVal_ListCount(args_list);
  206. bsize_t elem_count = bsize_fromsize(0);
  207. for (size_t i = 0; i < arg_count; i++) {
  208. NCDValRef arg = NCDVal_ListGet(args_list, i);
  209. if (!NCDVal_IsList(arg)) {
  210. return FunctionLog(&call, BLOG_ERROR, "concatlist: argument is not a list");
  211. }
  212. elem_count = bsize_add(elem_count, bsize_fromsize(NCDVal_ListCount(arg)));
  213. }
  214. if (elem_count.is_overflow) {
  215. return FunctionLog(&call, BLOG_ERROR, "concatlist: count overflow");
  216. }
  217. NCDValRef res = NCDVal_NewList(NCDCall_ResMem(&call), elem_count.value);
  218. if (NCDVal_IsInvalid(res)) {
  219. return;
  220. }
  221. for (size_t i = 0; i < arg_count; i++) {
  222. NCDValRef arg = NCDVal_ListGet(args_list, i);
  223. size_t arg_list_count = NCDVal_ListCount(arg);
  224. for (size_t j = 0; j < arg_list_count; j++) {
  225. NCDValRef copy = NCDVal_NewCopy(NCDCall_ResMem(&call), NCDVal_ListGet(arg, j));
  226. if (NCDVal_IsInvalid(copy)) {
  227. return;
  228. }
  229. if (!NCDVal_ListAppend(res, copy)) {
  230. return;
  231. }
  232. }
  233. }
  234. NCDCall_SetResult(&call, res);
  235. }
  236. // Integer comparison functions.
  237. typedef int (*integer_compare_func) (uintmax_t n1, uintmax_t n2);
  238. static void integer_compare_eval (NCDCall call, integer_compare_func func)
  239. {
  240. if (NCDCall_ArgCount(&call) != 2) {
  241. return FunctionLog(&call, BLOG_ERROR, "integer_compare: need two arguments");
  242. }
  243. uintmax_t ints[2];
  244. for (int i = 0; i < 2; i++) {
  245. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  246. if (NCDVal_IsInvalid(arg)) {
  247. return;
  248. }
  249. if (!ncd_read_uintmax(arg, &ints[i])) {
  250. return FunctionLog(&call, BLOG_ERROR, "integer_compare: wrong value");
  251. }
  252. }
  253. int res = func(ints[0], ints[1]);
  254. NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res, NCDCall_Iparams(&call)->string_index));
  255. }
  256. #define DEFINE_INT_COMPARE(name, expr) \
  257. static int integer_compare_##name##_func (uintmax_t n1, uintmax_t n2) { return expr; } \
  258. static void integer_compare_##name##_eval (NCDCall call) { return integer_compare_eval(call, integer_compare_##name##_func); }
  259. DEFINE_INT_COMPARE(lesser, (n1 < n2))
  260. DEFINE_INT_COMPARE(greater, (n1 > n2))
  261. DEFINE_INT_COMPARE(lesser_equal, (n1 <= n2))
  262. DEFINE_INT_COMPARE(greater_equal, (n1 >= n2))
  263. DEFINE_INT_COMPARE(equal, (n1 == n2))
  264. DEFINE_INT_COMPARE(different, (n1 != n2))
  265. // Integer operators.
  266. typedef int (*integer_operator_func) (uintmax_t n1, uintmax_t n2, uintmax_t *out, NCDCall const *call);
  267. static void integer_operator_eval (NCDCall call, integer_operator_func func)
  268. {
  269. if (NCDCall_ArgCount(&call) != 2) {
  270. return FunctionLog(&call, BLOG_ERROR, "integer_operator: need two arguments");
  271. }
  272. uintmax_t ints[2];
  273. for (int i = 0; i < 2; i++) {
  274. NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call));
  275. if (NCDVal_IsInvalid(arg)) {
  276. return;
  277. }
  278. if (!ncd_read_uintmax(arg, &ints[i])) {
  279. return FunctionLog(&call, BLOG_ERROR, "integer_operator: wrong value");
  280. }
  281. }
  282. uintmax_t res;
  283. if (!func(ints[0], ints[1], &res, &call)) {
  284. return;
  285. }
  286. NCDCall_SetResult(&call, ncd_make_uintmax(NCDCall_ResMem(&call), res));
  287. }
  288. #define DEFINE_INT_OPERATOR(name, expr, check_expr, check_err_str) \
  289. static int integer_operator_##name##_func (uintmax_t n1, uintmax_t n2, uintmax_t *out, NCDCall const *call) \
  290. { \
  291. if (check_expr) { \
  292. FunctionLog(call, BLOG_ERROR, check_err_str); \
  293. return 0; \
  294. } \
  295. *out = expr; \
  296. return 1; \
  297. } \
  298. static void integer_operator_##name##_eval (NCDCall call) { return integer_operator_eval(call, integer_operator_##name##_func); }
  299. DEFINE_INT_OPERATOR(add, (n1 + n2), (n1 > UINTMAX_MAX - n2), "addition overflow")
  300. DEFINE_INT_OPERATOR(subtract, (n1 - n2), (n1 < n2), "subtraction underflow")
  301. DEFINE_INT_OPERATOR(multiply, (n1 * n2), (n2 != 0 && n1 > UINTMAX_MAX / n2), "multiplication overflow")
  302. DEFINE_INT_OPERATOR(divide, (n1 / n2), (n2 == 0), "division quotient is zero")
  303. DEFINE_INT_OPERATOR(modulo, (n1 % n2), (n2 == 0), "modulo modulus is zero")
  304. // Encode and decode value.
  305. static void encode_value_eval (NCDCall call)
  306. {
  307. if (NCDCall_ArgCount(&call) != 1) {
  308. return FunctionLog(&call, BLOG_ERROR, "encode_value: need one argument");
  309. }
  310. NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call));
  311. if (NCDVal_IsInvalid(arg)) {
  312. return;
  313. }
  314. char *str = NCDValGenerator_Generate(arg);
  315. if (!str) {
  316. return FunctionLog(&call, BLOG_ERROR, "encode_value: NCDValGenerator_Generate failed");
  317. }
  318. NCDCall_SetResult(&call, NCDVal_NewString(NCDCall_ResMem(&call), str));
  319. free(str);
  320. }
  321. static void decode_value_eval (NCDCall call)
  322. {
  323. if (NCDCall_ArgCount(&call) != 1) {
  324. return FunctionLog(&call, BLOG_ERROR, "decode_value: need one argument");
  325. }
  326. NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call));
  327. if (NCDVal_IsInvalid(arg)) {
  328. return;
  329. }
  330. if (!NCDVal_IsString(arg)) {
  331. return;
  332. }
  333. NCDValRef value;
  334. int res = NCDValParser_Parse(NCDVal_StringData(arg), NCDVal_StringLength(arg), NCDCall_ResMem(&call), &value);
  335. if (!res) {
  336. return FunctionLog(&call, BLOG_ERROR, "decode_value: NCDValParser_Parse failed");
  337. }
  338. NCDCall_SetResult(&call, value);
  339. }
  340. // ASCII case conversion
  341. typedef char (*perchar_func) (char ch);
  342. static void perchar_eval (NCDCall call, perchar_func func)
  343. {
  344. if (NCDCall_ArgCount(&call) != 1) {
  345. return FunctionLog(&call, BLOG_ERROR, "tolower: need one argument");
  346. }
  347. NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call));
  348. if (NCDVal_IsInvalid(arg)) {
  349. return;
  350. }
  351. if (!NCDVal_IsString(arg)) {
  352. return FunctionLog(&call, BLOG_ERROR, "tolower: argument not a string");
  353. }
  354. NCDValRef value = NCDVal_NewStringUninitialized(NCDCall_ResMem(&call), NCDVal_StringLength(arg));
  355. if (NCDVal_IsInvalid(value)) {
  356. return;
  357. }
  358. char *out_data = (char *)NCDVal_StringData(value);
  359. MEMREF_LOOP_CHARS(NCDVal_StringMemRef(arg), i, ch, {
  360. out_data[i] = func(ch);
  361. })
  362. NCDCall_SetResult(&call, value);
  363. }
  364. #define DEFINE_PERCHAR(name, expr) \
  365. static char perchar_##name##_func (char ch) { return expr; } \
  366. static void perchar_##name##_eval (NCDCall call) { return perchar_eval(call, perchar_##name##_func); }
  367. DEFINE_PERCHAR(tolower, b_ascii_tolower(ch))
  368. DEFINE_PERCHAR(toupper, b_ascii_toupper(ch))
  369. static struct NCDModuleFunction const functions[] = {
  370. {
  371. .func_name = "__error__",
  372. .func_eval = error_eval
  373. }, {
  374. .func_name = "__identity__",
  375. .func_eval = identity_eval
  376. }, {
  377. .func_name = "__if__",
  378. .func_eval = if_eval
  379. }, {
  380. .func_name = "__bool__",
  381. .func_eval = bool_eval
  382. }, {
  383. .func_name = "__not__",
  384. .func_eval = not_eval
  385. }, {
  386. .func_name = "__and__",
  387. .func_eval = and_eval
  388. }, {
  389. .func_name = "__or__",
  390. .func_eval = or_eval
  391. }, {
  392. .func_name = "__imp__",
  393. .func_eval = imp_eval
  394. }, {
  395. .func_name = "__val_lesser__",
  396. .func_eval = value_compare_lesser_eval
  397. }, {
  398. .func_name = "__val_greater__",
  399. .func_eval = value_compare_greater_eval
  400. }, {
  401. .func_name = "__val_lesser_equal__",
  402. .func_eval = value_compare_lesser_equal_eval
  403. }, {
  404. .func_name = "__val_greater_equal__",
  405. .func_eval = value_compare_greater_equal_eval
  406. }, {
  407. .func_name = "__val_equal__",
  408. .func_eval = value_compare_equal_eval
  409. }, {
  410. .func_name = "__val_different__",
  411. .func_eval = value_compare_different_eval
  412. }, {
  413. .func_name = "__concat__",
  414. .func_eval = concat_eval
  415. }, {
  416. .func_name = "__concatlist__",
  417. .func_eval = concatlist_eval
  418. }, {
  419. .func_name = "__num_lesser__",
  420. .func_eval = integer_compare_lesser_eval
  421. }, {
  422. .func_name = "__num_greater__",
  423. .func_eval = integer_compare_greater_eval
  424. }, {
  425. .func_name = "__num_lesser_equal__",
  426. .func_eval = integer_compare_lesser_equal_eval
  427. }, {
  428. .func_name = "__num_greater_equal__",
  429. .func_eval = integer_compare_greater_equal_eval
  430. }, {
  431. .func_name = "__num_equal__",
  432. .func_eval = integer_compare_equal_eval
  433. }, {
  434. .func_name = "__num_different__",
  435. .func_eval = integer_compare_different_eval
  436. }, {
  437. .func_name = "__num_add__",
  438. .func_eval = integer_operator_add_eval
  439. }, {
  440. .func_name = "__num_subtract__",
  441. .func_eval = integer_operator_subtract_eval
  442. }, {
  443. .func_name = "__num_multiply__",
  444. .func_eval = integer_operator_multiply_eval
  445. }, {
  446. .func_name = "__num_divide__",
  447. .func_eval = integer_operator_divide_eval
  448. }, {
  449. .func_name = "__num_modulo__",
  450. .func_eval = integer_operator_modulo_eval
  451. }, {
  452. .func_name = "__encode_value__",
  453. .func_eval = encode_value_eval
  454. }, {
  455. .func_name = "__decode_value__",
  456. .func_eval = decode_value_eval
  457. }, {
  458. .func_name = "__tolower__",
  459. .func_eval = perchar_tolower_eval
  460. }, {
  461. .func_name = "__toupper__",
  462. .func_eval = perchar_toupper_eval
  463. }, {
  464. .func_name = NULL
  465. }
  466. };
  467. const struct NCDModuleGroup ncdmodule_basic_functions = {
  468. .functions = functions
  469. };