simple_coder.c 8.2 KB

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