summaryrefslogtreecommitdiffstats
path: root/sbin/hastd/hast_compression.c
blob: 4f4a93a59ea5ddad51b92a972458d02f143cc1fb (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
/*-
 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/endian.h>

#include <errno.h>
#include <string.h>
#include <strings.h>

#include <hast.h>
#include <lzf.h>
#include <nv.h>
#include <pjdlog.h>

#include "hast_compression.h"

static bool
allzeros(const void *data, size_t size)
{
	const uint64_t *p = data;
	unsigned int i;
	uint64_t v;

	PJDLOG_ASSERT((size % sizeof(*p)) == 0);

	/*
	 * This is the fastest method I found for checking if the given
	 * buffer contain all zeros.
	 * Because inside the loop we don't check at every step, we would
	 * get an answer only after walking through entire buffer.
	 * To return early if the buffer doesn't contain all zeros, we probe
	 * 8 bytes at the begining, in the middle and at the end of the buffer
	 * first.
	 */

	size >>= 3;	/* divide by 8 */
	if ((p[0] | p[size >> 1] | p[size - 1]) != 0)
		return (false);
	v = 0;
	for (i = 0; i < size; i++)
		v |= *p++;
	return (v == 0);
}

static void *
hast_hole_compress(const unsigned char *data, size_t *sizep)
{
	uint32_t size;
	void *newbuf;

	if (!allzeros(data, *sizep))
		return (NULL);

	newbuf = malloc(sizeof(size));
	if (newbuf == NULL) {
		pjdlog_warning("Unable to compress (no memory: %zu).",
		    (size_t)*sizep);
		return (NULL);
	}
	size = htole32((uint32_t)*sizep);
	bcopy(&size, newbuf, sizeof(size));
	*sizep = sizeof(size);

	return (newbuf);
}

static void *
hast_hole_decompress(const unsigned char *data, size_t *sizep)
{
	uint32_t size;
	void *newbuf;

	if (*sizep != sizeof(size)) {
		pjdlog_error("Unable to decompress (invalid size: %zu).",
		    *sizep);
		return (NULL);
	}

	bcopy(data, &size, sizeof(size));
	size = le32toh(size);

	newbuf = malloc(size);
	if (newbuf == NULL) {
		pjdlog_error("Unable to decompress (no memory: %zu).",
		    (size_t)size);
		return (NULL);
	}
	bzero(newbuf, size);
	*sizep = size;

	return (newbuf);
}

/* Minimum block size to try to compress. */
#define	HAST_LZF_COMPRESS_MIN	1024

static void *
hast_lzf_compress(const unsigned char *data, size_t *sizep)
{
	unsigned char *newbuf;
	uint32_t origsize;
	size_t newsize;

	origsize = *sizep;

	if (origsize <= HAST_LZF_COMPRESS_MIN)
		return (NULL);

	newsize = sizeof(origsize) + origsize - HAST_LZF_COMPRESS_MIN;
	newbuf = malloc(newsize);
	if (newbuf == NULL) {
		pjdlog_warning("Unable to compress (no memory: %zu).",
		    newsize);
		return (NULL);
	}
	newsize = lzf_compress(data, *sizep, newbuf + sizeof(origsize),
	    newsize - sizeof(origsize));
	if (newsize == 0) {
		free(newbuf);
		return (NULL);
	}
	origsize = htole32(origsize);
	bcopy(&origsize, newbuf, sizeof(origsize));

	*sizep = sizeof(origsize) + newsize;
	return (newbuf);
}

static void *
hast_lzf_decompress(const unsigned char *data, size_t *sizep)
{
	unsigned char *newbuf;
	uint32_t origsize;
	size_t newsize;

	PJDLOG_ASSERT(*sizep > sizeof(origsize));

	bcopy(data, &origsize, sizeof(origsize));
	origsize = le32toh(origsize);
	PJDLOG_ASSERT(origsize > HAST_LZF_COMPRESS_MIN);

	newbuf = malloc(origsize);
	if (newbuf == NULL) {
		pjdlog_error("Unable to decompress (no memory: %zu).",
		    (size_t)origsize);
		return (NULL);
	}
	newsize = lzf_decompress(data + sizeof(origsize),
	    *sizep - sizeof(origsize), newbuf, origsize);
	if (newsize == 0) {
		free(newbuf);
		pjdlog_error("Unable to decompress.");
		return (NULL);
	}
	PJDLOG_ASSERT(newsize == origsize);

	*sizep = newsize;
	return (newbuf);
}

const char *
compression_name(int num)
{

	switch (num) {
	case HAST_COMPRESSION_NONE:
		return ("none");
	case HAST_COMPRESSION_HOLE:
		return ("hole");
	case HAST_COMPRESSION_LZF:
		return ("lzf");
	}
	return ("unknown");
}

int
compression_send(const struct hast_resource *res, struct nv *nv, void **datap,
    size_t *sizep, bool *freedatap)
{
	unsigned char *newbuf;
	int compression;
	size_t size;

	size = *sizep;
	compression = res->hr_compression;

	switch (compression) {
	case HAST_COMPRESSION_NONE:
		return (0);
	case HAST_COMPRESSION_HOLE:
		newbuf = hast_hole_compress(*datap, &size);
		break;
	case HAST_COMPRESSION_LZF:
		/* Try 'hole' compression first. */
		newbuf = hast_hole_compress(*datap, &size);
		if (newbuf != NULL)
			compression = HAST_COMPRESSION_HOLE;
		else
			newbuf = hast_lzf_compress(*datap, &size);
		break;
	default:
		PJDLOG_ABORT("Invalid compression: %d.", res->hr_compression);
	}

	if (newbuf == NULL) {
		/* Unable to compress the data. */
		return (0);
	}
	nv_add_string(nv, compression_name(compression), "compression");
	if (nv_error(nv) != 0) {
		free(newbuf);
		errno = nv_error(nv);
		return (-1);
	}
	if (*freedatap)
		free(*datap);
	*freedatap = true;
	*datap = newbuf;
	*sizep = size;

	return (0);
}

int
compression_recv(const struct hast_resource *res __unused, struct nv *nv,
    void **datap, size_t *sizep, bool *freedatap)
{
	unsigned char *newbuf;
	const char *algo;
	size_t size;

	algo = nv_get_string(nv, "compression");
	if (algo == NULL)
		return (0);	/* No compression. */

	newbuf = NULL;
	size = *sizep;

	if (strcmp(algo, "hole") == 0)
		newbuf = hast_hole_decompress(*datap, &size);
	else if (strcmp(algo, "lzf") == 0)
		newbuf = hast_lzf_decompress(*datap, &size);
	else {
		pjdlog_error("Unknown compression algorithm '%s'.", algo);
		return (-1);	/* Unknown compression algorithm. */
	}

	if (newbuf == NULL)
		return (-1);
	if (*freedatap)
		free(*datap);
	*freedatap = true;
	*datap = newbuf;
	*sizep = size;

	return (0);
}
OpenPOWER on IntegriCloud