Skip to content
Permalink
Browse files

Add gzip compression/decompression filters for C.

  • Loading branch information...
dwsteele committed Aug 14, 2018
1 parent e3ff6b2 commit 6643afe9a86bf617d2e90687130248c4a6ee6c14
@@ -57,6 +57,10 @@
<p>Validate configuration options in a single pass. By pre-calculating and storing the option dependencies in <file>parse.auto.c</file> validation can be completed in a single pass, which is both simpler and faster.</p>
</release-item>

<release-item>
<p>Add gzip compression/decompression filters for C.</p>
</release-item>

<release-item>
<release-item-contributor-list>
<release-item-ideator id="stephen.frost"/>
@@ -73,6 +73,9 @@ my @stryCFile =
'common/type/variant.c',
'common/type/variantList.c',
'common/wait.c',
'compress/gzip.c',
'compress/gzipCompress.c',
'compress/gzipDecompress.c',
'config/config.c',
'config/define.c',
'config/load.c',
@@ -42,7 +42,7 @@ LDPERL = `perl -MExtUtils::Embed -e ldopts`
LDEXTRA =

# Concatenate options for easy usage
LDFLAGS = -lcrypto $(LDPERL) $(LDEXTRA)
LDFLAGS = -lcrypto -lz $(LDPERL) $(LDEXTRA)

####################################################################################################################################
# Install options
@@ -91,6 +91,9 @@ SRCS = \
common/type/variant.c \
common/type/variantList.c \
common/wait.c \
compress/gzip.c \
compress/gzipCompress.c \
compress/gzipDecompress.c \
config/config.c \
config/define.c \
config/load.c \
@@ -246,6 +249,15 @@ common/type/variantList.o: common/type/variantList.c common/debug.h common/error
common/wait.o: common/wait.c common/debug.h common/error.auto.h common/error.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/convert.h common/wait.h
$(CC) $(CFLAGS) -c common/wait.c -o common/wait.o

compress/gzip.o: compress/gzip.c common/debug.h common/error.auto.h common/error.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/convert.h compress/gzip.h
$(CC) $(CFLAGS) -c compress/gzip.c -o compress/gzip.o

compress/gzipCompress.o: compress/gzipCompress.c common/assert.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/variant.h common/type/variantList.h compress/gzip.h compress/gzipCompress.h
$(CC) $(CFLAGS) -c compress/gzipCompress.c -o compress/gzipCompress.o

compress/gzipDecompress.o: compress/gzipDecompress.c common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/variant.h common/type/variantList.h compress/gzip.h compress/gzipDecompress.h
$(CC) $(CFLAGS) -c compress/gzipDecompress.c -o compress/gzipDecompress.o

config/config.o: config/config.c common/assert.h common/debug.h common/error.auto.h common/error.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h config/config.auto.c config/config.auto.h config/config.h config/define.auto.h config/define.h
$(CC) $(CFLAGS) -c config/config.c -o config/config.o

@@ -0,0 +1,102 @@
/***********************************************************************************************************************************
Gzip Common
***********************************************************************************************************************************/
#include <zlib.h>

#include "common/debug.h"
#include "common/memContext.h"
#include "compress/gzip.h"

/***********************************************************************************************************************************
Constants
***********************************************************************************************************************************/
#define WINDOW_BITS 15
#define WANT_GZIP 16

/***********************************************************************************************************************************
Process gzip errors
***********************************************************************************************************************************/
int
gzipError(int error)
{
if (error != Z_OK && error != Z_STREAM_END)
{
const char *errorMsg;
const ErrorType *errorType = &FormatError;

switch (error)
{
// Not exactly an error, but since we are not using custom dictionaries it shouldn't be possible to get this result
case Z_NEED_DICT:
{
errorMsg = "need dictionary";
errorType = &AssertError;
break;
}

// We should not get this error -- included for completeness
case Z_ERRNO:
{
errorMsg = "file error";
errorType = &AssertError;
break;
}

case Z_STREAM_ERROR:
{
errorMsg = "stream error";
break;
}

case Z_DATA_ERROR:
{
errorMsg = "data error";
break;
}

case Z_MEM_ERROR:
{
errorMsg = "insufficient memory";
errorType = &MemoryError;
break;
}

// This error indicates an error in the code -- there should always be space in the buffer
case Z_BUF_ERROR:
{
errorMsg = "no space in buffer";
errorType = &AssertError;
break;
}

case Z_VERSION_ERROR:
{
errorMsg = "incompatible version";
break;
}

default:
{
errorMsg = "unknown error";
errorType = &AssertError;
}
}

THROWP_FMT(errorType, "zlib threw error: [%d] %s", error, errorMsg);
}

return error;
}

/***********************************************************************************************************************************
Get gzip window bits
Window bits define how large the compression window is. Larger window sizes generally result in better compression so we'll always
use the largest size. When raw is specified disable the gzip header and produce raw compressed output (this is indicated by passing
negative window bits).
***********************************************************************************************************************************/
int
gzipWindowBits(bool raw)
{
return raw ? -WINDOW_BITS : WANT_GZIP | WINDOW_BITS;
}
@@ -0,0 +1,13 @@
/***********************************************************************************************************************************
Gzip Common
***********************************************************************************************************************************/
#ifndef COMPRESS_GZIP_H
#define COMPRESS_GZIP_H

/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
int gzipError(int error);
int gzipWindowBits(bool raw);

#endif

0 comments on commit 6643afe

Please sign in to comment.
You can’t perform that action at this time.