jaulib v1.3.0
Jau Support Library (C++, Java, ..)
IOUtils.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2013 Gothel Software e.K.
5 * Copyright (c) 2013 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package org.jau.sys.elf;
27
28import java.io.IOException;
29import java.io.RandomAccessFile;
30
31import org.jau.io.Bitstream;
32
33class IOUtils {
34 static final long MAX_INT_VALUE = ( Integer.MAX_VALUE & 0xffffffffL ) ;
35
36 static String toHexString(final int i) { return "0x"+Integer.toHexString(i); }
37
38 static String toHexString(final long i) { return "0x"+Long.toHexString(i); }
39
40 static int shortToInt(final short s) {
41 return s & 0x0000ffff;
42 }
43
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));
47 }
48 return (int)v;
49 }
50
51 static void readBytes(final RandomAccessFile in, final byte[] out, final int offset, final int len)
52 throws IOException, IllegalArgumentException
53 {
54 in.readFully(out, offset, len);
55 }
56
57 static void seek(final RandomAccessFile in, final long newPos) throws IOException {
58 in.seek(newPos);
59 }
60
61 static int readUInt32(final boolean isBigEndian, final byte[] in, final int offset) {
62 final int v = Bitstream.uint32LongToInt(Bitstream.readUInt32(isBigEndian, in, offset));
63 if( 0 > v ) {
64 throw new IllegalArgumentException("Read uint32 value "+toHexString(v)+" > int32-max "+toHexString(MAX_INT_VALUE));
65 }
66 return v;
67 }
68
69 /**
70 * @param sb byte source buffer to parse
71 * @param offset offset within byte source buffer to start parsing
72 * @param remaining remaining numbers of bytes to parse beginning w/ <code>sb_off</code>,
73 * which shall not exceed <code>sb.length - offset</code>.
74 * @param offset_post optional integer array holding offset post parsing
75 * @return the parsed string
76 * @throws IndexOutOfBoundsException if <code>offset + remaining > sb.length</code>.
77 */
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);
80 int strlen = 0;
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; // incl. EOS
85 }
86 return s;
87 }
88
89 /**
90 * @param sb byte source buffer to parse
91 * @param offset offset within byte source buffer to start parsing
92 * @param remaining remaining numbers of bytes to parse beginning w/ <code>sb_off</code>,
93 * which shall not exceed <code>sb.length - offset</code>.
94 * @return the number of parsed strings
95 * @throws IndexOutOfBoundsException if <code>offset + remaining > sb.length</code>.
96 */
97 static int getStringCount(final byte[] sb, final int offset, final int remaining) throws IndexOutOfBoundsException {
98 Bitstream.checkBounds(sb, offset, remaining);
99 int strnum=0;
100 for(int i=0; i < remaining; i++) {
101 for(; i < remaining && sb[i + offset] != 0; i++) { }
102 strnum++;
103 }
104 return strnum;
105 }
106
107 /**
108 * @param sb byte source buffer to parse
109 * @param offset offset within byte source buffer to start parsing
110 * @param remaining remaining numbers of bytes to parse beginning w/ <code>sb_off</code>,
111 * which shall not exceed <code>sb.length - offset</code>.
112 * @return the parsed strings
113 * @throws IndexOutOfBoundsException if <code>offset + remaining > sb.length</code>.
114 */
115 public static String[] getStrings(final byte[] sb, final int offset, final int remaining) throws IndexOutOfBoundsException {
116 final int strnum = getStringCount(sb, offset, remaining);
117 // System.err.println("XXX: strnum "+strnum+", sb_off "+sb_off+", sb_len "+sb_len);
118
119 final String[] sa = new String[strnum];
120 final int[] io_off = new int[] { offset };
121 for(int i=0; i < strnum; i++) {
122 // System.err.print("XXX: str["+i+"] ["+io_off[0]);
123 sa[i] = getString(sb, io_off[0], remaining - io_off[0], io_off);
124 // System.err.println(".. "+io_off[0]+"[ "+sa[i]);
125 }
126 return sa;
127 }
128
129}