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