delta_common.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file delta_common.c
  4. /// \brief Common stuff for Delta encoder and decoder
  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_common.h"
  13. #include "delta_private.h"
  14. static void
  15. delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
  16. {
  17. lzma_next_end(&coder->next, allocator);
  18. lzma_free(coder, allocator);
  19. return;
  20. }
  21. extern lzma_ret
  22. lzma_delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
  23. const lzma_filter_info *filters)
  24. {
  25. const lzma_options_delta *opt;
  26. // Allocate memory for the decoder if needed.
  27. if (next->coder == NULL) {
  28. next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
  29. if (next->coder == NULL)
  30. return LZMA_MEM_ERROR;
  31. // End function is the same for encoder and decoder.
  32. next->end = &delta_coder_end;
  33. next->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. opt = filters[0].options;
  40. next->coder->distance = opt->dist;
  41. // Initialize the rest of the variables.
  42. next->coder->pos = 0;
  43. memzero(next->coder->history, LZMA_DELTA_DIST_MAX);
  44. // Initialize the next decoder in the chain, if any.
  45. return lzma_next_filter_init(&next->coder->next,
  46. allocator, filters + 1);
  47. }
  48. extern uint64_t
  49. lzma_delta_coder_memusage(const void *options)
  50. {
  51. const lzma_options_delta *opt = options;
  52. if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE
  53. || opt->dist < LZMA_DELTA_DIST_MIN
  54. || opt->dist > LZMA_DELTA_DIST_MAX)
  55. return UINT64_MAX;
  56. return sizeof(lzma_coder);
  57. }