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