]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/simple/ia64.c
Moved var declarations out of for-loops. Makes pre-C99 compilers happier.
[icculus/xz.git] / src / liblzma / simple / ia64.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       ia64.c
4 /// \brief      Filter for IA64 (Itanium) binaries
5 ///
6 //  Authors:    Igor Pavlov
7 //              Lasse Collin
8 //
9 //  This file has been put into the public domain.
10 //  You can do whatever you want with this file.
11 //
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #include "simple_private.h"
15
16
17 static size_t
18 ia64_code(lzma_simple *simple lzma_attribute((unused)),
19                 uint32_t now_pos, bool is_encoder,
20                 uint8_t *buffer, size_t size)
21 {
22         static const uint32_t BRANCH_TABLE[32] = {
23                 0, 0, 0, 0, 0, 0, 0, 0,
24                 0, 0, 0, 0, 0, 0, 0, 0,
25                 4, 4, 6, 6, 0, 0, 7, 7,
26                 4, 4, 0, 0, 4, 4, 0, 0
27         };
28
29         size_t i;
30         for (i = 0; i + 16 <= size; i += 16) {
31                 const uint32_t instr_template = buffer[i] & 0x1F;
32                 const uint32_t mask = BRANCH_TABLE[instr_template];
33                 uint32_t bit_pos = 5;
34                 size_t slot;
35
36                 for (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                         size_t j;
44
45                         for (j = 0; j < 6; ++j)
46                                 instruction += (uint64_t)(
47                                                 buffer[i + j + byte_pos])
48                                                 << (8 * j);
49
50                         uint64_t inst_norm = instruction >> bit_res;
51
52                         if (((inst_norm >> 37) & 0xF) == 0x5
53                                         && ((inst_norm >> 9) & 0x7) == 0
54                                         /* &&  (inst_norm & 0x3F)== 0 */
55                                         ) {
56                                 uint32_t src = (uint32_t)(
57                                                 (inst_norm >> 13) & 0xFFFFF);
58                                 src |= ((inst_norm >> 36) & 1) << 20;
59
60                                 src <<= 4;
61
62                                 uint32_t dest;
63                                 if (is_encoder)
64                                         dest = now_pos + (uint32_t)(i) + src;
65                                 else
66                                         dest = src - (now_pos + (uint32_t)(i));
67
68                                 dest >>= 4;
69
70                                 inst_norm &= ~((uint64_t)(0x8FFFFF) << 13);
71                                 inst_norm |= (uint64_t)(dest & 0xFFFFF) << 13;
72                                 inst_norm |= (uint64_t)(dest & 0x100000)
73                                                 << (36 - 20);
74
75                                 instruction &= (1 << bit_res) - 1;
76                                 instruction |= (inst_norm << bit_res);
77
78                                 for (j = 0; j < 6; j++)
79                                         buffer[i + j + byte_pos] = (uint8_t)(
80                                                         instruction
81                                                         >> (8 * j));
82                         }
83                 }
84         }
85
86         return i;
87 }
88
89
90 static lzma_ret
91 ia64_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
92                 const lzma_filter_info *filters, bool is_encoder)
93 {
94         return lzma_simple_coder_init(next, allocator, filters,
95                         &ia64_code, 0, 16, 16, is_encoder);
96 }
97
98
99 extern lzma_ret
100 lzma_simple_ia64_encoder_init(lzma_next_coder *next,
101                 lzma_allocator *allocator, const lzma_filter_info *filters)
102 {
103         return ia64_coder_init(next, allocator, filters, true);
104 }
105
106
107 extern lzma_ret
108 lzma_simple_ia64_decoder_init(lzma_next_coder *next,
109                 lzma_allocator *allocator, const lzma_filter_info *filters)
110 {
111         return ia64_coder_init(next, allocator, filters, false);
112 }