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