1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file delta_common.c
6 /// \brief Common stuff for Delta encoder and decoder
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "delta_common.h"
13 #include "delta_private.h"
17 delta_coder_end(void *coder_ptr
, const lzma_allocator
*allocator
)
19 lzma_delta_coder
*coder
= coder_ptr
;
20 lzma_next_end(&coder
->next
, allocator
);
21 lzma_free(coder
, allocator
);
27 lzma_delta_coder_init(lzma_next_coder
*next
, const lzma_allocator
*allocator
,
28 const lzma_filter_info
*filters
)
30 // Allocate memory for the decoder if needed.
31 lzma_delta_coder
*coder
= next
->coder
;
33 coder
= lzma_alloc(sizeof(lzma_delta_coder
), allocator
);
35 return LZMA_MEM_ERROR
;
39 // End function is the same for encoder and decoder.
40 next
->end
= &delta_coder_end
;
41 coder
->next
= LZMA_NEXT_CODER_INIT
;
44 // Validate the options.
45 if (lzma_delta_coder_memusage(filters
[0].options
) == UINT64_MAX
)
46 return LZMA_OPTIONS_ERROR
;
48 // Set the delta distance.
49 const lzma_options_delta
*opt
= filters
[0].options
;
50 coder
->distance
= opt
->dist
;
52 // Initialize the rest of the variables.
54 memzero(coder
->history
, LZMA_DELTA_DIST_MAX
);
56 // Initialize the next decoder in the chain, if any.
57 return lzma_next_filter_init(&coder
->next
, allocator
, filters
+ 1);
62 lzma_delta_coder_memusage(const void *options
)
64 const lzma_options_delta
*opt
= options
;
66 if (opt
== NULL
|| opt
->type
!= LZMA_DELTA_TYPE_BYTE
67 || opt
->dist
< LZMA_DELTA_DIST_MIN
68 || opt
->dist
> LZMA_DELTA_DIST_MAX
)
71 return sizeof(lzma_delta_coder
);