RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / tools / misc / xz / src / liblzma / delta / delta_common.c
blob930ad215131ca99c30c0605bfcde6dbd05a1e7c3
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file delta_common.c
4 /// \brief Common stuff for Delta encoder and 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.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "delta_common.h"
14 #include "delta_private.h"
17 static void
18 delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
20 lzma_next_end(&coder->next, allocator);
21 lzma_free(coder, allocator);
22 return;
26 extern lzma_ret
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.
50 next->coder->pos = 0;
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);
59 extern uint64_t
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)
67 return UINT64_MAX;
69 return sizeof(lzma_coder);