xref: /titanic_50/usr/src/uts/common/os/compress.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * NOTE: this file is compiled into the kernel, cprboot, and savecore.
31*7c478bd9Sstevel@tonic-gate  * Therefore it must compile in kernel, boot, and userland source context;
32*7c478bd9Sstevel@tonic-gate  * so if you ever change this code, avoid references to external symbols.
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * This compression algorithm is a derivative of LZRW1, which I'll call
35*7c478bd9Sstevel@tonic-gate  * LZJB in the classic LZ* spirit.  All LZ* (Lempel-Ziv) algorithms are
36*7c478bd9Sstevel@tonic-gate  * based on the same basic principle: when a "phrase" (sequences of bytes)
37*7c478bd9Sstevel@tonic-gate  * is repeated in a data stream, we can save space by storing a reference to
38*7c478bd9Sstevel@tonic-gate  * the previous instance of that phrase (a "copy item") rather than storing
39*7c478bd9Sstevel@tonic-gate  * the phrase itself (a "literal item").  The compressor remembers phrases
40*7c478bd9Sstevel@tonic-gate  * in a simple hash table (the "Lempel history") that maps three-character
41*7c478bd9Sstevel@tonic-gate  * sequences (the minimum match) to the addresses where they were last seen.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * A copy item must encode both the length and the location of the matching
44*7c478bd9Sstevel@tonic-gate  * phrase so that decompress() can reconstruct the original data stream.
45*7c478bd9Sstevel@tonic-gate  * For example, here's how we'd encode "yadda yadda yadda, blah blah blah"
46*7c478bd9Sstevel@tonic-gate  * (with "_" replacing spaces for readability):
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  * Original:
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * y a d d a _ y a d d a _ y a d d a , _ b l a h _ b l a h _ b l a h
51*7c478bd9Sstevel@tonic-gate  *
52*7c478bd9Sstevel@tonic-gate  * Compressed:
53*7c478bd9Sstevel@tonic-gate  *
54*7c478bd9Sstevel@tonic-gate  * y a d d a _ 6 11 , _ b l a h 5 10
55*7c478bd9Sstevel@tonic-gate  *
56*7c478bd9Sstevel@tonic-gate  * In the compressed output, the "6 11" simply means "to get the original
57*7c478bd9Sstevel@tonic-gate  * data, execute memmove(ptr, ptr - 6, 11)".  Note that in this example,
58*7c478bd9Sstevel@tonic-gate  * the match at "6 11" actually extends beyond the current location and
59*7c478bd9Sstevel@tonic-gate  * overlaps it.  That's OK; like memmove(), decompress() handles overlap.
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  * There's still one more thing decompress() needs to know, which is how to
62*7c478bd9Sstevel@tonic-gate  * distinguish literal items from copy items.  We encode this information
63*7c478bd9Sstevel@tonic-gate  * in an 8-bit bitmap that precedes each 8 items of output; if the Nth bit
64*7c478bd9Sstevel@tonic-gate  * is set, then the Nth item is a copy item.  Thus the full encoding for
65*7c478bd9Sstevel@tonic-gate  * the example above would be:
66*7c478bd9Sstevel@tonic-gate  *
67*7c478bd9Sstevel@tonic-gate  * 0x40 y a d d a _ 6 11 , 0x20 _ b l a h 5 10
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  * Finally, the "6 11" isn't really encoded as the two byte values 6 and 11
70*7c478bd9Sstevel@tonic-gate  * in the output stream because, empirically, we get better compression by
71*7c478bd9Sstevel@tonic-gate  * dedicating more bits to offset, fewer to match length.  LZJB uses 6 bits
72*7c478bd9Sstevel@tonic-gate  * to encode the match length, 10 bits to encode the offset.  Since copy-item
73*7c478bd9Sstevel@tonic-gate  * encoding consumes 2 bytes, we don't generate copy items unless the match
74*7c478bd9Sstevel@tonic-gate  * length is at least 3; therefore, we can store (length - 3) in the 6-bit
75*7c478bd9Sstevel@tonic-gate  * match length field, which extends the maximum match from 63 to 66 bytes.
76*7c478bd9Sstevel@tonic-gate  * Thus the 2-byte encoding for a copy item is as follows:
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  *	byte[0] = ((length - 3) << 2) | (offset >> 8);
79*7c478bd9Sstevel@tonic-gate  *	byte[1] = (uint8_t)offset;
80*7c478bd9Sstevel@tonic-gate  *
81*7c478bd9Sstevel@tonic-gate  * In our example above, an offset of 6 with length 11 would be encoded as:
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  *	byte[0] = ((11 - 3) << 2) | (6 >> 8) = 0x20
84*7c478bd9Sstevel@tonic-gate  *	byte[1] = (uint8_t)6 = 0x6
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  * Similarly, an offset of 5 with length 10 would be encoded as:
87*7c478bd9Sstevel@tonic-gate  *
88*7c478bd9Sstevel@tonic-gate  *	byte[0] = ((10 - 3) << 2) | (5 >> 8) = 0x1c
89*7c478bd9Sstevel@tonic-gate  *	byte[1] = (uint8_t)5 = 0x5
90*7c478bd9Sstevel@tonic-gate  *
91*7c478bd9Sstevel@tonic-gate  * Putting it all together, the actual LZJB output for our example is:
92*7c478bd9Sstevel@tonic-gate  *
93*7c478bd9Sstevel@tonic-gate  * 0x40 y a d d a _ 0x2006 , 0x20 _ b l a h 0x1c05
94*7c478bd9Sstevel@tonic-gate  *
95*7c478bd9Sstevel@tonic-gate  * The main differences between LZRW1 and LZJB are as follows:
96*7c478bd9Sstevel@tonic-gate  *
97*7c478bd9Sstevel@tonic-gate  * (1) LZRW1 is sloppy about buffer overruns.  LZJB never reads past the
98*7c478bd9Sstevel@tonic-gate  *     end of its input, and never writes past the end of its output.
99*7c478bd9Sstevel@tonic-gate  *
100*7c478bd9Sstevel@tonic-gate  * (2) LZJB allows a maximum match length of 66 (vs. 18 for LZRW1), with
101*7c478bd9Sstevel@tonic-gate  *     the trade-off being a shorter look-behind (1K vs. 4K for LZRW1).
102*7c478bd9Sstevel@tonic-gate  *
103*7c478bd9Sstevel@tonic-gate  * (3) LZJB records only the low-order 16 bits of pointers in the Lempel
104*7c478bd9Sstevel@tonic-gate  *     history (which is all we need since the maximum look-behind is 1K),
105*7c478bd9Sstevel@tonic-gate  *     and uses only 256 hash entries (vs. 4096 for LZRW1).  This makes
106*7c478bd9Sstevel@tonic-gate  *     the compression hash small enough to allocate on the stack, which
107*7c478bd9Sstevel@tonic-gate  *     solves two problems: (1) it saves 64K of kernel/cprboot memory,
108*7c478bd9Sstevel@tonic-gate  *     and (2) it makes the code MT-safe without any locking, since we
109*7c478bd9Sstevel@tonic-gate  *     don't have multiple threads sharing a common hash table.
110*7c478bd9Sstevel@tonic-gate  *
111*7c478bd9Sstevel@tonic-gate  * (4) LZJB is faster at both compression and decompression, has a
112*7c478bd9Sstevel@tonic-gate  *     better compression ratio, and is somewhat simpler than LZRW1.
113*7c478bd9Sstevel@tonic-gate  *
114*7c478bd9Sstevel@tonic-gate  * Finally, note that LZJB is non-deterministic: given the same input,
115*7c478bd9Sstevel@tonic-gate  * two calls to compress() may produce different output.  This is a
116*7c478bd9Sstevel@tonic-gate  * general characteristic of most Lempel-Ziv derivatives because there's
117*7c478bd9Sstevel@tonic-gate  * no need to initialize the Lempel history; not doing so saves time.
118*7c478bd9Sstevel@tonic-gate  */
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate #define	MATCH_BITS	6
123*7c478bd9Sstevel@tonic-gate #define	MATCH_MIN	3
124*7c478bd9Sstevel@tonic-gate #define	MATCH_MAX	((1 << MATCH_BITS) + (MATCH_MIN - 1))
125*7c478bd9Sstevel@tonic-gate #define	OFFSET_MASK	((1 << (16 - MATCH_BITS)) - 1)
126*7c478bd9Sstevel@tonic-gate #define	LEMPEL_SIZE	256
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate size_t
129*7c478bd9Sstevel@tonic-gate compress(void *s_start, void *d_start, size_t s_len)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	uchar_t *src = s_start;
132*7c478bd9Sstevel@tonic-gate 	uchar_t *dst = d_start;
133*7c478bd9Sstevel@tonic-gate 	uchar_t *cpy, *copymap;
134*7c478bd9Sstevel@tonic-gate 	int copymask = 1 << (NBBY - 1);
135*7c478bd9Sstevel@tonic-gate 	int mlen, offset;
136*7c478bd9Sstevel@tonic-gate 	uint16_t *hp;
137*7c478bd9Sstevel@tonic-gate 	uint16_t lempel[LEMPEL_SIZE];	/* uninitialized; see above */
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	while (src < (uchar_t *)s_start + s_len) {
140*7c478bd9Sstevel@tonic-gate 		if ((copymask <<= 1) == (1 << NBBY)) {
141*7c478bd9Sstevel@tonic-gate 			if (dst >= (uchar_t *)d_start + s_len - 1 - 2 * NBBY) {
142*7c478bd9Sstevel@tonic-gate 				mlen = s_len;
143*7c478bd9Sstevel@tonic-gate 				for (src = s_start, dst = d_start; mlen; mlen--)
144*7c478bd9Sstevel@tonic-gate 					*dst++ = *src++;
145*7c478bd9Sstevel@tonic-gate 				return (s_len);
146*7c478bd9Sstevel@tonic-gate 			}
147*7c478bd9Sstevel@tonic-gate 			copymask = 1;
148*7c478bd9Sstevel@tonic-gate 			copymap = dst;
149*7c478bd9Sstevel@tonic-gate 			*dst++ = 0;
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 		if (src > (uchar_t *)s_start + s_len - MATCH_MAX) {
152*7c478bd9Sstevel@tonic-gate 			*dst++ = *src++;
153*7c478bd9Sstevel@tonic-gate 			continue;
154*7c478bd9Sstevel@tonic-gate 		}
155*7c478bd9Sstevel@tonic-gate 		hp = &lempel[((src[0] + 13) ^ (src[1] - 13) ^ src[2]) &
156*7c478bd9Sstevel@tonic-gate 		    (LEMPEL_SIZE - 1)];
157*7c478bd9Sstevel@tonic-gate 		offset = (intptr_t)(src - *hp) & OFFSET_MASK;
158*7c478bd9Sstevel@tonic-gate 		*hp = (uint16_t)(uintptr_t)src;
159*7c478bd9Sstevel@tonic-gate 		cpy = src - offset;
160*7c478bd9Sstevel@tonic-gate 		if (cpy >= (uchar_t *)s_start && cpy != src &&
161*7c478bd9Sstevel@tonic-gate 		    src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) {
162*7c478bd9Sstevel@tonic-gate 			*copymap |= copymask;
163*7c478bd9Sstevel@tonic-gate 			for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++)
164*7c478bd9Sstevel@tonic-gate 				if (src[mlen] != cpy[mlen])
165*7c478bd9Sstevel@tonic-gate 					break;
166*7c478bd9Sstevel@tonic-gate 			*dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) |
167*7c478bd9Sstevel@tonic-gate 			    (offset >> NBBY);
168*7c478bd9Sstevel@tonic-gate 			*dst++ = (uchar_t)offset;
169*7c478bd9Sstevel@tonic-gate 			src += mlen;
170*7c478bd9Sstevel@tonic-gate 		} else {
171*7c478bd9Sstevel@tonic-gate 			*dst++ = *src++;
172*7c478bd9Sstevel@tonic-gate 		}
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate 	return (dst - (uchar_t *)d_start);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate size_t
178*7c478bd9Sstevel@tonic-gate decompress(void *s_start, void *d_start, size_t s_len, size_t d_len)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 	uchar_t *src = s_start;
181*7c478bd9Sstevel@tonic-gate 	uchar_t *dst = d_start;
182*7c478bd9Sstevel@tonic-gate 	uchar_t *s_end = (uchar_t *)s_start + s_len;
183*7c478bd9Sstevel@tonic-gate 	uchar_t *d_end = (uchar_t *)d_start + d_len;
184*7c478bd9Sstevel@tonic-gate 	uchar_t *cpy, copymap;
185*7c478bd9Sstevel@tonic-gate 	int copymask = 1 << (NBBY - 1);
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	if (s_len >= d_len) {
188*7c478bd9Sstevel@tonic-gate 		size_t d_rem = d_len;
189*7c478bd9Sstevel@tonic-gate 		while (d_rem-- != 0)
190*7c478bd9Sstevel@tonic-gate 			*dst++ = *src++;
191*7c478bd9Sstevel@tonic-gate 		return (d_len);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	while (src < s_end && dst < d_end) {
195*7c478bd9Sstevel@tonic-gate 		if ((copymask <<= 1) == (1 << NBBY)) {
196*7c478bd9Sstevel@tonic-gate 			copymask = 1;
197*7c478bd9Sstevel@tonic-gate 			copymap = *src++;
198*7c478bd9Sstevel@tonic-gate 		}
199*7c478bd9Sstevel@tonic-gate 		if (copymap & copymask) {
200*7c478bd9Sstevel@tonic-gate 			int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
201*7c478bd9Sstevel@tonic-gate 			int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
202*7c478bd9Sstevel@tonic-gate 			src += 2;
203*7c478bd9Sstevel@tonic-gate 			if ((cpy = dst - offset) >= (uchar_t *)d_start)
204*7c478bd9Sstevel@tonic-gate 				while (--mlen >= 0 && dst < d_end)
205*7c478bd9Sstevel@tonic-gate 					*dst++ = *cpy++;
206*7c478bd9Sstevel@tonic-gate 			else
207*7c478bd9Sstevel@tonic-gate 				/*
208*7c478bd9Sstevel@tonic-gate 				 * offset before start of destination buffer
209*7c478bd9Sstevel@tonic-gate 				 * indicates corrupt source data
210*7c478bd9Sstevel@tonic-gate 				 */
211*7c478bd9Sstevel@tonic-gate 				return (dst - (uchar_t *)d_start);
212*7c478bd9Sstevel@tonic-gate 		} else {
213*7c478bd9Sstevel@tonic-gate 			*dst++ = *src++;
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	return (dst - (uchar_t *)d_start);
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate uint32_t
220*7c478bd9Sstevel@tonic-gate checksum32(void *cp_arg, size_t length)
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate 	uchar_t *cp, *ep;
223*7c478bd9Sstevel@tonic-gate 	uint32_t sum = 0;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	for (cp = cp_arg, ep = cp + length; cp < ep; cp++)
226*7c478bd9Sstevel@tonic-gate 		sum = ((sum >> 1) | (sum << 31)) + *cp;
227*7c478bd9Sstevel@tonic-gate 	return (sum);
228*7c478bd9Sstevel@tonic-gate }
229