aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-05-07 18:16:34 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-05-07 18:16:34 +0100
commitc37c80afb15d4d7327d4caf2992b586161343eb0 (patch)
tree3c26412a6b6e334b68168fc504766ad3f68b10ed /src
parent1388a2607685646b3898c5135a62bdf4ba1955fb (diff)
downloadrspamd-c37c80afb15d4d7327d4caf2992b586161343eb0.tar.gz
rspamd-c37c80afb15d4d7327d4caf2992b586161343eb0.zip
Add function regexp:set_limit
With this function it will be possible to limit the maximum size of text to search using this regexp.
Diffstat (limited to 'src')
-rw-r--r--src/lua/lua_regexp.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/lua/lua_regexp.c b/src/lua/lua_regexp.c
index d8b3c8147..221d8d308 100644
--- a/src/lua/lua_regexp.c
+++ b/src/lua/lua_regexp.c
@@ -41,6 +41,7 @@ LUA_FUNCTION_DEF (regexp, create);
LUA_FUNCTION_DEF (regexp, create_cached);
LUA_FUNCTION_DEF (regexp, get_cached);
LUA_FUNCTION_DEF (regexp, get_pattern);
+LUA_FUNCTION_DEF (regexp, set_limit);
LUA_FUNCTION_DEF (regexp, search);
LUA_FUNCTION_DEF (regexp, match);
LUA_FUNCTION_DEF (regexp, matchn);
@@ -50,6 +51,7 @@ LUA_FUNCTION_DEF (regexp, gc);
static const struct luaL_reg regexplib_m[] = {
LUA_INTERFACE_DEF (regexp, get_pattern),
+ LUA_INTERFACE_DEF (regexp, set_limit),
LUA_INTERFACE_DEF (regexp, match),
LUA_INTERFACE_DEF (regexp, matchn),
LUA_INTERFACE_DEF (regexp, search),
@@ -74,6 +76,7 @@ rspamd_mempool_t *regexp_static_pool = NULL;
struct rspamd_lua_regexp {
rspamd_regexp_t *re;
gchar *re_pattern;
+ gsize match_limit;
gint re_flags;
};
@@ -245,6 +248,32 @@ lua_regexp_get_pattern (lua_State *L)
}
/***
+ * @method re:set_limit(lim)
+ * Set maximum size of text length to be matched with this regexp (if `lim` is
+ * less or equal to zero then all texts are checked)
+ * @param {number} lim limit in bytes
+ */
+static int
+lua_regexp_set_limit (lua_State *L)
+{
+ struct rspamd_lua_regexp *re = lua_check_regexp (L);
+ gint64 lim;
+
+ lim = luaL_checknumber (L, 2);
+
+ if (re && re->re && !IS_DESTROYED (re)) {
+ if (lim > 0) {
+ re->match_limit = lim;
+ }
+ else {
+ re->match_limit = 0;
+ }
+ }
+
+ return 0;
+}
+
+/***
* @method re:search(line)
* Search line in regular expression object. If line matches then this
* function returns the table of captured strings. Otherwise, nil is returned.
@@ -274,6 +303,11 @@ lua_regexp_search (lua_State *L)
if (data) {
lua_newtable (L);
i = 0;
+
+ if (re->match_limit > 0) {
+ len = MIN (len, re->match_limit);
+ }
+
while (rspamd_regexp_search (re->re, data, len, &start, &end, FALSE)) {
lua_pushlstring (L, start, end - start);
lua_rawseti (L, -2, ++i);
@@ -332,6 +366,10 @@ lua_regexp_match (lua_State *L)
}
if (data) {
+ if (re->match_limit > 0) {
+ len = MIN (len, re->match_limit);
+ }
+
if (rspamd_regexp_search (re->re, data, len, NULL, NULL, raw)) {
lua_pushboolean (L, TRUE);
}
@@ -395,6 +433,10 @@ lua_regexp_matchn (lua_State *L)
if (data) {
matches = 0;
+ if (re->match_limit > 0) {
+ len = MIN (len, re->match_limit);
+ }
+
for (;;) {
if (rspamd_regexp_search (re->re, data, len, &start, &end, raw)) {
matches ++;
@@ -442,6 +484,11 @@ lua_regexp_split (lua_State *L)
if (re && !IS_DESTROYED (re)) {
data = luaL_checklstring (L, 2, &len);
+
+ if (re->match_limit > 0) {
+ len = MIN (len, re->match_limit);
+ }
+
if (data) {
lua_newtable (L);
i = 0;