delta_common.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file delta_common.c
  5. /// \brief Common stuff for Delta encoder and decoder
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "delta_common.h"
  11. #include "delta_private.h"
  12. static void
  13. delta_coder_end(void *coder_ptr, const lzma_allocator *allocator)
  14. {
  15. lzma_delta_coder *coder = coder_ptr;
  16. lzma_next_end(&coder->next, allocator);
  17. lzma_free(coder, allocator);
  18. return;
  19. }
  20. extern lzma_ret
  21. lzma_delta_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  22. const lzma_filter_info *filters)
  23. {
  24. // Allocate memory for the decoder if needed.
  25. lzma_delta_coder *coder = next->coder;
  26. if (coder == NULL) {
  27. coder = lzma_alloc(sizeof(lzma_delta_coder), allocator);
  28. if (coder == NULL)
  29. return LZMA_MEM_ERROR;
  30. next->coder = coder;
  31. // End function is the same for encoder and decoder.
  32. next->end = &delta_coder_end;
  33. coder->next = LZMA_NEXT_CODER_INIT;
  34. }
  35. // Validate the options.
  36. if (lzma_delta_coder_memusage(filters[0].options) == UINT64_MAX)
  37. return LZMA_OPTIONS_ERROR;
  38. // Set the delta distance.
  39. const lzma_options_delta *opt = filters[0].options;
  40. coder->distance = opt->dist;
  41. // Initialize the rest of the variables.
  42. coder->pos = 0;
  43. memzero(coder->history, LZMA_DELTA_DIST_MAX);
  44. // Initialize the next decoder in the chain, if any.
  45. return lzma_next_filter_init(&coder->next, allocator, filters + 1);
  46. }
  47. extern uint64_t
  48. lzma_delta_coder_memusage(const void *options)
  49. {
  50. const lzma_options_delta *opt = options;
  51. if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE
  52. || opt->dist < LZMA_DELTA_DIST_MIN
  53. || opt->dist > LZMA_DELTA_DIST_MAX)
  54. return UINT64_MAX;
  55. return sizeof(lzma_delta_coder);
  56. }