1 /*
2  * BCJ filter for big endian PowerPC 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 
11 package org.tukaani.xz.simple;
12 
13 public final class PowerPC implements SimpleFilter {
14     private final boolean isEncoder;
15     private int pos;
16 
PowerPC(boolean isEncoder, int startPos)17     public PowerPC(boolean isEncoder, int startPos) {
18         this.isEncoder = isEncoder;
19         pos = startPos;
20     }
21 
code(byte[] buf, int off, int len)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] & 0xFC) == 0x48 && (buf[i + 3] & 0x03) == 0x01) {
28                 int src = ((buf[i] & 0x03) << 24)
29                           | ((buf[i + 1] & 0xFF) << 16)
30                           | ((buf[i + 2] & 0xFF) << 8)
31                           | (buf[i + 3] & 0xFC);
32 
33                 int dest;
34                 if (isEncoder)
35                     dest = src + (pos + i - off);
36                 else
37                     dest = src - (pos + i - off);
38 
39                 buf[i] = (byte)(0x48 | ((dest >>> 24) & 0x03));
40                 buf[i + 1] = (byte)(dest >>> 16);
41                 buf[i + 2] = (byte)(dest >>> 8);
42                 buf[i + 3] = (byte)((buf[i + 3] & 0x03) | dest);
43             }
44         }
45 
46         i -= off;
47         pos += i;
48         return i;
49     }
50 }
51