]> git.wh0rd.org - dump.git/blame - common/transformation_zlib.c
Encryption (and compression as plugins) support.
[dump.git] / common / transformation_zlib.c
CommitLineData
e3956dfb
SP
1#include <stdio.h>
2#include <config.h>
3
4#ifdef HAVE_ZLIB
5#include <zlib.h>
6#endif /* HAVE_ZLIB */
7
8#include "transformation.h"
9
10/*
11 * Initialize
12 */
13static int
14zlib_initialize(Transformation *xform)
15{
16 return 0;
17}
18
19/*
20 * Shut down.
21 */
22static int
23zlib_shutdown(Transformation *xform)
24{
25 return 0;
26}
27
28/*
29 * Handle forks.
30 */
31static int
32zlib_startNewTape(Transformation *xform, struct tapebuf *bin,
33 unsigned long *destlen)
34{
35 return 0;
36}
37
38/*
39 * Start slave process
40 */
41static int
42zlib_startDiskIOProcess(Transformation *xform)
43{
44 return 0;
45}
46
47/*
48 * End slave process
49 */
50static int
51zlib_endDiskIOProcess(Transformation *xform)
52{
53 if (xform != NULL) {
54 free(xform);
55 }
56
57 return 0;
58}
59
60struct req {
61 ext2_loff_t dblk;
62 int count;
63};
64
65/*
66 * Compress a buffer.
67 */
68static int
69zlib_compress(Transformation *xform, struct tapebuf *tpbin,
70 unsigned long *destlen, const char *src, int srclen)
71{
72#ifdef HAVE_ZLIB
73 int compresult;
74 compresult = compress2(tpbin->buf, destlen, src, srclen, xform->state.zlib.complvl);
75 return compresult == Z_OK ? 1 : 0;
76#else
77 return 1;
78#endif /* HAVE_ZLIB */
79}
80
81/*
82 * Decompress a buffer.
83 */
84static int
85zlib_decompress(Transformation *xform, struct tapebuf *tpbin,
86 unsigned long *destlen, const char *src, int srclen, char **reason)
87{
88#ifdef HAVE_ZLIB
89 int cresult;
90 cresult = uncompress(tpbin->buf, destlen, src, srclen);
91 switch (cresult) {
92 case Z_OK:
93 *reason = "";
94 break;
95 case Z_MEM_ERROR:
96 *reason = "not enough memory";
97 break;
98 case Z_BUF_ERROR:
99 *reason = "buffer too small";
100 break;
101 case Z_DATA_ERROR:
102 *reason = "data error";
103 break;
104 default:
105 *reason = "unknown";
106 }
107 return (cresult == Z_OK) ? 1 : 0;
108#else
109 return 1;
110#endif /* HAVE_ZLIB */
111}
112
113
114/*
115 * Factory
116 */
117Transformation
118*transformation_zlib_factory(int enc, int complvl)
119{
120 Transformation *t = (Transformation *) malloc(sizeof (Transformation));
121
122 t->enc = enc;
123 t->state.zlib.complvl = complvl;
124
125 t->name = "zlib";
126 t->mandatory = 0;
127 t->initialize = &zlib_initialize;
128 t->shutdown = &zlib_shutdown;
129 t->startNewTape = &zlib_startNewTape;
130 t->startDiskIOProcess = &zlib_startDiskIOProcess;
131 t->endDiskIOProcess = &zlib_endDiskIOProcess;
132 t->compress = &zlib_compress;
133 t->decompress = &zlib_decompress;
134
135 return t;
136}