delta_encoder.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // in and out might be NULL. In such cases size == 0.
  55. // Null pointer + 0 is undefined behavior so skip
  56. // the call in that case as it would do nothing anyway.
  57. if (size > 0)
  58. copy_and_encode(coder, in + *in_pos, out + *out_pos,
  59. size);
  60. *in_pos += size;
  61. *out_pos += size;
  62. ret = action != LZMA_RUN && *in_pos == in_size
  63. ? LZMA_STREAM_END : LZMA_OK;
  64. } else {
  65. const size_t out_start = *out_pos;
  66. ret = coder->next.code(coder->next.coder, allocator,
  67. in, in_pos, in_size, out, out_pos, out_size,
  68. action);
  69. // Like above, avoid null pointer + 0.
  70. const size_t size = *out_pos - out_start;
  71. if (size > 0)
  72. encode_in_place(coder, out + out_start, size);
  73. }
  74. return ret;
  75. }
  76. static lzma_ret
  77. delta_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
  78. const lzma_filter *filters_null lzma_attribute((__unused__)),
  79. const lzma_filter *reversed_filters)
  80. {
  81. lzma_delta_coder *coder = coder_ptr;
  82. // Delta doesn't and will never support changing the options in
  83. // the middle of encoding. If the app tries to change them, we
  84. // simply ignore them.
  85. return lzma_next_filter_update(
  86. &coder->next, allocator, reversed_filters + 1);
  87. }
  88. extern lzma_ret
  89. lzma_delta_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  90. const lzma_filter_info *filters)
  91. {
  92. next->code = &delta_encode;
  93. next->update = &delta_encoder_update;
  94. return lzma_delta_coder_init(next, allocator, filters);
  95. }
  96. extern lzma_ret
  97. lzma_delta_props_encode(const void *options, uint8_t *out)
  98. {
  99. // The caller must have already validated the options, so it's
  100. // LZMA_PROG_ERROR if they are invalid.
  101. if (lzma_delta_coder_memusage(options) == UINT64_MAX)
  102. return LZMA_PROG_ERROR;
  103. const lzma_options_delta *opt = options;
  104. out[0] = opt->dist - LZMA_DELTA_DIST_MIN;
  105. return LZMA_OK;
  106. }