xz_private.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. # 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. # define memeq(a, b, size) (memcmp(a, b, size) == 0)
  39. # define memzero(buf, size) memset(buf, 0, size)
  40. # endif
  41. # define get_le32(p) le32_to_cpup((const uint32_t *)(p))
  42. #else
  43. /*
  44. * For userspace builds, use a separate header to define the required
  45. * macros and functions. This makes it easier to adapt the code into
  46. * different environments and avoids clutter in the Linux kernel tree.
  47. */
  48. # include "xz_config.h"
  49. #endif
  50. /* If no specific decoding mode is requested, enable support for all modes. */
  51. #if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \
  52. && !defined(XZ_DEC_DYNALLOC)
  53. # define XZ_DEC_SINGLE
  54. # define XZ_DEC_PREALLOC
  55. # define XZ_DEC_DYNALLOC
  56. #endif
  57. /*
  58. * The DEC_IS_foo(mode) macros are used in "if" statements. If only some
  59. * of the supported modes are enabled, these macros will evaluate to true or
  60. * false at compile time and thus allow the compiler to omit unneeded code.
  61. */
  62. #ifdef XZ_DEC_SINGLE
  63. # define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)
  64. #else
  65. # define DEC_IS_SINGLE(mode) (false)
  66. #endif
  67. #ifdef XZ_DEC_PREALLOC
  68. # define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)
  69. #else
  70. # define DEC_IS_PREALLOC(mode) (false)
  71. #endif
  72. #ifdef XZ_DEC_DYNALLOC
  73. # define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)
  74. #else
  75. # define DEC_IS_DYNALLOC(mode) (false)
  76. #endif
  77. #if !defined(XZ_DEC_SINGLE)
  78. # define DEC_IS_MULTI(mode) (true)
  79. #elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC)
  80. # define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE)
  81. #else
  82. # define DEC_IS_MULTI(mode) (false)
  83. #endif
  84. /*
  85. * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ.
  86. * XZ_DEC_BCJ is used to enable generic support for BCJ decoders.
  87. */
  88. #ifndef XZ_DEC_BCJ
  89. # if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
  90. || defined(XZ_DEC_IA64) || defined(XZ_DEC_ARM) \
  91. || defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
  92. || defined(XZ_DEC_SPARC)
  93. # define XZ_DEC_BCJ
  94. # endif
  95. #endif
  96. /*
  97. * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
  98. * before calling xz_dec_lzma2_run().
  99. */
  100. XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
  101. uint32_t dict_max);
  102. /*
  103. * Decode the LZMA2 properties (one byte) and reset the decoder. Return
  104. * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
  105. * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
  106. * decoder doesn't support.
  107. */
  108. XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s,
  109. uint8_t props);
  110. /* Decode raw LZMA2 stream from b->in to b->out. */
  111. XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
  112. struct xz_buf *b);
  113. /* Free the memory allocated for the LZMA2 decoder. */
  114. XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
  115. #ifdef XZ_DEC_BCJ
  116. /*
  117. * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
  118. * calling xz_dec_bcj_run().
  119. */
  120. XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool single_call);
  121. /*
  122. * Decode the Filter ID of a BCJ filter. This implementation doesn't
  123. * support custom start offsets, so no decoding of Filter Properties
  124. * is needed. Returns XZ_OK if the given Filter ID is supported.
  125. * Otherwise XZ_OPTIONS_ERROR is returned.
  126. */
  127. XZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);
  128. /*
  129. * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
  130. * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
  131. * must be called directly.
  132. */
  133. XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
  134. 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