zfs_lzjb.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /*
  20. * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
  21. * Use is subject to license terms.
  22. */
  23. #include <common.h>
  24. #include <malloc.h>
  25. #include <linux/stat.h>
  26. #include <linux/time.h>
  27. #include <linux/ctype.h>
  28. #include <asm/byteorder.h>
  29. #include "zfs_common.h"
  30. #include <zfs/zfs.h>
  31. #include <zfs/zio.h>
  32. #include <zfs/dnode.h>
  33. #include <zfs/uberblock_impl.h>
  34. #include <zfs/vdev_impl.h>
  35. #include <zfs/zio_checksum.h>
  36. #include <zfs/zap_impl.h>
  37. #include <zfs/zap_leaf.h>
  38. #include <zfs/zfs_znode.h>
  39. #include <zfs/dmu.h>
  40. #include <zfs/dmu_objset.h>
  41. #include <zfs/dsl_dir.h>
  42. #include <zfs/dsl_dataset.h>
  43. #define MATCH_BITS 6
  44. #define MATCH_MIN 3
  45. #define OFFSET_MASK ((1 << (16 - MATCH_BITS)) - 1)
  46. /*
  47. * Decompression Entry - lzjb
  48. */
  49. #ifndef NBBY
  50. #define NBBY 8
  51. #endif
  52. int
  53. lzjb_decompress(void *s_start, void *d_start, uint32_t s_len,
  54. uint32_t d_len)
  55. {
  56. uint8_t *src = s_start;
  57. uint8_t *dst = d_start;
  58. uint8_t *d_end = (uint8_t *) d_start + d_len;
  59. uint8_t *s_end = (uint8_t *) s_start + s_len;
  60. uint8_t *cpy, copymap = 0;
  61. int copymask = 1 << (NBBY - 1);
  62. while (dst < d_end && src < s_end) {
  63. if ((copymask <<= 1) == (1 << NBBY)) {
  64. copymask = 1;
  65. copymap = *src++;
  66. }
  67. if (src >= s_end) {
  68. printf("lzjb decompression failed\n");
  69. return ZFS_ERR_BAD_FS;
  70. }
  71. if (copymap & copymask) {
  72. int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
  73. int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
  74. src += 2;
  75. cpy = dst - offset;
  76. if (src > s_end || cpy < (uint8_t *) d_start) {
  77. printf("lzjb decompression failed\n");
  78. return ZFS_ERR_BAD_FS;
  79. }
  80. while (--mlen >= 0 && dst < d_end)
  81. *dst++ = *cpy++;
  82. } else {
  83. *dst++ = *src++;
  84. }
  85. }
  86. if (dst < d_end) {
  87. printf("lzjb decompression failed\n");
  88. return ZFS_ERR_BAD_FS;
  89. }
  90. return ZFS_ERR_NONE;
  91. }