File: transformation_zlib.c

package info (click to toggle)
dump 0.4b52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,076 kB
  • sloc: ansic: 15,394; sh: 5,006; cpp: 3,268; makefile: 183
file content (140 lines) | stat: -rw-r--r-- 2,669 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <config.h>

#ifdef HAVE_ZLIB
#include <zlib.h>
#define TRANSFORM_UNUSED(x) x
#else
#define TRANSFORM_UNUSED(x) UNUSED(x)
#endif /* HAVE_ZLIB */

#include "transformation.h"

/*
 * Initialize
 */
static int
zlib_initialize(UNUSED(Transformation *xform), UNUSED(int enc))
{
	return 0;
}

/*
 * Shut down.
 */
static int
zlib_shutdown(UNUSED(Transformation *xform))
{
	return 0;
}

/*
 * Handle forks.
 */
static int
zlib_startNewTape(UNUSED(Transformation *xform), UNUSED(struct tapebuf *bin),
	UNUSED(unsigned long *destlen))
{
	return 0;
}

/*
 * Start slave process
 */
static int
zlib_startDiskIOProcess(UNUSED(Transformation *xform))
{
	return 0;
}

/*
 * End slave process
 */
static int
zlib_endDiskIOProcess(Transformation *xform)
{
	if (xform != NULL) {
		free(xform);
	}

	return 0;
}

struct req {
	ext2_loff_t dblk;
	int count;
};

/*
 * Compress a buffer.
 */
static int
zlib_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_ZLIB
	int compresult;
	compresult = compress2((unsigned char*)tpbin->buf, destlen, (const unsigned char*)src, srclen, xform->state.zlib.complvl);
	return compresult == Z_OK ? 1 : 0;
#else
	return 1;
#endif /* HAVE_ZLIB */
}

/*
 * Decompress a buffer.
 */
static int
zlib_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(char **reason))
{
#ifdef HAVE_ZLIB
	int cresult;
	cresult = uncompress((unsigned char*)tpbin->buf, destlen, (const unsigned char*)src, srclen);
	switch (cresult) {
		case Z_OK:
			*reason = "";
			break;
		case Z_MEM_ERROR:
			*reason = "not enough memory";
			break;
		case Z_BUF_ERROR:
			*reason = "buffer too small";
			break;
		case Z_DATA_ERROR:
			*reason = "data error";
			break;
		default:
			*reason = "unknown";
	}
	return (cresult == Z_OK) ? 1 : 0;
#else
	return 1;
#endif /* HAVE_ZLIB */
}


/*
 * Factory
 */
Transformation
*transformation_zlib_factory(int enc, TRANSFORM_UNUSED(int complvl))
{
	Transformation *t = (Transformation *) malloc(sizeof (Transformation));

	t->enc = enc;
#ifdef HAVE_ZLIB
	t->state.zlib.complvl = complvl;
#endif

	t->name = "zlib";
	t->mandatory = 0;
	t->initialize = &zlib_initialize;
	t->shutdown = &zlib_shutdown;
	t->startNewTape = &zlib_startNewTape;
	t->startDiskIOProcess = &zlib_startDiskIOProcess;
	t->endDiskIOProcess = &zlib_endDiskIOProcess;
	t->compress = &zlib_compress;
	t->decompress = &zlib_decompress;

	return t;
}