1/*
2 * BCJ filter for little endian ARM instructions
3 *
4 * Authors: Lasse Collin <lasse.collin@tukaani.org>
5 *          Igor Pavlov <http://7-zip.org/>
6 *
7 * This file has been put into the public domain.
8 * You can do whatever you want with this file.
9 */
10
11package org.tukaani.xz.simple;
12
13public final class ARM implements SimpleFilter {
14    private final boolean isEncoder;
15    private int pos;
16
17    public ARM(boolean isEncoder, int startPos) {
18        this.isEncoder = isEncoder;
19        pos = startPos + 8;
20    }
21
22    public int code(byte[] buf, int off, int len) {
23        int end = off + len - 4;
24        int i;
25
26        for (i = off; i <= end; i += 4) {
27            if ((buf[i + 3] & 0xFF) == 0xEB) {
28                int src = ((buf[i + 2] & 0xFF) << 16)
29                          | ((buf[i + 1] & 0xFF) << 8)
30                          | (buf[i] & 0xFF);
31                src <<= 2;
32
33                int dest;
34                if (isEncoder)
35                    dest = src + (pos + i - off);
36                else
37                    dest = src - (pos + i - off);
38
39                dest >>>= 2;
40                buf[i + 2] = (byte)(dest >>> 16);
41                buf[i + 1] = (byte)(dest >>> 8);
42                buf[i] = (byte)dest;
43            }
44        }
45
46        i -= off;
47        pos += i;
48        return i;
49    }
50}
51