1*10922SJeff.Bonwick@Sun.COM /*
2*10922SJeff.Bonwick@Sun.COM * CDDL HEADER START
3*10922SJeff.Bonwick@Sun.COM *
4*10922SJeff.Bonwick@Sun.COM * The contents of this file are subject to the terms of the
5*10922SJeff.Bonwick@Sun.COM * Common Development and Distribution License (the "License").
6*10922SJeff.Bonwick@Sun.COM * You may not use this file except in compliance with the License.
7*10922SJeff.Bonwick@Sun.COM *
8*10922SJeff.Bonwick@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10922SJeff.Bonwick@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*10922SJeff.Bonwick@Sun.COM * See the License for the specific language governing permissions
11*10922SJeff.Bonwick@Sun.COM * and limitations under the License.
12*10922SJeff.Bonwick@Sun.COM *
13*10922SJeff.Bonwick@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*10922SJeff.Bonwick@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10922SJeff.Bonwick@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*10922SJeff.Bonwick@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*10922SJeff.Bonwick@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*10922SJeff.Bonwick@Sun.COM *
19*10922SJeff.Bonwick@Sun.COM * CDDL HEADER END
20*10922SJeff.Bonwick@Sun.COM */
21*10922SJeff.Bonwick@Sun.COM
22*10922SJeff.Bonwick@Sun.COM /*
23*10922SJeff.Bonwick@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24*10922SJeff.Bonwick@Sun.COM * Use is subject to license terms.
25*10922SJeff.Bonwick@Sun.COM */
26*10922SJeff.Bonwick@Sun.COM
27*10922SJeff.Bonwick@Sun.COM /*
28*10922SJeff.Bonwick@Sun.COM * Zero-length encoding. This is a fast and simple algorithm to eliminate
29*10922SJeff.Bonwick@Sun.COM * runs of zeroes. Each chunk of compressed data begins with a length byte, b.
30*10922SJeff.Bonwick@Sun.COM * If b < n (where n is the compression parameter) then the next b + 1 bytes
31*10922SJeff.Bonwick@Sun.COM * are literal values. If b >= n then the next (256 - b + 1) bytes are zero.
32*10922SJeff.Bonwick@Sun.COM */
33*10922SJeff.Bonwick@Sun.COM #include <sys/types.h>
34*10922SJeff.Bonwick@Sun.COM #include <sys/sysmacros.h>
35*10922SJeff.Bonwick@Sun.COM
36*10922SJeff.Bonwick@Sun.COM size_t
zle_compress(void * s_start,void * d_start,size_t s_len,size_t d_len,int n)37*10922SJeff.Bonwick@Sun.COM zle_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
38*10922SJeff.Bonwick@Sun.COM {
39*10922SJeff.Bonwick@Sun.COM uchar_t *src = s_start;
40*10922SJeff.Bonwick@Sun.COM uchar_t *dst = d_start;
41*10922SJeff.Bonwick@Sun.COM uchar_t *s_end = src + s_len;
42*10922SJeff.Bonwick@Sun.COM uchar_t *d_end = dst + d_len;
43*10922SJeff.Bonwick@Sun.COM
44*10922SJeff.Bonwick@Sun.COM while (src < s_end && dst < d_end - 1) {
45*10922SJeff.Bonwick@Sun.COM uchar_t *first = src;
46*10922SJeff.Bonwick@Sun.COM uchar_t *len = dst++;
47*10922SJeff.Bonwick@Sun.COM if (src[0] == 0) {
48*10922SJeff.Bonwick@Sun.COM uchar_t *last = src + (256 - n);
49*10922SJeff.Bonwick@Sun.COM while (src < MIN(last, s_end) && src[0] == 0)
50*10922SJeff.Bonwick@Sun.COM src++;
51*10922SJeff.Bonwick@Sun.COM *len = src - first - 1 + n;
52*10922SJeff.Bonwick@Sun.COM } else {
53*10922SJeff.Bonwick@Sun.COM uchar_t *last = src + n;
54*10922SJeff.Bonwick@Sun.COM if (d_end - dst < n)
55*10922SJeff.Bonwick@Sun.COM break;
56*10922SJeff.Bonwick@Sun.COM while (src < MIN(last, s_end) - 1 && (src[0] | src[1]))
57*10922SJeff.Bonwick@Sun.COM *dst++ = *src++;
58*10922SJeff.Bonwick@Sun.COM if (src[0])
59*10922SJeff.Bonwick@Sun.COM *dst++ = *src++;
60*10922SJeff.Bonwick@Sun.COM *len = src - first - 1;
61*10922SJeff.Bonwick@Sun.COM }
62*10922SJeff.Bonwick@Sun.COM }
63*10922SJeff.Bonwick@Sun.COM return (src == s_end ? dst - (uchar_t *)d_start : s_len);
64*10922SJeff.Bonwick@Sun.COM }
65*10922SJeff.Bonwick@Sun.COM
66*10922SJeff.Bonwick@Sun.COM int
zle_decompress(void * s_start,void * d_start,size_t s_len,size_t d_len,int n)67*10922SJeff.Bonwick@Sun.COM zle_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
68*10922SJeff.Bonwick@Sun.COM {
69*10922SJeff.Bonwick@Sun.COM uchar_t *src = s_start;
70*10922SJeff.Bonwick@Sun.COM uchar_t *dst = d_start;
71*10922SJeff.Bonwick@Sun.COM uchar_t *s_end = src + s_len;
72*10922SJeff.Bonwick@Sun.COM uchar_t *d_end = dst + d_len;
73*10922SJeff.Bonwick@Sun.COM
74*10922SJeff.Bonwick@Sun.COM while (src < s_end && dst < d_end) {
75*10922SJeff.Bonwick@Sun.COM int len = 1 + *src++;
76*10922SJeff.Bonwick@Sun.COM if (len <= n) {
77*10922SJeff.Bonwick@Sun.COM while (len-- != 0)
78*10922SJeff.Bonwick@Sun.COM *dst++ = *src++;
79*10922SJeff.Bonwick@Sun.COM } else {
80*10922SJeff.Bonwick@Sun.COM len -= n;
81*10922SJeff.Bonwick@Sun.COM while (len-- != 0)
82*10922SJeff.Bonwick@Sun.COM *dst++ = 0;
83*10922SJeff.Bonwick@Sun.COM }
84*10922SJeff.Bonwick@Sun.COM }
85*10922SJeff.Bonwick@Sun.COM return (dst == d_end ? 0 : -1);
86*10922SJeff.Bonwick@Sun.COM }
87