delta_decoder.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file delta_decoder.c
  4. /// \brief Delta filter 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_decoder.h"
  13. #include "delta_private.h"
  14. static void
  15. decode_buffer(lzma_delta_coder *coder, uint8_t *buffer, size_t size)
  16. {
  17. const size_t distance = coder->distance;
  18. for (size_t i = 0; i < size; ++i) {
  19. buffer[i] += coder->history[(distance + coder->pos) & 0xFF];
  20. coder->history[coder->pos-- & 0xFF] = buffer[i];
  21. }
  22. }
  23. static lzma_ret
  24. delta_decode(void *coder_ptr, const lzma_allocator *allocator,
  25. const uint8_t *restrict in, size_t *restrict in_pos,
  26. size_t in_size, uint8_t *restrict out,
  27. size_t *restrict out_pos, size_t out_size, lzma_action action)
  28. {
  29. lzma_delta_coder *coder = coder_ptr;
  30. assert(coder->next.code != NULL);
  31. const size_t out_start = *out_pos;
  32. const lzma_ret ret = coder->next.code(coder->next.coder, allocator,
  33. in, in_pos, in_size, out, out_pos, out_size,
  34. action);
  35. decode_buffer(coder, out + out_start, *out_pos - out_start);
  36. return ret;
  37. }
  38. extern lzma_ret
  39. lzma_delta_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  40. const lzma_filter_info *filters)
  41. {
  42. next->code = &delta_decode;
  43. return lzma_delta_coder_init(next, allocator, filters);
  44. }
  45. extern lzma_ret
  46. lzma_delta_props_decode(void **options, const lzma_allocator *allocator,
  47. const uint8_t *props, size_t props_size)
  48. {
  49. if (props_size != 1)
  50. return LZMA_OPTIONS_ERROR;
  51. lzma_options_delta *opt
  52. = lzma_alloc(sizeof(lzma_options_delta), allocator);
  53. if (opt == NULL)
  54. return LZMA_MEM_ERROR;
  55. opt->type = LZMA_DELTA_TYPE_BYTE;
  56. opt->dist = props[0] + 1;
  57. *options = opt;
  58. return LZMA_OK;
  59. }