Skip to content

Commit

Permalink
bzip2, driver param
Browse files Browse the repository at this point in the history
  • Loading branch information
artemg committed Jun 28, 2011
1 parent 4f1d7e5 commit 27266cd
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 65 deletions.
30 changes: 30 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ AS_IF([test "x$with_zlib" = "xyes"], [
AC_DEFINE(HAVE_GZ)
])

dnl
dnl Get bzip2 library and include locations
dnl

AC_ARG_WITH([bzip2],
[AS_HELP_STRING([--with-bzip2], [bzip2 support])],
[],
[with_bzip2=yes])

if test "x${with_bzip2}" = "xyes"; then
have_bzip2=no
AC_SEARCH_LIBS([BZ2_bzBuffToBuffCompress], [bz2], [have_bzip2=yes])

if test "x${have_bzip2}" = xyes; then
AC_CHECK_HEADERS([bzlib.h], [], [have_bzip2=no])
fi

if test "x${have_bzip2}" = xno; then
AC_MSG_WARN([
------------------------------------
Unable to find bzip2 on this system.
Building without it.
------------------------------------])
with_bzip2=no
fi
fi

if test "x${with_bzip2}" = xyes; then
AC_DEFINE([HAVE_BZIP])
fi


dnl Output
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lib_LTLIBRARIES = libral.la
libral_la_SOURCES = main.c ral.c ral_stdc.c ral_gz.c ral_lzo.c ral_snappy.c
libral_la_SOURCES = main.c ral.c ral_stdc.c ral_gz.c ral_lzo.c ral_snappy.c ral_bzip.c
libral_la_CPPFLAGS = $(DEPS_CFLAGS)
libral_la_LIBADD = $(DEPS_LIBS)
include_HEADERS = ral.h
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ int check_cmp(struct file_driver_t *d, const size_t size){
size_t out_len = size;

buf_rnd(in, size);
if( d->compress(tmp, &tmp_len, in, size) != RAL_OK ){
if( d->compress(tmp, &tmp_len, in, size, NULL) != RAL_OK ){
test_name = "compress";
FAIL;
goto err;
}
if( d->uncompress(out, &out_len, tmp, tmp_len) != RAL_OK ){
if( d->uncompress(out, &out_len, tmp, tmp_len, NULL) != RAL_OK ){
test_name = "uncompress";
FAIL;
goto err;
Expand Down
28 changes: 14 additions & 14 deletions src/ral.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ static struct file_driver_t ral_driver_snappy = {
};
#endif

#ifdef HAVE_BZIP2
#include "ral_bzip2.h"
static struct file_driver_t ral_driver_bzip2 = {
&f_bzip2_open,
&f_bzip2_close,
&f_bzop2_seek,
&f_bzip2_tell,
&f_bzip2_read,
&f_bzip2_write,
&f_bzip2_compress,
&f_bzip2_uncompress
#ifdef HAVE_BZIP
#include "ral_bzip.h"
static struct file_driver_t ral_driver_bzip = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
&f_bzip_compress,
&f_bzip_uncompress
};
#endif

Expand All @@ -107,9 +107,9 @@ struct file_driver_t *get_driver(enum drivers_t d){
ret = &ral_driver_snappy;
break;
#endif
#ifdef HAVE_BZIP2
case RAL_BZIP2:
ret = &ral_driver_bzip2;
#ifdef HAVE_BZIP
case RAL_BZIP:
ret = &ral_driver_bzip;
break;
#endif
default:
Expand Down
14 changes: 9 additions & 5 deletions src/ral.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ typedef void fd_t;

enum ral_status{
RAL_OK,
RAL_ERROR
RAL_EINVALID_INPUT,
RAL_EBUFFER_TOO_SMALL,
RAL_ENOT_ENOUGH_MEM,
RAL_EUNKNOWN
};

struct file_driver_t{
Expand All @@ -33,15 +36,16 @@ struct file_driver_t{
off_t (* tell)(fd_t *fd);
size_t(* read)(fd_t *fd, char *buf, size_t size);
size_t(* write)(fd_t *fd, char *buf, size_t size);
enum ral_status (* compress)(char *dest, size_t *destLen, const char *source, size_t sourceLen);
enum ral_status (* uncompress)(char *dest, size_t *destLen, const char *source, size_t sourceLen);
enum ral_status (* compress)(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param);
enum ral_status (* uncompress)(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param);
};

static const char *driver_names[] = {"STDC", "GZ", "LZO", "SNAPPY", "BZIP2", "UNKNOWN"};
enum drivers_t {RAL_STDC, RAL_GZ, RAL_LZO, RAL_SNAPPY, RAL_BZIP2, RAL_END};
static const char *driver_names[] = {"STDC", "GZ", "LZO", "SNAPPY", "BZIP", "UNKNOWN"};
enum drivers_t {RAL_STDC, RAL_GZ, RAL_LZO, RAL_SNAPPY, RAL_BZIP, RAL_END};
struct file_driver_t *get_driver(enum drivers_t d);
const char *get_driver_name(enum drivers_t);
#ifdef __cplusplus
}
#endif
#endif

63 changes: 63 additions & 0 deletions src/ral_bzip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "ral_bzip.h"
#include <bzlib.h>

enum ral_status f_bzip_compress(char *dest, size_t *destLen,
const char *source, size_t sourceLen, void *param){
enum ral_status ret;
static const int default_blockSize100k = 5;
static const int default_verbosity = 0;
static const int default_workFactor = 0;
int res = BZ2_bzBuffToBuffCompress(dest, (unsigned int *)destLen, (char *)source, sourceLen,
default_blockSize100k, default_verbosity, default_workFactor);
switch (res){
case BZ_OK:
ret = RAL_OK;
break;
case BZ_OUTBUFF_FULL:
ret = RAL_EBUFFER_TOO_SMALL;
break;
case BZ_MEM_ERROR:
ret = RAL_ENOT_ENOUGH_MEM;
break;
case BZ_CONFIG_ERROR:
case BZ_PARAM_ERROR:
default:
ret = RAL_EUNKNOWN;
break;
};
return ret;
}

enum ral_status f_bzip_uncompress(char *dest, size_t *destLen,
const char *source, size_t sourceLen, void *param)
{
enum ral_status ret;
static const int default_verbosity = 0;
static const int default_small = 0;
int res = BZ2_bzBuffToBuffDecompress(dest, (unsigned int *)destLen, (char *)source, sourceLen,
default_small, default_verbosity);
switch (res){
case BZ_OK:
ret = RAL_OK;
break;
case BZ_OUTBUFF_FULL:
ret = RAL_EBUFFER_TOO_SMALL;
break;
case BZ_MEM_ERROR:
ret = RAL_ENOT_ENOUGH_MEM;
break;
case BZ_DATA_ERROR:
case BZ_DATA_ERROR_MAGIC:
case BZ_UNEXPECTED_EOF:
ret = RAL_EINVALID_INPUT;
break;
case BZ_CONFIG_ERROR:
case BZ_PARAM_ERROR:
default:
ret = RAL_EUNKNOWN;
break;
};
return ret;
}


9 changes: 9 additions & 0 deletions src/ral_bzip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef RAL_BZIP_H
#define RAL_BZIP_H

#include "ral.h"
enum ral_status f_bzip_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param);
enum ral_status f_bzip_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param);

#endif

47 changes: 38 additions & 9 deletions src/ral_gz.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,46 @@ size_t f_gz_write(fd_t *fd, char *buf, size_t size){
return gzwrite(fd, buf, size);
}

enum ral_status f_gz_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen){
if( compress((Bytef *) dest, (uLongf *)destLen, (const Bytef *) source, sourceLen) != Z_OK){
return RAL_ERROR;
enum ral_status f_gz_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param){
enum ral_status ret;
int res = compress((Bytef *) dest, (uLongf *)destLen, (const Bytef *) source, sourceLen);
switch (res){
case Z_OK:
ret = RAL_OK;
break;
case Z_BUF_ERROR:
ret = RAL_EBUFFER_TOO_SMALL;
break;
case Z_MEM_ERROR:
ret = RAL_ENOT_ENOUGH_MEM;
break;
default:
ret = RAL_EUNKNOWN;
break;
}
return RAL_OK;
return ret;
}
enum ral_status f_gz_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen){
if( uncompress((Bytef *) dest, (uLongf *)destLen, (const Bytef *) source, sourceLen) != Z_OK){
return RAL_ERROR;

enum ral_status f_gz_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param){
enum ral_status ret;
int res = uncompress((Bytef *) dest, (uLongf *)destLen, (const Bytef *) source, sourceLen);
switch (res){
case Z_OK:
ret = RAL_OK;
break;
case Z_BUF_ERROR:
ret = RAL_EBUFFER_TOO_SMALL;
break;
case Z_MEM_ERROR:
ret = RAL_ENOT_ENOUGH_MEM;
break;
case Z_DATA_ERROR:
ret = RAL_EINVALID_INPUT;
break;
default:
ret = RAL_EUNKNOWN;
break;
}
return RAL_OK;
return ret;
}


4 changes: 2 additions & 2 deletions src/ral_gz.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ off_t f_gz_seek(fd_t *fd, size_t offset, int whence);
off_t f_gz_tell(fd_t *fd);
size_t f_gz_read(fd_t *fd, char *buf, size_t size);
size_t f_gz_write(fd_t *fd, char *buf, size_t size);
enum ral_status f_gz_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen);
enum ral_status f_gz_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen);
enum ral_status f_gz_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param);
enum ral_status f_gz_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param);

#endif

46 changes: 34 additions & 12 deletions src/ral_lzo.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,50 @@ size_t f_lzo_write(fd_t *fd_, char *buf, size_t size){
}
*/

enum ral_status f_lzo_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen){
enum ral_status f_lzo_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param){
enum ral_status ret;
if (lzo_init() != LZO_E_OK){
return RAL_ERROR;
return RAL_EUNKNOWN;
}
unsigned char *workingMemory = (unsigned char*)malloc(LZO1X_1_11_MEM_COMPRESS);
if( workingMemory == NULL ){
return RAL_ENOT_ENOUGH_MEM;
}

if( lzo1x_1_11_compress((const unsigned char *)source, sourceLen, (unsigned char *)dest, destLen, workingMemory) == LZO_E_OK ){
ret = RAL_OK;
} else {
ret = RAL_ERROR;
int res = lzo1x_1_11_compress((const unsigned char *)source, sourceLen, (unsigned char *)dest, destLen, workingMemory);
switch (res){
case LZO_E_OK:
ret = RAL_OK;
break;
default:
ret = RAL_EUNKNOWN;
break;
}
free(workingMemory);

return ret;
}

enum ral_status f_lzo_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen){
if( lzo1x_decompress( (const unsigned char *)source, sourceLen, (unsigned char *)dest, destLen, NULL) == LZO_E_OK ){
return RAL_OK;
} else {
return RAL_ERROR;
enum ral_status f_lzo_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param){
enum ral_status ret;
int res = lzo1x_decompress( (const unsigned char *)source, sourceLen, (unsigned char *)dest, destLen, NULL);
switch (res){
case LZO_E_OK:
ret = RAL_OK;
break;
case LZO_E_OUTPUT_OVERRUN:
ret = RAL_EBUFFER_TOO_SMALL;
break;
case LZO_E_INPUT_NOT_CONSUMED:
case LZO_E_INPUT_OVERRUN:
case LZO_E_LOOKBEHIND_OVERRUN:
case LZO_E_EOF_NOT_FOUND:
case LZO_E_ERROR:
ret = RAL_EINVALID_INPUT;
break;
default:
ret = RAL_EUNKNOWN;
break;
}
return ret;
}

4 changes: 2 additions & 2 deletions src/ral_lzo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ off_t f_lzo_seek(fd_t *fd, size_t offset, int whence);
off_t f_lzo_tell(fd_t *fd);
size_t f_lzo_read(fd_t *fd, char *buf, size_t size);
size_t f_lzo_write(fd_t *fd, char *buf, size_t size);
enum ral_status f_lzo_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen);
enum ral_status f_lzo_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen);
enum ral_status f_lzo_compress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param);
enum ral_status f_lzo_uncompress(char *dest, size_t *destLen, const char *source, size_t sourceLen, void *param);

#endif

Loading

0 comments on commit 27266cd

Please sign in to comment.