FreeBSD ZFS
The Zettabyte File System

zle.c

Go to the documentation of this file.
00001 /*
00002  * CDDL HEADER START
00003  *
00004  * The contents of this file are subject to the terms of the
00005  * Common Development and Distribution License (the "License").
00006  * You may not use this file except in compliance with the License.
00007  *
00008  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
00009  * or http://www.opensolaris.org/os/licensing.
00010  * See the License for the specific language governing permissions
00011  * and limitations under the License.
00012  *
00013  * When distributing Covered Code, include this CDDL HEADER in each
00014  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
00015  * If applicable, add the following below this CDDL HEADER, with the
00016  * fields enclosed by brackets "[]" replaced with your own identifying
00017  * information: Portions Copyright [yyyy] [name of copyright owner]
00018  *
00019  * CDDL HEADER END
00020  */
00021 
00022 /*
00023  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
00024  * Use is subject to license terms.
00025  */
00026 
00034 #include <sys/types.h>
00035 #include <sys/sysmacros.h>
00036 
00037 size_t
00038 zle_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
00039 {
00040         uchar_t *src = s_start;
00041         uchar_t *dst = d_start;
00042         uchar_t *s_end = src + s_len;
00043         uchar_t *d_end = dst + d_len;
00044 
00045         while (src < s_end && dst < d_end - 1) {
00046                 uchar_t *first = src;
00047                 uchar_t *len = dst++;
00048                 if (src[0] == 0) {
00049                         uchar_t *last = src + (256 - n);
00050                         while (src < MIN(last, s_end) && src[0] == 0)
00051                                 src++;
00052                         *len = src - first - 1 + n;
00053                 } else {
00054                         uchar_t *last = src + n;
00055                         if (d_end - dst < n)
00056                                 break;
00057                         while (src < MIN(last, s_end) - 1 && (src[0] | src[1]))
00058                                 *dst++ = *src++;
00059                         if (src[0])
00060                                 *dst++ = *src++;
00061                         *len = src - first - 1;
00062                 }
00063         }
00064         return (src == s_end ? dst - (uchar_t *)d_start : s_len);
00065 }
00066 
00067 int
00068 zle_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
00069 {
00070         uchar_t *src = s_start;
00071         uchar_t *dst = d_start;
00072         uchar_t *s_end = src + s_len;
00073         uchar_t *d_end = dst + d_len;
00074 
00075         while (src < s_end && dst < d_end) {
00076                 int len = 1 + *src++;
00077                 if (len <= n) {
00078                         while (len-- != 0)
00079                                 *dst++ = *src++;
00080                 } else {
00081                         len -= n;
00082                         while (len-- != 0)
00083                                 *dst++ = 0;
00084                 }
00085         }
00086         return (dst == d_end ? 0 : -1);
00087 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines