1b1b8ab34Slling /*
2b1b8ab34Slling  *  GRUB  --  GRand Unified Bootloader
3b1b8ab34Slling  *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
4b1b8ab34Slling  *
5b1b8ab34Slling  *  This program is free software; you can redistribute it and/or modify
6b1b8ab34Slling  *  it under the terms of the GNU General Public License as published by
7b1b8ab34Slling  *  the Free Software Foundation; either version 2 of the License, or
8b1b8ab34Slling  *  (at your option) any later version.
9b1b8ab34Slling  *
10b1b8ab34Slling  *  This program is distributed in the hope that it will be useful,
11b1b8ab34Slling  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12b1b8ab34Slling  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13b1b8ab34Slling  *  GNU General Public License for more details.
14b1b8ab34Slling  *
15b1b8ab34Slling  *  You should have received a copy of the GNU General Public License
16b1b8ab34Slling  *  along with this program; if not, write to the Free Software
17b1b8ab34Slling  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18b1b8ab34Slling  */
19b1b8ab34Slling /*
20b1b8ab34Slling  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
21b1b8ab34Slling  * Use is subject to license terms.
22b1b8ab34Slling  */
23b1b8ab34Slling 
24b1b8ab34Slling #include "fsys_zfs.h"
25b1b8ab34Slling 
26b1b8ab34Slling #define	MATCH_BITS	6
27b1b8ab34Slling #define	MATCH_MIN	3
28b1b8ab34Slling #define	OFFSET_MASK	((1 << (16 - MATCH_BITS)) - 1)
29b1b8ab34Slling 
30b1b8ab34Slling 
31b1b8ab34Slling /*ARGSUSED*/
32b1b8ab34Slling int
lzjb_decompress(void * s_start,void * d_start,size_t s_len,size_t d_len)33b1b8ab34Slling lzjb_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len)
34b1b8ab34Slling {
35b1b8ab34Slling 	uchar_t *src = s_start;
36b1b8ab34Slling 	uchar_t *dst = d_start;
37b1b8ab34Slling 	uchar_t *d_end = (uchar_t *)d_start + d_len;
38b1b8ab34Slling 	uchar_t *cpy, copymap;
39b1b8ab34Slling 	int copymask = 1 << (NBBY - 1);
40b1b8ab34Slling 
41b1b8ab34Slling 	while (dst < d_end) {
42b1b8ab34Slling 		if ((copymask <<= 1) == (1 << NBBY)) {
43b1b8ab34Slling 			copymask = 1;
44b1b8ab34Slling 			copymap = *src++;
45b1b8ab34Slling 		}
46b1b8ab34Slling 		if (copymap & copymask) {
47b1b8ab34Slling 			int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
48b1b8ab34Slling 			int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
49b1b8ab34Slling 			src += 2;
50b1b8ab34Slling 			if ((cpy = dst - offset) < (uchar_t *)d_start)
51b1b8ab34Slling 				return (-1);
52b1b8ab34Slling 			while (--mlen >= 0 && dst < d_end)
53b1b8ab34Slling 				*dst++ = *cpy++;
54b1b8ab34Slling 		} else {
55b1b8ab34Slling 			*dst++ = *src++;
56b1b8ab34Slling 		}
57b1b8ab34Slling 	}
58b1b8ab34Slling 	return (0);
59b1b8ab34Slling }
60