xref: /freebsd/contrib/xz/src/liblzma/simple/x86.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       x86.c
6 /// \brief      Filter for x86 binaries (BCJ filter)
7 ///
8 //  Authors:    Igor Pavlov
9 //              Lasse Collin
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "simple_private.h"
14 
15 
16 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
17 
18 
19 typedef struct {
20 	uint32_t prev_mask;
21 	uint32_t prev_pos;
22 } lzma_simple_x86;
23 
24 
25 static size_t
26 x86_code(void *simple_ptr, uint32_t now_pos, bool is_encoder,
27 		uint8_t *buffer, size_t size)
28 {
29 	static const bool MASK_TO_ALLOWED_STATUS[8]
30 		= { true, true, true, false, true, false, false, false };
31 
32 	static const uint32_t MASK_TO_BIT_NUMBER[8]
33 			= { 0, 1, 2, 2, 3, 3, 3, 3 };
34 
35 	lzma_simple_x86 *simple = simple_ptr;
36 	uint32_t prev_mask = simple->prev_mask;
37 	uint32_t prev_pos = simple->prev_pos;
38 
39 	if (size < 5)
40 		return 0;
41 
42 	if (now_pos - prev_pos > 5)
43 		prev_pos = now_pos - 5;
44 
45 	const size_t limit = size - 5;
46 	size_t buffer_pos = 0;
47 
48 	while (buffer_pos <= limit) {
49 		uint8_t b = buffer[buffer_pos];
50 		if (b != 0xE8 && b != 0xE9) {
51 			++buffer_pos;
52 			continue;
53 		}
54 
55 		const uint32_t offset = now_pos + (uint32_t)(buffer_pos)
56 				- prev_pos;
57 		prev_pos = now_pos + (uint32_t)(buffer_pos);
58 
59 		if (offset > 5) {
60 			prev_mask = 0;
61 		} else {
62 			for (uint32_t i = 0; i < offset; ++i) {
63 				prev_mask &= 0x77;
64 				prev_mask <<= 1;
65 			}
66 		}
67 
68 		b = buffer[buffer_pos + 4];
69 
70 		if (Test86MSByte(b)
71 			&& MASK_TO_ALLOWED_STATUS[(prev_mask >> 1) & 0x7]
72 				&& (prev_mask >> 1) < 0x10) {
73 
74 			uint32_t src = ((uint32_t)(b) << 24)
75 				| ((uint32_t)(buffer[buffer_pos + 3]) << 16)
76 				| ((uint32_t)(buffer[buffer_pos + 2]) << 8)
77 				| (buffer[buffer_pos + 1]);
78 
79 			uint32_t dest;
80 			while (true) {
81 				if (is_encoder)
82 					dest = src + (now_pos + (uint32_t)(
83 							buffer_pos) + 5);
84 				else
85 					dest = src - (now_pos + (uint32_t)(
86 							buffer_pos) + 5);
87 
88 				if (prev_mask == 0)
89 					break;
90 
91 				const uint32_t i = MASK_TO_BIT_NUMBER[
92 						prev_mask >> 1];
93 
94 				b = (uint8_t)(dest >> (24 - i * 8));
95 
96 				if (!Test86MSByte(b))
97 					break;
98 
99 				src = dest ^ ((1U << (32 - i * 8)) - 1);
100 			}
101 
102 			buffer[buffer_pos + 4]
103 					= (uint8_t)(~(((dest >> 24) & 1) - 1));
104 			buffer[buffer_pos + 3] = (uint8_t)(dest >> 16);
105 			buffer[buffer_pos + 2] = (uint8_t)(dest >> 8);
106 			buffer[buffer_pos + 1] = (uint8_t)(dest);
107 			buffer_pos += 5;
108 			prev_mask = 0;
109 
110 		} else {
111 			++buffer_pos;
112 			prev_mask |= 1;
113 			if (Test86MSByte(b))
114 				prev_mask |= 0x10;
115 		}
116 	}
117 
118 	simple->prev_mask = prev_mask;
119 	simple->prev_pos = prev_pos;
120 
121 	return buffer_pos;
122 }
123 
124 
125 static lzma_ret
126 x86_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
127 		const lzma_filter_info *filters, bool is_encoder)
128 {
129 	const lzma_ret ret = lzma_simple_coder_init(next, allocator, filters,
130 			&x86_code, sizeof(lzma_simple_x86), 5, 1, is_encoder);
131 
132 	if (ret == LZMA_OK) {
133 		lzma_simple_coder *coder = next->coder;
134 		lzma_simple_x86 *simple = coder->simple;
135 		simple->prev_mask = 0;
136 		simple->prev_pos = (uint32_t)(-5);
137 	}
138 
139 	return ret;
140 }
141 
142 
143 #ifdef HAVE_ENCODER_X86
144 extern lzma_ret
145 lzma_simple_x86_encoder_init(lzma_next_coder *next,
146 		const lzma_allocator *allocator,
147 		const lzma_filter_info *filters)
148 {
149 	return x86_coder_init(next, allocator, filters, true);
150 }
151 #endif
152 
153 
154 #ifdef HAVE_DECODER_X86
155 extern lzma_ret
156 lzma_simple_x86_decoder_init(lzma_next_coder *next,
157 		const lzma_allocator *allocator,
158 		const lzma_filter_info *filters)
159 {
160 	return x86_coder_init(next, allocator, filters, false);
161 }
162 #endif
163