]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/simple/arm.c
284371c3fd2a3cc34d5d83b887404a0c1efa9dfe
[icculus/xz.git] / src / liblzma / simple / arm.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       arm.c
4 /// \brief      Filter for ARM 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 arm_code(lzma_simple *simple lzma_attribute((unused)),
26                 uint32_t now_pos, bool is_encoder,
27                 uint8_t *buffer, size_t size)
28 {
29         uint32_t i;
30         for (i = 0; i + 4 <= size; i += 4) {
31                 if (buffer[i + 3] == 0xEB) {
32                         uint32_t src = (buffer[i + 2] << 16)
33                                         | (buffer[i + 1] << 8)
34                                         | (buffer[i + 0]);
35                         src <<= 2;
36
37                         uint32_t dest;
38                         if (is_encoder)
39                                 dest = now_pos + (uint32_t)(i) + 8 + src;
40                         else
41                                 dest = src - (now_pos + (uint32_t)(i) + 8);
42
43                         dest >>= 2;
44                         buffer[i + 2] = (dest >> 16);
45                         buffer[i + 1] = (dest >> 8);
46                         buffer[i + 0] = dest;
47                 }
48         }
49
50         return i;
51 }
52
53
54 static lzma_ret
55 arm_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
56                 const lzma_filter_info *filters, bool is_encoder)
57 {
58         return lzma_simple_coder_init(next, allocator, filters,
59                         &arm_code, 0, 4, is_encoder);
60 }
61
62
63 extern lzma_ret
64 lzma_simple_arm_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
65                 const lzma_filter_info *filters)
66 {
67         return arm_coder_init(next, allocator, filters, true);
68 }
69
70
71 extern lzma_ret
72 lzma_simple_arm_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
73                 const lzma_filter_info *filters)
74 {
75         return arm_coder_init(next, allocator, filters, false);
76 }