]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/lzjb.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / lzjb.c
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/zfs_context.h>
41 #include <sys/types.h>
42
43 #define MATCH_BITS      6
44 #define MATCH_MIN       3
45 #define MATCH_MAX       ((1 << MATCH_BITS) + (MATCH_MIN - 1))
46 #define OFFSET_MASK     ((1 << (16 - MATCH_BITS)) - 1)
47 #define LEMPEL_SIZE     256
48
49 /*ARGSUSED*/
50 size_t
51 lzjb_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
52 {
53         uchar_t *src = s_start;
54         uchar_t *dst = d_start;
55         uchar_t *cpy, *copymap;
56         int copymask = 1 << (NBBY - 1);
57         int mlen, offset;
58         uint16_t *hp;
59         uint16_t lempel[LEMPEL_SIZE];   /* uninitialized; see above */
60
61         while (src < (uchar_t *)s_start + s_len) {
62                 if ((copymask <<= 1) == (1 << NBBY)) {
63                         if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY) {
64                                 if (d_len != s_len)
65                                         return (s_len);
66                                 mlen = s_len;
67                                 for (src = s_start, dst = d_start; mlen; mlen--)
68                                         *dst++ = *src++;
69                                 return (s_len);
70                         }
71                         copymask = 1;
72                         copymap = dst;
73                         *dst++ = 0;
74                 }
75                 if (src > (uchar_t *)s_start + s_len - MATCH_MAX) {
76                         *dst++ = *src++;
77                         continue;
78                 }
79                 hp = &lempel[((src[0] + 13) ^ (src[1] - 13) ^ src[2]) &
80                     (LEMPEL_SIZE - 1)];
81                 offset = (intptr_t)(src - *hp) & OFFSET_MASK;
82                 *hp = (uint16_t)(uintptr_t)src;
83                 cpy = src - offset;
84                 if (cpy >= (uchar_t *)s_start && cpy != src &&
85                     src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) {
86                         *copymap |= copymask;
87                         for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++)
88                                 if (src[mlen] != cpy[mlen])
89                                         break;
90                         *dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) |
91                             (offset >> NBBY);
92                         *dst++ = (uchar_t)offset;
93                         src += mlen;
94                 } else {
95                         *dst++ = *src++;
96                 }
97         }
98         return (dst - (uchar_t *)d_start);
99 }
100
101 /*ARGSUSED*/
102 int
103 lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
104 {
105         uchar_t *src = s_start;
106         uchar_t *dst = d_start;
107         uchar_t *d_end = (uchar_t *)d_start + d_len;
108         uchar_t *cpy, copymap;
109         int copymask = 1 << (NBBY - 1);
110
111         while (dst < d_end) {
112                 if ((copymask <<= 1) == (1 << NBBY)) {
113                         copymask = 1;
114                         copymap = *src++;
115                 }
116                 if (copymap & copymask) {
117                         int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
118                         int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
119                         src += 2;
120                         if ((cpy = dst - offset) < (uchar_t *)d_start)
121                                 return (-1);
122                         while (--mlen >= 0 && dst < d_end)
123                                 *dst++ = *cpy++;
124                 } else {
125                         *dst++ = *src++;
126                 }
127         }
128         return (0);
129 }