xref: /freebsd/contrib/xz/src/liblzma/simple/ia64.c (revision 128836d304d93f2d00eb14069c27089ab46c38d4)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       ia64.c
6 /// \brief      Filter for IA64 (Itanium) binaries
7 ///
8 //  Authors:    Igor Pavlov
9 //              Lasse Collin
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "simple_private.h"
14 
15 
16 static size_t
ia64_code(void * simple lzma_attribute ((__unused__)),uint32_t now_pos,bool is_encoder,uint8_t * buffer,size_t size)17 ia64_code(void *simple lzma_attribute((__unused__)),
18 		uint32_t now_pos, bool is_encoder,
19 		uint8_t *buffer, size_t size)
20 {
21 	static const uint32_t BRANCH_TABLE[32] = {
22 		0, 0, 0, 0, 0, 0, 0, 0,
23 		0, 0, 0, 0, 0, 0, 0, 0,
24 		4, 4, 6, 6, 0, 0, 7, 7,
25 		4, 4, 0, 0, 4, 4, 0, 0
26 	};
27 
28 	size &= ~(size_t)15;
29 
30 	size_t i;
31 	for (i = 0; i < size; i += 16) {
32 		const uint32_t instr_template = buffer[i] & 0x1F;
33 		const uint32_t mask = BRANCH_TABLE[instr_template];
34 		uint32_t bit_pos = 5;
35 
36 		for (size_t slot = 0; slot < 3; ++slot, bit_pos += 41) {
37 			if (((mask >> slot) & 1) == 0)
38 				continue;
39 
40 			const size_t byte_pos = (bit_pos >> 3);
41 			const uint32_t bit_res = bit_pos & 0x7;
42 			uint64_t instruction = 0;
43 
44 			for (size_t j = 0; j < 6; ++j)
45 				instruction += (uint64_t)(
46 						buffer[i + j + byte_pos])
47 						<< (8 * j);
48 
49 			uint64_t inst_norm = instruction >> bit_res;
50 
51 			if (((inst_norm >> 37) & 0xF) == 0x5
52 					&& ((inst_norm >> 9) & 0x7) == 0
53 					/* &&  (inst_norm & 0x3F)== 0 */
54 					) {
55 				uint32_t src = (uint32_t)(
56 						(inst_norm >> 13) & 0xFFFFF);
57 				src |= ((inst_norm >> 36) & 1) << 20;
58 
59 				src <<= 4;
60 
61 				uint32_t dest;
62 				if (is_encoder)
63 					dest = now_pos + (uint32_t)(i) + src;
64 				else
65 					dest = src - (now_pos + (uint32_t)(i));
66 
67 				dest >>= 4;
68 
69 				inst_norm &= ~((uint64_t)(0x8FFFFF) << 13);
70 				inst_norm |= (uint64_t)(dest & 0xFFFFF) << 13;
71 				inst_norm |= (uint64_t)(dest & 0x100000)
72 						<< (36 - 20);
73 
74 				instruction &= (1U << bit_res) - 1;
75 				instruction |= (inst_norm << bit_res);
76 
77 				for (size_t j = 0; j < 6; j++)
78 					buffer[i + j + byte_pos] = (uint8_t)(
79 							instruction
80 							>> (8 * j));
81 			}
82 		}
83 	}
84 
85 	return i;
86 }
87 
88 
89 static lzma_ret
ia64_coder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters,bool is_encoder)90 ia64_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
91 		const lzma_filter_info *filters, bool is_encoder)
92 {
93 	return lzma_simple_coder_init(next, allocator, filters,
94 			&ia64_code, 0, 16, 16, is_encoder);
95 }
96 
97 
98 #ifdef HAVE_ENCODER_IA64
99 extern lzma_ret
lzma_simple_ia64_encoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters)100 lzma_simple_ia64_encoder_init(lzma_next_coder *next,
101 		const lzma_allocator *allocator,
102 		const lzma_filter_info *filters)
103 {
104 	return ia64_coder_init(next, allocator, filters, true);
105 }
106 #endif
107 
108 
109 #ifdef HAVE_DECODER_IA64
110 extern lzma_ret
lzma_simple_ia64_decoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters)111 lzma_simple_ia64_decoder_init(lzma_next_coder *next,
112 		const lzma_allocator *allocator,
113 		const lzma_filter_info *filters)
114 {
115 	return ia64_coder_init(next, allocator, filters, false);
116 }
117 #endif
118