]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
zlib: move to own file
authorSven Wegener <sven.wegener@stealer.net>
Mon, 9 Nov 2015 19:44:51 +0000 (20:44 +0100)
committerJaroslav Kysela <perex@perex.cz>
Mon, 9 Nov 2015 21:11:16 +0000 (22:11 +0100)
Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
Makefile
src/filebundle.c
src/filebundle.h
src/http.c
src/tvheadend.h
src/zlib.c [new file with mode: 0644]

index d1772a949bf0a57e8576009548e781a248c84c27..f90166fafa64bc7bc7e626de12056a2042665f54 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -211,6 +211,10 @@ SRCS-1 = \
 SRCS = $(SRCS-1)
 I18N-C = $(SRCS-1)
 
+SRCS-ZLIB = \
+       src/zlib.c
+SRCS-${CONFIG_ZLIB} += $(SRCS-ZLIB)
+
 SRCS-UPNP = \
        src/upnp.c
 SRCS-${CONFIG_UPNP} += $(SRCS-UPNP)
index 7d88ea3d88970d7589d79b187659a49e7e83fd6a..5e800a8558bb8774c4b743964b0553b9eef6c103 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
-#if ENABLE_ZLIB
-#define ZLIB_CONST 1
-#include <zlib.h>
-#ifndef z_const
-#define z_const
-#endif
-#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <assert.h>
@@ -73,85 +66,6 @@ struct filebundle_file
   };
 };
 
-/* **************************************************************************
- * Compression/Decompression
- * *************************************************************************/
-
-#if ENABLE_ZLIB
-uint8_t *gzip_inflate ( const uint8_t *data, size_t size, size_t orig )
-{
-  int err;
-  z_stream zstr;
-  uint8_t *bufout;
-
-  /* Setup buffers */
-  bufout = malloc(orig);
-
-  /* Setup zlib */
-  memset(&zstr, 0, sizeof(zstr));
-  inflateInit2(&zstr, MAX_WBITS + 16 /* gzip */);
-  zstr.avail_in  = size;
-  zstr.next_in   = (z_const uint8_t *)data;
-  zstr.avail_out = orig;
-  zstr.next_out  = bufout;
-    
-  /* Decompress */
-  err = inflate(&zstr, Z_NO_FLUSH);
-  if ( err != Z_STREAM_END || zstr.avail_out != 0 ) {
-    free(bufout);
-    bufout = NULL;
-  }
-  inflateEnd(&zstr);
-  
-  return bufout;
-}
-#endif
-
-#if ENABLE_ZLIB
-uint8_t *gzip_deflate ( const uint8_t *data, size_t orig, size_t *size )
-{
-  int err;
-  z_stream zstr;
-  uint8_t *bufout;
-
-  /* Setup buffers */
-  bufout = malloc(orig);
-
-  /* Setup zlib */
-  memset(&zstr, 0, sizeof(zstr));
-  err = deflateInit2(&zstr, Z_BEST_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16 /* gzip */, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
-  zstr.avail_in  = orig;
-  zstr.next_in   = (z_const uint8_t *)data;
-  zstr.avail_out = orig;
-  zstr.next_out  = bufout;
-    
-  /* Compress */
-  while (1) {
-    err = deflate(&zstr, Z_FINISH);
-
-    /* Need more space */
-    if (err == Z_OK && zstr.avail_out == 0) {
-      bufout         = realloc(bufout, zstr.total_out * 2);
-      zstr.avail_out = zstr.total_out;
-      zstr.next_out  = bufout + zstr.total_out;
-      continue;
-    }
-
-    /* Error */
-    if ( (err != Z_STREAM_END && err != Z_OK) || zstr.total_out == 0 ) {
-      free(bufout);
-      bufout = NULL;
-    } else {
-      *size  = zstr.total_out;
-    }
-    break;
-  }
-  deflateEnd(&zstr);
-  
-  return bufout;
-}
-#endif
-
 /* **************************************************************************
  * Miscellanous
  * *************************************************************************/
@@ -395,7 +309,7 @@ fb_file *fb_open2
 #if (ENABLE_ZLIB && ENABLE_BUNDLE)
         ret->gzip = 0;
         ret->size = fb->f.orig;
-        ret->buf  = gzip_inflate(fb->f.data, fb->f.size, fb->f.orig);
+        ret->buf  = tvh_gzip_inflate(fb->f.data, fb->f.size, fb->f.orig);
         if (!ret->buf) {
           free(ret);
           ret = NULL;
@@ -432,12 +346,12 @@ fb_file *fb_open2
     if (ret->type == FB_BUNDLE) {
       const uint8_t *data;
       data     = ret->b.root->f.data;
-      ret->buf = gzip_deflate(data, ret->size, &ret->size);
+      ret->buf = tvh_gzip_deflate(data, ret->size, &ret->size);
     } else {
       uint8_t *data = malloc(ret->size);
       ssize_t c = fread(data, 1, ret->size, ret->d.cur);
       if (c == ret->size)
-        ret->buf = gzip_deflate(data, ret->size, &ret->size);
+        ret->buf = tvh_gzip_deflate(data, ret->size, &ret->size);
       fclose(ret->d.cur);
       ret->d.cur = NULL;
       free(data);
index ea515cfe1590899735521ae23ff365e99aa38d1f..0063234345b88135b2d7f63afcc6195f4b32e8e1 100644 (file)
@@ -82,10 +82,6 @@ extern filebundle_entry_t *filebundle_root;
 
 /* Miscellaneous */
 int fb_stat ( const char *path, struct filebundle_stat *st );
-#if ENABLE_ZLIB
-uint8_t *gzip_deflate ( const uint8_t *data, size_t orig, size_t *size );
-uint8_t *gzip_inflate ( const uint8_t *data, size_t size, size_t orig );
-#endif
 
 /* Directory processing wrappers */
 fb_dir    *fb_opendir  ( const char *path );
index f9214739f4359e1918f4e7831e9e8d6172bcc967..b71c05b503342bc4cef85cfc341d0113406fddb7 100644 (file)
@@ -34,7 +34,6 @@
 #include "tvheadend.h"
 #include "tcp.h"
 #include "http.h"
-#include "filebundle.h"
 #include "access.h"
 #include "notify.h"
 #include "channels.h"
@@ -373,7 +372,7 @@ http_send_reply(http_connection_t *hc, int rc, const char *content,
 #if ENABLE_ZLIB
   if (http_encoding_valid(hc, "gzip") && encoding == NULL && size > 256) {
     uint8_t *data2 = (uint8_t *)htsbuf_to_string(&hc->hc_reply);
-    data = gzip_deflate(data2, size, &size);
+    data = tvh_gzip_deflate(data2, size, &size);
     free(data2);
     encoding = "gzip";
   }
index 86ab28bb2a6d1ced756656170aec01760bb9654e..2527678d605c4b9ddf578d5c38c4461b082ad87e 100644 (file)
@@ -758,6 +758,11 @@ int rmtree ( const char *path );
 
 char *regexp_escape ( const char *str );
 
+#if ENABLE_ZLIB
+uint8_t *tvh_gzip_inflate ( const uint8_t *data, size_t size, size_t orig );
+uint8_t *tvh_gzip_deflate ( const uint8_t *data, size_t orig, size_t *size );
+#endif
+
 /* URL decoding */
 char to_hex(char code);
 char *url_encode(const char *str);
diff --git a/src/zlib.c b/src/zlib.c
new file mode 100644 (file)
index 0000000..9086497
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ *  TV headend - zlib integration
+ *  Copyright (C) 2012 Adam Sutton
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tvheadend.h"
+
+#define ZLIB_CONST 1
+#include <zlib.h>
+#ifndef z_const
+#define z_const
+#endif
+
+/* **************************************************************************
+ * Compression/Decompression
+ * *************************************************************************/
+
+uint8_t *tvh_gzip_inflate ( const uint8_t *data, size_t size, size_t orig )
+{
+  int err;
+  z_stream zstr;
+  uint8_t *bufout;
+
+  /* Setup buffers */
+  bufout = malloc(orig);
+
+  /* Setup zlib */
+  memset(&zstr, 0, sizeof(zstr));
+  inflateInit2(&zstr, MAX_WBITS + 16 /* gzip */);
+  zstr.avail_in  = size;
+  zstr.next_in   = (z_const uint8_t *)data;
+  zstr.avail_out = orig;
+  zstr.next_out  = bufout;
+
+  /* Decompress */
+  err = inflate(&zstr, Z_NO_FLUSH);
+  if ( err != Z_STREAM_END || zstr.avail_out != 0 ) {
+    free(bufout);
+    bufout = NULL;
+  }
+  inflateEnd(&zstr);
+
+  return bufout;
+}
+
+uint8_t *tvh_gzip_deflate ( const uint8_t *data, size_t orig, size_t *size )
+{
+  int err;
+  z_stream zstr;
+  uint8_t *bufout;
+
+  /* Setup buffers */
+  bufout = malloc(orig);
+
+  /* Setup zlib */
+  memset(&zstr, 0, sizeof(zstr));
+  err = deflateInit2(&zstr, Z_BEST_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16 /* gzip */, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
+  zstr.avail_in  = orig;
+  zstr.next_in   = (z_const uint8_t *)data;
+  zstr.avail_out = orig;
+  zstr.next_out  = bufout;
+
+  /* Compress */
+  while (1) {
+    err = deflate(&zstr, Z_FINISH);
+
+    /* Need more space */
+    if (err == Z_OK && zstr.avail_out == 0) {
+      bufout         = realloc(bufout, zstr.total_out * 2);
+      zstr.avail_out = zstr.total_out;
+      zstr.next_out  = bufout + zstr.total_out;
+      continue;
+    }
+
+    /* Error */
+    if ( (err != Z_STREAM_END && err != Z_OK) || zstr.total_out == 0 ) {
+      free(bufout);
+      bufout = NULL;
+    } else {
+      *size  = zstr.total_out;
+    }
+    break;
+  }
+  deflateEnd(&zstr);
+
+  return bufout;
+}