From: Sven Wegener Date: Mon, 9 Nov 2015 19:44:51 +0000 (+0100) Subject: zlib: move to own file X-Git-Tag: v4.2.1~1617 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ea0983b3828d645e15c53b0227fb05df19b1795e;p=thirdparty%2Ftvheadend.git zlib: move to own file Signed-off-by: Sven Wegener --- diff --git a/Makefile b/Makefile index d1772a949..f90166faf 100644 --- 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) diff --git a/src/filebundle.c b/src/filebundle.c index 7d88ea3d8..5e800a855 100644 --- a/src/filebundle.c +++ b/src/filebundle.c @@ -23,13 +23,6 @@ #include #include #include -#if ENABLE_ZLIB -#define ZLIB_CONST 1 -#include -#ifndef z_const -#define z_const -#endif -#endif #include #include #include @@ -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); diff --git a/src/filebundle.h b/src/filebundle.h index ea515cfe1..006323434 100644 --- a/src/filebundle.h +++ b/src/filebundle.h @@ -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 ); diff --git a/src/http.c b/src/http.c index f9214739f..b71c05b50 100644 --- a/src/http.c +++ b/src/http.c @@ -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"; } diff --git a/src/tvheadend.h b/src/tvheadend.h index 86ab28bb2..2527678d6 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -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 index 000000000..908649796 --- /dev/null +++ b/src/zlib.c @@ -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 . + */ + +#include "tvheadend.h" + +#define ZLIB_CONST 1 +#include +#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; +}