xref: /freebsd/contrib/xz/src/liblzma/delta/delta_decoder.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       delta_decoder.c
6 /// \brief      Delta filter decoder
7 //
8 //  Author:     Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
11 
12 #include "delta_decoder.h"
13 #include "delta_private.h"
14 
15 
16 static void
17 decode_buffer(lzma_delta_coder *coder, uint8_t *buffer, size_t size)
18 {
19 	const size_t distance = coder->distance;
20 
21 	for (size_t i = 0; i < size; ++i) {
22 		buffer[i] += coder->history[(distance + coder->pos) & 0xFF];
23 		coder->history[coder->pos-- & 0xFF] = buffer[i];
24 	}
25 }
26 
27 
28 static lzma_ret
29 delta_decode(void *coder_ptr, const lzma_allocator *allocator,
30 		const uint8_t *restrict in, size_t *restrict in_pos,
31 		size_t in_size, uint8_t *restrict out,
32 		size_t *restrict out_pos, size_t out_size, lzma_action action)
33 {
34 	lzma_delta_coder *coder = coder_ptr;
35 
36 	assert(coder->next.code != NULL);
37 
38 	const size_t out_start = *out_pos;
39 
40 	const lzma_ret ret = coder->next.code(coder->next.coder, allocator,
41 			in, in_pos, in_size, out, out_pos, out_size,
42 			action);
43 
44 	// out might be NULL. In that case size == 0. Null pointer + 0 is
45 	// undefined behavior so skip the call in that case as it would
46 	// do nothing anyway.
47 	const size_t size = *out_pos - out_start;
48 	if (size > 0)
49 		decode_buffer(coder, out + out_start, size);
50 
51 	return ret;
52 }
53 
54 
55 extern lzma_ret
56 lzma_delta_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
57 		const lzma_filter_info *filters)
58 {
59 	next->code = &delta_decode;
60 	return lzma_delta_coder_init(next, allocator, filters);
61 }
62 
63 
64 extern lzma_ret
65 lzma_delta_props_decode(void **options, const lzma_allocator *allocator,
66 		const uint8_t *props, size_t props_size)
67 {
68 	if (props_size != 1)
69 		return LZMA_OPTIONS_ERROR;
70 
71 	lzma_options_delta *opt
72 			= lzma_alloc(sizeof(lzma_options_delta), allocator);
73 	if (opt == NULL)
74 		return LZMA_MEM_ERROR;
75 
76 	opt->type = LZMA_DELTA_TYPE_BYTE;
77 	opt->dist = props[0] + 1U;
78 
79 	*options = opt;
80 
81 	return LZMA_OK;
82 }
83