filter_decoder.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file filter_decoder.c
  5. /// \brief Filter ID mapping to filter-specific functions
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "filter_decoder.h"
  11. #include "filter_common.h"
  12. #include "lzma_decoder.h"
  13. #include "lzma2_decoder.h"
  14. #include "simple_decoder.h"
  15. #include "delta_decoder.h"
  16. typedef struct {
  17. /// Filter ID
  18. lzma_vli id;
  19. /// Initializes the filter encoder and calls lzma_next_filter_init()
  20. /// for filters + 1.
  21. lzma_init_function init;
  22. /// Calculates memory usage of the encoder. If the options are
  23. /// invalid, UINT64_MAX is returned.
  24. uint64_t (*memusage)(const void *options);
  25. /// Decodes Filter Properties.
  26. ///
  27. /// \return - LZMA_OK: Properties decoded successfully.
  28. /// - LZMA_OPTIONS_ERROR: Unsupported properties
  29. /// - LZMA_MEM_ERROR: Memory allocation failed.
  30. lzma_ret (*props_decode)(
  31. void **options, const lzma_allocator *allocator,
  32. const uint8_t *props, size_t props_size);
  33. } lzma_filter_decoder;
  34. static const lzma_filter_decoder decoders[] = {
  35. #ifdef HAVE_DECODER_LZMA1
  36. {
  37. .id = LZMA_FILTER_LZMA1,
  38. .init = &lzma_lzma_decoder_init,
  39. .memusage = &lzma_lzma_decoder_memusage,
  40. .props_decode = &lzma_lzma_props_decode,
  41. },
  42. {
  43. .id = LZMA_FILTER_LZMA1EXT,
  44. .init = &lzma_lzma_decoder_init,
  45. .memusage = &lzma_lzma_decoder_memusage,
  46. .props_decode = &lzma_lzma_props_decode,
  47. },
  48. #endif
  49. #ifdef HAVE_DECODER_LZMA2
  50. {
  51. .id = LZMA_FILTER_LZMA2,
  52. .init = &lzma_lzma2_decoder_init,
  53. .memusage = &lzma_lzma2_decoder_memusage,
  54. .props_decode = &lzma_lzma2_props_decode,
  55. },
  56. #endif
  57. #ifdef HAVE_DECODER_X86
  58. {
  59. .id = LZMA_FILTER_X86,
  60. .init = &lzma_simple_x86_decoder_init,
  61. .memusage = NULL,
  62. .props_decode = &lzma_simple_props_decode,
  63. },
  64. #endif
  65. #ifdef HAVE_DECODER_POWERPC
  66. {
  67. .id = LZMA_FILTER_POWERPC,
  68. .init = &lzma_simple_powerpc_decoder_init,
  69. .memusage = NULL,
  70. .props_decode = &lzma_simple_props_decode,
  71. },
  72. #endif
  73. #ifdef HAVE_DECODER_IA64
  74. {
  75. .id = LZMA_FILTER_IA64,
  76. .init = &lzma_simple_ia64_decoder_init,
  77. .memusage = NULL,
  78. .props_decode = &lzma_simple_props_decode,
  79. },
  80. #endif
  81. #ifdef HAVE_DECODER_ARM
  82. {
  83. .id = LZMA_FILTER_ARM,
  84. .init = &lzma_simple_arm_decoder_init,
  85. .memusage = NULL,
  86. .props_decode = &lzma_simple_props_decode,
  87. },
  88. #endif
  89. #ifdef HAVE_DECODER_ARMTHUMB
  90. {
  91. .id = LZMA_FILTER_ARMTHUMB,
  92. .init = &lzma_simple_armthumb_decoder_init,
  93. .memusage = NULL,
  94. .props_decode = &lzma_simple_props_decode,
  95. },
  96. #endif
  97. #ifdef HAVE_DECODER_ARM64
  98. {
  99. .id = LZMA_FILTER_ARM64,
  100. .init = &lzma_simple_arm64_decoder_init,
  101. .memusage = NULL,
  102. .props_decode = &lzma_simple_props_decode,
  103. },
  104. #endif
  105. #ifdef HAVE_DECODER_SPARC
  106. {
  107. .id = LZMA_FILTER_SPARC,
  108. .init = &lzma_simple_sparc_decoder_init,
  109. .memusage = NULL,
  110. .props_decode = &lzma_simple_props_decode,
  111. },
  112. #endif
  113. #ifdef HAVE_DECODER_RISCV
  114. {
  115. .id = LZMA_FILTER_RISCV,
  116. .init = &lzma_simple_riscv_decoder_init,
  117. .memusage = NULL,
  118. .props_decode = &lzma_simple_props_decode,
  119. },
  120. #endif
  121. #ifdef HAVE_DECODER_DELTA
  122. {
  123. .id = LZMA_FILTER_DELTA,
  124. .init = &lzma_delta_decoder_init,
  125. .memusage = &lzma_delta_coder_memusage,
  126. .props_decode = &lzma_delta_props_decode,
  127. },
  128. #endif
  129. };
  130. static const lzma_filter_decoder *
  131. decoder_find(lzma_vli id)
  132. {
  133. for (size_t i = 0; i < ARRAY_SIZE(decoders); ++i)
  134. if (decoders[i].id == id)
  135. return decoders + i;
  136. return NULL;
  137. }
  138. // lzma_filter_coder begins with the same members as lzma_filter_decoder.
  139. // This function is a wrapper with a type that is compatible with the
  140. // typedef of lzma_filter_find in filter_common.h.
  141. static const lzma_filter_coder *
  142. coder_find(lzma_vli id)
  143. {
  144. return (const lzma_filter_coder *)decoder_find(id);
  145. }
  146. extern LZMA_API(lzma_bool)
  147. lzma_filter_decoder_is_supported(lzma_vli id)
  148. {
  149. return decoder_find(id) != NULL;
  150. }
  151. extern lzma_ret
  152. lzma_raw_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  153. const lzma_filter *options)
  154. {
  155. return lzma_raw_coder_init(next, allocator,
  156. options, &coder_find, false);
  157. }
  158. extern LZMA_API(lzma_ret)
  159. lzma_raw_decoder(lzma_stream *strm, const lzma_filter *options)
  160. {
  161. lzma_next_strm_init(lzma_raw_decoder_init, strm, options);
  162. strm->internal->supported_actions[LZMA_RUN] = true;
  163. strm->internal->supported_actions[LZMA_FINISH] = true;
  164. return LZMA_OK;
  165. }
  166. extern LZMA_API(uint64_t)
  167. lzma_raw_decoder_memusage(const lzma_filter *filters)
  168. {
  169. return lzma_raw_coder_memusage(&coder_find, filters);
  170. }
  171. extern LZMA_API(lzma_ret)
  172. lzma_properties_decode(lzma_filter *filter, const lzma_allocator *allocator,
  173. const uint8_t *props, size_t props_size)
  174. {
  175. // Make it always NULL so that the caller can always safely free() it.
  176. filter->options = NULL;
  177. const lzma_filter_decoder *const fd = decoder_find(filter->id);
  178. if (fd == NULL)
  179. return LZMA_OPTIONS_ERROR;
  180. if (fd->props_decode == NULL)
  181. return props_size == 0 ? LZMA_OK : LZMA_OPTIONS_ERROR;
  182. return fd->props_decode(
  183. &filter->options, allocator, props, props_size);
  184. }