xz_private.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Private includes and definitions
  3. *
  4. * Author: Lasse Collin <lasse.collin@tukaani.org>
  5. *
  6. * This file has been put into the public domain.
  7. * You can do whatever you want with this file.
  8. */
  9. #ifndef XZ_PRIVATE_H
  10. #define XZ_PRIVATE_H
  11. #ifdef __KERNEL__
  12. /* XZ_PREBOOT may be defined only via decompress_unxz.c. */
  13. # ifndef XZ_PREBOOT
  14. # include <linux/slab.h>
  15. # include <linux/vmalloc.h>
  16. # include <linux/string.h>
  17. # define memeq(a, b, size) (memcmp(a, b, size) == 0)
  18. # define memzero(buf, size) memset(buf, 0, size)
  19. # endif
  20. # include <asm/byteorder.h>
  21. # include <asm/unaligned.h>
  22. # define get_le32(p) le32_to_cpup((const uint32_t *)(p))
  23. /* XZ_IGNORE_KCONFIG may be defined only via decompress_unxz.c. */
  24. # ifndef XZ_IGNORE_KCONFIG
  25. # ifdef CONFIG_XZ_DEC_X86
  26. # define XZ_DEC_X86
  27. # endif
  28. # ifdef CONFIG_XZ_DEC_POWERPC
  29. # define XZ_DEC_POWERPC
  30. # endif
  31. # ifdef CONFIG_XZ_DEC_IA64
  32. # define XZ_DEC_IA64
  33. # endif
  34. # ifdef CONFIG_XZ_DEC_ARM
  35. # define XZ_DEC_ARM
  36. # endif
  37. # ifdef CONFIG_XZ_DEC_ARMTHUMB
  38. # define XZ_DEC_ARMTHUMB
  39. # endif
  40. # ifdef CONFIG_XZ_DEC_SPARC
  41. # define XZ_DEC_SPARC
  42. # endif
  43. # endif
  44. # include <linux/xz.h>
  45. #else
  46. /*
  47. * For userspace builds, use a separate header to define the required
  48. * macros and functions. This makes it easier to adapt the code into
  49. * different environments and avoids clutter in the Linux kernel tree.
  50. */
  51. # include "xz_config.h"
  52. #endif
  53. /* If no specific decoding mode is requested, enable support for all modes. */
  54. #if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \
  55. && !defined(XZ_DEC_DYNALLOC)
  56. # define XZ_DEC_SINGLE
  57. # define XZ_DEC_PREALLOC
  58. # define XZ_DEC_DYNALLOC
  59. #endif
  60. /*
  61. * The DEC_IS_foo(mode) macros are used in "if" statements. If only some
  62. * of the supported modes are enabled, these macros will evaluate to true or
  63. * false at compile time and thus allow the compiler to omit unneeded code.
  64. */
  65. #ifdef XZ_DEC_SINGLE
  66. # define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)
  67. #else
  68. # define DEC_IS_SINGLE(mode) (false)
  69. #endif
  70. #ifdef XZ_DEC_PREALLOC
  71. # define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)
  72. #else
  73. # define DEC_IS_PREALLOC(mode) (false)
  74. #endif
  75. #ifdef XZ_DEC_DYNALLOC
  76. # define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)
  77. #else
  78. # define DEC_IS_DYNALLOC(mode) (false)
  79. #endif
  80. #if !defined(XZ_DEC_SINGLE)
  81. # define DEC_IS_MULTI(mode) (true)
  82. #elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC)
  83. # define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE)
  84. #else
  85. # define DEC_IS_MULTI(mode) (false)
  86. #endif
  87. /*
  88. * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ.
  89. * XZ_DEC_BCJ is used to enable generic support for BCJ decoders.
  90. */
  91. #ifndef XZ_DEC_BCJ
  92. # if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
  93. || defined(XZ_DEC_IA64) || defined(XZ_DEC_ARM) \
  94. || defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
  95. || defined(XZ_DEC_SPARC)
  96. # define XZ_DEC_BCJ
  97. # endif
  98. #endif
  99. /*
  100. * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
  101. * before calling xz_dec_lzma2_run().
  102. */
  103. XZ_EXTERN struct xz_dec_lzma2 * XZ_FUNC xz_dec_lzma2_create(
  104. enum xz_mode mode, uint32_t dict_max);
  105. /*
  106. * Decode the LZMA2 properties (one byte) and reset the decoder. Return
  107. * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
  108. * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
  109. * decoder doesn't support.
  110. */
  111. XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_lzma2_reset(
  112. struct xz_dec_lzma2 *s, uint8_t props);
  113. /* Decode raw LZMA2 stream from b->in to b->out. */
  114. XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_lzma2_run(
  115. struct xz_dec_lzma2 *s, struct xz_buf *b);
  116. /* Free the memory allocated for the LZMA2 decoder. */
  117. XZ_EXTERN void XZ_FUNC xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
  118. #ifdef XZ_DEC_BCJ
  119. /*
  120. * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
  121. * calling xz_dec_bcj_run().
  122. */
  123. XZ_EXTERN struct xz_dec_bcj * XZ_FUNC xz_dec_bcj_create(bool single_call);
  124. /*
  125. * Decode the Filter ID of a BCJ filter. This implementation doesn't
  126. * support custom start offsets, so no decoding of Filter Properties
  127. * is needed. Returns XZ_OK if the given Filter ID is supported.
  128. * Otherwise XZ_OPTIONS_ERROR is returned.
  129. */
  130. XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_bcj_reset(
  131. struct xz_dec_bcj *s, uint8_t id);
  132. /*
  133. * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
  134. * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
  135. * must be called directly.
  136. */
  137. XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_bcj_run(struct xz_dec_bcj *s,
  138. struct xz_dec_lzma2 *lzma2, struct xz_buf *b);
  139. /* Free the memory allocated for the BCJ filters. */
  140. #define xz_dec_bcj_end(s) kfree(s)
  141. #endif
  142. #endif