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