1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       armthumb.c
4207753Smm/// \brief      Filter for ARM-Thumb binaries
5207753Smm///
6207753Smm//  Authors:    Igor Pavlov
7207753Smm//              Lasse Collin
8207753Smm//
9207753Smm//  This file has been put into the public domain.
10207753Smm//  You can do whatever you want with this file.
11207753Smm//
12207753Smm///////////////////////////////////////////////////////////////////////////////
13207753Smm
14207753Smm#include "simple_private.h"
15207753Smm
16207753Smm
17207753Smmstatic size_t
18312518Sdelphijarmthumb_code(void *simple lzma_attribute((__unused__)),
19207753Smm		uint32_t now_pos, bool is_encoder,
20207753Smm		uint8_t *buffer, size_t size)
21207753Smm{
22207753Smm	size_t i;
23207753Smm	for (i = 0; i + 4 <= size; i += 2) {
24207753Smm		if ((buffer[i + 1] & 0xF8) == 0xF0
25207753Smm				&& (buffer[i + 3] & 0xF8) == 0xF8) {
26207753Smm			uint32_t src = ((buffer[i + 1] & 0x7) << 19)
27207753Smm					| (buffer[i + 0] << 11)
28207753Smm					| ((buffer[i + 3] & 0x7) << 8)
29207753Smm					| (buffer[i + 2]);
30207753Smm
31207753Smm			src <<= 1;
32207753Smm
33207753Smm			uint32_t dest;
34207753Smm			if (is_encoder)
35207753Smm				dest = now_pos + (uint32_t)(i) + 4 + src;
36207753Smm			else
37207753Smm				dest = src - (now_pos + (uint32_t)(i) + 4);
38207753Smm
39207753Smm			dest >>= 1;
40207753Smm			buffer[i + 1] = 0xF0 | ((dest >> 19) & 0x7);
41207753Smm			buffer[i + 0] = (dest >> 11);
42207753Smm			buffer[i + 3] = 0xF8 | ((dest >> 8) & 0x7);
43207753Smm			buffer[i + 2] = (dest);
44207753Smm			i += 2;
45207753Smm		}
46207753Smm	}
47207753Smm
48207753Smm	return i;
49207753Smm}
50207753Smm
51207753Smm
52207753Smmstatic lzma_ret
53292588Sdelphijarmthumb_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
54207753Smm		const lzma_filter_info *filters, bool is_encoder)
55207753Smm{
56207753Smm	return lzma_simple_coder_init(next, allocator, filters,
57207753Smm			&armthumb_code, 0, 4, 2, is_encoder);
58207753Smm}
59207753Smm
60207753Smm
61207753Smmextern lzma_ret
62207753Smmlzma_simple_armthumb_encoder_init(lzma_next_coder *next,
63292588Sdelphij		const lzma_allocator *allocator,
64292588Sdelphij		const lzma_filter_info *filters)
65207753Smm{
66207753Smm	return armthumb_coder_init(next, allocator, filters, true);
67207753Smm}
68207753Smm
69207753Smm
70207753Smmextern lzma_ret
71207753Smmlzma_simple_armthumb_decoder_init(lzma_next_coder *next,
72292588Sdelphij		const lzma_allocator *allocator,
73292588Sdelphij		const lzma_filter_info *filters)
74207753Smm{
75207753Smm	return armthumb_coder_init(next, allocator, filters, false);
76207753Smm}
77