xref: /titanic_44/usr/src/uts/common/fs/zfs/lzjb.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * We keep our own copy of this algorithm for 2 main reasons:
30  * 	1. If we didn't, anyone modifying common/os/compress.c would
31  *         directly break our on disk format
32  * 	2. Our version of lzjb does not have a number of checks that the
33  *         common/os version needs and uses
34  * In particular, we are adding the "feature" that compress() can
35  * take a destination buffer size and return -1 if the data will not
36  * compress to d_len or less.
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)
45 #define	LEMPEL_SIZE	256
46 
47 size_t
48 lzjb_compress(void *s_start, void *d_start, size_t s_len, size_t d_len)
49 {
50 	uchar_t *src = s_start;
51 	uchar_t *dst = d_start;
52 	uchar_t *cpy, *copymap;
53 	int copymask = 1 << (NBBY - 1);
54 	int mlen, offset;
55 	uint16_t *hp;
56 	uint16_t lempel[LEMPEL_SIZE];	/* uninitialized; see above */
57 
58 	while (src < (uchar_t *)s_start + s_len) {
59 		if ((copymask <<= 1) == (1 << NBBY)) {
60 			if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY) {
61 				if (d_len != s_len)
62 					return (s_len);
63 				mlen = s_len;
64 				for (src = s_start, dst = d_start; mlen; mlen--)
65 					*dst++ = *src++;
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 		hp = &lempel[((src[0] + 13) ^ (src[1] - 13) ^ src[2]) &
77 		    (LEMPEL_SIZE - 1)];
78 		offset = (intptr_t)(src - *hp) & OFFSET_MASK;
79 		*hp = (uint16_t)(uintptr_t)src;
80 		cpy = src - offset;
81 		if (cpy >= (uchar_t *)s_start && cpy != src &&
82 		    src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) {
83 			*copymap |= copymask;
84 			for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++)
85 				if (src[mlen] != cpy[mlen])
86 					break;
87 			*dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) |
88 			    (offset >> NBBY);
89 			*dst++ = (uchar_t)offset;
90 			src += mlen;
91 		} else {
92 			*dst++ = *src++;
93 		}
94 	}
95 	return (dst - (uchar_t *)d_start);
96 }
97 
98 /*ARGSUSED*/
99 int
100 lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len)
101 {
102 	uchar_t *src = s_start;
103 	uchar_t *dst = d_start;
104 	uchar_t *d_end = (uchar_t *)d_start + d_len;
105 	uchar_t *cpy, copymap;
106 	int copymask = 1 << (NBBY - 1);
107 
108 	while (dst < d_end) {
109 		if ((copymask <<= 1) == (1 << NBBY)) {
110 			copymask = 1;
111 			copymap = *src++;
112 		}
113 		if (copymap & copymask) {
114 			int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
115 			int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
116 			src += 2;
117 			if ((cpy = dst - offset) < (uchar_t *)d_start)
118 				return (-1);
119 			while (--mlen >= 0 && dst < d_end)
120 				*dst++ = *cpy++;
121 		} else {
122 			*dst++ = *src++;
123 		}
124 	}
125 	return (0);
126 }
127