xref: /freebsd/contrib/xz/src/liblzma/simple/arm.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       arm.c
6 /// \brief      Filter for ARM binaries
7 ///
8 //  Authors:    Igor Pavlov
9 //              Lasse Collin
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "simple_private.h"
14 
15 
16 static size_t
17 arm_code(void *simple lzma_attribute((__unused__)),
18 		uint32_t now_pos, bool is_encoder,
19 		uint8_t *buffer, size_t size)
20 {
21 	size_t i;
22 	for (i = 0; i + 4 <= size; i += 4) {
23 		if (buffer[i + 3] == 0xEB) {
24 			uint32_t src = ((uint32_t)(buffer[i + 2]) << 16)
25 					| ((uint32_t)(buffer[i + 1]) << 8)
26 					| (uint32_t)(buffer[i + 0]);
27 			src <<= 2;
28 
29 			uint32_t dest;
30 			if (is_encoder)
31 				dest = now_pos + (uint32_t)(i) + 8 + src;
32 			else
33 				dest = src - (now_pos + (uint32_t)(i) + 8);
34 
35 			dest >>= 2;
36 			buffer[i + 2] = (dest >> 16);
37 			buffer[i + 1] = (dest >> 8);
38 			buffer[i + 0] = dest;
39 		}
40 	}
41 
42 	return i;
43 }
44 
45 
46 static lzma_ret
47 arm_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
48 		const lzma_filter_info *filters, bool is_encoder)
49 {
50 	return lzma_simple_coder_init(next, allocator, filters,
51 			&arm_code, 0, 4, 4, is_encoder);
52 }
53 
54 
55 #ifdef HAVE_ENCODER_ARM
56 extern lzma_ret
57 lzma_simple_arm_encoder_init(lzma_next_coder *next,
58 		const lzma_allocator *allocator,
59 		const lzma_filter_info *filters)
60 {
61 	return arm_coder_init(next, allocator, filters, true);
62 }
63 #endif
64 
65 
66 #ifdef HAVE_DECODER_ARM
67 extern lzma_ret
68 lzma_simple_arm_decoder_init(lzma_next_coder *next,
69 		const lzma_allocator *allocator,
70 		const lzma_filter_info *filters)
71 {
72 	return arm_coder_init(next, allocator, filters, false);
73 }
74 #endif
75