simple_coder.c 8.6 KB

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