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