delta_encoder.c 3.6 KB

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