#include "config.h" // IWYU pragma: keep #include "common/transformation.h" #ifdef HAVE_LZO #include #include #define TRANSFORM_UNUSED(x) x #else #define TRANSFORM_UNUSED(x) UNUSED(x) #endif /* HAVE_LZO */ #include #include "compat/include/protocols/dumprestore.h" #include "compat/include/bsdcompat.h" // IWYU pragma: no_include "dump_globals.h" #include "common/shared_globals.h" // IWYU pragma: keep /* * Initialize */ static int lzo_initialize(UNUSED(Transformation *xform), UNUSED(int enc)) { return 0; } /* * Shut down. */ static int lzo_shutdown(UNUSED(Transformation *xform)) { return 0; } /* * Handle forks. */ static int lzo_startNewTape(UNUSED(Transformation *xform), UNUSED(struct tapebuf *tpbin), UNUSED(unsigned long *destlen)) { return 0; } /* * Start slave process. */ static int lzo_startDiskIOProcess(UNUSED(Transformation *xform)) { return 0; } /* * End slave process. */ static int lzo_endDiskIOProcess(Transformation *xform) { if (xform != NULL) { #ifdef HAVE_LZO if (xform->state.lzo.LZO_WorkMem != NULL) { free(xform->state.lzo.LZO_WorkMem); } #endif /* HAVE_LZO */ free(xform); } return 0; } /* * Compress a buffer. */ static int lzo_compress(TRANSFORM_UNUSED(Transformation *xform), TRANSFORM_UNUSED(struct tapebuf *tpbin), TRANSFORM_UNUSED(unsigned long *destlen), TRANSFORM_UNUSED(const char *src), TRANSFORM_UNUSED(int srclen)) { #ifdef HAVE_LZO lzo_uint *worklen2 = (lzo_uint *) destlen; int compresult; compresult = lzo1x_1_compress((const unsigned char*)src, srclen, (unsigned char*)tpbin->buf, worklen2, xform->state.lzo.LZO_WorkMem); return compresult == LZO_E_OK ? 1 : 0; #else return 1; #endif /* HAVE_LZO */ } /* * Decompress a buffer. */ static int lzo_decompress(UNUSED(Transformation *xform), TRANSFORM_UNUSED(struct tapebuf *tpbin), TRANSFORM_UNUSED(unsigned long *destlen), TRANSFORM_UNUSED(const char *src), TRANSFORM_UNUSED(int srclen), TRANSFORM_UNUSED(const char **reason)) { #ifdef HAVE_LZO int cresult = 0; lzo_uint destlen2 = *destlen; cresult = lzo1x_decompress((const unsigned char*)src, srclen, (unsigned char*)tpbin->buf, &destlen2, NULL); *destlen = destlen2; switch (cresult) { case LZO_E_OK: *reason = ""; break; case LZO_E_ERROR: case LZO_E_EOF_NOT_FOUND: *reason = "data error"; break; default: *reason = "unknown"; } return (cresult == LZO_E_OK) ? 1 : 0; #else return 1; #endif /* HAVE_LZO */ } /* * Factory */ Transformation *transformation_lzo_factory(UNUSED(int enc)) { #ifdef HAVE_LZO Transformation *t = (Transformation *) malloc(sizeof (Transformation)); if (!t) { msg("Cannot allocate Transformation\n"); return NULL; } t->state.lzo.LZO_WorkMem = malloc(LZO1X_1_MEM_COMPRESS); if (!t->state.lzo.LZO_WorkMem) { msg("couldn't allocate a compress buffer.\n"); free(t); return NULL; } t->name = "lzo"; t->mandatory = 0; t->initialize = &lzo_initialize; t->shutdown = &lzo_shutdown; t->startNewTape = &lzo_startNewTape; t->startDiskIOProcess = &lzo_startDiskIOProcess; t->endDiskIOProcess = &lzo_endDiskIOProcess; t->compress = &lzo_compress; t->decompress = &lzo_decompress; return t; #else return NULL; #endif /* HAVE_LZO */ }