xref: /titanic_51/usr/src/uts/common/fs/zfs/lzjb.c (revision a60fc142342386d0b786e65fba901234400d7020)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * We keep our own copy of this algorithm for 2 main reasons:
31  * 	1. If we didn't, anyone modifying common/os/compress.c would
32  *         directly break our on disk format
33  * 	2. Our version of lzjb does not have a number of checks that the
34  *         common/os version needs and uses
35  * In particular, we are adding the "feature" that compress() can
36  * take a destination buffer size and return -1 if the data will not
37  * compress to d_len or less.
38  */
39 
40 #include <sys/types.h>
41 
42 #define	MATCH_BITS	6
43 #define	MATCH_MIN	3
44 #define	MATCH_MAX	((1 << MATCH_BITS) + (MATCH_MIN - 1))
45 #define	OFFSET_MASK	((1 << (16 - MATCH_BITS)) - 1)
46 #define	LEMPEL_SIZE	256
47 
48 /*ARGSUSED*/
49 size_t
50 lzjb_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
51 {
52 	uchar_t *src = s_start;
53 	uchar_t *dst = d_start;
54 	uchar_t *cpy, *copymap;
55 	int copymask = 1 << (NBBY - 1);
56 	int mlen, offset;
57 	uint16_t *hp;
58 	uint16_t lempel[LEMPEL_SIZE];	/* uninitialized; see above */
59 
60 	while (src < (uchar_t *)s_start + s_len) {
61 		if ((copymask <<= 1) == (1 << NBBY)) {
62 			if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY) {
63 				if (d_len != s_len)
64 					return (s_len);
65 				mlen = s_len;
66 				for (src = s_start, dst = d_start; mlen; mlen--)
67 					*dst++ = *src++;
68 				return (s_len);
69 			}
70 			copymask = 1;
71 			copymap = dst;
72 			*dst++ = 0;
73 		}
74 		if (src > (uchar_t *)s_start + s_len - MATCH_MAX) {
75 			*dst++ = *src++;
76 			continue;
77 		}
78 		hp = &lempel[((src[0] + 13) ^ (src[1] - 13) ^ src[2]) &
79 		    (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 	return (dst - (uchar_t *)d_start);
98 }
99 
100 /*ARGSUSED*/
101 int
102 lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
103 {
104 	uchar_t *src = s_start;
105 	uchar_t *dst = d_start;
106 	uchar_t *d_end = (uchar_t *)d_start + d_len;
107 	uchar_t *cpy, copymap;
108 	int copymask = 1 << (NBBY - 1);
109 
110 	while (dst < d_end) {
111 		if ((copymask <<= 1) == (1 << NBBY)) {
112 			copymask = 1;
113 			copymap = *src++;
114 		}
115 		if (copymap & copymask) {
116 			int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
117 			int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
118 			src += 2;
119 			if ((cpy = dst - offset) < (uchar_t *)d_start)
120 				return (-1);
121 			while (--mlen >= 0 && dst < d_end)
122 				*dst++ = *cpy++;
123 		} else {
124 			*dst++ = *src++;
125 		}
126 	}
127 	return (0);
128 }
129