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