Add 'disabled/zfs/' from commit '329a68bffcefbab64b8aae205dc10f1e1129081f'
[grub-extras.git] / disabled / zfs / zfs_lzjb.c
blob0965d2d1f4dc42727cd2de10d42ceabbc397cb79
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
4 * Copyright 2007 Sun Microsystems, Inc.
5 * Copyright (C) 2009 Vladimir Serbinenko <phcoder@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <grub/err.h>
23 #include <grub/file.h>
24 #include <grub/mm.h>
25 #include <grub/misc.h>
26 #include <grub/disk.h>
27 #include <grub/dl.h>
28 #include <grub/types.h>
29 #include <grub/zfs/zfs.h>
30 #include <grub/zfs/zio.h>
31 #include <grub/zfs/dnode.h>
32 #include <grub/zfs/uberblock_impl.h>
33 #include <grub/zfs/vdev_impl.h>
34 #include <grub/zfs/zio_checksum.h>
35 #include <grub/zfs/zap_impl.h>
36 #include <grub/zfs/zap_leaf.h>
37 #include <grub/zfs/zfs_znode.h>
38 #include <grub/zfs/dmu.h>
39 #include <grub/zfs/dmu_objset.h>
40 #include <grub/zfs/dsl_dir.h>
41 #include <grub/zfs/dsl_dataset.h>
43 #define MATCH_BITS 6
44 #define MATCH_MIN 3
45 #define OFFSET_MASK ((1 << (16 - MATCH_BITS)) - 1)
48 * Decompression Entry - lzjb
50 #ifndef NBBY
51 #define NBBY 8
52 #endif
54 grub_err_t
55 lzjb_decompress (void *s_start, void *d_start, grub_size_t s_len,
56 grub_size_t d_len);
58 grub_err_t
59 lzjb_decompress (void *s_start, void *d_start, grub_size_t s_len,
60 grub_size_t d_len)
62 grub_uint8_t *src = s_start;
63 grub_uint8_t *dst = d_start;
64 grub_uint8_t *d_end = (grub_uint8_t *) d_start + d_len;
65 grub_uint8_t *s_end = (grub_uint8_t *) s_start + s_len;
66 grub_uint8_t *cpy, copymap = 0;
67 int copymask = 1 << (NBBY - 1);
69 while (dst < d_end && src < s_end)
71 if ((copymask <<= 1) == (1 << NBBY))
73 copymask = 1;
74 copymap = *src++;
76 if (src >= s_end)
77 return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed");
78 if (copymap & copymask)
80 int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
81 int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
82 src += 2;
83 cpy = dst - offset;
84 if (src > s_end || cpy < (grub_uint8_t *) d_start)
85 return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed");
86 while (--mlen >= 0 && dst < d_end)
87 *dst++ = *cpy++;
89 else
90 *dst++ = *src++;
92 if (dst < d_end)
93 return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed");
94 return GRUB_ERR_NONE;