]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/simple/powerpc.c
a3089fca9c1a226b51f84d38058a8f6e0d945f1b
[icculus/xz.git] / src / liblzma / simple / powerpc.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       powerpc.c
4 /// \brief      Filter for PowerPC (big endian) binaries
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 static size_t
25 powerpc_code(lzma_simple *simple lzma_attribute((unused)),
26                 uint32_t now_pos, bool is_encoder,
27                 uint8_t *buffer, size_t size)
28 {
29         size_t i;
30         for (i = 0; i + 4 <= size; i += 4) {
31                 // PowerPC branch 6(48) 24(Offset) 1(Abs) 1(Link)
32                 if ((buffer[i] >> 2) == 0x12
33                                 && ((buffer[i + 3] & 3) == 1)) {
34
35                         const uint32_t src = ((buffer[i + 0] & 3) << 24)
36                                         | (buffer[i + 1] << 16)
37                                         | (buffer[i + 2] << 8)
38                                         | (buffer[i + 3] & (~3));
39
40                         uint32_t dest;
41                         if (is_encoder)
42                                 dest = now_pos + (uint32_t)(i) + src;
43                         else
44                                 dest = src - (now_pos + (uint32_t)(i));
45
46                         buffer[i + 0] = 0x48 | ((dest >> 24) &  0x03);
47                         buffer[i + 1] = (dest >> 16);
48                         buffer[i + 2] = (dest >> 8);
49                         buffer[i + 3] &= 0x03;
50                         buffer[i + 3] |= dest;
51                 }
52         }
53
54         return i;
55 }
56
57
58 static lzma_ret
59 powerpc_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
60                 const lzma_filter_info *filters, bool is_encoder)
61 {
62         return lzma_simple_coder_init(next, allocator, filters,
63                         &powerpc_code, 0, 4, is_encoder);
64 }
65
66
67 extern lzma_ret
68 lzma_simple_powerpc_encoder_init(lzma_next_coder *next,
69                 lzma_allocator *allocator, const lzma_filter_info *filters)
70 {
71         return powerpc_coder_init(next, allocator, filters, true);
72 }
73
74
75 extern lzma_ret
76 lzma_simple_powerpc_decoder_init(lzma_next_coder *next,
77                 lzma_allocator *allocator, const lzma_filter_info *filters)
78 {
79         return powerpc_coder_init(next, allocator, filters, false);
80 }