]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Make a more universal gzip compress utility
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 12 Aug 2017 21:38:25 +0000 (22:38 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 12 Aug 2017 21:38:25 +0000 (22:38 +0100)
src/libserver/worker_util.c
src/libutil/util.c
src/libutil/util.h

index 77770ae3f680f3cf30a3b31758d9d5d167323459..2edaa2612b833739a64b2fb497d0881195199677 100644 (file)
@@ -370,64 +370,10 @@ static rspamd_fstring_t *
 rspamd_controller_maybe_compress (struct rspamd_http_connection_entry *entry,
                rspamd_fstring_t *buf, struct rspamd_http_message *msg)
 {
-       z_stream strm;
-       gint rc;
-       rspamd_fstring_t *comp;
-       gchar *p;
-       gsize remain;
-
        if (entry->support_gzip) {
-               memset (&strm, 0, sizeof (strm));
-               rc = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
-                       MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY);
-
-               if (rc != Z_OK) {
-                       return buf;
-               }
-
-               comp = rspamd_fstring_sized_new (MIN (buf->len, 32768));
-
-               strm.avail_in = buf->len;
-               strm.next_in = (guchar *)buf->str;
-               p = comp->str;
-               remain = comp->allocated;
-
-               while (strm.avail_in != 0) {
-                       strm.avail_out = remain;
-                       strm.next_out = p;
-
-                       rc = deflate (&strm, Z_FINISH);
-
-                       if (rc != Z_OK) {
-                               if (rc == Z_STREAM_END) {
-                                       break;
-                               }
-                               else {
-                                       rspamd_fstring_free (comp);
-                                       deflateEnd (&strm);
-
-                                       return buf;
-                               }
-                       }
-
-                       comp->len = strm.total_out;
-
-                       if (strm.avail_out == 0 && strm.avail_in != 0) {
-                               /* Need to allocate more */
-                               remain = comp->len;
-                               comp = rspamd_fstring_grow (comp, comp->allocated +
-                                               strm.avail_in + 10);
-                               p = comp->str + remain;
-                               remain = comp->allocated - remain;
-                       }
+               if (rspamd_fstring_gzip (&buf)) {
+                       rspamd_http_message_add_header (msg, "Content-Encoding", "gzip");
                }
-
-               deflateEnd (&strm);
-               comp->len = strm.total_out;
-               rspamd_fstring_free (buf); /* We replace buf with its compressed version */
-               rspamd_http_message_add_header (msg, "Content-Encoding", "gzip");
-
-               return comp;
        }
 
        return buf;
index 54cacef44a9318cfd74cff379183fdac1e36cdd5..fbd1ba8239e5b6785f44c55e972f58614e9b0976 100644 (file)
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2016 Vsevolod Stakhov
+ * Copyright 2017 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -73,6 +73,7 @@
 #include <math.h> /* for pow */
 
 #include "cryptobox.h"
+#include "zlib.h"
 
 /* Check log messages intensity once per minute */
 #define CHECK_TIME 60
@@ -2688,3 +2689,65 @@ rspamd_tm_to_time (const struct tm *tm, glong tz)
 
        return result;
 }
+
+gboolean
+rspamd_fstring_gzip (rspamd_fstring_t **in)
+{
+       z_stream strm;
+       gint rc;
+       rspamd_fstring_t *comp, *buf = *in;
+       gchar *p;
+       gsize remain;
+
+       memset (&strm, 0, sizeof (strm));
+       rc = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
+                       MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY);
+
+       if (rc != Z_OK) {
+               return FALSE;
+       }
+
+       comp = rspamd_fstring_sized_new (MIN (buf->len, 32768));
+
+       strm.avail_in = buf->len;
+       strm.next_in = (guchar *)buf->str;
+       p = comp->str;
+       remain = comp->allocated;
+
+       while (strm.avail_in != 0) {
+               strm.avail_out = remain;
+               strm.next_out = p;
+
+               rc = deflate (&strm, Z_FINISH);
+
+               if (rc != Z_OK) {
+                       if (rc == Z_STREAM_END) {
+                               break;
+                       }
+                       else {
+                               rspamd_fstring_free (comp);
+                               deflateEnd (&strm);
+
+                               return FALSE;
+                       }
+               }
+
+               comp->len = strm.total_out;
+
+               if (strm.avail_out == 0 && strm.avail_in != 0) {
+                       /* Need to allocate more */
+                       remain = comp->len;
+                       comp = rspamd_fstring_grow (comp, comp->allocated +
+                                       strm.avail_in + 10);
+                       p = comp->str + remain;
+                       remain = comp->allocated - remain;
+               }
+       }
+
+       deflateEnd (&strm);
+       comp->len = strm.total_out;
+       rspamd_fstring_free (buf); /* We replace buf with its compressed version */
+       *in = comp;
+
+       return TRUE;
+}
\ No newline at end of file
index 9f42d6c924353b9b4975485cb580a7e892235253..d1d83716efe31f493996b9e10e1c7e7822959fe1 100644 (file)
@@ -510,5 +510,12 @@ guint64 rspamd_tm_to_time (const struct tm *tm, glong tz);
 
 #define PTR_ARRAY_FOREACH(ar, i, cur) for ((i) = 0; (ar) != NULL && (i) < (ar)->len && (((cur) = g_ptr_array_index((ar), (i))) || 1); ++(i))
 
+/**
+ * Compresses the input string using gzip+zlib. Old string is replaced and freed
+ * if compressed. If not compressed it is untouched.
+ * @param in
+ * @return TRUE if a string has been compressed
+ */
+gboolean rspamd_fstring_gzip (rspamd_fstring_t **in);
 
 #endif