]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/lzjb.c
Fix gcc uninitialized variable warnings
[mirror_zfs.git] / module / zfs / lzjb.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
572e2857 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
34dc7c2f
BB
24 */
25
34dc7c2f 26/*
572e2857 27 * We keep our own copy of this algorithm for 3 main reasons:
428870ff 28 * 1. If we didn't, anyone modifying common/os/compress.c would
34dc7c2f 29 * directly break our on disk format
428870ff 30 * 2. Our version of lzjb does not have a number of checks that the
34dc7c2f 31 * common/os version needs and uses
428870ff
BB
32 * 3. We initialize the lempel to ensure deterministic results,
33 * so that identical blocks can always be deduplicated.
34dc7c2f 34 * In particular, we are adding the "feature" that compress() can
572e2857
BB
35 * take a destination buffer size and returns the compressed length, or the
36 * source length if compression would overflow the destination buffer.
34dc7c2f
BB
37 */
38
39#include <sys/types.h>
40
41#define MATCH_BITS 6
42#define MATCH_MIN 3
43#define MATCH_MAX ((1 << MATCH_BITS) + (MATCH_MIN - 1))
44#define OFFSET_MASK ((1 << (16 - MATCH_BITS)) - 1)
428870ff 45#define LEMPEL_SIZE 1024
34dc7c2f
BB
46
47/*ARGSUSED*/
48size_t
49lzjb_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
50{
51 uchar_t *src = s_start;
52 uchar_t *dst = d_start;
d4ed6673 53 uchar_t *cpy, *copymap = NULL;
34dc7c2f 54 int copymask = 1 << (NBBY - 1);
428870ff 55 int mlen, offset, hash;
34dc7c2f 56 uint16_t *hp;
428870ff 57 uint16_t lempel[LEMPEL_SIZE] = { 0 };
34dc7c2f
BB
58
59 while (src < (uchar_t *)s_start + s_len) {
60 if ((copymask <<= 1) == (1 << NBBY)) {
428870ff 61 if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY)
34dc7c2f 62 return (s_len);
34dc7c2f
BB
63 copymask = 1;
64 copymap = dst;
65 *dst++ = 0;
66 }
67 if (src > (uchar_t *)s_start + s_len - MATCH_MAX) {
68 *dst++ = *src++;
69 continue;
70 }
428870ff
BB
71 hash = (src[0] << 16) + (src[1] << 8) + src[2];
72 hash += hash >> 9;
73 hash += hash >> 5;
74 hp = &lempel[hash & (LEMPEL_SIZE - 1)];
34dc7c2f
BB
75 offset = (intptr_t)(src - *hp) & OFFSET_MASK;
76 *hp = (uint16_t)(uintptr_t)src;
77 cpy = src - offset;
78 if (cpy >= (uchar_t *)s_start && cpy != src &&
79 src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) {
80 *copymap |= copymask;
81 for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++)
82 if (src[mlen] != cpy[mlen])
83 break;
84 *dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) |
85 (offset >> NBBY);
86 *dst++ = (uchar_t)offset;
87 src += mlen;
88 } else {
89 *dst++ = *src++;
90 }
91 }
92 return (dst - (uchar_t *)d_start);
93}
94
95/*ARGSUSED*/
96int
97lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
98{
99 uchar_t *src = s_start;
100 uchar_t *dst = d_start;
101 uchar_t *d_end = (uchar_t *)d_start + d_len;
d4ed6673 102 uchar_t *cpy, copymap = 0;
34dc7c2f
BB
103 int copymask = 1 << (NBBY - 1);
104
105 while (dst < d_end) {
106 if ((copymask <<= 1) == (1 << NBBY)) {
107 copymask = 1;
108 copymap = *src++;
109 }
110 if (copymap & copymask) {
111 int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
112 int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
113 src += 2;
114 if ((cpy = dst - offset) < (uchar_t *)d_start)
115 return (-1);
116 while (--mlen >= 0 && dst < d_end)
117 *dst++ = *cpy++;
118 } else {
119 *dst++ = *src++;
120 }
121 }
122 return (0);
123}