xref: /freebsd/sys/cddl/boot/zfs/zle.c (revision db06a6f4ef6cbe2fecc10a453cfa70611fc4ac27)
1*10b9d77bSPawel Jakub Dawidek /*
2*10b9d77bSPawel Jakub Dawidek  * CDDL HEADER START
3*10b9d77bSPawel Jakub Dawidek  *
4*10b9d77bSPawel Jakub Dawidek  * The contents of this file are subject to the terms of the
5*10b9d77bSPawel Jakub Dawidek  * Common Development and Distribution License (the "License").
6*10b9d77bSPawel Jakub Dawidek  * You may not use this file except in compliance with the License.
7*10b9d77bSPawel Jakub Dawidek  *
8*10b9d77bSPawel Jakub Dawidek  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10b9d77bSPawel Jakub Dawidek  * or http://www.opensolaris.org/os/licensing.
10*10b9d77bSPawel Jakub Dawidek  * See the License for the specific language governing permissions
11*10b9d77bSPawel Jakub Dawidek  * and limitations under the License.
12*10b9d77bSPawel Jakub Dawidek  *
13*10b9d77bSPawel Jakub Dawidek  * When distributing Covered Code, include this CDDL HEADER in each
14*10b9d77bSPawel Jakub Dawidek  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10b9d77bSPawel Jakub Dawidek  * If applicable, add the following below this CDDL HEADER, with the
16*10b9d77bSPawel Jakub Dawidek  * fields enclosed by brackets "[]" replaced with your own identifying
17*10b9d77bSPawel Jakub Dawidek  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10b9d77bSPawel Jakub Dawidek  *
19*10b9d77bSPawel Jakub Dawidek  * CDDL HEADER END
20*10b9d77bSPawel Jakub Dawidek  */
21*10b9d77bSPawel Jakub Dawidek 
22*10b9d77bSPawel Jakub Dawidek /*
23*10b9d77bSPawel Jakub Dawidek  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*10b9d77bSPawel Jakub Dawidek  * Use is subject to license terms.
25*10b9d77bSPawel Jakub Dawidek  */
26*10b9d77bSPawel Jakub Dawidek 
27*10b9d77bSPawel Jakub Dawidek /*
28*10b9d77bSPawel Jakub Dawidek  * Zero-length encoding.  This is a fast and simple algorithm to eliminate
29*10b9d77bSPawel Jakub Dawidek  * runs of zeroes.  Each chunk of compressed data begins with a length byte, b.
30*10b9d77bSPawel Jakub Dawidek  * If b < n (where n is the compression parameter) then the next b + 1 bytes
31*10b9d77bSPawel Jakub Dawidek  * are literal values.  If b >= n then the next (256 - b + 1) bytes are zero.
32*10b9d77bSPawel Jakub Dawidek  */
33*10b9d77bSPawel Jakub Dawidek 
34*10b9d77bSPawel Jakub Dawidek static int
zle_decompress(void * s_start,void * d_start,size_t s_len,size_t d_len,int n)35*10b9d77bSPawel Jakub Dawidek zle_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
36*10b9d77bSPawel Jakub Dawidek {
37*10b9d77bSPawel Jakub Dawidek 	unsigned char *src = s_start;
38*10b9d77bSPawel Jakub Dawidek 	unsigned char *dst = d_start;
39*10b9d77bSPawel Jakub Dawidek 	unsigned char *s_end = src + s_len;
40*10b9d77bSPawel Jakub Dawidek 	unsigned char *d_end = dst + d_len;
41*10b9d77bSPawel Jakub Dawidek 
42*10b9d77bSPawel Jakub Dawidek 	while (src < s_end && dst < d_end) {
43*10b9d77bSPawel Jakub Dawidek 		int len = 1 + *src++;
44*10b9d77bSPawel Jakub Dawidek 		if (len <= n) {
45*10b9d77bSPawel Jakub Dawidek 			while (len-- != 0)
46*10b9d77bSPawel Jakub Dawidek 				*dst++ = *src++;
47*10b9d77bSPawel Jakub Dawidek 		} else {
48*10b9d77bSPawel Jakub Dawidek 			len -= n;
49*10b9d77bSPawel Jakub Dawidek 			while (len-- != 0)
50*10b9d77bSPawel Jakub Dawidek 				*dst++ = 0;
51*10b9d77bSPawel Jakub Dawidek 		}
52*10b9d77bSPawel Jakub Dawidek 	}
53*10b9d77bSPawel Jakub Dawidek 	return (dst == d_end ? 0 : -1);
54*10b9d77bSPawel Jakub Dawidek }
55