xref: /openbmc/u-boot/fs/zfs/zfs_lzjb.c (revision 7f6a6db6)
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 /*
8  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
9  * Use is subject to license terms.
10  */
11 
12 #include <common.h>
13 #include <malloc.h>
14 #include <linux/stat.h>
15 #include <linux/time.h>
16 #include <linux/ctype.h>
17 #include <asm/byteorder.h>
18 #include "zfs_common.h"
19 
20 #include <zfs/zfs.h>
21 #include <zfs/zio.h>
22 #include <zfs/dnode.h>
23 #include <zfs/uberblock_impl.h>
24 #include <zfs/vdev_impl.h>
25 #include <zfs/zio_checksum.h>
26 #include <zfs/zap_impl.h>
27 #include <zfs/zap_leaf.h>
28 #include <zfs/zfs_znode.h>
29 #include <zfs/dmu.h>
30 #include <zfs/dmu_objset.h>
31 #include <zfs/dsl_dir.h>
32 #include <zfs/dsl_dataset.h>
33 
34 #define	MATCH_BITS	6
35 #define	MATCH_MIN	3
36 #define	OFFSET_MASK	((1 << (16 - MATCH_BITS)) - 1)
37 
38 /*
39  * Decompression Entry - lzjb
40  */
41 #ifndef	NBBY
42 #define	NBBY	8
43 #endif
44 
45 int
46 lzjb_decompress(void *s_start, void *d_start, uint32_t s_len,
47 				uint32_t d_len)
48 {
49 	uint8_t *src = s_start;
50 	uint8_t *dst = d_start;
51 	uint8_t *d_end = (uint8_t *) d_start + d_len;
52 	uint8_t *s_end = (uint8_t *) s_start + s_len;
53 	uint8_t *cpy, copymap = 0;
54 	int copymask = 1 << (NBBY - 1);
55 
56 	while (dst < d_end && src < s_end) {
57 		if ((copymask <<= 1) == (1 << NBBY)) {
58 			copymask = 1;
59 			copymap = *src++;
60 		}
61 		if (src >= s_end) {
62 			printf("lzjb decompression failed\n");
63 			return ZFS_ERR_BAD_FS;
64 		}
65 		if (copymap & copymask) {
66 			int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
67 			int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
68 			src += 2;
69 			cpy = dst - offset;
70 			if (src > s_end || cpy < (uint8_t *) d_start) {
71 				printf("lzjb decompression failed\n");
72 				return ZFS_ERR_BAD_FS;
73 			}
74 			while (--mlen >= 0 && dst < d_end)
75 				*dst++ = *cpy++;
76 		} else {
77 			*dst++ = *src++;
78 		}
79 	}
80 	if (dst < d_end) {
81 		printf("lzjb decompression failed\n");
82 		return ZFS_ERR_BAD_FS;
83 	}
84 	return ZFS_ERR_NONE;
85 }
86