strfilter.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "util.h"
  3. #include "string2.h"
  4. #include "strfilter.h"
  5. #include <errno.h>
  6. #include "sane_ctype.h"
  7. /* Operators */
  8. static const char *OP_and = "&"; /* Logical AND */
  9. static const char *OP_or = "|"; /* Logical OR */
  10. static const char *OP_not = "!"; /* Logical NOT */
  11. #define is_operator(c) ((c) == '|' || (c) == '&' || (c) == '!')
  12. #define is_separator(c) (is_operator(c) || (c) == '(' || (c) == ')')
  13. static void strfilter_node__delete(struct strfilter_node *node)
  14. {
  15. if (node) {
  16. if (node->p && !is_operator(*node->p))
  17. zfree((char **)&node->p);
  18. strfilter_node__delete(node->l);
  19. strfilter_node__delete(node->r);
  20. free(node);
  21. }
  22. }
  23. void strfilter__delete(struct strfilter *filter)
  24. {
  25. if (filter) {
  26. strfilter_node__delete(filter->root);
  27. free(filter);
  28. }
  29. }
  30. static const char *get_token(const char *s, const char **e)
  31. {
  32. const char *p;
  33. while (isspace(*s)) /* Skip spaces */
  34. s++;
  35. if (*s == '\0') {
  36. p = s;
  37. goto end;
  38. }
  39. p = s + 1;
  40. if (!is_separator(*s)) {
  41. /* End search */
  42. retry:
  43. while (*p && !is_separator(*p) && !isspace(*p))
  44. p++;
  45. /* Escape and special case: '!' is also used in glob pattern */
  46. if (*(p - 1) == '\\' || (*p == '!' && *(p - 1) == '[')) {
  47. p++;
  48. goto retry;
  49. }
  50. }
  51. end:
  52. *e = p;
  53. return s;
  54. }
  55. static struct strfilter_node *strfilter_node__alloc(const char *op,
  56. struct strfilter_node *l,
  57. struct strfilter_node *r)
  58. {
  59. struct strfilter_node *node = zalloc(sizeof(*node));
  60. if (node) {
  61. node->p = op;
  62. node->l = l;
  63. node->r = r;
  64. }
  65. return node;
  66. }
  67. static struct strfilter_node *strfilter_node__new(const char *s,
  68. const char **ep)
  69. {
  70. struct strfilter_node root, *cur, *last_op;
  71. const char *e;
  72. if (!s)
  73. return NULL;
  74. memset(&root, 0, sizeof(root));
  75. last_op = cur = &root;
  76. s = get_token(s, &e);
  77. while (*s != '\0' && *s != ')') {
  78. switch (*s) {
  79. case '&': /* Exchg last OP->r with AND */
  80. if (!cur->r || !last_op->r)
  81. goto error;
  82. cur = strfilter_node__alloc(OP_and, last_op->r, NULL);
  83. if (!cur)
  84. goto nomem;
  85. last_op->r = cur;
  86. last_op = cur;
  87. break;
  88. case '|': /* Exchg the root with OR */
  89. if (!cur->r || !root.r)
  90. goto error;
  91. cur = strfilter_node__alloc(OP_or, root.r, NULL);
  92. if (!cur)
  93. goto nomem;
  94. root.r = cur;
  95. last_op = cur;
  96. break;
  97. case '!': /* Add NOT as a leaf node */
  98. if (cur->r)
  99. goto error;
  100. cur->r = strfilter_node__alloc(OP_not, NULL, NULL);
  101. if (!cur->r)
  102. goto nomem;
  103. cur = cur->r;
  104. break;
  105. case '(': /* Recursively parses inside the parenthesis */
  106. if (cur->r)
  107. goto error;
  108. cur->r = strfilter_node__new(s + 1, &s);
  109. if (!s)
  110. goto nomem;
  111. if (!cur->r || *s != ')')
  112. goto error;
  113. e = s + 1;
  114. break;
  115. default:
  116. if (cur->r)
  117. goto error;
  118. cur->r = strfilter_node__alloc(NULL, NULL, NULL);
  119. if (!cur->r)
  120. goto nomem;
  121. cur->r->p = strndup(s, e - s);
  122. if (!cur->r->p)
  123. goto nomem;
  124. }
  125. s = get_token(e, &e);
  126. }
  127. if (!cur->r)
  128. goto error;
  129. *ep = s;
  130. return root.r;
  131. nomem:
  132. s = NULL;
  133. error:
  134. *ep = s;
  135. strfilter_node__delete(root.r);
  136. return NULL;
  137. }
  138. /*
  139. * Parse filter rule and return new strfilter.
  140. * Return NULL if fail, and *ep == NULL if memory allocation failed.
  141. */
  142. struct strfilter *strfilter__new(const char *rules, const char **err)
  143. {
  144. struct strfilter *filter = zalloc(sizeof(*filter));
  145. const char *ep = NULL;
  146. if (filter)
  147. filter->root = strfilter_node__new(rules, &ep);
  148. if (!filter || !filter->root || *ep != '\0') {
  149. if (err)
  150. *err = ep;
  151. strfilter__delete(filter);
  152. filter = NULL;
  153. }
  154. return filter;
  155. }
  156. static int strfilter__append(struct strfilter *filter, bool _or,
  157. const char *rules, const char **err)
  158. {
  159. struct strfilter_node *right, *root;
  160. const char *ep = NULL;
  161. if (!filter || !rules)
  162. return -EINVAL;
  163. right = strfilter_node__new(rules, &ep);
  164. if (!right || *ep != '\0') {
  165. if (err)
  166. *err = ep;
  167. goto error;
  168. }
  169. root = strfilter_node__alloc(_or ? OP_or : OP_and, filter->root, right);
  170. if (!root) {
  171. ep = NULL;
  172. goto error;
  173. }
  174. filter->root = root;
  175. return 0;
  176. error:
  177. strfilter_node__delete(right);
  178. return ep ? -EINVAL : -ENOMEM;
  179. }
  180. int strfilter__or(struct strfilter *filter, const char *rules, const char **err)
  181. {
  182. return strfilter__append(filter, true, rules, err);
  183. }
  184. int strfilter__and(struct strfilter *filter, const char *rules,
  185. const char **err)
  186. {
  187. return strfilter__append(filter, false, rules, err);
  188. }
  189. static bool strfilter_node__compare(struct strfilter_node *node,
  190. const char *str)
  191. {
  192. if (!node || !node->p)
  193. return false;
  194. switch (*node->p) {
  195. case '|': /* OR */
  196. return strfilter_node__compare(node->l, str) ||
  197. strfilter_node__compare(node->r, str);
  198. case '&': /* AND */
  199. return strfilter_node__compare(node->l, str) &&
  200. strfilter_node__compare(node->r, str);
  201. case '!': /* NOT */
  202. return !strfilter_node__compare(node->r, str);
  203. default:
  204. return strglobmatch(str, node->p);
  205. }
  206. }
  207. /* Return true if STR matches the filter rules */
  208. bool strfilter__compare(struct strfilter *filter, const char *str)
  209. {
  210. if (!filter)
  211. return false;
  212. return strfilter_node__compare(filter->root, str);
  213. }
  214. static int strfilter_node__sprint(struct strfilter_node *node, char *buf);
  215. /* sprint node in parenthesis if needed */
  216. static int strfilter_node__sprint_pt(struct strfilter_node *node, char *buf)
  217. {
  218. int len;
  219. int pt = node->r ? 2 : 0; /* don't need to check node->l */
  220. if (buf && pt)
  221. *buf++ = '(';
  222. len = strfilter_node__sprint(node, buf);
  223. if (len < 0)
  224. return len;
  225. if (buf && pt)
  226. *(buf + len) = ')';
  227. return len + pt;
  228. }
  229. static int strfilter_node__sprint(struct strfilter_node *node, char *buf)
  230. {
  231. int len = 0, rlen;
  232. if (!node || !node->p)
  233. return -EINVAL;
  234. switch (*node->p) {
  235. case '|':
  236. case '&':
  237. len = strfilter_node__sprint_pt(node->l, buf);
  238. if (len < 0)
  239. return len;
  240. __fallthrough;
  241. case '!':
  242. if (buf) {
  243. *(buf + len++) = *node->p;
  244. buf += len;
  245. } else
  246. len++;
  247. rlen = strfilter_node__sprint_pt(node->r, buf);
  248. if (rlen < 0)
  249. return rlen;
  250. len += rlen;
  251. break;
  252. default:
  253. len = strlen(node->p);
  254. if (buf)
  255. strcpy(buf, node->p);
  256. }
  257. return len;
  258. }
  259. char *strfilter__string(struct strfilter *filter)
  260. {
  261. int len;
  262. char *ret = NULL;
  263. len = strfilter_node__sprint(filter->root, NULL);
  264. if (len < 0)
  265. return NULL;
  266. ret = malloc(len + 1);
  267. if (ret)
  268. strfilter_node__sprint(filter->root, ret);
  269. return ret;
  270. }