Tests: Fix memory leaks in test_block_header.
[xz/debian.git] / src / liblzma / delta / delta_common.c
blob4768201d1a9f2ce670c32279b521017180264641
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(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);
23 return;
27 extern lzma_ret
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;
33 if (coder == NULL) {
34 coder = lzma_alloc(sizeof(lzma_delta_coder), allocator);
35 if (coder == NULL)
36 return LZMA_MEM_ERROR;
38 next->coder = coder;
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.
54 coder->pos = 0;
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);
62 extern uint64_t
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)
70 return UINT64_MAX;
72 return sizeof(lzma_delta_coder);