1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file delta_common.c
4 /// \brief Common stuff for Delta encoder and decoder
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "delta_common.h"
14 #include "delta_private.h"
18 delta_coder_end(void *coder_ptr
, const lzma_allocator
*allocator
)
20 lzma_delta_coder
*coder
= coder_ptr
;
21 lzma_next_end(&coder
->next
, allocator
);
22 lzma_free(coder
, allocator
);
28 lzma_delta_coder_init(lzma_next_coder
*next
, const lzma_allocator
*allocator
,
29 const lzma_filter_info
*filters
)
31 // Allocate memory for the decoder if needed.
32 lzma_delta_coder
*coder
= next
->coder
;
34 coder
= lzma_alloc(sizeof(lzma_delta_coder
), allocator
);
36 return LZMA_MEM_ERROR
;
40 // End function is the same for encoder and decoder.
41 next
->end
= &delta_coder_end
;
42 coder
->next
= LZMA_NEXT_CODER_INIT
;
45 // Validate the options.
46 if (lzma_delta_coder_memusage(filters
[0].options
) == UINT64_MAX
)
47 return LZMA_OPTIONS_ERROR
;
49 // Set the delta distance.
50 const lzma_options_delta
*opt
= filters
[0].options
;
51 coder
->distance
= opt
->dist
;
53 // Initialize the rest of the variables.
55 memzero(coder
->history
, LZMA_DELTA_DIST_MAX
);
57 // Initialize the next decoder in the chain, if any.
58 return lzma_next_filter_init(&coder
->next
, allocator
, filters
+ 1);
63 lzma_delta_coder_memusage(const void *options
)
65 const lzma_options_delta
*opt
= options
;
67 if (opt
== NULL
|| opt
->type
!= LZMA_DELTA_TYPE_BYTE
68 || opt
->dist
< LZMA_DELTA_DIST_MIN
69 || opt
->dist
> LZMA_DELTA_DIST_MAX
)
72 return sizeof(lzma_delta_coder
);