delta_decoder.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // out might be NULL. In that case size == 0. Null pointer + 0 is
  36. // undefined behavior so skip the call in that case as it would
  37. // do nothing anyway.
  38. const size_t size = *out_pos - out_start;
  39. if (size > 0)
  40. decode_buffer(coder, out + out_start, size);
  41. return ret;
  42. }
  43. extern lzma_ret
  44. lzma_delta_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  45. const lzma_filter_info *filters)
  46. {
  47. next->code = &delta_decode;
  48. return lzma_delta_coder_init(next, allocator, filters);
  49. }
  50. extern lzma_ret
  51. lzma_delta_props_decode(void **options, const lzma_allocator *allocator,
  52. const uint8_t *props, size_t props_size)
  53. {
  54. if (props_size != 1)
  55. return LZMA_OPTIONS_ERROR;
  56. lzma_options_delta *opt
  57. = lzma_alloc(sizeof(lzma_options_delta), allocator);
  58. if (opt == NULL)
  59. return LZMA_MEM_ERROR;
  60. opt->type = LZMA_DELTA_TYPE_BYTE;
  61. opt->dist = props[0] + 1U;
  62. *options = opt;
  63. return LZMA_OK;
  64. }