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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
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.
40 #include <sys/types.h>
44 #define MATCH_MAX ((1 << MATCH_BITS) + (MATCH_MIN - 1))
45 #define OFFSET_MASK ((1 << (16 - MATCH_BITS)) - 1)
46 #define LEMPEL_SIZE 256
50 lzjb_compress(void *s_start
, void *d_start
, size_t s_len
, size_t d_len
, int n
)
52 uchar_t
*src
= s_start
;
53 uchar_t
*dst
= d_start
;
54 uchar_t
*cpy
, *copymap
;
55 int copymask
= 1 << (NBBY
- 1);
58 uint16_t lempel
[LEMPEL_SIZE
]; /* uninitialized; see above */
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
) {
66 for (src
= s_start
, dst
= d_start
; mlen
; mlen
--)
74 if (src
> (uchar_t
*)s_start
+ s_len
- MATCH_MAX
) {
78 hp
= &lempel
[((src
[0] + 13) ^ (src
[1] - 13) ^ src
[2]) &
80 offset
= (intptr_t)(src
- *hp
) & OFFSET_MASK
;
81 *hp
= (uint16_t)(uintptr_t)src
;
83 if (cpy
>= (uchar_t
*)s_start
&& cpy
!= src
&&
84 src
[0] == cpy
[0] && src
[1] == cpy
[1] && src
[2] == cpy
[2]) {
86 for (mlen
= MATCH_MIN
; mlen
< MATCH_MAX
; mlen
++)
87 if (src
[mlen
] != cpy
[mlen
])
89 *dst
++ = ((mlen
- MATCH_MIN
) << (NBBY
- MATCH_BITS
)) |
91 *dst
++ = (uchar_t
)offset
;
97 return (dst
- (uchar_t
*)d_start
);
102 lzjb_decompress(void *s_start
, void *d_start
, size_t s_len
, size_t d_len
, int n
)
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);
110 while (dst
< d_end
) {
111 if ((copymask
<<= 1) == (1 << NBBY
)) {
115 if (copymap
& copymask
) {
116 int mlen
= (src
[0] >> (NBBY
- MATCH_BITS
)) + MATCH_MIN
;
117 int offset
= ((src
[0] << NBBY
) | src
[1]) & OFFSET_MASK
;
119 if ((cpy
= dst
- offset
) < (uchar_t
*)d_start
)
121 while (--mlen
>= 0 && dst
< d_end
)