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;
043:
044: import java.io.FileNotFoundException;
045: import java.io.IOException;
046: import java.io.RandomAccessFile;
047: import org.netbeans.modules.cnd.dwarfdump.exception.WrongFileFormatException;
048:
049: /**
050: *
051: * @author Alexander Simon
052: */
053: public class FileMagic {
054: private RandomAccessFile reader;
055: private Magic magic;
056:
057: public FileMagic(String objFileName) throws FileNotFoundException,
058: WrongFileFormatException {
059: reader = new RandomAccessFile(objFileName, "r"); // NOI18N
060: readMagic();
061: }
062:
063: public RandomAccessFile getReader() {
064: return reader;
065: }
066:
067: public Magic getMagic() {
068: return magic;
069: }
070:
071: private void readMagic() throws WrongFileFormatException {
072: byte[] bytes = new byte[8];
073: try {
074: reader.readFully(bytes);
075: } catch (IOException ex) {
076: dispose();
077: throw new WrongFileFormatException(
078: "Not an ELF/PE/COFF/MACH-O file"); // NOI18N
079: }
080: if (isElfMagic(bytes)) {
081: magic = Magic.Elf;
082: } else if (isCoffMagic(bytes)) {
083: magic = Magic.Coff;
084: } else if (isExeMagic(bytes)) {
085: magic = Magic.Exe;
086: } else if (isPeMagic(bytes)) {
087: magic = Magic.Pe;
088: } else if (isMachoMagic(bytes)) {
089: magic = Magic.Macho;
090: } else if (isArchiveMagic(bytes)) {
091: magic = Magic.Arch;
092: } else {
093: dispose();
094: throw new WrongFileFormatException(
095: "Not an ELF/PE/COFF/MACH-O file"); // NOI18N
096: }
097: }
098:
099: public void dispose() {
100: if (reader != null) {
101: try {
102: reader.close();
103: } catch (IOException ex) {
104: ex.printStackTrace();
105: }
106: reader = null;
107: }
108: }
109:
110: public static boolean isExeMagic(byte[] bytes) {
111: return bytes[0] == 'M' && bytes[1] == 'Z';
112: }
113:
114: public static boolean isPeMagic(byte[] bytes) {
115: return bytes[0] == 'P' && bytes[1] == 'E' && bytes[2] == 0
116: && bytes[3] == 0;
117: }
118:
119: public static boolean isCoffMagic(byte[] bytes) {
120: return bytes[0] == 0x4c && bytes[1] == 0x01;
121: }
122:
123: public static boolean isElfMagic(byte[] bytes) {
124: return bytes[0] == 0x7f && bytes[1] == 'E' && bytes[2] == 'L'
125: && bytes[3] == 'F';
126: }
127:
128: public static boolean isMachoMagic(byte[] bytes) {
129: return (bytes[0] == (byte) 0xce || bytes[0] == (byte) 0xcf)
130: && bytes[1] == (byte) 0xfa && bytes[2] == (byte) 0xed
131: && bytes[3] == (byte) 0xfe;
132: }
133:
134: public static boolean isArchiveMagic(byte[] bytes) {
135: return bytes[0] == '!' && bytes[1] == '<' && bytes[2] == 'a'
136: && bytes[3] == 'r' && bytes[4] == 'c'
137: && bytes[5] == 'h' && bytes[6] == '>'
138: && bytes[7] == '\n';
139: }
140:
141: }
|