X-Git-Url: https://git.wh0rd.org/?p=dump.git;a=blobdiff_plain;f=common%2Ftransformation_zlib.c;fp=common%2Ftransformation_zlib.c;h=3bb904819d7640dcd549d0207e8150313e81f9d9;hp=0000000000000000000000000000000000000000;hb=e3956dfb7715a21919aa66dd4209a2dc1c3c82da;hpb=acf85e7a305b04b699e17104be519912a7ae90f0 diff --git a/common/transformation_zlib.c b/common/transformation_zlib.c new file mode 100644 index 0000000..3bb9048 --- /dev/null +++ b/common/transformation_zlib.c @@ -0,0 +1,136 @@ +#include +#include + +#ifdef HAVE_ZLIB +#include +#endif /* HAVE_ZLIB */ + +#include "transformation.h" + +/* + * Initialize + */ +static int +zlib_initialize(Transformation *xform) +{ + return 0; +} + +/* + * Shut down. + */ +static int +zlib_shutdown(Transformation *xform) +{ + return 0; +} + +/* + * Handle forks. + */ +static int +zlib_startNewTape(Transformation *xform, struct tapebuf *bin, + unsigned long *destlen) +{ + return 0; +} + +/* + * Start slave process + */ +static int +zlib_startDiskIOProcess(Transformation *xform) +{ + return 0; +} + +/* + * End slave process + */ +static int +zlib_endDiskIOProcess(Transformation *xform) +{ + if (xform != NULL) { + free(xform); + } + + return 0; +} + +struct req { + ext2_loff_t dblk; + int count; +}; + +/* + * Compress a buffer. + */ +static int +zlib_compress(Transformation *xform, struct tapebuf *tpbin, + unsigned long *destlen, const char *src, int srclen) +{ +#ifdef HAVE_ZLIB + int compresult; + compresult = compress2(tpbin->buf, destlen, src, srclen, xform->state.zlib.complvl); + return compresult == Z_OK ? 1 : 0; +#else + return 1; +#endif /* HAVE_ZLIB */ +} + +/* + * Decompress a buffer. + */ +static int +zlib_decompress(Transformation *xform, struct tapebuf *tpbin, + unsigned long *destlen, const char *src, int srclen, char **reason) +{ +#ifdef HAVE_ZLIB + int cresult; + cresult = uncompress(tpbin->buf, destlen, src, srclen); + switch (cresult) { + case Z_OK: + *reason = ""; + break; + case Z_MEM_ERROR: + *reason = "not enough memory"; + break; + case Z_BUF_ERROR: + *reason = "buffer too small"; + break; + case Z_DATA_ERROR: + *reason = "data error"; + break; + default: + *reason = "unknown"; + } + return (cresult == Z_OK) ? 1 : 0; +#else + return 1; +#endif /* HAVE_ZLIB */ +} + + +/* + * Factory + */ +Transformation +*transformation_zlib_factory(int enc, int complvl) +{ + Transformation *t = (Transformation *) malloc(sizeof (Transformation)); + + t->enc = enc; + t->state.zlib.complvl = complvl; + + t->name = "zlib"; + t->mandatory = 0; + t->initialize = &zlib_initialize; + t->shutdown = &zlib_shutdown; + t->startNewTape = &zlib_startNewTape; + t->startDiskIOProcess = &zlib_startDiskIOProcess; + t->endDiskIOProcess = &zlib_endDiskIOProcess; + t->compress = &zlib_compress; + t->decompress = &zlib_decompress; + + return t; +}