xref: /titanic_50/usr/src/uts/common/fs/zfs/lzjb.c (revision 60b08185ce63023f22fd6b2ed0db8c0d119b2023)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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  * This is stolen from common/os/compress.c and will be removed once
31  * our changes have made it into the on10 source base.
32  *
33  * In particular, we are adding the "feature" that compress() can
34  * take a destination buffer size and return -1 if the data will not
35  * compress to d_len or less.
36  */
37 
38 #include <sys/types.h>
39 
40 #define	MATCH_BITS	6
41 #define	MATCH_MIN	3
42 #define	MATCH_MAX	((1 << MATCH_BITS) + (MATCH_MIN - 1))
43 #define	OFFSET_MASK	((1 << (16 - MATCH_BITS)) - 1)
44 #define	LEMPEL_SIZE	256
45 
46 size_t
47 lzjb_compress(void *s_start, void *d_start, size_t s_len, size_t d_len)
48 {
49 	uchar_t *src = s_start;
50 	uchar_t *dst = d_start;
51 	uchar_t *cpy, *copymap;
52 	int copymask = 1 << (NBBY - 1);
53 	int mlen, offset;
54 	uint16_t *hp;
55 	uint16_t lempel[LEMPEL_SIZE];	/* uninitialized; see above */
56 
57 	while (src < (uchar_t *)s_start + s_len) {
58 		if ((copymask <<= 1) == (1 << NBBY)) {
59 			if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY) {
60 				if (d_len != s_len)
61 					return (s_len);
62 				mlen = s_len;
63 				for (src = s_start, dst = d_start; mlen; mlen--)
64 					*dst++ = *src++;
65 				return (s_len);
66 			}
67 			copymask = 1;
68 			copymap = dst;
69 			*dst++ = 0;
70 		}
71 		if (src > (uchar_t *)s_start + s_len - MATCH_MAX) {
72 			*dst++ = *src++;
73 			continue;
74 		}
75 		hp = &lempel[((src[0] + 13) ^ (src[1] - 13) ^ src[2]) &
76 		    (LEMPEL_SIZE - 1)];
77 		offset = (intptr_t)(src - *hp) & OFFSET_MASK;
78 		*hp = (uint16_t)(uintptr_t)src;
79 		cpy = src - offset;
80 		if (cpy >= (uchar_t *)s_start && cpy != src &&
81 		    src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) {
82 			*copymap |= copymask;
83 			for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++)
84 				if (src[mlen] != cpy[mlen])
85 					break;
86 			*dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) |
87 			    (offset >> NBBY);
88 			*dst++ = (uchar_t)offset;
89 			src += mlen;
90 		} else {
91 			*dst++ = *src++;
92 		}
93 	}
94 	return (dst - (uchar_t *)d_start);
95 }
96 
97 /*ARGSUSED*/
98 int
99 lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len)
100 {
101 	uchar_t *src = s_start;
102 	uchar_t *dst = d_start;
103 	uchar_t *d_end = (uchar_t *)d_start + d_len;
104 	uchar_t *cpy, copymap;
105 	int copymask = 1 << (NBBY - 1);
106 
107 	while (dst < d_end) {
108 		if ((copymask <<= 1) == (1 << NBBY)) {
109 			copymask = 1;
110 			copymap = *src++;
111 		}
112 		if (copymap & copymask) {
113 			int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
114 			int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
115 			src += 2;
116 			if ((cpy = dst - offset) < (uchar_t *)d_start)
117 				return (-1);
118 			while (--mlen >= 0 && dst < d_end)
119 				*dst++ = *cpy++;
120 		} else {
121 			*dst++ = *src++;
122 		}
123 	}
124 	return (0);
125 }
126