xz_private.h 4.4 KB

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