From b7a03ef26dd7b3587979cba5defb67c28303d511 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 20 Mar 2004 23:25:31 +0000 Subject: [PATCH] Interface to our own regex library. --- lib/Makefile | 5 ++ lib/regex.c | 214 ++++++++++++++++++++++++++------------------------- lib/regex.t | 12 +++ 3 files changed, 128 insertions(+), 103 deletions(-) diff --git a/lib/Makefile b/lib/Makefile index 8791d240..c3b93e83 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -11,6 +11,11 @@ LIBSH_MODS=alloc alloc_str ctmatch db fastbuf fb-file fb-mem lists \ finger proctitle ipaccess profile bitsig randomkey \ hashfunc base64 base224 fb-temp fb-mmap fb-printf urlkey \ partmap fb-limfd fb-buffer + +ifdef CONFIG_OWN_REGEX +include lib/regex/Makefile +endif + LIBSH_MOD_PATHS=$(addprefix obj/lib/,$(LIBSH_MODS)) $(CUSTOM_LIB_MODULES) obj/lib/libsh.a: $(addsuffix .o,$(LIBSH_MOD_PATHS)) diff --git a/lib/regex.c b/lib/regex.c index a80ed741..f952333e 100644 --- a/lib/regex.c +++ b/lib/regex.c @@ -16,112 +16,15 @@ #include #include -#if !defined(CONFIG_PCRE) && !defined(CONFIG_POSIX_RE) - -/* BSD regular expression library */ - -#include - -#define INITIAL_MEM 1024 /* Initial space allocated for each pattern */ -#define CHAR_SET_SIZE 256 /* How many characters in the character set. */ - -struct regex { - struct re_pattern_buffer buf; - struct re_registers regs; /* Must not change between re_match() calls */ - int len_cache; -}; - -regex * -rx_compile(byte *p, int icase) -{ - regex *r = xmalloc_zero(sizeof(regex)); - const char *msg; - - r->buf.buffer = xmalloc(INITIAL_MEM); - r->buf.allocated = INITIAL_MEM; - if (icase) - { - unsigned i; - r->buf.translate = xmalloc (CHAR_SET_SIZE); - /* Map uppercase characters to corresponding lowercase ones. */ - for (i = 0; i < CHAR_SET_SIZE; i++) - r->buf.translate[i] = Cupcase(i); - } - else - r->buf.translate = NULL; - re_set_syntax(RE_SYNTAX_POSIX_EXTENDED); - msg = re_compile_pattern(p, strlen(p), &r->buf); - if (!msg) - return r; - die("Error parsing pattern `%s': %s", p, msg); -} - -void -rx_free(regex *r) -{ - xfree(r->buf.buffer); - if (r->buf.translate) - xfree(r->buf.translate); - xfree(r); -} - -int -rx_match(regex *r, byte *s) -{ - int len = strlen(s); - - r->len_cache = len; - if (re_match(&r->buf, s, len, 0, &r->regs) < 0) - return 0; - if (r->regs.start[0] || r->regs.end[0] != len) /* XXX: Why regex doesn't enforce implicit "^...$" ? */ - return 0; - return 1; -} - -int -rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) -{ - byte *end = dest + destlen - 1; - - if (!rx_match(r, src)) - return 0; - - while (*by) - { - if (*by == '\\') - { - by++; - if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */ - { - uns j = *by++ - '0'; - if (j < r->regs.num_regs) - { - byte *s = src + r->regs.start[j]; - uns i = r->regs.end[j] - r->regs.start[j]; - if (r->regs.start[j] > r->len_cache || r->regs.end[j] > r->len_cache) - return -1; - if (dest + i >= end) - return -1; - memcpy(dest, s, i); - dest += i; - continue; - } - } - } - if (dest < end) - *dest++ = *by++; - else - return -1; - } - *dest = 0; - return 1; -} - -#elif defined(CONFIG_POSIX_RE) +#if defined(CONFIG_OWN_REGEX) || defined(CONFIG_POSIX_REGEX) /* POSIX regular expression library */ +#ifdef CONFIG_OWN_REGEX +#include "lib/regex/regex-sh.h" +#else #include +#endif struct regex { regex_t rx; @@ -205,7 +108,7 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) return 1; } -#else +#elif defined(CONFIG_PCRE) /* PCRE library */ @@ -305,6 +208,111 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) return 1; } +#else + +/* BSD regular expression library */ + +#ifdef CONFIG_OWN_BSD_REGEX +#include "lib/regex/regex-sh.h" +#else +#include +#endif + +#define INITIAL_MEM 1024 /* Initial space allocated for each pattern */ +#define CHAR_SET_SIZE 256 /* How many characters in the character set. */ + +struct regex { + struct re_pattern_buffer buf; + struct re_registers regs; /* Must not change between re_match() calls */ + int len_cache; +}; + +regex * +rx_compile(byte *p, int icase) +{ + regex *r = xmalloc_zero(sizeof(regex)); + const char *msg; + + r->buf.buffer = xmalloc(INITIAL_MEM); + r->buf.allocated = INITIAL_MEM; + if (icase) + { + unsigned i; + r->buf.translate = xmalloc (CHAR_SET_SIZE); + /* Map uppercase characters to corresponding lowercase ones. */ + for (i = 0; i < CHAR_SET_SIZE; i++) + r->buf.translate[i] = Cupcase(i); + } + else + r->buf.translate = NULL; + re_set_syntax(RE_SYNTAX_POSIX_EXTENDED); + msg = re_compile_pattern(p, strlen(p), &r->buf); + if (!msg) + return r; + die("Error parsing pattern `%s': %s", p, msg); +} + +void +rx_free(regex *r) +{ + xfree(r->buf.buffer); + if (r->buf.translate) + xfree(r->buf.translate); + xfree(r); +} + +int +rx_match(regex *r, byte *s) +{ + int len = strlen(s); + + r->len_cache = len; + if (re_match(&r->buf, s, len, 0, &r->regs) < 0) + return 0; + if (r->regs.start[0] || r->regs.end[0] != len) /* XXX: Why regex doesn't enforce implicit "^...$" ? */ + return 0; + return 1; +} + +int +rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) +{ + byte *end = dest + destlen - 1; + + if (!rx_match(r, src)) + return 0; + + while (*by) + { + if (*by == '\\') + { + by++; + if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */ + { + uns j = *by++ - '0'; + if (j < r->regs.num_regs) + { + byte *s = src + r->regs.start[j]; + uns i = r->regs.end[j] - r->regs.start[j]; + if (r->regs.start[j] > r->len_cache || r->regs.end[j] > r->len_cache) + return -1; + if (dest + i >= end) + return -1; + memcpy(dest, s, i); + dest += i; + continue; + } + } + } + if (dest < end) + *dest++ = *by++; + else + return -1; + } + *dest = 0; + return 1; +} + #endif #ifdef TEST diff --git a/lib/regex.t b/lib/regex.t index 4cf0d08c..9b47cd99 100644 --- a/lib/regex.t +++ b/lib/regex.t @@ -50,4 +50,16 @@ Out: ab Run: obj/lib/regex-t '.*\?(.*&)*([a-z_]*sess[a-z_]*|random|sid|S_ID|rnd|timestamp|referer)=.*' In: /nemecky/ubytovani/hotel.php?sort=&cislo=26&mena=EUR&typ=Hotel&luz1=ANO&luz2=ANO&luz3=&luz4=&luz5=&maxp1=99999&maxp2=99999&maxp3=99999&maxp4=99999&maxp5=99999&apart=&rada=8,9,10,11,19,22,26,27,28,29,3&cislo=26&mena=EUR&typ=Hotel&luz1=ANO&luz2=ANO&luz3=&luz4=&luz5=&maxp1=99999&maxp2=99999&maxp3=99999&maxp4=99999&maxp5=99999&apart=&rada=8,9,10,11,19,22,26,27,28,29,3&cislo=26&mena=EUR&typ=Hotel&luz1=ANO&luz2=ANO&luz3=&luz4=&luz5=&maxp1=99999&maxp2=99999&maxp3=99999&maxp4=99999&maxp5=99999&apart=&rada=8,9,10,11,19,22,26,27,28,29,3 + /test...?f=1&s=3&sid=123&q=3& Out: NO MATCH + MATCH + +Run: obj/lib/regex-t '.*[0-9a-f]{8,16}.*' +In: abcdabcdabcd + aaaaaaaaaaaaaaaaaaaaaaaaaaaa + asddajlkdkajlqwepoiequwiouio + 000001111p101010101010q12032 +Out: MATCH + MATCH + NO MATCH + MATCH -- 2.39.2