2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/endian.h>
43 #include "hast_compression.h"
46 allzeros(const void *data, size_t size)
48 const uint64_t *p = data;
52 PJDLOG_ASSERT((size % sizeof(*p)) == 0);
55 * This is the fastest method I found for checking if the given
56 * buffer contain all zeros.
57 * Because inside the loop we don't check at every step, we would
58 * get an answer only after walking through entire buffer.
59 * To return early if the buffer doesn't contain all zeros, we probe
60 * 8 bytes at the beginning, in the middle and at the end of the buffer
64 size >>= 3; /* divide by 8 */
65 if ((p[0] | p[size >> 1] | p[size - 1]) != 0)
68 for (i = 0; i < size; i++)
74 hast_hole_compress(const unsigned char *data, size_t *sizep)
79 if (!allzeros(data, *sizep))
82 newbuf = malloc(sizeof(size));
84 pjdlog_warning("Unable to compress (no memory: %zu).",
88 size = htole32((uint32_t)*sizep);
89 bcopy(&size, newbuf, sizeof(size));
90 *sizep = sizeof(size);
96 hast_hole_decompress(const unsigned char *data, size_t *sizep)
101 if (*sizep != sizeof(size)) {
102 pjdlog_error("Unable to decompress (invalid size: %zu).",
107 bcopy(data, &size, sizeof(size));
108 size = le32toh(size);
110 newbuf = malloc(size);
111 if (newbuf == NULL) {
112 pjdlog_error("Unable to decompress (no memory: %zu).",
122 /* Minimum block size to try to compress. */
123 #define HAST_LZF_COMPRESS_MIN 1024
126 hast_lzf_compress(const unsigned char *data, size_t *sizep)
128 unsigned char *newbuf;
134 if (origsize <= HAST_LZF_COMPRESS_MIN)
137 newsize = sizeof(origsize) + origsize - HAST_LZF_COMPRESS_MIN;
138 newbuf = malloc(newsize);
139 if (newbuf == NULL) {
140 pjdlog_warning("Unable to compress (no memory: %zu).",
144 newsize = lzf_compress(data, *sizep, newbuf + sizeof(origsize),
145 newsize - sizeof(origsize));
150 origsize = htole32(origsize);
151 bcopy(&origsize, newbuf, sizeof(origsize));
153 *sizep = sizeof(origsize) + newsize;
158 hast_lzf_decompress(const unsigned char *data, size_t *sizep)
160 unsigned char *newbuf;
164 PJDLOG_ASSERT(*sizep > sizeof(origsize));
166 bcopy(data, &origsize, sizeof(origsize));
167 origsize = le32toh(origsize);
168 PJDLOG_ASSERT(origsize > HAST_LZF_COMPRESS_MIN);
170 newbuf = malloc(origsize);
171 if (newbuf == NULL) {
172 pjdlog_error("Unable to decompress (no memory: %zu).",
176 newsize = lzf_decompress(data + sizeof(origsize),
177 *sizep - sizeof(origsize), newbuf, origsize);
180 pjdlog_error("Unable to decompress.");
183 PJDLOG_ASSERT(newsize == origsize);
190 compression_name(int num)
194 case HAST_COMPRESSION_NONE:
196 case HAST_COMPRESSION_HOLE:
198 case HAST_COMPRESSION_LZF:
205 compression_send(const struct hast_resource *res, struct nv *nv, void **datap,
206 size_t *sizep, bool *freedatap)
208 unsigned char *newbuf;
213 compression = res->hr_compression;
215 switch (compression) {
216 case HAST_COMPRESSION_NONE:
218 case HAST_COMPRESSION_HOLE:
219 newbuf = hast_hole_compress(*datap, &size);
221 case HAST_COMPRESSION_LZF:
222 /* Try 'hole' compression first. */
223 newbuf = hast_hole_compress(*datap, &size);
225 compression = HAST_COMPRESSION_HOLE;
227 newbuf = hast_lzf_compress(*datap, &size);
230 PJDLOG_ABORT("Invalid compression: %d.", res->hr_compression);
233 if (newbuf == NULL) {
234 /* Unable to compress the data. */
237 nv_add_string(nv, compression_name(compression), "compression");
238 if (nv_error(nv) != 0) {
240 errno = nv_error(nv);
253 compression_recv(const struct hast_resource *res __unused, struct nv *nv,
254 void **datap, size_t *sizep, bool *freedatap)
256 unsigned char *newbuf;
260 algo = nv_get_string(nv, "compression");
262 return (0); /* No compression. */
267 if (strcmp(algo, "hole") == 0)
268 newbuf = hast_hole_decompress(*datap, &size);
269 else if (strcmp(algo, "lzf") == 0)
270 newbuf = hast_lzf_decompress(*datap, &size);
272 pjdlog_error("Unknown compression algorithm '%s'.", algo);
273 return (-1); /* Unknown compression algorithm. */