1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dexfuzz.rawdex.formats;
18 
19 /**
20  * Class consisting of static methods used for common read/write operations
21  * perfomed in the Format classes.
22  */
23 public class RawInsnHelper {
24   /**
25    * Read a signed byte from the idx into the raw array.
26    */
getSignedByteFromByte(byte[] raw, int idx)27   public static long getSignedByteFromByte(byte[] raw, int idx) {
28     return (long) raw[idx];
29   }
30 
31   /**
32    * Read an unsigned byte from the idx into the raw array.
33    */
getUnsignedByteFromByte(byte[] raw, int idx)34   public static long getUnsignedByteFromByte(byte[] raw, int idx) {
35     return ((long) raw[idx]) & 0xff;
36   }
37 
38   /**
39    * Read an unsigned lower 4 bits from the idx into the raw array.
40    */
getUnsignedLowNibbleFromByte(byte[] raw, int idx)41   public static long getUnsignedLowNibbleFromByte(byte[] raw, int idx) {
42     return ((long) raw[idx]) & 0xf;
43   }
44 
45   /**
46    * Read an unsigned higher 4 bits from the idx into the raw array.
47    */
getUnsignedHighNibbleFromByte(byte[] raw, int idx)48   public static long getUnsignedHighNibbleFromByte(byte[] raw, int idx) {
49     return (((long) raw[idx]) >> 4) & 0xf;
50   }
51 
52   /**
53    * Read an unsigned 2 bytes as a short from the idx into the raw array.
54    */
getUnsignedShortFromTwoBytes(byte[] raw, int idx)55   public static long getUnsignedShortFromTwoBytes(byte[] raw, int idx) {
56     return (long) ( (((long) raw[idx]) & 0xff)
57         | ((((long) raw[idx + 1]) & 0xff) << 8));
58   }
59 
60   /**
61    * Read a signed 2 bytes as a short from the idx into the raw array.
62    */
getSignedShortFromTwoBytes(byte[] raw, int idx)63   public static long getSignedShortFromTwoBytes(byte[] raw, int idx) {
64     return (long) ( (((long) raw[idx]) & 0xff)
65         | (((long) raw[idx + 1]) << 8));
66   }
67 
68   /**
69    * Read an unsigned 4 bytes as an int from the idx into the raw array.
70    */
getUnsignedIntFromFourBytes(byte[] raw, int idx)71   public static long getUnsignedIntFromFourBytes(byte[] raw, int idx) {
72     return (long) ( (((long) raw[idx]) & 0xff)
73         | ((((long) raw[idx + 1]) & 0xff) << 8)
74         | ((((long) raw[idx + 2]) & 0xff) << 16)
75         | ((((long) raw[idx + 3]) & 0xff) << 24) );
76   }
77 
78   /**
79    * Read a signed 4 bytes as an int from the idx into the raw array.
80    */
getSignedIntFromFourBytes(byte[] raw, int idx)81   public static long getSignedIntFromFourBytes(byte[] raw, int idx) {
82     return (long) ( (((long) raw[idx]) & 0xff)
83         | ((((long) raw[idx + 1]) & 0xff) << 8)
84         | ((((long) raw[idx + 2]) & 0xff) << 16)
85         | (((long) raw[idx + 3]) << 24) );
86   }
87 
88   /**
89    * Read a signed 8 bytes as a long from the idx into the raw array.
90    */
getSignedLongFromEightBytes(byte[] raw, int idx)91   public static long getSignedLongFromEightBytes(byte[] raw, int idx) {
92     return (long) ( (((long) raw[idx]) & 0xff)
93         | ((((long) raw[idx + 1]) & 0xff) << 8)
94         | ((((long) raw[idx + 2]) & 0xff) << 16)
95         | ((((long) raw[idx + 3]) & 0xff) << 24)
96         | ((((long) raw[idx + 4]) & 0xff) << 32)
97         | ((((long) raw[idx + 5]) & 0xff) << 40)
98         | ((((long) raw[idx + 6]) & 0xff) << 48)
99         | (((long) raw[idx + 7]) << 56) );
100   }
101 
102   /**
103    * Given an idx into a raw array, and an int, write that int into the array at that position.
104    */
writeUnsignedIntToFourBytes(byte[] raw, int idx, int value)105   public static void writeUnsignedIntToFourBytes(byte[] raw, int idx, int value) {
106     raw[idx] = (byte) (value & 0xFF);
107     raw[idx + 1] = (byte) ((value & 0xFF00) >>> 8);
108     raw[idx + 2] = (byte) ((value & 0xFF0000) >>> 16);
109     raw[idx + 3] = (byte) ((value & 0xFF000000) >>> 24);
110   }
111 
112   /**
113    * Given an idx into a raw array, and a short, write that int into the array at that position.
114    */
writeUnsignedShortToTwoBytes(byte[] raw, int idx, int value)115   public static void writeUnsignedShortToTwoBytes(byte[] raw, int idx, int value) {
116     raw[idx] = (byte) (value & 0xFF);
117     raw[idx + 1] = (byte) ((value & 0xFF00) >>> 8);
118   }
119 }
120