summaryrefslogtreecommitdiff
path: root/src/org/catacombae/rarx/Util.java
blob: 80cb47b94d05377f1f1ea3d441440ec4fee8cf4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*-
 * Copyright (C) 2006 Erik Larsson
 * 
 * All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package org.catacombae.rarx;

public class Util {
    public static int sectorSize = 0x800;
    
    public static String byteArrayToHexString(byte[] array) { 
	return byteArrayToHexString(array, 0, array.length);
    }
    public static String byteArrayToHexString(byte[] array, int offset, int length) { 
	String result = "";
	for(int i = offset; i < (offset+length); ++i) {
	    byte b = array[i];
	    String currentHexString = Integer.toHexString(b & 0xFF);
	    if(currentHexString.length() == 1)
		currentHexString = "0" + currentHexString;
	    result += currentHexString;
	}
	return result;
    }
    public static String toHexStringBE(int[] array) {
	return toHexStringBE(array, 0, array.length);
    }
    public static String toHexStringBE(int[] array, int offset, int length) {
	StringBuilder result = new StringBuilder();
	for(int i : array)
	    result.append(toHexStringBE(i));
	return result.toString();
    }
    
    public static String toHexStringLE(byte n) { return byteArrayToHexString(toByteArrayLE(n)); }
    public static String toHexStringLE(short n) { return byteArrayToHexString(toByteArrayLE(n)); }
    public static String toHexStringLE(int n) { return byteArrayToHexString(toByteArrayLE(n)); }
    public static String toHexStringLE(long n) { return byteArrayToHexString(toByteArrayLE(n)); }
    public static String toHexStringBE(byte n) { return byteArrayToHexString(toByteArrayBE(n)); }
    public static String toHexStringBE(short n) { return byteArrayToHexString(toByteArrayBE(n)); }
    public static String toHexStringBE(int n) { return byteArrayToHexString(toByteArrayBE(n)); }
    public static String toHexStringBE(long n) { return byteArrayToHexString(toByteArrayBE(n)); }
    
    public static byte[] invert(byte[] array) {
	byte[] newArray = new byte[array.length];
	for(int i = 0; i < array.length; ++i)
	    newArray[newArray.length-i-1] = array[i];
	return newArray;
    }
    
    public static int readIntLE(byte[] data) {
	return readIntLE(data, 0);
    }
    public static int readIntLE(byte[] data, int offset) {
	return ((data[offset+3] & 0xFF) << 24 |
		(data[offset+2] & 0xFF) << 16 |
		(data[offset+1] & 0xFF) << 8 |
		(data[offset+0] & 0xFF) << 0);
    }
    public static short readShortLE(byte[] data) {
	return readShortLE(data, 0);
    }
    public static short readShortLE(byte[] data, int offset) {
	return (short) ((data[offset+1] & 0xFF) << 8 |
			(data[offset+0] & 0xFF) << 0);
    }
    public static byte readByteLE(byte[] data) {
	return readByteLE(data, 0);
    }
    public static byte readByteLE(byte[] data, int offset) {
	return data[offset];
    }

    public static byte[] toByteArrayLE(byte b) {
	byte[] result = new byte[1];
	result[0] = b;
	return result;
    }
    public static byte[] toByteArrayLE(short s) {
	byte[] result = new byte[2];
	result[0] = (byte) ((s >> 0) & 0xFF);
	result[1] = (byte) ((s >> 8) & 0xFF);
	return result;
    }
    public static byte[] toByteArrayLE(int i) {
	byte[] result = new byte[4];
	result[0] = (byte) ((i >> 0) & 0xFF);
	result[1] = (byte) ((i >> 8) & 0xFF);
	result[2] = (byte) ((i >> 16) & 0xFF);
	result[3] = (byte) ((i >> 24) & 0xFF);
	return result;
    }
    public static byte[] toByteArrayLE(long l) {
	byte[] result = new byte[8];
	result[0] = (byte) ((l >> 0) & 0xFF);
	result[1] = (byte) ((l >> 8) & 0xFF);
	result[2] = (byte) ((l >> 16) & 0xFF);
	result[3] = (byte) ((l >> 24) & 0xFF);
	result[4] = (byte) ((l >> 32) & 0xFF);
	result[5] = (byte) ((l >> 40) & 0xFF);
	result[6] = (byte) ((l >> 48) & 0xFF);
	result[7] = (byte) ((l >> 56) & 0xFF);
	return result;
    }
    public static byte[] toByteArrayBE(byte b) {
	byte[] result = new byte[1];
	result[0] = b;
	return result;
    }
    public static byte[] toByteArrayBE(short s) {
	byte[] result = new byte[2];
	result[0] = (byte) ((s >> 8) & 0xFF);
	result[1] = (byte) ((s >> 0) & 0xFF);
	return result;
    }
    public static byte[] toByteArrayBE(int i) {
	byte[] result = new byte[4];
	result[0] = (byte) ((i >> 24) & 0xFF);
	result[1] = (byte) ((i >> 16) & 0xFF);
	result[2] = (byte) ((i >> 8) & 0xFF);
	result[3] = (byte) ((i >> 0) & 0xFF);
	return result;
    }
    public static byte[] toByteArrayBE(long l) {
	byte[] result = new byte[8];
	result[0] = (byte) ((l >> 56) & 0xFF);
	result[1] = (byte) ((l >> 48) & 0xFF);
	result[2] = (byte) ((l >> 40) & 0xFF);
	result[3] = (byte) ((l >> 32) & 0xFF);
	result[4] = (byte) ((l >> 24) & 0xFF);
	result[5] = (byte) ((l >> 16) & 0xFF);
	result[6] = (byte) ((l >> 8) & 0xFF);
	result[7] = (byte) ((l >> 0) & 0xFF);
	return result;
    }

    public static boolean zeroed(byte[] ba) {
	for(byte b : ba)
	    if(b != 0)
		return false;
	return true;
    }
    
    public static void zero(byte[] ba) {
	set(ba, 0, ba.length, (byte)0);
    }
    public static void zero(byte[] ba, int offset, int length) {
	set(ba, offset, length, (byte)0);
    }
    public static void zero(short[] ba) {
	set(ba, 0, ba.length, (short)0);
    }
    public static void zero(short[] ba, int offset, int length) {
	set(ba, offset, length, (short)0);
    }
    public static void zero(int[] ba) {
	set(ba, 0, ba.length, (int)0);
    }
    public static void zero(int[] ba, int offset, int length) {
	set(ba, offset, length, (int)0);
    }
    public static void zero(long[] ba) {
	set(ba, 0, ba.length, (long)0);
    }
    public static void zero(long[] ba, int offset, int length) {
	set(ba, offset, length, (long)0);
    }
    
    public static void set(byte[] ba, int offset, int length, byte value) {
	for(int i = offset; i < length; ++i)
	    ba[i] = value;
    }
    public static void set(short[] ba, int offset, int length, short value) {
	for(int i = offset; i < length; ++i)
	    ba[i] = value;
    }
    public static void set(int[] ba, int offset, int length, int value) {
	for(int i = offset; i < length; ++i)
	    ba[i] = value;
    }
    public static void set(long[] ba, int offset, int length, long value) {
	for(int i = offset; i < length; ++i)
	    ba[i] = value;
    }

    public static byte[] createCopy(byte[] data) {
	return createCopy(data, 0, data.length);
    }
    
    public static byte[] createCopy(byte[] data, int offset, int length) {
	byte[] copy = new byte[length];
	System.arraycopy(data, offset, copy, 0, length);
	return copy;
    }

    public static boolean arraysEqual(boolean[] a, boolean[] b) {
	if(a.length != b.length)
	    return false;
	else {
	    for(int i = 0; i < a.length; ++i)
		if(a[i] != b[i])
		    return false;
	    return true;
	}
    }
    public static boolean arraysEqual(byte[] a, byte[] b) {
	if(a.length != b.length)
	    return false;
	else {
	    for(int i = 0; i < a.length; ++i)
		if(a[i] != b[i])
		    return false;
	    return true;
	}
    }
    public static boolean arraysEqual(char[] a, char[] b) {
	if(a.length != b.length)
	    return false;
	else {
	    for(int i = 0; i < a.length; ++i)
		if(a[i] != b[i])
		    return false;
	    return true;
	}
    }
    public static boolean arraysEqual(short[] a, short[] b) {
	if(a.length != b.length)
	    return false;
	else {
	    for(int i = 0; i < a.length; ++i)
		if(a[i] != b[i])
		    return false;
	    return true;
	}
    }
    public static boolean arraysEqual(int[] a, int[] b) {
	if(a.length != b.length)
	    return false;
	else {
	    for(int i = 0; i < a.length; ++i)
		if(a[i] != b[i])
		    return false;
	    return true;
	}
    }
    public static boolean arraysEqual(long[] a, long[] b) {
	if(a.length != b.length)
	    return false;
	else {
	    for(int i = 0; i < a.length; ++i)
		if(a[i] != b[i])
		    return false;
	    return true;
	}
    }
    public static boolean arraysEqual(Object[] a, Object[] b) {
	if(a.length != b.length)
	    return false;
	else {
	    for(int i = 0; i < a.length; ++i)
		if(!a[i].equals(b[i]))
		    return false;
	    return true;
	}
    }
    
    public static long pow(long a, long b) {
	if(b < 0) throw new IllegalArgumentException("b can not be negative");
	
	long result = 1;
	for(long i = 0; i < b; ++i)
	    result *= a;
	return result;
    }

    public static int strlen(byte[] data) {
	int length = 0;
	for(byte b : data) {
	    if(b == 0)
		break;
	    else
		++length;
	}
	return length;
    }
    
    public static boolean getBit(long data, int bitNumber) {
	return ((data >>> bitNumber) & 0x1) == 0x1;
    }
}