Debian Codegraph @

common/transformation_lzo.c

raw symbols SCIP
1 #include "config.h" // IWYU pragma: keep
2 #include "common/transformation.h"
3
4 #ifdef HAVE_LZO
5 #include <lzo/lzo1x.h>
6 #include <lzo/lzoconf.h>
7 #define TRANSFORM_UNUSED(x) x
8 #else
9 #define TRANSFORM_UNUSED(x) UNUSED(x)
10 #endif /* HAVE_LZO */
11
12 #include <stdlib.h>
13 #include "compat/include/protocols/dumprestore.h"
14 #include "compat/include/bsdcompat.h"
15 // IWYU pragma: no_include "dump_globals.h"
16 #include "common/shared_globals.h" // IWYU pragma: keep
17
18 /*
19 * Initialize
20 */
21 static int
22 lzo_initialize(UNUSED(Transformation *xform), UNUSED(int enc))
23 {
24 return 0;
25 }
26
27 /*
28 * Shut down.
29 */
30 static int
31 lzo_shutdown(UNUSED(Transformation *xform))
32 {
33 return 0;
34 }
35
36 /*
37 * Handle forks.
38 */
39 static int
40 lzo_startNewTape(UNUSED(Transformation *xform), UNUSED(struct tapebuf *tpbin),
41 UNUSED(unsigned long *destlen))
42 {
43 return 0;
44 }
45
46 /*
47 * Start slave process.
48 */
49 static int
50 lzo_startDiskIOProcess(UNUSED(Transformation *xform))
51 {
52 return 0;
53 }
54
55 /*
56 * End slave process.
57 */
58 static int
59 lzo_endDiskIOProcess(Transformation *xform)
60 {
61 if (xform != NULL) {
62 #ifdef HAVE_LZO
63 if (xform->state.lzo.LZO_WorkMem != NULL) {
64 free(xform->state.lzo.LZO_WorkMem);
65 }
66 #endif /* HAVE_LZO */
67 free(xform);
68 }
69
70 return 0;
71 }
72
73 /*
74 * Compress a buffer.
75 */
76 static int
77 lzo_compress(TRANSFORM_UNUSED(Transformation *xform), TRANSFORM_UNUSED(struct tapebuf *tpbin),
78 TRANSFORM_UNUSED(unsigned long *destlen), TRANSFORM_UNUSED(const char *src), TRANSFORM_UNUSED(int srclen))
79 {
80 #ifdef HAVE_LZO
81 lzo_uint *worklen2 = (lzo_uint *) destlen;
82 int compresult;
83
84 compresult = lzo1x_1_compress((const unsigned char*)src, srclen, (unsigned char*)tpbin->buf, worklen2,
85 xform->state.lzo.LZO_WorkMem);
86
87 return compresult == LZO_E_OK ? 1 : 0;
88 #else
89 return 1;
90 #endif /* HAVE_LZO */
91 }
92
93 /*
94 * Decompress a buffer.
95 */
96 static int
97 lzo_decompress(UNUSED(Transformation *xform), TRANSFORM_UNUSED(struct tapebuf *tpbin),
98 TRANSFORM_UNUSED(unsigned long *destlen), TRANSFORM_UNUSED(const char *src), TRANSFORM_UNUSED(int srclen), TRANSFORM_UNUSED(const char **reason))
99 {
100 #ifdef HAVE_LZO
101 int cresult = 0;
102 lzo_uint destlen2 = *destlen;
103 cresult = lzo1x_decompress((const unsigned char*)src, srclen, (unsigned char*)tpbin->buf, &destlen2, NULL);
104 *destlen = destlen2;
105 switch (cresult) {
106 case LZO_E_OK:
107 *reason = "";
108 break;
109 case LZO_E_ERROR:
110 case LZO_E_EOF_NOT_FOUND:
111 *reason = "data error";
112 break;
113 default:
114 *reason = "unknown";
115 }
116
117 return (cresult == LZO_E_OK) ? 1 : 0;
118 #else
119 return 1;
120 #endif /* HAVE_LZO */
121 }
122
123 /*
124 * Factory
125 */
126 Transformation
127 *transformation_lzo_factory(UNUSED(int enc))
128 {
129 #ifdef HAVE_LZO
130 Transformation *t = (Transformation *) malloc(sizeof (Transformation));
131 if (!t) {
132 msg("Cannot allocate Transformation\n");
133 return NULL;
134 }
135
136 t->state.lzo.LZO_WorkMem = malloc(LZO1X_1_MEM_COMPRESS);
137 if (!t->state.lzo.LZO_WorkMem) {
138 msg("couldn't allocate a compress buffer.\n");
139 free(t);
140 return NULL;
141 }
142
143 t->name = "lzo";
144 t->mandatory = 0;
145 t->initialize = &lzo_initialize;
146 t->shutdown = &lzo_shutdown;
147 t->startNewTape = &lzo_startNewTape;
148 t->startDiskIOProcess = &lzo_startDiskIOProcess;
149 t->endDiskIOProcess = &lzo_endDiskIOProcess;
150 t->compress = &lzo_compress;
151 t->decompress = &lzo_decompress;
152
153 return t;
154 #else
155 return NULL;
156 #endif /* HAVE_LZO */
157 }