001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.dwarfdump.reader;
043:
044: import java.io.ByteArrayInputStream;
045: import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.ElfConstants;
046: import java.io.DataInput;
047: import java.io.DataInputStream;
048: import java.io.IOException;
049: import java.io.RandomAccessFile;
050:
051: /**
052: * I decided not to extend RandomAccessFile because in this case I cannot
053: * overwrite readXXX() methods (they are final in RandomAccessFile).
054: * But we have to deal with bytes order...
055: *
056: * @author ak119685
057: */
058: public class ByteStreamReader implements DataInput {
059: private RandomAccessFile file = null;
060: private String fileName;
061: private int dataEncoding = 0;
062: private int fileClass = 0;
063: private byte address_size = -1;
064:
065: public static final int LSB = 1;
066: public static final int MSB = 2;
067:
068: public ByteStreamReader(String fname, RandomAccessFile reader) {
069: file = reader;
070: this .fileName = fname;
071: }
072:
073: public void dispose() {
074: if (file != null) {
075: try {
076: file.close();
077: } catch (IOException ex) {
078: ex.printStackTrace();
079: }
080: file = null;
081: }
082: }
083:
084: public String getFileName() {
085: return fileName;
086: }
087:
088: public void setDataEncoding(int encoding) {
089: if (encoding == LSB || encoding == MSB) {
090: dataEncoding = encoding;
091: } else {
092: throw new IllegalArgumentException(
093: "Wrong Data Encoding specified (" + encoding + ")."); // NOI18N
094: }
095: }
096:
097: public int getDataEncoding() {
098: return dataEncoding;
099: }
100:
101: public void seek(long pos) throws IOException {
102: file.seek(pos);
103: }
104:
105: public long getFilePointer() throws IOException {
106: return file.getFilePointer();
107: }
108:
109: public long length() throws IOException {
110: return file.length();
111: }
112:
113: public void setAddressSize(byte size) {
114: address_size = size;
115: }
116:
117: public byte getAddressSize() {
118: return address_size;
119: }
120:
121: public long readNumber(int size) throws IOException {
122: byte[] bytes = new byte[size];
123: long n = 0;
124:
125: file.readFully(bytes);
126:
127: for (int i = 0; i < size; i++) {
128: long u = 0;
129:
130: if (dataEncoding == LSB) {
131: u = (0xff & bytes[i]);
132: } else {
133: u = (0xff & bytes[size - i - 1]);
134: }
135:
136: n |= (u << (i * 8));
137: }
138:
139: return n;
140: }
141:
142: public short readShort() throws IOException {
143: return (short) readNumber(2);
144: }
145:
146: public int readInt() throws IOException {
147: return (int) readNumber(4);
148: }
149:
150: public int readInt(boolean useEncoding) throws IOException {
151: if (useEncoding) {
152: return readInt();
153: } else {
154: return file.readInt();
155: }
156: }
157:
158: public long readDWlen() throws IOException {
159: long res = readInt();
160: if (res == -1) {
161: res = readLong();
162: }
163: return res;
164: }
165:
166: public long readLong() throws IOException {
167: return readNumber(8);
168: }
169:
170: public byte[] read(byte b[]) throws IOException {
171: readFully(b);
172: return b;
173: }
174:
175: public void readFully(byte[] b) throws IOException {
176: file.readFully(b);
177: }
178:
179: public void readFully(byte[] b, int off, int len)
180: throws IOException {
181: file.readFully(b, off, len);
182: }
183:
184: public int skipBytes(int n) throws IOException {
185: return file.skipBytes(n);
186: }
187:
188: public boolean readBoolean() throws IOException {
189: return file.readBoolean();
190: }
191:
192: public byte readByte() throws IOException {
193: return file.readByte();
194: }
195:
196: public int readUnsignedByte() throws IOException {
197: return file.readUnsignedByte();
198: }
199:
200: public int readUnsignedShort() throws IOException {
201: return file.readUnsignedShort();
202: }
203:
204: public char readChar() throws IOException {
205: return file.readChar();
206: }
207:
208: public float readFloat() throws IOException {
209: return file.readFloat();
210: }
211:
212: public double readDouble() throws IOException {
213: return file.readDouble();
214: }
215:
216: public String readLine() throws IOException {
217: return file.readLine();
218: }
219:
220: public String readUTF() throws IOException {
221: return file.readUTF();
222: }
223:
224: /**
225: * Reads null-padded limited-lenght UTF-8 string from file. If the string is
226: * exactly len characters long, terminating null is not required.
227: * Note: this function will read len bytes from the stream in ANY case.
228: * @param len bytes to be read
229: * @return Read string
230: */
231: public String readUTF(int len) throws IOException {
232: /*
233: * ReadUTF from DataInputStream will be used for reading.
234: * This assumes that string is represented in modified UTF-8 encoding
235: * of a Unicode string in a file. This, in case, assumes that first 2
236: * bytes indicate length of the string.
237: * So, add 2 bytes and add length
238: */
239:
240: byte[] bytes = new byte[len + 2];
241: bytes[1] = (byte) (0xFF & len);
242: bytes[0] = (byte) (0xFF & (len >> 8));
243: readFully(bytes, 2, len);
244:
245: return new DataInputStream(new ByteArrayInputStream(bytes))
246: .readUTF();
247: }
248:
249: // Little Endian Base 128 (LEB128)
250: private int readLEB128(boolean signed) throws IOException {
251: int result = 0;
252: int shift = 0;
253: int b = 0x80;
254:
255: while ((0x80 & b) != 0) {
256: b = file.readByte();
257: result |= ((0x7f & b) << shift);
258: shift += 7;
259: }
260:
261: if (signed && shift < 32 && (0x40 & b) != 0) {
262: result |= -(1 << shift);
263: }
264:
265: return result;
266: }
267:
268: public int readUnsignedLEB128() throws IOException {
269: return readLEB128(false);
270: }
271:
272: public int readSignedLEB128() throws IOException {
273: return readLEB128(true);
274: }
275:
276: public int getFileClass() {
277: return fileClass;
278: }
279:
280: public void setFileClass(int fileClass) {
281: if (fileClass == ElfConstants.ELFCLASS32
282: || fileClass == ElfConstants.ELFCLASS64) {
283: this .fileClass = fileClass;
284: } else {
285: throw new IllegalArgumentException(
286: "Wrong File Class specified (" + fileClass + ")."); // NOI18N
287: }
288: }
289:
290: public String readString() throws IOException {
291: StringBuilder str = new StringBuilder();
292: byte b = -1;
293:
294: while (b != 0) {
295: b = readByte();
296:
297: if (b != 0) {
298: str.append((char) b);
299: }
300: }
301:
302: return str.toString();
303: }
304:
305: public boolean is32Bit() {
306: return fileClass == ElfConstants.ELFCLASS32;
307: }
308:
309: public boolean is64Bit() {
310: return fileClass == ElfConstants.ELFCLASS64;
311: }
312:
313: public long read3264() throws IOException {
314: return (fileClass == ElfConstants.ELFCLASS32) ? readInt()
315: : readLong();
316: }
317:
318: }
|