summaryrefslogtreecommitdiffstats
path: root/src/lib-compression/compression.c
blob: e562e7390d1da14acb5b24d0e47b61163cc5ee56 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "istream.h"
#include "istream-zlib.h"
#include "ostream-zlib.h"
#include "iostream-lz4.h"
#include "compression.h"

#ifndef HAVE_ZLIB
#  define i_stream_create_gz NULL
#  define o_stream_create_gz NULL
#  define i_stream_create_deflate NULL
#  define o_stream_create_deflate NULL
#  define compression_get_min_level_gz NULL
#  define compression_get_default_level_gz NULL
#  define compression_get_max_level_gz NULL
#endif
#ifndef HAVE_BZLIB
#  define i_stream_create_bz2 NULL
#  define o_stream_create_bz2 NULL
#  define compression_get_min_level_bz2 NULL
#  define compression_get_default_level_bz2 NULL
#  define compression_get_max_level_bz2 NULL
#endif
#ifndef HAVE_LZMA
#  define i_stream_create_lzma NULL
#endif
#ifndef HAVE_LZ4
#  define i_stream_create_lz4 NULL
#  define o_stream_create_lz4 NULL
#  define compression_get_min_level_lz4 NULL
#  define compression_get_default_level_lz4 NULL
#  define compression_get_max_level_lz4 NULL
#endif
#ifndef HAVE_ZSTD
#  define i_stream_create_zstd NULL
#  define o_stream_create_zstd NULL
#  define compression_get_min_level_zstd NULL
#  define compression_get_default_level_zstd NULL
#  define compression_get_max_level_zstd NULL
#endif

static bool is_compressed_zlib(struct istream *input)
{
	const unsigned char *data;
	size_t size;

	/* Peek in to the stream and see if it looks like it's compressed
	   (based on its header). This also means that users can try to exploit
	   security holes in the uncompression library by APPENDing a specially
	   crafted mail. So let's hope zlib is free of holes. */
	if (i_stream_read_bytes(input, &data, &size, 2) <= 0)
		return FALSE;
	i_assert(size >= 2);

	return data[0] == 31 && data[1] == 139;
}

static bool is_compressed_bzlib(struct istream *input)
{
	const unsigned char *data;
	size_t size;

	if (i_stream_read_bytes(input, &data, &size, 4) <= 0)
		return FALSE;
	if (memcmp(data, "BZh", 3) != 0)
		return FALSE;
	if (data[3] < '1' || data[3] > '9')
		return FALSE;
	/* The above is enough to be considered as the bzlib magic.
	   Normally it's followed by data header beginning with 0x31. However,
	   with empty compressed files it's followed by 0x17. */
	return TRUE;
}

static bool is_compressed_xz(struct istream *input)
{
	const unsigned char *data;
	size_t size;

	if (i_stream_read_bytes(input, &data, &size, 6) <= 0)
		return FALSE;
	return memcmp(data, "\xfd\x37\x7a\x58\x5a\x00", 6) == 0;
}

static bool is_compressed_lz4(struct istream *input)
{
	const unsigned char *data;
	size_t size;

	if (i_stream_read_bytes(input, &data, &size, IOSTREAM_LZ4_MAGIC_LEN) <= 0)
		return FALSE;
	/* there is no standard LZ4 header, so we've created our own */
	return memcmp(data, IOSTREAM_LZ4_MAGIC, IOSTREAM_LZ4_MAGIC_LEN) == 0;
}

#define ZSTD_MAGICNUMBER            0xFD2FB528    /* valid since v0.8.0 */
static bool is_compressed_zstd(struct istream *input)
{
	const unsigned char *data;
	size_t size = 0;

	if (i_stream_read_bytes(input, &data, &size, sizeof(uint32_t)) <= 0)
	        return FALSE;
	i_assert(size >= sizeof(uint32_t));

	return le32_to_cpu_unaligned(data) == ZSTD_MAGICNUMBER;
}

int compression_lookup_handler(const char *name,
			       const struct compression_handler **handler_r)
{
	unsigned int i;

	for (i = 0; compression_handlers[i].name != NULL; i++) {
		if (strcmp(name, compression_handlers[i].name) == 0) {
			if (compression_handlers[i].create_istream == NULL ||
			    compression_handlers[i].create_ostream == NULL) {
				/* Handler is known but not compiled in */
				return 0;
			}
			(*handler_r) = &compression_handlers[i];
			return 1;
		}
	}
	return -1;
}

const struct compression_handler *
compression_detect_handler(struct istream *input)
{
	unsigned int i;

	for (i = 0; compression_handlers[i].name != NULL; i++) {
		if (compression_handlers[i].is_compressed != NULL &&
		    compression_handlers[i].is_compressed(input))
			return &compression_handlers[i];
	}
	return NULL;
}

int compression_lookup_handler_from_ext(const char *path,
					const struct compression_handler **handler_r)
{
	unsigned int i;
	size_t len, path_len = strlen(path);

	for (i = 0; compression_handlers[i].name != NULL; i++) {
		if (compression_handlers[i].ext == NULL)
			continue;

		len = strlen(compression_handlers[i].ext);
		if (path_len > len &&
		    strcmp(path + path_len - len, compression_handlers[i].ext) == 0) {
			if (compression_handlers[i].create_istream == NULL ||
			    compression_handlers[i].create_ostream == NULL) {
				/* Handler is known but not compiled in */
				return 0;
			}
			(*handler_r) = &compression_handlers[i];
			return 1;
		}
	}
	return -1;
}

static int compression_get_min_level_unsupported(void)
{
	return -1;
}

static int compression_get_default_level_unsupported(void)
{
	return -1;
}

static int compression_get_max_level_unsupported(void)
{
	return -1;
}

const struct compression_handler compression_handlers[] = {
	{
		.name = "gz",
		.ext = ".gz",
		.is_compressed = is_compressed_zlib,
		.create_istream = i_stream_create_gz,
		.create_ostream = o_stream_create_gz,
		.get_min_level = compression_get_min_level_gz,
		.get_default_level = compression_get_default_level_gz,
		.get_max_level = compression_get_max_level_gz,
	},
	{
		.name = "bz2",
		.ext = ".bz2",
		.is_compressed = is_compressed_bzlib,
		.create_istream = i_stream_create_bz2,
		.create_ostream = o_stream_create_bz2,
		.get_min_level = compression_get_min_level_bz2,
		.get_default_level = compression_get_default_level_bz2,
		.get_max_level = compression_get_max_level_bz2,
	},
	{
		.name = "deflate",
		.ext = NULL,
		.is_compressed = NULL,
		.create_istream = i_stream_create_deflate,
		.create_ostream = o_stream_create_deflate,
		.get_min_level = compression_get_min_level_gz,
		.get_default_level = compression_get_default_level_gz,
		.get_max_level = compression_get_max_level_gz,
	},
	{
		.name = "xz",
		.ext = ".xz",
		.is_compressed = is_compressed_xz,
		.create_istream = i_stream_create_lzma,
		.create_ostream = NULL,
		.get_min_level = compression_get_min_level_unsupported,
		.get_default_level = compression_get_default_level_unsupported,
		.get_max_level = compression_get_max_level_unsupported,
	},
	{
		.name = "lz4",
		.ext = ".lz4",
		.is_compressed = is_compressed_lz4,
		.create_istream = i_stream_create_lz4,
		.create_ostream = o_stream_create_lz4,
		.get_min_level = compression_get_min_level_lz4, /* does not actually support any of this */
		.get_default_level = compression_get_default_level_lz4,
		.get_max_level = compression_get_max_level_lz4,
	},
	{
		.name = "zstd",
		.ext = ".zstd",
		.is_compressed = is_compressed_zstd,
		.create_istream = i_stream_create_zstd,
		.create_ostream = o_stream_create_zstd,
		.get_min_level = compression_get_min_level_zstd,
		.get_default_level = compression_get_default_level_zstd,
		.get_max_level = compression_get_max_level_zstd,
	},
	{
		.name = "unsupported",
	},
	{
		.name = NULL,
	}
};