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(lzma_coder
*coder
, lzma_allocator
*allocator
)
20 lzma_next_end(&coder
->next
, allocator
);
21 lzma_free(coder
, allocator
);
27 lzma_delta_coder_init(lzma_next_coder
*next
, lzma_allocator
*allocator
,
28 const lzma_filter_info
*filters
)
30 // Allocate memory for the decoder if needed.
31 if (next
->coder
== NULL
) {
32 next
->coder
= lzma_alloc(sizeof(lzma_coder
), allocator
);
33 if (next
->coder
== NULL
)
34 return LZMA_MEM_ERROR
;
36 // End function is the same for encoder and decoder.
37 next
->end
= &delta_coder_end
;
38 next
->coder
->next
= LZMA_NEXT_CODER_INIT
;
41 // Validate the options.
42 if (lzma_delta_coder_memusage(filters
[0].options
) == UINT64_MAX
)
43 return LZMA_OPTIONS_ERROR
;
45 // Set the delta distance.
46 const lzma_options_delta
*opt
= filters
[0].options
;
47 next
->coder
->distance
= opt
->dist
;
49 // Initialize the rest of the variables.
51 memzero(next
->coder
->history
, LZMA_DELTA_DIST_MAX
);
53 // Initialize the next decoder in the chain, if any.
54 return lzma_next_filter_init(&next
->coder
->next
,
55 allocator
, filters
+ 1);
60 lzma_delta_coder_memusage(const void *options
)
62 const lzma_options_delta
*opt
= options
;
64 if (opt
== NULL
|| opt
->type
!= LZMA_DELTA_TYPE_BYTE
65 || opt
->dist
< LZMA_DELTA_DIST_MIN
66 || opt
->dist
> LZMA_DELTA_DIST_MAX
)
69 return sizeof(lzma_coder
);