26package org.jau.sys.elf;
28import java.io.IOException;
29import java.io.RandomAccessFile;
31import org.jau.io.Bitstream;
34 static final long MAX_INT_VALUE = ( Integer.MAX_VALUE & 0xffffffffL ) ;
36 static String toHexString(
final int i) {
return "0x"+Integer.toHexString(i); }
38 static String toHexString(
final long i) {
return "0x"+Long.toHexString(i); }
40 static int shortToInt(
final short s) {
41 return s & 0x0000ffff;
44 static int long2Int(
final long v) {
45 if( MAX_INT_VALUE < v ) {
46 throw new IllegalArgumentException(
"Read uint32 value "+toHexString(v)+
" > int32-max "+toHexString(MAX_INT_VALUE));
51 static void readBytes(
final RandomAccessFile in,
final byte[] out,
final int offset,
final int len)
52 throws IOException, IllegalArgumentException
54 in.readFully(out, offset, len);
57 static void seek(
final RandomAccessFile in,
final long newPos)
throws IOException {
61 static int readUInt32(
final boolean isBigEndian,
final byte[] in,
final int offset) {
62 final int v = Bitstream.uint32LongToInt(Bitstream.readUInt32(isBigEndian, in, offset));
64 throw new IllegalArgumentException(
"Read uint32 value "+toHexString(v)+
" > int32-max "+toHexString(MAX_INT_VALUE));
78 static String getString(
final byte[] sb,
final int offset,
final int remaining,
final int[] offset_post)
throws IndexOutOfBoundsException {
79 Bitstream.checkBounds(sb, offset, remaining);
81 for(; strlen < remaining && sb[strlen + offset] != 0; strlen++) { }
82 final String s = 0 < strlen ?
new String(sb, offset, strlen) :
"" ;
83 if(
null != offset_post ) {
84 offset_post[0] = offset + strlen + 1;
97 static int getStringCount(
final byte[] sb,
final int offset,
final int remaining)
throws IndexOutOfBoundsException {
98 Bitstream.checkBounds(sb, offset, remaining);
100 for(
int i=0; i < remaining; i++) {
101 for(; i < remaining && sb[i + offset] != 0; i++) { }
115 public static String[] getStrings(
final byte[] sb,
final int offset,
final int remaining)
throws IndexOutOfBoundsException {
116 final int strnum = getStringCount(sb, offset, remaining);
119 final String[] sa =
new String[strnum];
120 final int[] io_off =
new int[] { offset };
121 for(
int i=0; i < strnum; i++) {
123 sa[i] = getString(sb, io_off[0], remaining - io_off[0], io_off);