simple_coder.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file simple_coder.c
  5. /// \brief Wrapper for simple filters
  6. ///
  7. /// Simple filters don't change the size of the data i.e. number of bytes
  8. /// in equals the number of bytes out.
  9. //
  10. // Author: Lasse Collin
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #include "simple_private.h"
  14. /// Copied or encodes/decodes more data to out[].
  15. static lzma_ret
  16. copy_or_code(lzma_simple_coder *coder, const lzma_allocator *allocator,
  17. const uint8_t *restrict in, size_t *restrict in_pos,
  18. size_t in_size, uint8_t *restrict out,
  19. size_t *restrict out_pos, size_t out_size, lzma_action action)
  20. {
  21. assert(!coder->end_was_reached);
  22. if (coder->next.code == NULL) {
  23. lzma_bufcpy(in, in_pos, in_size, out, out_pos, out_size);
  24. // Check if end of stream was reached.
  25. if (coder->is_encoder && action == LZMA_FINISH
  26. && *in_pos == in_size)
  27. coder->end_was_reached = true;
  28. } else {
  29. // Call the next coder in the chain to provide us some data.
  30. const lzma_ret ret = coder->next.code(
  31. coder->next.coder, allocator,
  32. in, in_pos, in_size,
  33. out, out_pos, out_size, action);
  34. if (ret == LZMA_STREAM_END) {
  35. assert(!coder->is_encoder
  36. || action == LZMA_FINISH);
  37. coder->end_was_reached = true;
  38. } else if (ret != LZMA_OK) {
  39. return ret;
  40. }
  41. }
  42. return LZMA_OK;
  43. }
  44. static size_t
  45. call_filter(lzma_simple_coder *coder, uint8_t *buffer, size_t size)
  46. {
  47. const size_t filtered = coder->filter(coder->simple,
  48. coder->now_pos, coder->is_encoder,
  49. buffer, size);
  50. coder->now_pos += filtered;
  51. return filtered;
  52. }
  53. static lzma_ret
  54. simple_code(void *coder_ptr, const lzma_allocator *allocator,
  55. const uint8_t *restrict in, size_t *restrict in_pos,
  56. size_t in_size, uint8_t *restrict out,
  57. size_t *restrict out_pos, size_t out_size, lzma_action action)
  58. {
  59. lzma_simple_coder *coder = coder_ptr;
  60. // TODO: Add partial support for LZMA_SYNC_FLUSH. We can support it
  61. // in cases when the filter is able to filter everything. With most
  62. // simple filters it can be done at offset that is a multiple of 2,
  63. // 4, or 16. With x86 filter, it needs good luck, and thus cannot
  64. // be made to work predictably.
  65. if (action == LZMA_SYNC_FLUSH)
  66. return LZMA_OPTIONS_ERROR;
  67. // Flush already filtered data from coder->buffer[] to out[].
  68. if (coder->pos < coder->filtered) {
  69. lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,
  70. out, out_pos, out_size);
  71. // If we couldn't flush all the filtered data, return to
  72. // application immediately.
  73. if (coder->pos < coder->filtered)
  74. return LZMA_OK;
  75. if (coder->end_was_reached) {
  76. assert(coder->filtered == coder->size);
  77. return LZMA_STREAM_END;
  78. }
  79. }
  80. // If we get here, there is no filtered data left in the buffer.
  81. coder->filtered = 0;
  82. assert(!coder->end_was_reached);
  83. // If there is more output space left than there is unfiltered data
  84. // in coder->buffer[], flush coder->buffer[] to out[], and copy/code
  85. // more data to out[] hopefully filling it completely. Then filter
  86. // the data in out[]. This step is where most of the data gets
  87. // filtered if the buffer sizes used by the application are reasonable.
  88. const size_t out_avail = out_size - *out_pos;
  89. const size_t buf_avail = coder->size - coder->pos;
  90. if (out_avail > buf_avail || buf_avail == 0) {
  91. // Store the old position so that we know from which byte
  92. // to start filtering.
  93. const size_t out_start = *out_pos;
  94. // Flush data from coder->buffer[] to out[], but don't reset
  95. // coder->pos and coder->size yet. This way the coder can be
  96. // restarted if the next filter in the chain returns e.g.
  97. // LZMA_MEM_ERROR.
  98. //
  99. // Do the memcpy() conditionally because out can be NULL
  100. // (in which case buf_avail is always 0). Calling memcpy()
  101. // with a null-pointer is undefined even if the third
  102. // argument is 0.
  103. if (buf_avail > 0)
  104. memcpy(out + *out_pos, coder->buffer + coder->pos,
  105. buf_avail);
  106. *out_pos += buf_avail;
  107. // Copy/Encode/Decode more data to out[].
  108. {
  109. const lzma_ret ret = copy_or_code(coder, allocator,
  110. in, in_pos, in_size,
  111. out, out_pos, out_size, action);
  112. assert(ret != LZMA_STREAM_END);
  113. if (ret != LZMA_OK)
  114. return ret;
  115. }
  116. // Filter out[] unless there is nothing to filter.
  117. // This way we avoid null pointer + 0 (undefined behavior)
  118. // when out == NULL.
  119. const size_t size = *out_pos - out_start;
  120. const size_t filtered = size == 0 ? 0 : call_filter(
  121. coder, out + out_start, size);
  122. const size_t unfiltered = size - filtered;
  123. assert(unfiltered <= coder->allocated / 2);
  124. // Now we can update coder->pos and coder->size, because
  125. // the next coder in the chain (if any) was successful.
  126. coder->pos = 0;
  127. coder->size = unfiltered;
  128. if (coder->end_was_reached) {
  129. // The last byte has been copied to out[] already.
  130. // They are left as is.
  131. coder->size = 0;
  132. } else if (unfiltered > 0) {
  133. // There is unfiltered data left in out[]. Copy it to
  134. // coder->buffer[] and rewind *out_pos appropriately.
  135. *out_pos -= unfiltered;
  136. memcpy(coder->buffer, out + *out_pos, unfiltered);
  137. }
  138. } else if (coder->pos > 0) {
  139. memmove(coder->buffer, coder->buffer + coder->pos, buf_avail);
  140. coder->size -= coder->pos;
  141. coder->pos = 0;
  142. }
  143. assert(coder->pos == 0);
  144. // If coder->buffer[] isn't empty, try to fill it by copying/decoding
  145. // more data. Then filter coder->buffer[] and copy the successfully
  146. // filtered data to out[]. It is probable, that some filtered and
  147. // unfiltered data will be left to coder->buffer[].
  148. if (coder->size > 0) {
  149. {
  150. const lzma_ret ret = copy_or_code(coder, allocator,
  151. in, in_pos, in_size,
  152. coder->buffer, &coder->size,
  153. coder->allocated, action);
  154. assert(ret != LZMA_STREAM_END);
  155. if (ret != LZMA_OK)
  156. return ret;
  157. }
  158. coder->filtered = call_filter(
  159. coder, coder->buffer, coder->size);
  160. // Everything is considered to be filtered if coder->buffer[]
  161. // contains the last bytes of the data.
  162. if (coder->end_was_reached)
  163. coder->filtered = coder->size;
  164. // Flush as much as possible.
  165. lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,
  166. out, out_pos, out_size);
  167. }
  168. // Check if we got everything done.
  169. if (coder->end_was_reached && coder->pos == coder->size)
  170. return LZMA_STREAM_END;
  171. return LZMA_OK;
  172. }
  173. static void
  174. simple_coder_end(void *coder_ptr, const lzma_allocator *allocator)
  175. {
  176. lzma_simple_coder *coder = coder_ptr;
  177. lzma_next_end(&coder->next, allocator);
  178. lzma_free(coder->simple, allocator);
  179. lzma_free(coder, allocator);
  180. return;
  181. }
  182. static lzma_ret
  183. simple_coder_update(void *coder_ptr, const lzma_allocator *allocator,
  184. const lzma_filter *filters_null lzma_attribute((__unused__)),
  185. const lzma_filter *reversed_filters)
  186. {
  187. lzma_simple_coder *coder = coder_ptr;
  188. // No update support, just call the next filter in the chain.
  189. return lzma_next_filter_update(
  190. &coder->next, allocator, reversed_filters + 1);
  191. }
  192. extern lzma_ret
  193. lzma_simple_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  194. const lzma_filter_info *filters,
  195. size_t (*filter)(void *simple, uint32_t now_pos,
  196. bool is_encoder, uint8_t *buffer, size_t size),
  197. size_t simple_size, size_t unfiltered_max,
  198. uint32_t alignment, bool is_encoder)
  199. {
  200. // Allocate memory for the lzma_simple_coder structure if needed.
  201. lzma_simple_coder *coder = next->coder;
  202. if (coder == NULL) {
  203. // Here we allocate space also for the temporary buffer. We
  204. // need twice the size of unfiltered_max, because then it
  205. // is always possible to filter at least unfiltered_max bytes
  206. // more data in coder->buffer[] if it can be filled completely.
  207. coder = lzma_alloc(sizeof(lzma_simple_coder)
  208. + 2 * unfiltered_max, allocator);
  209. if (coder == NULL)
  210. return LZMA_MEM_ERROR;
  211. next->coder = coder;
  212. next->code = &simple_code;
  213. next->end = &simple_coder_end;
  214. next->update = &simple_coder_update;
  215. coder->next = LZMA_NEXT_CODER_INIT;
  216. coder->filter = filter;
  217. coder->allocated = 2 * unfiltered_max;
  218. // Allocate memory for filter-specific data structure.
  219. if (simple_size > 0) {
  220. coder->simple = lzma_alloc(simple_size, allocator);
  221. if (coder->simple == NULL)
  222. return LZMA_MEM_ERROR;
  223. } else {
  224. coder->simple = NULL;
  225. }
  226. }
  227. if (filters[0].options != NULL) {
  228. const lzma_options_bcj *simple = filters[0].options;
  229. coder->now_pos = simple->start_offset;
  230. if (coder->now_pos & (alignment - 1))
  231. return LZMA_OPTIONS_ERROR;
  232. } else {
  233. coder->now_pos = 0;
  234. }
  235. // Reset variables.
  236. coder->is_encoder = is_encoder;
  237. coder->end_was_reached = false;
  238. coder->pos = 0;
  239. coder->filtered = 0;
  240. coder->size = 0;
  241. return lzma_next_filter_init(&coder->next, allocator, filters + 1);
  242. }