zle.c revision 219089
1219089Spjd/* 2219089Spjd * CDDL HEADER START 3219089Spjd * 4219089Spjd * The contents of this file are subject to the terms of the 5219089Spjd * Common Development and Distribution License (the "License"). 6219089Spjd * You may not use this file except in compliance with the License. 7219089Spjd * 8219089Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9219089Spjd * or http://www.opensolaris.org/os/licensing. 10219089Spjd * See the License for the specific language governing permissions 11219089Spjd * and limitations under the License. 12219089Spjd * 13219089Spjd * When distributing Covered Code, include this CDDL HEADER in each 14219089Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15219089Spjd * If applicable, add the following below this CDDL HEADER, with the 16219089Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17219089Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18219089Spjd * 19219089Spjd * CDDL HEADER END 20219089Spjd */ 21219089Spjd 22219089Spjd/* 23219089Spjd * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24219089Spjd * Use is subject to license terms. 25219089Spjd */ 26219089Spjd 27219089Spjd/* 28219089Spjd * Zero-length encoding. This is a fast and simple algorithm to eliminate 29219089Spjd * runs of zeroes. Each chunk of compressed data begins with a length byte, b. 30219089Spjd * If b < n (where n is the compression parameter) then the next b + 1 bytes 31219089Spjd * are literal values. If b >= n then the next (256 - b + 1) bytes are zero. 32219089Spjd */ 33219089Spjd 34219089Spjdstatic int 35219089Spjdzle_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n) 36219089Spjd{ 37219089Spjd unsigned char *src = s_start; 38219089Spjd unsigned char *dst = d_start; 39219089Spjd unsigned char *s_end = src + s_len; 40219089Spjd unsigned char *d_end = dst + d_len; 41219089Spjd 42219089Spjd while (src < s_end && dst < d_end) { 43219089Spjd int len = 1 + *src++; 44219089Spjd if (len <= n) { 45219089Spjd while (len-- != 0) 46219089Spjd *dst++ = *src++; 47219089Spjd } else { 48219089Spjd len -= n; 49219089Spjd while (len-- != 0) 50219089Spjd *dst++ = 0; 51219089Spjd } 52219089Spjd } 53219089Spjd return (dst == d_end ? 0 : -1); 54219089Spjd} 55