| 1 |
#include "config.h" |
| 2 |
#include "common/transformation.h" |
| 3 |
|
| 4 |
#ifdef HAVE_ZLIB |
| 5 |
#include <zlib.h> |
| 6 |
#define TRANSFORM_UNUSED(x) x |
| 7 |
#else |
| 8 |
#define TRANSFORM_UNUSED(x) UNUSED(x) |
| 9 |
#endif |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <com_err.h> |
| 13 |
#include <ext2fs/ext2_io.h> |
| 14 |
#include "compat/include/protocols/dumprestore.h" |
| 15 |
#include "compat/include/bsdcompat.h" |
| 16 |
|
| 17 |
#include "common/shared_globals.h" |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
static int |
| 23 |
zlib_initialize(UNUSED(Transformation *xform), UNUSED(int enc)) |
| 24 |
{ |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static int |
| 32 |
zlib_shutdown(UNUSED(Transformation *xform)) |
| 33 |
{ |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static int |
| 41 |
zlib_startNewTape(UNUSED(Transformation *xform), UNUSED(struct tapebuf *bin), |
| 42 |
UNUSED(unsigned long *destlen)) |
| 43 |
{ |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static int |
| 51 |
zlib_startDiskIOProcess(UNUSED(Transformation *xform)) |
| 52 |
{ |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static int |
| 60 |
zlib_endDiskIOProcess(Transformation *xform) |
| 61 |
{ |
| 62 |
if (xform != NULL) { |
| 63 |
free(xform); |
| 64 |
} |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
struct req { |
| 70 |
ext2_loff_t dblk; |
| 71 |
int count; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static int |
| 78 |
zlib_compress(TRANSFORM_UNUSED(Transformation *xform), TRANSFORM_UNUSED(struct tapebuf *tpbin), |
| 79 |
TRANSFORM_UNUSED(unsigned long *destlen), TRANSFORM_UNUSED(const char *src), TRANSFORM_UNUSED(int srclen)) |
| 80 |
{ |
| 81 |
#ifdef HAVE_ZLIB |
| 82 |
int compresult; |
| 83 |
compresult = compress2((unsigned char*)tpbin->buf, destlen, (const unsigned char*)src, srclen, xform->state.zlib.complvl); |
| 84 |
return compresult == Z_OK ? 1 : 0; |
| 85 |
#else |
| 86 |
return 1; |
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static int |
| 94 |
zlib_decompress(UNUSED(Transformation *xform), TRANSFORM_UNUSED(struct tapebuf *tpbin), |
| 95 |
TRANSFORM_UNUSED(unsigned long *destlen), TRANSFORM_UNUSED(const char *src), TRANSFORM_UNUSED(int srclen), TRANSFORM_UNUSED(const char **reason)) |
| 96 |
{ |
| 97 |
#ifdef HAVE_ZLIB |
| 98 |
int cresult; |
| 99 |
cresult = uncompress((unsigned char*)tpbin->buf, destlen, (const unsigned char*)src, srclen); |
| 100 |
switch (cresult) { |
| 101 |
case Z_OK: |
| 102 |
*reason = ""; |
| 103 |
break; |
| 104 |
case Z_MEM_ERROR: |
| 105 |
*reason = "not enough memory"; |
| 106 |
break; |
| 107 |
case Z_BUF_ERROR: |
| 108 |
*reason = "buffer too small"; |
| 109 |
break; |
| 110 |
case Z_DATA_ERROR: |
| 111 |
*reason = "data error"; |
| 112 |
break; |
| 113 |
default: |
| 114 |
*reason = "unknown"; |
| 115 |
} |
| 116 |
return (cresult == Z_OK) ? 1 : 0; |
| 117 |
#else |
| 118 |
return 1; |
| 119 |
#endif |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
Transformation |
| 127 |
*transformation_zlib_factory(int enc, TRANSFORM_UNUSED(int complvl)) |
| 128 |
{ |
| 129 |
#ifdef HAVE_ZLIB |
| 130 |
Transformation *t = (Transformation *) malloc(sizeof (Transformation)); |
| 131 |
if (!t) { |
| 132 |
msg("Cannot allocate Transformation\n"); |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
t->enc = enc; |
| 137 |
t->state.zlib.complvl = complvl; |
| 138 |
|
| 139 |
t->name = "zlib"; |
| 140 |
t->mandatory = 0; |
| 141 |
t->initialize = &zlib_initialize; |
| 142 |
t->shutdown = &zlib_shutdown; |
| 143 |
t->startNewTape = &zlib_startNewTape; |
| 144 |
t->startDiskIOProcess = &zlib_startDiskIOProcess; |
| 145 |
t->endDiskIOProcess = &zlib_endDiskIOProcess; |
| 146 |
t->compress = &zlib_compress; |
| 147 |
t->decompress = &zlib_decompress; |
| 148 |
|
| 149 |
return t; |
| 150 |
#else |
| 151 |
return NULL; |
| 152 |
#endif |
| 153 |
} |