]> source.dussan.org Git - rspamd.git/commitdiff
Start miration to the new strings.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 6 Oct 2015 14:41:16 +0000 (15:41 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 6 Oct 2015 14:41:16 +0000 (15:41 +0100)
src/libserver/task.h
src/libutil/fstring.c
src/libutil/fstring.h
src/libutil/mem_pool.c
src/libutil/printf.c
src/libutil/shingles.c
src/lua/lua_buffer.c
src/lua/lua_rsa.c
src/lua/lua_task.c
src/lua/lua_trie.c
src/lua/lua_util.c

index 16c3d4a70b9af1c0cb8d6f38091e2ceb0c15e106..02f6f9ea0252b91290b1ec8ad19e99a4a45f7ebe 100644 (file)
@@ -128,16 +128,13 @@ struct rspamd_task {
        gchar *hostname;                                                                /**< hostname reported by MTA                                           */
        GHashTable *request_headers;                                    /**< HTTP headers in a request                                          */
        GHashTable *reply_headers;                                              /**< Custom reply headers                                                       */
-       struct {
-               const gchar *start;
-               gsize len;
-       } msg;                                                                                  /**< message buffer                                                                     */
+       rspamd_ftok_t msg;                                                              /**< message buffer                                                                     */
        struct rspamd_http_connection *http_conn;               /**< HTTP server connection                                                     */
        struct rspamd_async_session * s;                                /**< async session object                                                       */
        GMimeMessage *message;                                                  /**< message, parsed with GMime                                         */
        GPtrArray *parts;                                                               /**< list of parsed parts                                                       */
        GPtrArray *text_parts;                                                  /**< list of text parts                                                         */
-       rspamd_fstring_t raw_headers_content;                   /**< list of raw headers                                                        */
+       rspamd_ftok_t raw_headers_content;                              /**< list of raw headers                                                        */
        GPtrArray *received;                                                    /**< list of received headers                                           */
        GHashTable *urls;                                                               /**< list of parsed urls                                                        */
        GHashTable *emails;                                                             /**< list of parsed emails                                                      */
index 9e96aac3ce8ec69a319ddbc7626b224c74580ee9..ba580cd8b57be9182438e32a5f571d06cbbf32cb 100644 (file)
@@ -48,7 +48,7 @@ rspamd_fstring_t *
 rspamd_fstring_sized_new (gsize initial_size)
 {
        rspamd_fstring_t *s;
-       gsize real_size = MAX(default_initial_size, initial_size);
+       gsize real_size = MAX (default_initial_size, initial_size);
 
        g_assert (posix_memalign ((void **)&s, 16, real_size + sizeof (*s)) == 0);
        s->len = 0;
@@ -57,6 +57,20 @@ rspamd_fstring_sized_new (gsize initial_size)
        return s;
 }
 
+rspamd_fstring_t *
+rspamd_fstring_new_init (const gchar *init, gsize len)
+{
+       rspamd_fstring_t *s;
+       gsize real_size = MAX (default_initial_size, len);
+
+       g_assert (posix_memalign ((void **) &s, 16, real_size + sizeof (*s)) == 0);
+       s->len = len;
+       s->allocated = real_size;
+       memcpy (s->str, init, len);
+
+       return s;
+}
+
 void
 rspamd_fstring_free (rspamd_fstring_t *str)
 {
index 9fdf472118fcd296f17e6119130ae5e64f24e8d2..0d257633bbbdd703db4e3db6edcb857f4e13e62f 100644 (file)
@@ -55,6 +55,11 @@ rspamd_fstring_t* rspamd_fstring_new (void);
  */
 rspamd_fstring_t *rspamd_fstring_sized_new (gsize initial_size);
 
+/**
+ * Create new fixed length string and initialize it with the initial data
+ */
+rspamd_fstring_t *rspamd_fstring_new_init (const gchar *init, gsize len);
+
 /**
  * Free fixed length string
  */
index ea5fa0bd83c61a3d6f223a496b4feed0dc02055c..6d8c37223c7333875f933e78026683f22756463b 100644 (file)
@@ -424,7 +424,7 @@ rspamd_mempool_fstrdup (rspamd_mempool_t * pool, const struct f_str_s *src)
        }
 
        newstr = rspamd_mempool_alloc (pool, src->len + 1);
-       memcpy (newstr, src->begin, src->len);
+       memcpy (newstr, src->str, src->len);
        newstr[src->len] = '\0';
        return newstr;
 }
index 328173bd06d1f6edd6e7ce42940a9f5ffa920020..7fe7a9c26ae3a75476525fddfce326cd459ed62e 100644 (file)
@@ -453,7 +453,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
 
                        case 'V':
                                v = va_arg (args, rspamd_fstring_t *);
-                               RSPAMD_PRINTF_APPEND (v->begin, v->len);
+                               RSPAMD_PRINTF_APPEND (v->str, v->len);
 
                                continue;
 
index 776ecfa9635c1da0f8e5d23a3211f9dee8f25413..e3bcc0f1031744a171ea940e019b4ddde4ee3605 100644 (file)
@@ -41,7 +41,7 @@ rspamd_shingles_generate (GArray *input,
        guchar shabuf[BLAKE2B_OUTBYTES], *out_key;
        const guchar *cur_key;
        GString *row;
-       rspamd_fstring_t *word;
+       rspamd_ftok_t *word;
        blake2b_state bs;
        guint64 val;
        gint i, j, beg = 0;
@@ -84,7 +84,7 @@ rspamd_shingles_generate (GArray *input,
        for (i = 0; i <= (gint)input->len; i ++) {
                if (i - beg >= SHINGLES_WINDOW || i == (gint)input->len) {
                        for (j = beg; j < i; j ++) {
-                               word = &g_array_index (input, rspamd_fstring_t, j);
+                               word = &g_array_index (input, rspamd_ftok_t, j);
                                g_string_append_len (row, word->begin, word->len);
                        }
                        beg++;
index 7a23a9579365f122141bcc10ad4fc0bba24200ca..eedd7f05b7740ec9416d6576925cc7315f58272f 100644 (file)
@@ -91,7 +91,7 @@ lua_io_read_cb (rspamd_fstring_t * in, void *arg)
                lua_newuserdata (cbdata->L, sizeof (struct rspamd_io_dispatcher_s *));
        rspamd_lua_setclass (cbdata->L, "rspamd{io_dispatcher}", -1);
        *pdispatcher = cbdata->d;
-       lua_pushlstring (cbdata->L, in->begin, in->len);
+       lua_pushlstring (cbdata->L, in->str, in->len);
 
        if (lua_pcall (cbdata->L, 2, 1, 0) != 0) {
                msg_info ("call to session finalizer failed: %s",
index 394332fd046e751657efc4a323a8b492cbd75d91..a85d0db7150bc6d5eaffe1fb6570201a59a70373 100644 (file)
@@ -298,16 +298,13 @@ lua_rsa_signature_load (lua_State *L)
                        sig = g_malloc (sizeof (rspamd_fstring_t));
                        if (fstat (fd, &st) == -1 ||
                                (data =
-                               mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd,
-                               0)) == MAP_FAILED) {
+                               mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0))
+                                               == MAP_FAILED) {
                                msg_err ("cannot mmap file %s: %s", filename, strerror (errno));
                                lua_pushnil (L);
                        }
                        else {
-                               sig->size = st.st_size;
-                               sig->len = sig->size;
-                               sig->begin = g_malloc (sig->len);
-                               memcpy (sig->begin, data, sig->len);
+                               sig = rspamd_fstring_new_init (data, st.st_size);
                                psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *));
                                rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1);
                                *psig = sig;
@@ -352,7 +349,7 @@ lua_rsa_signature_save (lua_State *L)
                        lua_pushboolean (L, FALSE);
                }
                else {
-                       while (write (fd, sig->begin, sig->len) == -1) {
+                       while (write (fd, sig->str, sig->len) == -1) {
                                if (errno == EINTR) {
                                        continue;
                                }
@@ -378,14 +375,11 @@ lua_rsa_signature_create (lua_State *L)
 {
        rspamd_fstring_t *sig, **psig;
        const gchar *data;
+       gsize dlen;
 
-       data = luaL_checkstring (L, 1);
+       data = luaL_checklstring (L, 1, &dlen);
        if (data != NULL) {
-               sig = g_malloc (sizeof (rspamd_fstring_t));
-               sig->len = strlen (data);
-               sig->size = sig->len;
-               sig->begin = g_malloc (sig->len);
-               memcpy (sig->begin, data, sig->len);
+               sig = rspamd_fstring_new_init (data, dlen);
                psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *));
                rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1);
                *psig = sig;
@@ -399,12 +393,7 @@ lua_rsa_signature_gc (lua_State *L)
 {
        rspamd_fstring_t *sig = lua_check_rsa_sign (L, 1);
 
-       if (sig != NULL) {
-               if (sig->begin != NULL) {
-                       g_free (sig->begin);
-               }
-               g_free (sig);
-       }
+       rspamd_fstring_free (sig);
 
        return 0;
 }
@@ -435,7 +424,7 @@ lua_rsa_verify_memory (lua_State *L)
        if (rsa != NULL && signature != NULL && data != NULL) {
                data_sig = g_compute_checksum_for_string (G_CHECKSUM_SHA256, data, -1);
                ret = RSA_verify (NID_sha1, data_sig, strlen (data_sig),
-                               signature->begin, signature->len, rsa);
+                               signature->str, signature->len, rsa);
                if (ret == 0) {
                        msg_info ("cannot check rsa signature for data: %s",
                                ERR_error_string (ERR_get_error (), NULL));
@@ -496,7 +485,7 @@ lua_rsa_verify_file (lua_State *L)
                                                data,
                                                st.st_size);
                                ret = RSA_verify (NID_sha1, data_sig, strlen (data_sig),
-                                               signature->begin, signature->len, rsa);
+                                               signature->str, signature->len, rsa);
                                if (ret == 0) {
                                        msg_info ("cannot check rsa signature for file: %s, %s",
                                                filename, ERR_error_string (ERR_get_error (), NULL));
@@ -534,26 +523,22 @@ lua_rsa_sign_memory (lua_State *L)
        RSA *rsa;
        rspamd_fstring_t *signature, **psig;
        const gchar *data;
-       gchar *data_sig;
+       guchar *data_sig;
        gint ret;
 
        rsa = lua_check_rsa_privkey (L, 1);
        data = luaL_checkstring (L, 2);
 
        if (rsa != NULL && data != NULL) {
-               signature = g_malloc (sizeof (rspamd_fstring_t));
-               signature->len = RSA_size (rsa);
-               signature->size = signature->len;
-               signature->begin = g_malloc (signature->len);
+               signature = rspamd_fstring_sized_new (RSA_size (rsa));
                data_sig = g_compute_checksum_for_string (G_CHECKSUM_SHA256, data, -1);
                ret = RSA_sign (NID_sha1, data_sig, strlen (data_sig),
-                               signature->begin, (guint *)&signature->len, rsa);
+                               signature->str, (guint *)&signature->len, rsa);
                if (ret == 0) {
                        msg_info ("cannot make a signature for data: %s",
                                ERR_error_string (ERR_get_error (), NULL));
                        lua_pushnil (L);
-                       g_free (signature->begin);
-                       g_free (signature);
+                       rspamd_fstring_free (signature);
                }
                else {
                        psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *));
@@ -607,21 +592,17 @@ lua_rsa_sign_file (lua_State *L)
                                lua_pushnil (L);
                        }
                        else {
-                               signature = g_malloc (sizeof (rspamd_fstring_t));
-                               signature->len = RSA_size (rsa);
-                               signature->size = signature->len;
-                               signature->begin = g_malloc (signature->len);
+                               signature = rspamd_fstring_sized_new (RSA_size (rsa));
                                data_sig = g_compute_checksum_for_string (G_CHECKSUM_SHA256,
                                                data,
                                                st.st_size);
                                ret = RSA_sign (NID_sha1, data_sig, strlen (data_sig),
-                                               signature->begin, (guint *)&signature->len, rsa);
+                                               signature->str, (guint *)&signature->len, rsa);
                                if (ret == 0) {
                                        msg_info ("cannot make a signature for data: %s",
                                                ERR_error_string (ERR_get_error (), NULL));
                                        lua_pushnil (L);
-                                       g_free (signature->begin);
-                                       g_free (signature);
+                                       rspamd_fstring_free (signature);
                                }
                                else {
                                        psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *));
index d52eeb166d1c72677e86d9026ea647352374eb7c..4ef772a5cdfb8b8aceeadda45ff960265e5f5afd 100644 (file)
@@ -638,8 +638,8 @@ lua_task_create_from_buffer (lua_State *L)
                ptask = lua_newuserdata (L, sizeof (gpointer));
                rspamd_lua_setclass (L, "rspamd{task}", -1);
                *ptask = task;
-               task->msg.start = rspamd_mempool_alloc (task->task_pool, len + 1);
-               memcpy ((gpointer)task->msg.start, data, len);
+               task->msg.begin = rspamd_mempool_alloc (task->task_pool, len);
+               memcpy ((gpointer)task->msg.begin, data, len);
                task->msg.len = len;
        }
        return 1;
@@ -883,7 +883,7 @@ lua_task_get_content (lua_State * L)
                t = lua_newuserdata (L, sizeof (*t));
                rspamd_lua_setclass (L, "rspamd{text}", -1);
                t->len = task->msg.len;
-               t->start = task->msg.start;
+               t->start = task->msg.begin;
                t->own = FALSE;
 
                return 1;
index bceda4502b5dcd5429ea7c09702d764298d809ed..b0b1551e307a9f7ec95573e284235a2fe3cee893 100644 (file)
@@ -303,7 +303,7 @@ lua_trie_search_rawmsg (lua_State *L)
        gboolean found = FALSE;
 
        if (trie) {
-               text = task->msg.start;
+               text = task->msg.begin;
                len = task->msg.len;
 
                if (lua_trie_search_str (L, trie, text, len, &state) != 0) {
index f9f71c532c73e812ec645507fd3ca255d3595d0d..3a7d002118083c8ad5541b9193ddb98e99e82ff1 100644 (file)
@@ -244,8 +244,8 @@ lua_util_process_message (lua_State *L)
                task = rspamd_task_new (NULL);
                task->cfg = cfg;
                task->ev_base = base;
-               task->msg.start = rspamd_mempool_alloc (task->task_pool, mlen + 1);
-               rspamd_strlcpy ((gpointer)task->msg.start, message, mlen + 1);
+               task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen);
+               rspamd_strlcpy ((gpointer)task->msg.begin, message, mlen);
                task->msg.len = mlen;
                task->fin_callback = lua_util_task_fin;
                task->fin_arg = &res;
@@ -397,7 +397,7 @@ lua_util_tokenize_text (lua_State *L)
        struct rspamd_lua_text *t;
        struct process_exception *ex;
        GArray *res;
-       rspamd_fstring_t *w;
+       rspamd_ftok_t *w;
        gboolean compat = FALSE, check_sig = FALSE;
 
        if (lua_type (L, 1) == LUA_TSTRING) {
@@ -465,7 +465,7 @@ lua_util_tokenize_text (lua_State *L)
                lua_newtable (L);
 
                for (i = 0; i < res->len; i ++) {
-                       w = &g_array_index (res, rspamd_fstring_t, i);
+                       w = &g_array_index (res, rspamd_ftok_t, i);
                        lua_pushlstring (L, w->begin, w->len);
                        lua_rawseti (L, -2, i + 1);
                }