delta_encoder.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file delta_encoder.c
  4. /// \brief Delta filter encoder
  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 "delta_encoder.h"
  13. #include "delta_private.h"
  14. /// Copies and encodes the data at the same time. This is used when Delta
  15. /// is the first filter in the chain (and thus the last filter in the
  16. /// encoder's filter stack).
  17. static void
  18. copy_and_encode(lzma_delta_coder *coder,
  19. const uint8_t *restrict in, uint8_t *restrict out, size_t size)
  20. {
  21. const size_t distance = coder->distance;
  22. for (size_t i = 0; i < size; ++i) {
  23. const uint8_t tmp = coder->history[
  24. (distance + coder->pos) & 0xFF];
  25. coder->history[coder->pos-- & 0xFF] = in[i];
  26. out[i] = in[i] - tmp;
  27. }
  28. }
  29. /// Encodes the data in place. This is used when we are the last filter
  30. /// in the chain (and thus non-last filter in the encoder's filter stack).
  31. static void
  32. encode_in_place(lzma_delta_coder *coder, uint8_t *buffer, size_t size)
  33. {
  34. const size_t distance = coder->distance;
  35. for (size_t i = 0; i < size; ++i) {
  36. const uint8_t tmp = coder->history[
  37. (distance + coder->pos) & 0xFF];
  38. coder->history[coder->pos-- & 0xFF] = buffer[i];
  39. buffer[i] -= tmp;
  40. }
  41. }
  42. static lzma_ret
  43. delta_encode(void *coder_ptr, const lzma_allocator *allocator,
  44. const uint8_t *restrict in, size_t *restrict in_pos,
  45. size_t in_size, uint8_t *restrict out,
  46. size_t *restrict out_pos, size_t out_size, lzma_action action)
  47. {
  48. lzma_delta_coder *coder = coder_ptr;
  49. lzma_ret ret;
  50. if (coder->next.code == NULL) {
  51. const size_t in_avail = in_size - *in_pos;
  52. const size_t out_avail = out_size - *out_pos;
  53. const size_t size = my_min(in_avail, out_avail);
  54. copy_and_encode(coder, in + *in_pos, out + *out_pos, size);
  55. *in_pos += size;
  56. *out_pos += size;
  57. ret = action != LZMA_RUN && *in_pos == in_size
  58. ? LZMA_STREAM_END : LZMA_OK;
  59. } else {
  60. const size_t out_start = *out_pos;
  61. ret = coder->next.code(coder->next.coder, allocator,
  62. in, in_pos, in_size, out, out_pos, out_size,
  63. action);
  64. encode_in_place(coder, out + out_start, *out_pos - out_start);
  65. }
  66. return ret;
  67. }
  68. static lzma_ret
  69. delta_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
  70. const lzma_filter *filters_null lzma_attribute((__unused__)),
  71. const lzma_filter *reversed_filters)
  72. {
  73. lzma_delta_coder *coder = coder_ptr;
  74. // Delta doesn't and will never support changing the options in
  75. // the middle of encoding. If the app tries to change them, we
  76. // simply ignore them.
  77. return lzma_next_filter_update(
  78. &coder->next, allocator, reversed_filters + 1);
  79. }
  80. extern lzma_ret
  81. lzma_delta_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  82. const lzma_filter_info *filters)
  83. {
  84. next->code = &delta_encode;
  85. next->update = &delta_encoder_update;
  86. return lzma_delta_coder_init(next, allocator, filters);
  87. }
  88. extern lzma_ret
  89. lzma_delta_props_encode(const void *options, uint8_t *out)
  90. {
  91. // The caller must have already validated the options, so it's
  92. // LZMA_PROG_ERROR if they are invalid.
  93. if (lzma_delta_coder_memusage(options) == UINT64_MAX)
  94. return LZMA_PROG_ERROR;
  95. const lzma_options_delta *opt = options;
  96. out[0] = opt->dist - LZMA_DELTA_DIST_MIN;
  97. return LZMA_OK;
  98. }