]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/delta/delta_common.c
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / src / liblzma / delta / delta_common.c
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.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "delta_common.h"
14 #include "delta_private.h"
15
16
17 static void
18 delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
19 {
20         lzma_next_end(&coder->next, allocator);
21         lzma_free(coder, allocator);
22         return;
23 }
24
25
26 extern lzma_ret
27 lzma_delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
28                 const lzma_filter_info *filters, lzma_code_function code)
29 {
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;
35
36                 // End function is the same for encoder and decoder.
37                 next->end = &delta_coder_end;
38                 next->coder->next = LZMA_NEXT_CODER_INIT;
39         }
40
41         // Coding function is different for encoder and decoder.
42         next->code = code;
43
44         // Validate the options.
45         if (lzma_delta_coder_memusage(filters[0].options) == UINT64_MAX)
46                 return LZMA_OPTIONS_ERROR;
47
48         // Set the delta distance.
49         const lzma_options_delta *opt = filters[0].options;
50         next->coder->distance = opt->dist;
51
52         // Initialize the rest of the variables.
53         next->coder->pos = 0;
54         memzero(next->coder->history, LZMA_DELTA_DIST_MAX);
55
56         // Initialize the next decoder in the chain, if any.
57         return lzma_next_filter_init(&next->coder->next,
58                         allocator, filters + 1);
59 }
60
61
62 extern uint64_t
63 lzma_delta_coder_memusage(const void *options)
64 {
65         const lzma_options_delta *opt = options;
66
67         if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE
68                         || opt->dist < LZMA_DELTA_DIST_MIN
69                         || opt->dist > LZMA_DELTA_DIST_MAX)
70                 return UINT64_MAX;
71
72         return sizeof(lzma_coder);
73 }