]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Simplify images shingles
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 9 Dec 2016 18:47:15 +0000 (18:47 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 9 Dec 2016 18:47:15 +0000 (18:47 +0000)
src/libutil/shingles.c
src/libutil/shingles.h

index 9d1068c3145567fc73eca51b1031b97186b74833..e957a72ccec1a8c89788b79e3e3bade2c85a5e80 100644 (file)
@@ -16,6 +16,7 @@
 #include "shingles.h"
 #include "fstring.h"
 #include "cryptobox.h"
+#include "images.h"
 
 #define SHINGLES_WINDOW 3
 
@@ -161,7 +162,7 @@ rspamd_shingles_from_text (GArray *input,
 }
 
 struct rspamd_shingle* RSPAMD_OPTIMIZE("unroll-loops")
-rspamd_shingles_from_image (gdouble *dct,
+rspamd_shingles_from_image (guchar *dct,
                const guchar key[16],
                rspamd_mempool_t *pool,
                rspamd_shingles_filter filter,
@@ -173,7 +174,7 @@ rspamd_shingles_from_image (gdouble *dct,
        rspamd_sipkey_t keys[RSPAMD_SHINGLE_SIZE];
        guchar shabuf[rspamd_cryptobox_HASHBYTES], *out_key;
        const guchar *cur_key;
-       gdouble d;
+       guint64 d;
        rspamd_cryptobox_hash_state_t bs;
        guint64 val;
        gint i, j, k;
@@ -194,7 +195,7 @@ rspamd_shingles_from_image (gdouble *dct,
 
        /* Init hashes pipes and keys */
        hashes = g_slice_alloc (sizeof (*hashes) * RSPAMD_SHINGLE_SIZE);
-       hlen = 64 - SHINGLES_WINDOW + 1;
+       hlen = RSPAMD_DCT_LEN / NBBY  + 1;
 
        for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) {
                hashes[i] = g_slice_alloc (hlen * sizeof (guint64));
@@ -232,33 +233,17 @@ rspamd_shingles_from_image (gdouble *dct,
 
        memset (res, 0, sizeof (res));
 
-       for (i = 0; i <=  64; i ++) {
-               if (i - beg >= SHINGLES_WINDOW || i == 64) {
-                       for (j = 0; j < RSPAMD_SHINGLE_SIZE; j ++) {
-                               /* Shift hashes window to right */
-                               for (k = 0; k < SHINGLES_WINDOW - 1; k ++) {
-                                       res[j * SHINGLES_WINDOW + k] =
-                                                       res[j * SHINGLES_WINDOW + k + 1];
-                               }
-
-                               d = dct[beg];
-                               /* Insert the last element to the pipe */
-                               memcpy (&seed, keys[j], sizeof (seed));
-                               res[j * SHINGLES_WINDOW + SHINGLES_WINDOW - 1] =
-                                               rspamd_cryptobox_fast_hash_specific (ht,
-                                                               &d, sizeof (d),
-                                                               seed);
-                               val = 0;
-                               for (k = 0; k < SHINGLES_WINDOW; k ++) {
-                                       val ^= res[j * SHINGLES_WINDOW + k] >>
-                                                       (8 * (SHINGLES_WINDOW - k - 1));
-                               }
-
-                               g_assert (hlen > beg);
-                               hashes[j][beg] = val;
-                       }
-                       beg++;
+       for (i = 0; i <= RSPAMD_DCT_LEN / NBBY; i ++) {
+               for (j = 0; j < RSPAMD_SHINGLE_SIZE; j ++) {
+                       d = dct[beg];
+                       /* Insert the last element to the pipe */
+                       memcpy (&seed, keys[j], sizeof (seed));
+                       val = rspamd_cryptobox_fast_hash_specific (ht,
+                                                       &d, sizeof (d),
+                                                       seed);
+                       hashes[j][beg] = val;
                }
+               beg++;
        }
 
        /* Now we need to filter all hashes and make a shingles result */
@@ -273,7 +258,6 @@ rspamd_shingles_from_image (gdouble *dct,
        return shingle;
 }
 
-
 guint64
 rspamd_shingles_default_filter (guint64 *input, gsize count,
                gint shno, const guchar *key, gpointer ud)
index 5f455cdf85aa299a005ee609284c9fe62711999c..5a107bdc4e567d27dc382bdddb3897f814813aaa 100644 (file)
@@ -67,7 +67,7 @@ struct rspamd_shingle* rspamd_shingles_from_text (GArray *input,
  * @param filterd opaque data for filtering function
  * @return shingles array
  */
-struct rspamd_shingle* rspamd_shingles_from_image (gdouble *dct,
+struct rspamd_shingle* rspamd_shingles_from_image (guchar *dct,
                const guchar key[16],
                rspamd_mempool_t *pool,
                rspamd_shingles_filter filter,