Browse code

add support for (?i). Now regular expressions that begin with (?i) will be case insensitive. (bb #1584, #1598).

git-svn: trunk@5067

Török Edvin authored on 2009/05/15 20:53:22
Showing 6 changed files
... ...
@@ -1,3 +1,11 @@
1
+Fri May 15 14:29:19 EEST 2009 (edwin)
2
+-------------------------------------
3
+ * libclamav/others.h, libclamav/others_common.c,
4
+ libclamav/regex/regcomp.c, libclamav/regex/regex.h,
5
+ unit_tests/check_regex.c: add support for (?i). Now regular
6
+ expressions that begin with (?i) will be case insensitive. (bb
7
+ #1584).
8
+
1 9
 Wed May  6 15:43:27 CEST 2009 (tk)
2 10
 ----------------------------------
3 11
  * docs/signatures.pdf: describe logical signatures;
... ...
@@ -34,6 +34,7 @@
34 34
 #include "clamav.h"
35 35
 #include "dconf.h"
36 36
 #include "libclamunrar_iface/unrar_iface.h"
37
+#include "regex/regex.h"
37 38
 
38 39
 /*
39 40
  * CL_FLEVEL is the signature f-level specific to the current code and
... ...
@@ -382,6 +383,7 @@ const char* cli_ctime(const time_t *timep, char *buf, const size_t bufsize);
382 382
 int cli_checklimits(const char *, cli_ctx *, unsigned long, unsigned long, unsigned long);
383 383
 int cli_updatelimits(cli_ctx *, unsigned long);
384 384
 unsigned long cli_getsizelimit(cli_ctx *, unsigned long);
385
+int cli_regcomp(regex_t *preg, const char *pattern, int cflags);
385 386
 int cli_matchregex(const char *str, const char *regex);
386 387
 
387 388
 /* symlink behaviour */
... ...
@@ -820,3 +820,12 @@ int cli_gentempfd(const char *dir, char **name, int *fd)
820 820
 
821 821
     return CL_SUCCESS;
822 822
 }
823
+
824
+int cli_regcomp(regex_t *preg, const char *pattern, int cflags)
825
+{
826
+    if (!strncmp(pattern, "(?i)", 4)) {
827
+	pattern += 4;
828
+	cflags |= REG_ICASE;
829
+    }
830
+    return cli_regcomp_real(preg, pattern, cflags);
831
+}
... ...
@@ -151,10 +151,10 @@ static int never = 0;		/* for use in asserts; shuts lint up */
151 151
 #endif
152 152
 
153 153
 /*
154
- - cli_regcomp - interface for parser and compilation
154
+ - cli_regcomp_real - interface for parser and compilation
155 155
  */
156 156
 int				/* 0 success, otherwise REG_something */
157
-cli_regcomp(regex_t *preg, const char *pattern, int cflags)
157
+cli_regcomp_real(regex_t *preg, const char *pattern, int cflags)
158 158
 {
159 159
 	struct parse pa;
160 160
 	struct re_guts *g;
... ...
@@ -93,7 +93,7 @@ typedef struct {
93 93
 #define	REG_LARGE	01000	/* force large representation */
94 94
 #define	REG_BACKR	02000	/* force use of backref code */
95 95
 
96
-int	cli_regcomp(regex_t *, const char *, int);
96
+int	cli_regcomp_real(regex_t *, const char *, int);
97 97
 size_t	cli_regerror(int, const regex_t *, char *, size_t);
98 98
 int	cli_regexec(const regex_t *, const char *, size_t, regmatch_t [], int);
99 99
 void	cli_regfree(regex_t *);
... ...
@@ -464,6 +464,30 @@ START_TEST (test_url_canon)
464 464
     fail_unless_fmt(!strcmp(u->path, path), "path incorrect: %s\n", path);
465 465
 }
466 466
 END_TEST
467
+
468
+static struct regex_test {
469
+    const char *regex;
470
+    const char *text;
471
+    int match;
472
+} rg[] = {
473
+    {"\\.exe$", "test.exe", 1},
474
+    {"\\.exe$", "test.eXe", 0},
475
+    {"(?i)\\.exe$", "test.exe", 1},
476
+    {"(?i)\\.exe$", "test.eXe", 1}
477
+};
478
+
479
+START_TEST (test_regexes)
480
+{
481
+    regex_t reg;
482
+    struct regex_test *tst = &rg[_i];
483
+    int match;
484
+
485
+    fail_unless(cli_regcomp(&reg, tst->regex, REG_EXTENDED | REG_NOSUB) == 0, "cli_regcomp");
486
+    match = (cli_regexec(&reg, tst->text, 0, NULL, 0) == REG_NOMATCH) ? 0 : 1;
487
+    fail_unless_fmt(match == tst->match, "cli_regexec failed for %s and %s\n", tst->regex, tst->text);
488
+    cli_regfree(&reg);
489
+}
490
+END_TEST
467 491
 #endif
468 492
 
469 493
 START_TEST(phishing_fake_test)
... ...
@@ -490,7 +514,7 @@ END_TEST
490 490
 Suite *test_regex_suite(void)
491 491
 {
492 492
 	Suite *s = suite_create("regex");
493
-	TCase *tc_api, *tc_matching, *tc_phish, *tc_phish2;
493
+	TCase *tc_api, *tc_matching, *tc_phish, *tc_phish2, *tc_regex;
494 494
 
495 495
 	tc_api = tcase_create("cli_regex2suffix");
496 496
 	suite_add_tcase(s, tc_api);
... ...
@@ -525,6 +549,11 @@ Suite *test_regex_suite(void)
525 525
 	tcase_add_loop_test(tc_phish, test_url_canon, 0, sizeof(uc)/sizeof(uc[0]));
526 526
 #endif
527 527
 
528
+	tc_regex = tcase_create("cli_regcomp/execute");
529
+	suite_add_tcase(s, tc_regex);
530
+#ifdef CHECK_HAVE_LOOPS
531
+	tcase_add_loop_test(tc_regex, test_regexes, 0, sizeof(rg)/sizeof(rg[0]));
532
+#endif
528 533
 	return s;
529 534
 }
530 535