Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
zlib: move to own file
Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
  • Loading branch information
swegener authored and perexg committed Nov 9, 2015
1 parent fe38680 commit ea0983b
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 95 deletions.
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -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)
Expand Down
92 changes: 3 additions & 89 deletions src/filebundle.c
Expand Up @@ -23,13 +23,6 @@
#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>
Expand Down Expand Up @@ -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
* *************************************************************************/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions src/filebundle.h
Expand Up @@ -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 );
Expand Down
3 changes: 1 addition & 2 deletions src/http.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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";
}
Expand Down
5 changes: 5 additions & 0 deletions src/tvheadend.h
Expand Up @@ -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);
Expand Down
100 changes: 100 additions & 0 deletions 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 <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;
}

0 comments on commit ea0983b

Please sign in to comment.