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#include <sys/types.h> 34219089Spjd#include <sys/sysmacros.h> 35219089Spjd 36219089Spjdsize_t 37219089Spjdzle_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n) 38219089Spjd{ 39219089Spjd uchar_t *src = s_start; 40219089Spjd uchar_t *dst = d_start; 41219089Spjd uchar_t *s_end = src + s_len; 42219089Spjd uchar_t *d_end = dst + d_len; 43219089Spjd 44219089Spjd while (src < s_end && dst < d_end - 1) { 45219089Spjd uchar_t *first = src; 46219089Spjd uchar_t *len = dst++; 47219089Spjd if (src[0] == 0) { 48219089Spjd uchar_t *last = src + (256 - n); 49219089Spjd while (src < MIN(last, s_end) && src[0] == 0) 50219089Spjd src++; 51219089Spjd *len = src - first - 1 + n; 52219089Spjd } else { 53219089Spjd uchar_t *last = src + n; 54219089Spjd if (d_end - dst < n) 55219089Spjd break; 56219089Spjd while (src < MIN(last, s_end) - 1 && (src[0] | src[1])) 57219089Spjd *dst++ = *src++; 58219089Spjd if (src[0]) 59219089Spjd *dst++ = *src++; 60219089Spjd *len = src - first - 1; 61219089Spjd } 62219089Spjd } 63219089Spjd return (src == s_end ? dst - (uchar_t *)d_start : s_len); 64219089Spjd} 65219089Spjd 66219089Spjdint 67219089Spjdzle_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n) 68219089Spjd{ 69219089Spjd uchar_t *src = s_start; 70219089Spjd uchar_t *dst = d_start; 71219089Spjd uchar_t *s_end = src + s_len; 72219089Spjd uchar_t *d_end = dst + d_len; 73219089Spjd 74219089Spjd while (src < s_end && dst < d_end) { 75219089Spjd int len = 1 + *src++; 76219089Spjd if (len <= n) { 77219089Spjd while (len-- != 0) 78219089Spjd *dst++ = *src++; 79219089Spjd } else { 80219089Spjd len -= n; 81219089Spjd while (len-- != 0) 82219089Spjd *dst++ = 0; 83219089Spjd } 84219089Spjd } 85219089Spjd return (dst == d_end ? 0 : -1); 86219089Spjd} 87