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
arm_code(void * simple lzma_attribute ((__unused__)),uint32_t now_pos,bool is_encoder,uint8_t * buffer,size_t size)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 &= ~(size_t)3;
22
23 size_t i;
24 for (i = 0; i < size; i += 4) {
25 if (buffer[i + 3] == 0xEB) {
26 uint32_t src = ((uint32_t)(buffer[i + 2]) << 16)
27 | ((uint32_t)(buffer[i + 1]) << 8)
28 | (uint32_t)(buffer[i + 0]);
29 src <<= 2;
30
31 uint32_t dest;
32 if (is_encoder)
33 dest = now_pos + (uint32_t)(i) + 8 + src;
34 else
35 dest = src - (now_pos + (uint32_t)(i) + 8);
36
37 dest >>= 2;
38 buffer[i + 2] = (dest >> 16);
39 buffer[i + 1] = (dest >> 8);
40 buffer[i + 0] = dest;
41 }
42 }
43
44 return i;
45 }
46
47
48 static lzma_ret
arm_coder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters,bool is_encoder)49 arm_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
50 const lzma_filter_info *filters, bool is_encoder)
51 {
52 return lzma_simple_coder_init(next, allocator, filters,
53 &arm_code, 0, 4, 4, is_encoder);
54 }
55
56
57 #ifdef HAVE_ENCODER_ARM
58 extern lzma_ret
lzma_simple_arm_encoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters)59 lzma_simple_arm_encoder_init(lzma_next_coder *next,
60 const lzma_allocator *allocator,
61 const lzma_filter_info *filters)
62 {
63 return arm_coder_init(next, allocator, filters, true);
64 }
65 #endif
66
67
68 #ifdef HAVE_DECODER_ARM
69 extern lzma_ret
lzma_simple_arm_decoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters)70 lzma_simple_arm_decoder_init(lzma_next_coder *next,
71 const lzma_allocator *allocator,
72 const lzma_filter_info *filters)
73 {
74 return arm_coder_init(next, allocator, filters, false);
75 }
76 #endif
77