Home | History | Annotate | Line # | Download | only in zfs
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     24  */
     25 
     26 /*
     27  * We keep our own copy of this algorithm for 3 main reasons:
     28  *	1. If we didn't, anyone modifying common/os/compress.c would
     29  *         directly break our on disk format
     30  *	2. Our version of lzjb does not have a number of checks that the
     31  *         common/os version needs and uses
     32  *	3. We initialize the lempel to ensure deterministic results,
     33  *	   so that identical blocks can always be deduplicated.
     34  * In particular, we are adding the "feature" that compress() can
     35  * take a destination buffer size and returns the compressed length, or the
     36  * source length if compression would overflow the destination buffer.
     37  */
     38 
     39 #include <sys/zfs_context.h>
     40 #include <sys/types.h>
     41 #include <sys/param.h>
     42 
     43 #define	MATCH_BITS	6
     44 #define	MATCH_MIN	3
     45 #define	MATCH_MAX	((1 << MATCH_BITS) + (MATCH_MIN - 1))
     46 #define	OFFSET_MASK	((1 << (16 - MATCH_BITS)) - 1)
     47 #define	LEMPEL_SIZE	1024
     48 
     49 /*ARGSUSED*/
     50 size_t
     51 lzjb_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
     52 {
     53 	uchar_t *src = s_start;
     54 	uchar_t *dst = d_start;
     55 	uchar_t *cpy;
     56 	uchar_t *copymap = NULL;
     57 	int copymask = 1 << (NBBY - 1);
     58 	int mlen, offset, hash;
     59 	uint16_t *hp;
     60 	uint16_t lempel[LEMPEL_SIZE] = { 0 };
     61 
     62 	while (src < (uchar_t *)s_start + s_len) {
     63 		if ((copymask <<= 1) == (1 << NBBY)) {
     64 			if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY)
     65 				return (s_len);
     66 			copymask = 1;
     67 			copymap = dst;
     68 			*dst++ = 0;
     69 		}
     70 		if (src > (uchar_t *)s_start + s_len - MATCH_MAX) {
     71 			*dst++ = *src++;
     72 			continue;
     73 		}
     74 		hash = (src[0] << 16) + (src[1] << 8) + src[2];
     75 		hash += hash >> 9;
     76 		hash += hash >> 5;
     77 		hp = &lempel[hash & (LEMPEL_SIZE - 1)];
     78 		offset = (intptr_t)(src - *hp) & OFFSET_MASK;
     79 		*hp = (uint16_t)(uintptr_t)src;
     80 		cpy = src - offset;
     81 		if (cpy >= (uchar_t *)s_start && cpy != src &&
     82 		    src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) {
     83 			*copymap |= copymask;
     84 			for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++)
     85 				if (src[mlen] != cpy[mlen])
     86 					break;
     87 			*dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) |
     88 			    (offset >> NBBY);
     89 			*dst++ = (uchar_t)offset;
     90 			src += mlen;
     91 		} else {
     92 			*dst++ = *src++;
     93 		}
     94 	}
     95 	return (dst - (uchar_t *)d_start);
     96 }
     97 
     98 /*ARGSUSED*/
     99 int
    100 lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
    101 {
    102 	uchar_t *src = s_start;
    103 	uchar_t *dst = d_start;
    104 	uchar_t *d_end = (uchar_t *)d_start + d_len;
    105 	uchar_t *cpy;
    106 	uchar_t copymap = 0;
    107 	int copymask = 1 << (NBBY - 1);
    108 
    109 	while (dst < d_end) {
    110 		if ((copymask <<= 1) == (1 << NBBY)) {
    111 			copymask = 1;
    112 			copymap = *src++;
    113 		}
    114 		if (copymap & copymask) {
    115 			int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
    116 			int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
    117 			src += 2;
    118 			if ((cpy = dst - offset) < (uchar_t *)d_start)
    119 				return (-1);
    120 			if (mlen > (d_end - dst))
    121 				mlen = d_end - dst;
    122 			while (--mlen >= 0)
    123 				*dst++ = *cpy++;
    124 		} else {
    125 			*dst++ = *src++;
    126 		}
    127 	}
    128 	return (0);
    129 }
    130