]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Accept `rspamd_text` as long as Lua native strings vstakhov-lua-text-api 5029/head
authorVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 25 Jun 2024 14:31:54 +0000 (15:31 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 25 Jun 2024 14:31:54 +0000 (15:31 +0100)
src/lua/lua_cryptobox.c
src/lua/lua_parsers.c
src/lua/lua_regexp.c
src/lua/lua_task.c

index bad7d7024c628a20fc2897b99b386b45da6b1e55..0cc9245f793d09021831b54b4230f48f6c00c561 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -341,12 +341,13 @@ lua_cryptobox_pubkey_create(lua_State *L)
 {
        LUA_TRACE_POINT;
        struct rspamd_cryptobox_pubkey *pkey = NULL, **ppkey;
-       const char *buf, *arg;
-       gsize len;
+       const char *arg;
        int type = RSPAMD_KEYPAIR_SIGN;
        int alg = RSPAMD_CRYPTOBOX_MODE_25519;
 
-       buf = luaL_checklstring(L, 1, &len);
+       struct rspamd_lua_text *buf;
+
+       buf = lua_check_text_or_string(L, 1);
        if (buf != NULL) {
                if (lua_type(L, 2) == LUA_TSTRING) {
                        /* keypair type */
@@ -371,7 +372,7 @@ lua_cryptobox_pubkey_create(lua_State *L)
                        }
                }
 
-               pkey = rspamd_pubkey_from_base32(buflen, type, alg);
+               pkey = rspamd_pubkey_from_base32(buf->start, buf->len, type, alg);
 
                if (pkey == NULL) {
                        msg_err("cannot load pubkey from string");
@@ -803,26 +804,12 @@ lua_cryptobox_signature_create(lua_State *L)
        LUA_TRACE_POINT;
        rspamd_fstring_t *sig, **psig;
        struct rspamd_lua_text *t;
-       const char *data;
-       gsize dlen;
-
-       if (lua_isuserdata(L, 1)) {
-               t = lua_check_text(L, 1);
 
-               if (!t) {
-                       return luaL_error(L, "invalid arguments");
-               }
-
-               data = t->start;
-               dlen = t->len;
-       }
-       else {
-               data = luaL_checklstring(L, 1, &dlen);
-       }
+       t = lua_check_text_or_string(L, 1);
 
-       if (data != NULL) {
-               if (dlen == rspamd_cryptobox_signature_bytes(RSPAMD_CRYPTOBOX_MODE_25519)) {
-                       sig = rspamd_fstring_new_init(data, dlen);
+       if (t != NULL) {
+               if (t->len == rspamd_cryptobox_signature_bytes(RSPAMD_CRYPTOBOX_MODE_25519)) {
+                       sig = rspamd_fstring_new_init(t->start, t->len);
                        psig = lua_newuserdata(L, sizeof(rspamd_fstring_t *));
                        rspamd_lua_setclass(L, rspamd_cryptobox_signature_classname, -1);
                        *psig = sig;
index f77b369523a82afb66459a550a0198cb7b39c357..babc955be1182485b7fd6b7e1f88489baa7491e5 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2020 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -102,8 +102,7 @@ static const struct luaL_reg parserslib_f[] = {
 int lua_parsers_tokenize_text(lua_State *L)
 {
        LUA_TRACE_POINT;
-       const char *in = NULL;
-       gsize len = 0, pos, ex_len, i;
+       gsize pos, ex_len, i;
        GList *exceptions = NULL, *cur;
        struct rspamd_lua_text *t;
        struct rspamd_process_exception *ex;
@@ -111,21 +110,10 @@ int lua_parsers_tokenize_text(lua_State *L)
        GArray *res;
        rspamd_stat_token_t *w;
 
-       if (lua_type(L, 1) == LUA_TSTRING) {
-               in = luaL_checklstring(L, 1, &len);
-       }
-       else if (lua_type(L, 1) == LUA_TUSERDATA) {
-               t = lua_check_text(L, 1);
-
-               if (t) {
-                       in = t->start;
-                       len = t->len;
-               }
-       }
+       t = lua_check_text_or_string(L, 1);
 
-       if (in == NULL) {
-               lua_pushnil(L);
-               return 1;
+       if (t == NULL) {
+               return luaL_error(L, "invalid arguments");
        }
 
        if (lua_gettop(L) > 1 && lua_type(L, 2) == LUA_TTABLE) {
@@ -161,11 +149,11 @@ int lua_parsers_tokenize_text(lua_State *L)
 
        UErrorCode uc_err = U_ZERO_ERROR;
        utext_openUTF8(&utxt,
-                                  in,
-                                  len,
+                                  t->start,
+                                  t->len,
                                   &uc_err);
 
-       res = rspamd_tokenize_text((char *) in, len,
+       res = rspamd_tokenize_text((char *) t->start, t->len,
                                                           &utxt,
                                                           RSPAMD_TOKENIZE_UTF, NULL,
                                                           exceptions,
index 4a209057e9870c9f098dd25799bc45aa8b39303f..ad45a594f3a4c71decec720824be01ff2e88a7f1 100644 (file)
@@ -147,27 +147,26 @@ lua_regexp_import_glob(lua_State *L)
        LUA_TRACE_POINT;
        rspamd_regexp_t *re;
        struct rspamd_lua_regexp *new, **pnew;
-       const char *string, *flags_str = NULL;
+       const char *flags_str = NULL;
        char *escaped;
-       gsize pat_len;
        GError *err = NULL;
 
-       string = luaL_checklstring(L, 1, &pat_len);
-
-       if (lua_gettop(L) == 2) {
-               flags_str = luaL_checkstring(L, 2);
-       }
+       struct rspamd_lua_text *t = lua_check_text(L, 1);
 
-       if (string) {
-               escaped = rspamd_str_regexp_escape(string, pat_len, NULL,
+       if (t) {
+               if (lua_gettop(L) == 2) {
+                       flags_str = luaL_checkstring(L, 2);
+               }
+               escaped = rspamd_str_regexp_escape(t->start, t->len, NULL,
                                                                                   RSPAMD_REGEXP_ESCAPE_GLOB | RSPAMD_REGEXP_ESCAPE_UTF);
 
                re = rspamd_regexp_new(escaped, flags_str, &err);
 
                if (re == NULL) {
                        lua_pushnil(L);
-                       msg_info("cannot parse regexp: %s, error: %s",
-                                        string,
+                       msg_info("cannot parse regexp: %*s, error: %s",
+                                        (int) t->len,
+                                        t->start,
                                         err == NULL ? "undefined" : err->message);
                        g_error_free(err);
                        g_free(escaped);
index dc41d4ab78583da09d5cb961d8097693189094cc..ae045e4584047f19c7436c0e524b575660375c02 100644 (file)
@@ -1809,13 +1809,11 @@ lua_task_load_from_string(lua_State *L)
 {
        LUA_TRACE_POINT;
        struct rspamd_task *task = NULL, **ptask;
-       const char *str_message;
-       gsize message_len;
        struct rspamd_config *cfg = NULL;
 
-       str_message = luaL_checklstring(L, 1, &message_len);
+       struct rspamd_lua_text *t = lua_check_text_or_string(L, 1);
 
-       if (str_message) {
+       if (t) {
 
                if (lua_type(L, 2) == LUA_TUSERDATA) {
                        gpointer p;
@@ -1827,9 +1825,9 @@ lua_task_load_from_string(lua_State *L)
                }
 
                task = rspamd_task_new(NULL, cfg, NULL, NULL, NULL, FALSE);
-               task->msg.begin = g_malloc(message_len);
-               memcpy((char *) task->msg.begin, str_message, message_len);
-               task->msg.len = message_len;
+               task->msg.begin = g_malloc(t->len);
+               memcpy((char *) task->msg.begin, t->start, t->len);
+               task->msg.len = t->len;
                rspamd_mempool_add_destructor(task->task_pool, lua_task_free_dtor,
                                                                          (gpointer) task->msg.begin);
        }
@@ -2642,7 +2640,7 @@ struct rspamd_url_query_to_inject_cbd {
 
 static gboolean
 inject_url_query_callback(struct rspamd_url *url, gsize start_offset,
-                                               gsize end_offset, gpointer ud)
+                                                 gsize end_offset, gpointer ud)
 {
        struct rspamd_url_query_to_inject_cbd *cbd =
                (struct rspamd_url_query_to_inject_cbd *) ud;
@@ -2661,7 +2659,7 @@ inject_url_query_callback(struct rspamd_url *url, gsize start_offset,
 
 static void
 inject_url_query(struct rspamd_task *task, struct rspamd_url *url,
-                                          GPtrArray *part_urls)
+                                GPtrArray *part_urls)
 {
        if (url->querylen > 0) {
                struct rspamd_url_query_to_inject_cbd cbd;
@@ -2692,11 +2690,11 @@ lua_task_inject_url(lua_State *L)
        if (lua_isuserdata(L, 3)) {
                /* We also have a mime part there */
                mpart = *((struct rspamd_mime_part **)
-                                                          rspamd_lua_check_udata_maybe(L, 3, rspamd_mimepart_classname));
+                                         rspamd_lua_check_udata_maybe(L, 3, rspamd_mimepart_classname));
        }
        if (task && task->message && url && url->url) {
                if (rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), url->url, false)) {
-                       if(mpart && mpart->urls) {
+                       if (mpart && mpart->urls) {
                                inject_url_query(task, url->url, mpart->urls);
                        }
                }