]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/simple/x86.c
a48a57501be8f13c05a2e541b925a4c0ea1c855b
[icculus/xz.git] / src / liblzma / simple / x86.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       x86.c
4 /// \brief      Filter for x86 binaries (BCJ filter)
5 //
6 //  Copyright (C) 1999-2006 Igor Pavlov
7 //  Copyright (C) 2007 Lasse Collin
8 //
9 //  This library is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU Lesser General Public
11 //  License as published by the Free Software Foundation; either
12 //  version 2.1 of the License, or (at your option) any later version.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //  Lesser General Public License for more details.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #include "simple_private.h"
22
23
24 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
25
26
27 struct lzma_simple_s {
28         uint32_t prev_mask;
29         uint32_t prev_pos;
30 };
31
32
33 static size_t
34 x86_code(lzma_simple *simple, uint32_t now_pos, bool is_encoder,
35                 uint8_t *buffer, size_t size)
36 {
37         static const bool MASK_TO_ALLOWED_STATUS[8]
38                 = { true, true, true, false, true, false, false, false };
39
40         static const uint32_t MASK_TO_BIT_NUMBER[8]
41                         = { 0, 1, 2, 2, 3, 3, 3, 3 };
42
43         uint32_t prev_mask = simple->prev_mask;
44         uint32_t prev_pos = simple->prev_pos;
45
46         if (size < 5)
47                 return 0;
48
49         if (now_pos - prev_pos > 5)
50                 prev_pos = now_pos - 5;
51
52         const size_t limit = size - 5;
53         size_t buffer_pos = 0;
54
55         while (buffer_pos <= limit) {
56                 uint8_t b = buffer[buffer_pos];
57                 if (b != 0xE8 && b != 0xE9) {
58                         ++buffer_pos;
59                         continue;
60                 }
61
62                 const uint32_t offset = now_pos + (uint32_t)(buffer_pos)
63                                 - prev_pos;
64                 prev_pos = now_pos + (uint32_t)(buffer_pos);
65
66                 if (offset > 5) {
67                         prev_mask = 0;
68                 } else {
69                         for (uint32_t i = 0; i < offset; ++i) {
70                                 prev_mask &= 0x77;
71                                 prev_mask <<= 1;
72                         }
73                 }
74
75                 b = buffer[buffer_pos + 4];
76
77                 if (Test86MSByte(b)
78                         && MASK_TO_ALLOWED_STATUS[(prev_mask >> 1) & 0x7]
79                                 && (prev_mask >> 1) < 0x10) {
80
81                         uint32_t src = ((uint32_t)(b) << 24)
82                                 | ((uint32_t)(buffer[buffer_pos + 3]) << 16)
83                                 | ((uint32_t)(buffer[buffer_pos + 2]) << 8)
84                                 | (buffer[buffer_pos + 1]);
85
86                         uint32_t dest;
87                         while (true) {
88                                 if (is_encoder)
89                                         dest = src + (now_pos + (uint32_t)(
90                                                         buffer_pos) + 5);
91                                 else
92                                         dest = src - (now_pos + (uint32_t)(
93                                                         buffer_pos) + 5);
94
95                                 if (prev_mask == 0)
96                                         break;
97
98                                 const uint32_t i = MASK_TO_BIT_NUMBER[
99                                                 prev_mask >> 1];
100
101                                 b = (uint8_t)(dest >> (24 - i * 8));
102
103                                 if (!Test86MSByte(b))
104                                         break;
105
106                                 src = dest ^ ((1 << (32 - i * 8)) - 1);
107                         }
108
109                         buffer[buffer_pos + 4]
110                                         = (uint8_t)(~(((dest >> 24) & 1) - 1));
111                         buffer[buffer_pos + 3] = (uint8_t)(dest >> 16);
112                         buffer[buffer_pos + 2] = (uint8_t)(dest >> 8);
113                         buffer[buffer_pos + 1] = (uint8_t)(dest);
114                         buffer_pos += 5;
115                         prev_mask = 0;
116
117                 } else {
118                         ++buffer_pos;
119                         prev_mask |= 1;
120                         if (Test86MSByte(b))
121                                 prev_mask |= 0x10;
122                 }
123         }
124
125         simple->prev_mask = prev_mask;
126         simple->prev_pos = prev_pos;
127
128         return buffer_pos;
129 }
130
131
132 static lzma_ret
133 x86_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
134                 const lzma_filter_info *filters, bool is_encoder)
135 {
136         const lzma_ret ret = lzma_simple_coder_init(next, allocator, filters,
137                         &x86_code, sizeof(lzma_simple), 5, is_encoder);
138
139         if (ret == LZMA_OK) {
140                 next->coder->simple->prev_mask = 0;
141                 next->coder->simple->prev_pos = (uint32_t)(-5);
142         }
143
144         return ret;
145 }
146
147
148 extern lzma_ret
149 lzma_simple_x86_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
150                 const lzma_filter_info *filters)
151 {
152         return x86_coder_init(next, allocator, filters, true);
153 }
154
155
156 extern lzma_ret
157 lzma_simple_x86_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
158                 const lzma_filter_info *filters)
159 {
160         return x86_coder_init(next, allocator, filters, false);
161 }