/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2007 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526/*#pragma ident "%Z%%M% %I% %E% SMI"*/2728/*29* We keep our own copy of this algorithm for 2 main reasons:30* 1. If we didn't, anyone modifying common/os/compress.c would31* directly break our on disk format32* 2. Our version of lzjb does not have a number of checks that the33* common/os version needs and uses34* In particular, we are adding the "feature" that compress() can35* take a destination buffer size and return -1 if the data will not36* compress to d_len or less.37*/3839#define MATCH_BITS 640#define MATCH_MIN 341#define MATCH_MAX ((1 << MATCH_BITS) + (MATCH_MIN - 1))42#define OFFSET_MASK ((1 << (16 - MATCH_BITS)) - 1)43#define LEMPEL_SIZE 2564445/*ARGSUSED*/46static int47lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)48{49unsigned char *src = s_start;50unsigned char *dst = d_start;51unsigned char *d_end = (unsigned char *)d_start + d_len;52unsigned char *cpy, copymap = 0;53int copymask = 1 << (NBBY - 1);5455while (dst < d_end) {56if ((copymask <<= 1) == (1 << NBBY)) {57copymask = 1;58copymap = *src++;59}60if (copymap & copymask) {61int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;62int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;63src += 2;64if ((cpy = dst - offset) < (unsigned char *)d_start)65return (-1);66while (--mlen >= 0 && dst < d_end)67*dst++ = *cpy++;68} else {69*dst++ = *src++;70}71}72return (0);73}747576