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