2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc.
2N/A * Copyright 2007 Sun Microsystems, Inc.
2N/A *
2N/A * GRUB is free software; you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation; either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/file.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/types.h>
2N/A#include <grub/zfs/zfs.h>
2N/A#include <grub/zfs/zio.h>
2N/A#include <grub/zfs/dnode.h>
2N/A#include <grub/zfs/uberblock_impl.h>
2N/A#include <grub/zfs/vdev_impl.h>
2N/A#include <grub/zfs/zio_checksum.h>
2N/A#include <grub/zfs/zap_impl.h>
2N/A#include <grub/zfs/zap_leaf.h>
2N/A#include <grub/zfs/zfs_znode.h>
2N/A#include <grub/zfs/dmu.h>
2N/A#include <grub/zfs/dmu_objset.h>
2N/A#include <grub/zfs/dsl_dir.h>
2N/A#include <grub/zfs/dsl_dataset.h>
2N/A
2N/A#define MATCH_BITS 6
2N/A#define MATCH_MIN 3
2N/A#define OFFSET_MASK ((1 << (16 - MATCH_BITS)) - 1)
2N/A
2N/A/*
2N/A * Decompression Entry - lzjb
2N/A */
2N/A#ifndef NBBY
2N/A#define NBBY 8
2N/A#endif
2N/A
2N/Agrub_err_t
2N/Alzjb_decompress (void *s_start, void *d_start, grub_size_t s_len,
2N/A grub_size_t d_len);
2N/A
2N/Agrub_err_t
2N/Alzjb_decompress (void *s_start, void *d_start, grub_size_t s_len,
2N/A grub_size_t d_len)
2N/A{
2N/A grub_uint8_t *src = s_start;
2N/A grub_uint8_t *dst = d_start;
2N/A grub_uint8_t *d_end = (grub_uint8_t *) d_start + d_len;
2N/A grub_uint8_t *s_end = (grub_uint8_t *) s_start + s_len;
2N/A grub_uint8_t *cpy, copymap = 0;
2N/A int copymask = 1 << (NBBY - 1);
2N/A
2N/A while (dst < d_end && src < s_end)
2N/A {
2N/A if ((copymask <<= 1) == (1 << NBBY))
2N/A {
2N/A copymask = 1;
2N/A copymap = *src++;
2N/A }
2N/A if (src >= s_end)
2N/A return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed");
2N/A if (copymap & copymask)
2N/A {
2N/A int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
2N/A int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
2N/A src += 2;
2N/A cpy = dst - offset;
2N/A if (src > s_end || cpy < (grub_uint8_t *) d_start)
2N/A return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed");
2N/A while (--mlen >= 0 && dst < d_end)
2N/A *dst++ = *cpy++;
2N/A }
2N/A else
2N/A *dst++ = *src++;
2N/A }
2N/A if (dst < d_end)
2N/A return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed");
2N/A return GRUB_ERR_NONE;
2N/A}