]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/lzjb.c
cstyle: Resolve C style issues
[mirror_zfs.git] / module / zfs / lzjb.c
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 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 /*
27 * We keep our own copy of this algorithm for 3 main reasons:
28 * 1. If we didn't, anyone modifying common/os/compress.c would
29 * directly break our on disk format
30 * 2. Our version of lzjb does not have a number of checks that the
31 * common/os version needs and uses
32 * 3. We initialize the lempel to ensure deterministic results,
33 * so that identical blocks can always be deduplicated.
34 * In particular, we are adding the "feature" that compress() can
35 * take a destination buffer size and returns the compressed length, or the
36 * source length if compression would overflow the destination buffer.
37 */
38
39 #include <sys/zfs_context.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)
45 #define LEMPEL_SIZE 1024
46
47 /*ARGSUSED*/
48 size_t
49 lzjb_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;
53 uchar_t *cpy;
54 uchar_t *copymap = NULL;
55 int copymask = 1 << (NBBY - 1);
56 int mlen, offset, hash;
57 uint16_t *hp;
58 uint16_t *lempel;
59
60 lempel = kmem_zalloc(LEMPEL_SIZE * sizeof (uint16_t), KM_PUSHPAGE);
61 while (src < (uchar_t *)s_start + s_len) {
62 if ((copymask <<= 1) == (1 << NBBY)) {
63 if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY) {
64 kmem_free(lempel,
65 LEMPEL_SIZE*sizeof (uint16_t));
66 return (s_len);
67 }
68 copymask = 1;
69 copymap = dst;
70 *dst++ = 0;
71 }
72 if (src > (uchar_t *)s_start + s_len - MATCH_MAX) {
73 *dst++ = *src++;
74 continue;
75 }
76 hash = (src[0] << 16) + (src[1] << 8) + src[2];
77 hash += hash >> 9;
78 hash += hash >> 5;
79 hp = &lempel[hash & (LEMPEL_SIZE - 1)];
80 offset = (intptr_t)(src - *hp) & OFFSET_MASK;
81 *hp = (uint16_t)(uintptr_t)src;
82 cpy = src - offset;
83 if (cpy >= (uchar_t *)s_start && cpy != src &&
84 src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) {
85 *copymap |= copymask;
86 for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++)
87 if (src[mlen] != cpy[mlen])
88 break;
89 *dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) |
90 (offset >> NBBY);
91 *dst++ = (uchar_t)offset;
92 src += mlen;
93 } else {
94 *dst++ = *src++;
95 }
96 }
97
98 kmem_free(lempel, LEMPEL_SIZE * sizeof (uint16_t));
99 return (dst - (uchar_t *)d_start);
100 }
101
102 /*ARGSUSED*/
103 int
104 lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
105 {
106 uchar_t *src = s_start;
107 uchar_t *dst = d_start;
108 uchar_t *d_end = (uchar_t *)d_start + d_len;
109 uchar_t *cpy;
110 uchar_t copymap = 0;
111 int copymask = 1 << (NBBY - 1);
112
113 while (dst < d_end) {
114 if ((copymask <<= 1) == (1 << NBBY)) {
115 copymask = 1;
116 copymap = *src++;
117 }
118 if (copymap & copymask) {
119 int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
120 int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
121 src += 2;
122 if ((cpy = dst - offset) < (uchar_t *)d_start)
123 return (-1);
124 while (--mlen >= 0 && dst < d_end)
125 *dst++ = *cpy++;
126 } else {
127 *dst++ = *src++;
128 }
129 }
130 return (0);
131 }