xz_private.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Private includes and definitions
  3. *
  4. * Author: Lasse Collin <[email protected]>
  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. # include <linux/xz.h>
  13. # include <linux/kernel.h>
  14. # include <asm/unaligned.h>
  15. /* XZ_PREBOOT may be defined only via decompress_unxz.c. */
  16. # ifndef XZ_PREBOOT
  17. # include <linux/slab.h>
  18. # include <linux/vmalloc.h>
  19. # include <linux/string.h>
  20. # ifdef CONFIG_XZ_DEC_X86
  21. # define XZ_DEC_X86
  22. # endif
  23. # ifdef CONFIG_XZ_DEC_POWERPC
  24. # define XZ_DEC_POWERPC
  25. # endif
  26. # ifdef CONFIG_XZ_DEC_IA64
  27. # define XZ_DEC_IA64
  28. # endif
  29. # ifdef CONFIG_XZ_DEC_ARM
  30. # define XZ_DEC_ARM
  31. # endif
  32. # ifdef CONFIG_XZ_DEC_ARMTHUMB
  33. # define XZ_DEC_ARMTHUMB
  34. # endif
  35. # ifdef CONFIG_XZ_DEC_SPARC
  36. # define XZ_DEC_SPARC
  37. # endif
  38. # ifdef CONFIG_XZ_DEC_MICROLZMA
  39. # define XZ_DEC_MICROLZMA
  40. # endif
  41. # define memeq(a, b, size) (memcmp(a, b, size) == 0)
  42. # define memzero(buf, size) memset(buf, 0, size)
  43. # endif
  44. # define get_le32(p) le32_to_cpup((const uint32_t *)(p))
  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. #ifndef CRC32_POLY_LE
  100. #define CRC32_POLY_LE 0xedb88320
  101. #endif
  102. /*
  103. * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
  104. * before calling xz_dec_lzma2_run().
  105. */
  106. XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
  107. uint32_t dict_max);
  108. /*
  109. * Decode the LZMA2 properties (one byte) and reset the decoder. Return
  110. * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
  111. * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
  112. * decoder doesn't support.
  113. */
  114. XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s,
  115. uint8_t props);
  116. /* Decode raw LZMA2 stream from b->in to b->out. */
  117. XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
  118. struct xz_buf *b);
  119. /* Free the memory allocated for the LZMA2 decoder. */
  120. XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
  121. #ifdef XZ_DEC_BCJ
  122. /*
  123. * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
  124. * calling xz_dec_bcj_run().
  125. */
  126. XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool single_call);
  127. /*
  128. * Decode the Filter ID of a BCJ filter. This implementation doesn't
  129. * support custom start offsets, so no decoding of Filter Properties
  130. * is needed. Returns XZ_OK if the given Filter ID is supported.
  131. * Otherwise XZ_OPTIONS_ERROR is returned.
  132. */
  133. XZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);
  134. /*
  135. * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
  136. * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
  137. * must be called directly.
  138. */
  139. XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
  140. struct xz_dec_lzma2 *lzma2,
  141. struct xz_buf *b);
  142. /* Free the memory allocated for the BCJ filters. */
  143. #define xz_dec_bcj_end(s) kfree(s)
  144. #endif
  145. #endif