delta_common.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(void *coder_ptr, const lzma_allocator *allocator)
  16. {
  17. lzma_delta_coder *coder = coder_ptr;
  18. lzma_next_end(&coder->next, allocator);
  19. lzma_free(coder, allocator);
  20. return;
  21. }
  22. extern lzma_ret
  23. lzma_delta_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  24. const lzma_filter_info *filters)
  25. {
  26. // Allocate memory for the decoder if needed.
  27. lzma_delta_coder *coder = next->coder;
  28. if (coder == NULL) {
  29. coder = lzma_alloc(sizeof(lzma_delta_coder), allocator);
  30. if (coder == NULL)
  31. return LZMA_MEM_ERROR;
  32. next->coder = coder;
  33. // End function is the same for encoder and decoder.
  34. next->end = &delta_coder_end;
  35. coder->next = LZMA_NEXT_CODER_INIT;
  36. }
  37. // Validate the options.
  38. if (lzma_delta_coder_memusage(filters[0].options) == UINT64_MAX)
  39. return LZMA_OPTIONS_ERROR;
  40. // Set the delta distance.
  41. const lzma_options_delta *opt = filters[0].options;
  42. coder->distance = opt->dist;
  43. // Initialize the rest of the variables.
  44. coder->pos = 0;
  45. memzero(coder->history, LZMA_DELTA_DIST_MAX);
  46. // Initialize the next decoder in the chain, if any.
  47. return lzma_next_filter_init(&coder->next, allocator, filters + 1);
  48. }
  49. extern uint64_t
  50. lzma_delta_coder_memusage(const void *options)
  51. {
  52. const lzma_options_delta *opt = options;
  53. if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE
  54. || opt->dist < LZMA_DELTA_DIST_MIN
  55. || opt->dist > LZMA_DELTA_DIST_MAX)
  56. return UINT64_MAX;
  57. return sizeof(lzma_delta_coder);
  58. }