filter_encoder.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file filter_decoder.c
  4. /// \brief Filter ID mapping to filter-specific functions
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "filter_encoder.h"
  13. #include "filter_common.h"
  14. #include "lzma_encoder.h"
  15. #include "lzma2_encoder.h"
  16. #include "simple_encoder.h"
  17. #include "delta_encoder.h"
  18. typedef struct {
  19. /// Filter ID
  20. lzma_vli id;
  21. /// Initializes the filter encoder and calls lzma_next_filter_init()
  22. /// for filters + 1.
  23. lzma_init_function init;
  24. /// Calculates memory usage of the encoder. If the options are
  25. /// invalid, UINT64_MAX is returned.
  26. uint64_t (*memusage)(const void *options);
  27. /// Calculates the recommended Uncompressed Size for .xz Blocks to
  28. /// which the input data can be split to make multithreaded
  29. /// encoding possible. If this is NULL, it is assumed that
  30. /// the encoder is fast enough with single thread.
  31. uint64_t (*block_size)(const void *options);
  32. /// Tells the size of the Filter Properties field. If options are
  33. /// invalid, UINT32_MAX is returned. If this is NULL, props_size_fixed
  34. /// is used.
  35. lzma_ret (*props_size_get)(uint32_t *size, const void *options);
  36. uint32_t props_size_fixed;
  37. /// Encodes Filter Properties.
  38. ///
  39. /// \return - LZMA_OK: Properties encoded successfully.
  40. /// - LZMA_OPTIONS_ERROR: Unsupported options
  41. /// - LZMA_PROG_ERROR: Invalid options or not enough
  42. /// output space
  43. lzma_ret (*props_encode)(const void *options, uint8_t *out);
  44. } lzma_filter_encoder;
  45. static const lzma_filter_encoder encoders[] = {
  46. #ifdef HAVE_ENCODER_LZMA1
  47. {
  48. .id = LZMA_FILTER_LZMA1,
  49. .init = &lzma_lzma_encoder_init,
  50. .memusage = &lzma_lzma_encoder_memusage,
  51. .block_size = NULL, // FIXME
  52. .props_size_get = NULL,
  53. .props_size_fixed = 5,
  54. .props_encode = &lzma_lzma_props_encode,
  55. },
  56. #endif
  57. #ifdef HAVE_ENCODER_LZMA2
  58. {
  59. .id = LZMA_FILTER_LZMA2,
  60. .init = &lzma_lzma2_encoder_init,
  61. .memusage = &lzma_lzma2_encoder_memusage,
  62. .block_size = &lzma_lzma2_block_size, // FIXME
  63. .props_size_get = NULL,
  64. .props_size_fixed = 1,
  65. .props_encode = &lzma_lzma2_props_encode,
  66. },
  67. #endif
  68. #ifdef HAVE_ENCODER_X86
  69. {
  70. .id = LZMA_FILTER_X86,
  71. .init = &lzma_simple_x86_encoder_init,
  72. .memusage = NULL,
  73. .block_size = NULL,
  74. .props_size_get = &lzma_simple_props_size,
  75. .props_encode = &lzma_simple_props_encode,
  76. },
  77. #endif
  78. #ifdef HAVE_ENCODER_POWERPC
  79. {
  80. .id = LZMA_FILTER_POWERPC,
  81. .init = &lzma_simple_powerpc_encoder_init,
  82. .memusage = NULL,
  83. .block_size = NULL,
  84. .props_size_get = &lzma_simple_props_size,
  85. .props_encode = &lzma_simple_props_encode,
  86. },
  87. #endif
  88. #ifdef HAVE_ENCODER_IA64
  89. {
  90. .id = LZMA_FILTER_IA64,
  91. .init = &lzma_simple_ia64_encoder_init,
  92. .memusage = NULL,
  93. .block_size = NULL,
  94. .props_size_get = &lzma_simple_props_size,
  95. .props_encode = &lzma_simple_props_encode,
  96. },
  97. #endif
  98. #ifdef HAVE_ENCODER_ARM
  99. {
  100. .id = LZMA_FILTER_ARM,
  101. .init = &lzma_simple_arm_encoder_init,
  102. .memusage = NULL,
  103. .block_size = NULL,
  104. .props_size_get = &lzma_simple_props_size,
  105. .props_encode = &lzma_simple_props_encode,
  106. },
  107. #endif
  108. #ifdef HAVE_ENCODER_ARMTHUMB
  109. {
  110. .id = LZMA_FILTER_ARMTHUMB,
  111. .init = &lzma_simple_armthumb_encoder_init,
  112. .memusage = NULL,
  113. .block_size = NULL,
  114. .props_size_get = &lzma_simple_props_size,
  115. .props_encode = &lzma_simple_props_encode,
  116. },
  117. #endif
  118. #ifdef HAVE_ENCODER_SPARC
  119. {
  120. .id = LZMA_FILTER_SPARC,
  121. .init = &lzma_simple_sparc_encoder_init,
  122. .memusage = NULL,
  123. .block_size = NULL,
  124. .props_size_get = &lzma_simple_props_size,
  125. .props_encode = &lzma_simple_props_encode,
  126. },
  127. #endif
  128. #ifdef HAVE_ENCODER_DELTA
  129. {
  130. .id = LZMA_FILTER_DELTA,
  131. .init = &lzma_delta_encoder_init,
  132. .memusage = &lzma_delta_coder_memusage,
  133. .block_size = NULL,
  134. .props_size_get = NULL,
  135. .props_size_fixed = 1,
  136. .props_encode = &lzma_delta_props_encode,
  137. },
  138. #endif
  139. };
  140. static const lzma_filter_encoder *
  141. encoder_find(lzma_vli id)
  142. {
  143. for (size_t i = 0; i < ARRAY_SIZE(encoders); ++i)
  144. if (encoders[i].id == id)
  145. return encoders + i;
  146. return NULL;
  147. }
  148. extern LZMA_API(lzma_bool)
  149. lzma_filter_encoder_is_supported(lzma_vli id)
  150. {
  151. return encoder_find(id) != NULL;
  152. }
  153. extern LZMA_API(lzma_ret)
  154. lzma_filters_update(lzma_stream *strm, const lzma_filter *filters)
  155. {
  156. if (strm->internal->next.update == NULL)
  157. return LZMA_PROG_ERROR;
  158. // Validate the filter chain.
  159. if (lzma_raw_encoder_memusage(filters) == UINT64_MAX)
  160. return LZMA_OPTIONS_ERROR;
  161. // The actual filter chain in the encoder is reversed. Some things
  162. // still want the normal order chain, so we provide both.
  163. size_t count = 1;
  164. while (filters[count].id != LZMA_VLI_UNKNOWN)
  165. ++count;
  166. lzma_filter reversed_filters[LZMA_FILTERS_MAX + 1];
  167. for (size_t i = 0; i < count; ++i)
  168. reversed_filters[count - i - 1] = filters[i];
  169. reversed_filters[count].id = LZMA_VLI_UNKNOWN;
  170. return strm->internal->next.update(strm->internal->next.coder,
  171. strm->allocator, filters, reversed_filters);
  172. }
  173. extern lzma_ret
  174. lzma_raw_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  175. const lzma_filter *options)
  176. {
  177. return lzma_raw_coder_init(next, allocator,
  178. options, (lzma_filter_find)(&encoder_find), true);
  179. }
  180. extern LZMA_API(lzma_ret)
  181. lzma_raw_encoder(lzma_stream *strm, const lzma_filter *options)
  182. {
  183. lzma_next_strm_init(lzma_raw_coder_init, strm, options,
  184. (lzma_filter_find)(&encoder_find), true);
  185. strm->internal->supported_actions[LZMA_RUN] = true;
  186. strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
  187. strm->internal->supported_actions[LZMA_FINISH] = true;
  188. return LZMA_OK;
  189. }
  190. extern LZMA_API(uint64_t)
  191. lzma_raw_encoder_memusage(const lzma_filter *filters)
  192. {
  193. return lzma_raw_coder_memusage(
  194. (lzma_filter_find)(&encoder_find), filters);
  195. }
  196. extern uint64_t
  197. lzma_mt_block_size(const lzma_filter *filters)
  198. {
  199. uint64_t max = 0;
  200. for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
  201. const lzma_filter_encoder *const fe
  202. = encoder_find(filters[i].id);
  203. if (fe->block_size != NULL) {
  204. const uint64_t size
  205. = fe->block_size(filters[i].options);
  206. if (size == 0)
  207. return 0;
  208. if (size > max)
  209. max = size;
  210. }
  211. }
  212. return max;
  213. }
  214. extern LZMA_API(lzma_ret)
  215. lzma_properties_size(uint32_t *size, const lzma_filter *filter)
  216. {
  217. const lzma_filter_encoder *const fe = encoder_find(filter->id);
  218. if (fe == NULL) {
  219. // Unknown filter - if the Filter ID is a proper VLI,
  220. // return LZMA_OPTIONS_ERROR instead of LZMA_PROG_ERROR,
  221. // because it's possible that we just don't have support
  222. // compiled in for the requested filter.
  223. return filter->id <= LZMA_VLI_MAX
  224. ? LZMA_OPTIONS_ERROR : LZMA_PROG_ERROR;
  225. }
  226. if (fe->props_size_get == NULL) {
  227. // No props_size_get() function, use props_size_fixed.
  228. *size = fe->props_size_fixed;
  229. return LZMA_OK;
  230. }
  231. return fe->props_size_get(size, filter->options);
  232. }
  233. extern LZMA_API(lzma_ret)
  234. lzma_properties_encode(const lzma_filter *filter, uint8_t *props)
  235. {
  236. const lzma_filter_encoder *const fe = encoder_find(filter->id);
  237. if (fe == NULL)
  238. return LZMA_PROG_ERROR;
  239. if (fe->props_encode == NULL)
  240. return LZMA_OK;
  241. return fe->props_encode(filter->options, props);
  242. }