summaryrefslogtreecommitdiff
path: root/ext/compressors/snappy/snappy_compress.c
blob: a86de5c3803206e8e914d26f7ed54347eb0a88c4 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*-
 * Public Domain 2014-2017 MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <snappy-c.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

/*
 * We need to include the configuration file to detect whether this extension
 * is being built into the WiredTiger library; application-loaded compression
 * functions won't need it.
 */
#include <wiredtiger_config.h>

#include <wiredtiger.h>
#include <wiredtiger_ext.h>

#ifdef _MSC_VER
#define	inline	__inline
#endif

/* Local compressor structure. */
typedef struct {
	WT_COMPRESSOR compressor;		/* Must come first */

	WT_EXTENSION_API *wt_api;		/* Extension API */
} SNAPPY_COMPRESSOR;

/*
 * Snappy decompression requires an exact compressed byte count. WiredTiger
 * doesn't track that value, store it in the destination buffer.
 */
#define	SNAPPY_PREFIX	sizeof(uint64_t)

#ifdef WORDS_BIGENDIAN
/*
 * snappy_bswap64 --
 *	64-bit unsigned little-endian to/from big-endian value.
 */
static inline uint64_t
snappy_bswap64(uint64_t v)
{
	return (
	    ((v << 56) & 0xff00000000000000UL) |
	    ((v << 40) & 0x00ff000000000000UL) |
	    ((v << 24) & 0x0000ff0000000000UL) |
	    ((v <<  8) & 0x000000ff00000000UL) |
	    ((v >>  8) & 0x00000000ff000000UL) |
	    ((v >> 24) & 0x0000000000ff0000UL) |
	    ((v >> 40) & 0x000000000000ff00UL) |
	    ((v >> 56) & 0x00000000000000ffUL)
	);
}
#endif

/*
 * snappy_error --
 *	Output an error message, and return a standard error code.
 */
static int
snappy_error(WT_COMPRESSOR *compressor,
    WT_SESSION *session, const char *call, snappy_status snret)
{
	WT_EXTENSION_API *wt_api;
	const char *msg;

	wt_api = ((SNAPPY_COMPRESSOR *)compressor)->wt_api;

	msg = "unknown snappy status error";
	switch (snret) {
	case SNAPPY_BUFFER_TOO_SMALL:
		msg = "SNAPPY_BUFFER_TOO_SMALL";
		break;
	case SNAPPY_INVALID_INPUT:
		msg = "SNAPPY_INVALID_INPUT";
		break;
	case SNAPPY_OK:
		return (0);
	}

	(void)wt_api->err_printf(wt_api,
	    session, "snappy error: %s: %s: %d", call, msg, snret);
	return (WT_ERROR);
}

/*
 * snappy_compression --
 *	WiredTiger snappy compression.
 */
static int
snappy_compression(WT_COMPRESSOR *compressor, WT_SESSION *session,
    uint8_t *src, size_t src_len,
    uint8_t *dst, size_t dst_len,
    size_t *result_lenp, int *compression_failed)
{
	snappy_status snret;
	size_t snaplen;
	uint64_t snaplen_u64;
	char *snapbuf;

	/*
	 * dst_len was computed in snappy_pre_size, so we know it's big enough.
	 * Skip past the space we'll use to store the final count of compressed
	 * bytes.
	 */
	snaplen = dst_len - SNAPPY_PREFIX;
	snapbuf = (char *)dst + SNAPPY_PREFIX;

	/* snaplen is an input and an output arg. */
	snret = snappy_compress((char *)src, src_len, snapbuf, &snaplen);

	if (snret == SNAPPY_OK && snaplen + SNAPPY_PREFIX < src_len) {
		*result_lenp = snaplen + SNAPPY_PREFIX;
		*compression_failed = 0;

		/*
		 * On decompression, snappy requires an exact compressed byte
		 * count (the current value of snaplen). WiredTiger does not
		 * preserve that value, so save snaplen at the beginning of
		 * the destination buffer.
		 *
		 * Store the value in little-endian format.
		 */
		snaplen_u64 = snaplen;
#ifdef WORDS_BIGENDIAN
		snaplen_u64 = snappy_bswap64(snaplen_u64);
#endif
		*(uint64_t *)dst = snaplen_u64;
		return (0);
	}

	*compression_failed = 1;
	return (snret == SNAPPY_OK ?
	    0 : snappy_error(compressor, session, "snappy_compress", snret));
}

/*
 * snappy_decompression --
 *	WiredTiger snappy decompression.
 */
static int
snappy_decompression(WT_COMPRESSOR *compressor, WT_SESSION *session,
    uint8_t *src, size_t src_len,
    uint8_t *dst, size_t dst_len,
    size_t *result_lenp)
{
	WT_EXTENSION_API *wt_api;
	snappy_status snret;
	uint64_t snaplen;

	wt_api = ((SNAPPY_COMPRESSOR *)compressor)->wt_api;

	/*
	 * Retrieve the saved length, handling little- to big-endian conversion
	 * as necessary.
	 */
	snaplen = *(uint64_t *)src;
#ifdef WORDS_BIGENDIAN
	snaplen = snappy_bswap64(snaplen);
#endif
	if (snaplen + SNAPPY_PREFIX > src_len) {
		(void)wt_api->err_printf(wt_api,
		    session,
		    "WT_COMPRESSOR.decompress: stored size exceeds source "
		    "size");
		return (WT_ERROR);
	}

	/* dst_len is an input and an output arg. */
	snret = snappy_uncompress(
	    (char *)src + SNAPPY_PREFIX,
	    (size_t)snaplen, (char *)dst, &dst_len);

	if (snret == SNAPPY_OK) {
		*result_lenp = dst_len;
		return (0);
	}
	return (snappy_error(compressor, session, "snappy_decompress", snret));
}

/*
 * snappy_pre_size --
 *	WiredTiger snappy destination buffer sizing.
 */
static int
snappy_pre_size(WT_COMPRESSOR *compressor, WT_SESSION *session,
    uint8_t *src, size_t src_len,
    size_t *result_lenp)
{
	(void)compressor;			/* Unused parameters */
	(void)session;
	(void)src;

	/*
	 * Snappy requires the dest buffer be somewhat larger than the source.
	 * Fortunately, this is fast to compute, and will give us a dest buffer
	 * in snappy_compress that we can compress to directly.  We add space
	 * in the dest buffer to store the accurate compressed size.
	 */
	*result_lenp = snappy_max_compressed_length(src_len) + SNAPPY_PREFIX;
	return (0);
}

/*
 * snappy_terminate --
 *	WiredTiger snappy compression termination.
 */
static int
snappy_terminate(WT_COMPRESSOR *compressor, WT_SESSION *session)
{
	(void)session;				/* Unused parameters */

	free(compressor);
	return (0);
}

int snappy_extension_init(WT_CONNECTION *, WT_CONFIG_ARG *);

/*
 * snappy_extension_init --
 *	WiredTiger snappy compression extension - called directly when snappy
 * support is built in, or via wiredtiger_extension_init when snappy support
 * is included via extension loading.
 */
int
snappy_extension_init(WT_CONNECTION *connection, WT_CONFIG_ARG *config)
{
	SNAPPY_COMPRESSOR *snappy_compressor;

	(void)config;				/* Unused parameters */

	if ((snappy_compressor = calloc(1, sizeof(SNAPPY_COMPRESSOR))) == NULL)
		return (errno);

	snappy_compressor->compressor.compress = snappy_compression;
	snappy_compressor->compressor.compress_raw = NULL;
	snappy_compressor->compressor.decompress = snappy_decompression;
	snappy_compressor->compressor.pre_size = snappy_pre_size;
	snappy_compressor->compressor.terminate = snappy_terminate;

	snappy_compressor->wt_api = connection->get_extension_api(connection);

	return (connection->add_compressor(
	    connection, "snappy", (WT_COMPRESSOR *)snappy_compressor, NULL));
}

/*
 * We have to remove this symbol when building as a builtin extension otherwise
 * it will conflict with other builtin libraries.
 */
#ifndef	HAVE_BUILTIN_EXTENSION_SNAPPY
/*
 * wiredtiger_extension_init --
 *	WiredTiger snappy compression extension.
 */
int
wiredtiger_extension_init(WT_CONNECTION *connection, WT_CONFIG_ARG *config)
{
	return snappy_extension_init(connection, config);
}
#endif