simple_coder.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_coder *coder, 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_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(lzma_coder *coder, 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. // TODO: Add partial support for LZMA_SYNC_FLUSH. We can support it
  62. // in cases when the filter is able to filter everything. With most
  63. // simple filters it can be done at offset that is a multiple of 2,
  64. // 4, or 16. With x86 filter, it needs good luck, and thus cannot
  65. // be made to work predictably.
  66. if (action == LZMA_SYNC_FLUSH)
  67. return LZMA_OPTIONS_ERROR;
  68. // Flush already filtered data from coder->buffer[] to out[].
  69. if (coder->pos < coder->filtered) {
  70. lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,
  71. out, out_pos, out_size);
  72. // If we couldn't flush all the filtered data, return to
  73. // application immediately.
  74. if (coder->pos < coder->filtered)
  75. return LZMA_OK;
  76. if (coder->end_was_reached) {
  77. assert(coder->filtered == coder->size);
  78. return LZMA_STREAM_END;
  79. }
  80. }
  81. // If we get here, there is no filtered data left in the buffer.
  82. coder->filtered = 0;
  83. assert(!coder->end_was_reached);
  84. // If there is more output space left than there is unfiltered data
  85. // in coder->buffer[], flush coder->buffer[] to out[], and copy/code
  86. // more data to out[] hopefully filling it completely. Then filter
  87. // the data in out[]. This step is where most of the data gets
  88. // filtered if the buffer sizes used by the application are reasonable.
  89. const size_t out_avail = out_size - *out_pos;
  90. const size_t buf_avail = coder->size - coder->pos;
  91. if (out_avail > buf_avail || buf_avail == 0) {
  92. // Store the old position so that we know from which byte
  93. // to start filtering.
  94. const size_t out_start = *out_pos;
  95. // Flush data from coder->buffer[] to out[], but don't reset
  96. // coder->pos and coder->size yet. This way the coder can be
  97. // restarted if the next filter in the chain returns e.g.
  98. // LZMA_MEM_ERROR.
  99. memcpy(out + *out_pos, coder->buffer + coder->pos, buf_avail);
  100. *out_pos += buf_avail;
  101. // Copy/Encode/Decode more data to out[].
  102. {
  103. const lzma_ret ret = copy_or_code(coder, allocator,
  104. in, in_pos, in_size,
  105. out, out_pos, out_size, action);
  106. assert(ret != LZMA_STREAM_END);
  107. if (ret != LZMA_OK)
  108. return ret;
  109. }
  110. // Filter out[].
  111. const size_t size = *out_pos - out_start;
  112. const size_t filtered = call_filter(
  113. coder, out + out_start, size);
  114. const size_t unfiltered = size - filtered;
  115. assert(unfiltered <= coder->allocated / 2);
  116. // Now we can update coder->pos and coder->size, because
  117. // the next coder in the chain (if any) was successful.
  118. coder->pos = 0;
  119. coder->size = unfiltered;
  120. if (coder->end_was_reached) {
  121. // The last byte has been copied to out[] already.
  122. // They are left as is.
  123. coder->size = 0;
  124. } else if (unfiltered > 0) {
  125. // There is unfiltered data left in out[]. Copy it to
  126. // coder->buffer[] and rewind *out_pos appropriately.
  127. *out_pos -= unfiltered;
  128. memcpy(coder->buffer, out + *out_pos, unfiltered);
  129. }
  130. } else if (coder->pos > 0) {
  131. memmove(coder->buffer, coder->buffer + coder->pos, buf_avail);
  132. coder->size -= coder->pos;
  133. coder->pos = 0;
  134. }
  135. assert(coder->pos == 0);
  136. // If coder->buffer[] isn't empty, try to fill it by copying/decoding
  137. // more data. Then filter coder->buffer[] and copy the successfully
  138. // filtered data to out[]. It is probable, that some filtered and
  139. // unfiltered data will be left to coder->buffer[].
  140. if (coder->size > 0) {
  141. {
  142. const lzma_ret ret = copy_or_code(coder, allocator,
  143. in, in_pos, in_size,
  144. coder->buffer, &coder->size,
  145. coder->allocated, action);
  146. assert(ret != LZMA_STREAM_END);
  147. if (ret != LZMA_OK)
  148. return ret;
  149. }
  150. coder->filtered = call_filter(
  151. coder, coder->buffer, coder->size);
  152. // Everything is considered to be filtered if coder->buffer[]
  153. // contains the last bytes of the data.
  154. if (coder->end_was_reached)
  155. coder->filtered = coder->size;
  156. // Flush as much as possible.
  157. lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,
  158. out, out_pos, out_size);
  159. }
  160. // Check if we got everything done.
  161. if (coder->end_was_reached && coder->pos == coder->size)
  162. return LZMA_STREAM_END;
  163. return LZMA_OK;
  164. }
  165. static void
  166. simple_coder_end(lzma_coder *coder, lzma_allocator *allocator)
  167. {
  168. lzma_next_end(&coder->next, allocator);
  169. lzma_free(coder->simple, allocator);
  170. lzma_free(coder, allocator);
  171. return;
  172. }
  173. static lzma_ret
  174. simple_coder_update(lzma_coder *coder, lzma_allocator *allocator,
  175. const lzma_filter *filters_null lzma_attribute((__unused__)),
  176. const lzma_filter *reversed_filters)
  177. {
  178. // No update support, just call the next filter in the chain.
  179. return lzma_next_filter_update(
  180. &coder->next, allocator, reversed_filters + 1);
  181. }
  182. extern lzma_ret
  183. lzma_simple_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
  184. const lzma_filter_info *filters,
  185. size_t (*filter)(lzma_simple *simple, uint32_t now_pos,
  186. bool is_encoder, uint8_t *buffer, size_t size),
  187. size_t simple_size, size_t unfiltered_max,
  188. uint32_t alignment, bool is_encoder)
  189. {
  190. // Allocate memory for the lzma_coder structure if needed.
  191. if (next->coder == NULL) {
  192. // Here we allocate space also for the temporary buffer. We
  193. // need twice the size of unfiltered_max, because then it
  194. // is always possible to filter at least unfiltered_max bytes
  195. // more data in coder->buffer[] if it can be filled completely.
  196. next->coder = lzma_alloc(sizeof(lzma_coder)
  197. + 2 * unfiltered_max, allocator);
  198. if (next->coder == NULL)
  199. return LZMA_MEM_ERROR;
  200. next->code = &simple_code;
  201. next->end = &simple_coder_end;
  202. next->update = &simple_coder_update;
  203. next->coder->next = LZMA_NEXT_CODER_INIT;
  204. next->coder->filter = filter;
  205. next->coder->allocated = 2 * unfiltered_max;
  206. // Allocate memory for filter-specific data structure.
  207. if (simple_size > 0) {
  208. next->coder->simple = lzma_alloc(
  209. simple_size, allocator);
  210. if (next->coder->simple == NULL)
  211. return LZMA_MEM_ERROR;
  212. } else {
  213. next->coder->simple = NULL;
  214. }
  215. }
  216. if (filters[0].options != NULL) {
  217. const lzma_options_bcj *simple = filters[0].options;
  218. next->coder->now_pos = simple->start_offset;
  219. if (next->coder->now_pos & (alignment - 1))
  220. return LZMA_OPTIONS_ERROR;
  221. } else {
  222. next->coder->now_pos = 0;
  223. }
  224. // Reset variables.
  225. next->coder->is_encoder = is_encoder;
  226. next->coder->end_was_reached = false;
  227. next->coder->pos = 0;
  228. next->coder->filtered = 0;
  229. next->coder->size = 0;
  230. return lzma_next_filter_init(
  231. &next->coder->next, allocator, filters + 1);
  232. }