diff --git a/.classpath b/.classpath index 7ed9b55..e37ce6c 100644 --- a/.classpath +++ b/.classpath @@ -1,9 +1,34 @@ - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project index 71e9037..08db56b 100644 --- a/.project +++ b/.project @@ -10,8 +10,14 @@ + + org.eclipse.m2e.core.maven2Builder + + + + org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..dea8fbd --- /dev/null +++ b/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + nl.lxtreme + binutils + 1.0.3 + Java Binary Utilities + Utilities for parsing various executable formats + + + + + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + + + + + junit + junit + 4.12 + test + + + \ No newline at end of file diff --git a/src/main/java/nl/lxtreme/binutils/elf/DynamicEntry.java b/src/main/java/nl/lxtreme/binutils/elf/DynamicEntry.java index c2d2f47..be12a0e 100644 --- a/src/main/java/nl/lxtreme/binutils/elf/DynamicEntry.java +++ b/src/main/java/nl/lxtreme/binutils/elf/DynamicEntry.java @@ -156,10 +156,12 @@ public String toString() { private final Tag tag; private final long value; + private Elf parentElf; - public DynamicEntry(Tag tag, long value) { + public DynamicEntry(Tag tag, long value, Elf parentElf) { this.tag = tag; this.value = value; + this.parentElf = parentElf; } @Override @@ -182,6 +184,12 @@ public Tag getTag() { public long getValue() { return value; } + + public String getValueAsString() { + if (!isStringOffset()) + throw new RuntimeException("This is not a string entry"); + return parentElf.getZString(value); + } public boolean isStringOffset() { return tag.strTableOffset; diff --git a/src/main/java/nl/lxtreme/binutils/elf/Elf.java b/src/main/java/nl/lxtreme/binutils/elf/Elf.java index 20f277d..3bc85f5 100644 --- a/src/main/java/nl/lxtreme/binutils/elf/Elf.java +++ b/src/main/java/nl/lxtreme/binutils/elf/Elf.java @@ -34,6 +34,16 @@ static int expectByteInRange( int in, int lowInclusive, int highInclusive, Strin return in; } + public String getZString( long offset ) + { + try { + return getZString( getDynamicStringTable(), offset ); + } + catch (IOException ex) { + throw new RuntimeException( "Unable to get dynamic string table!", ex ); + } + } + static String getZString( byte[] buf, long offset ) { return getZString( buf, ( int )( offset & 0xFFFFFFFF ) ); @@ -76,14 +86,14 @@ static void readFully( ReadableByteChannel ch, ByteBuffer buf, String errMsg ) t public final DynamicEntry[] dynamicTable; // locally managed. - private FileChannel channel; + private SeekableByteChannel channel; public Elf( File file ) throws IOException { this( FileChannel.open( file.toPath(), StandardOpenOption.READ ) ); } - public Elf( FileChannel channel ) throws IOException + public Elf( SeekableByteChannel channel ) throws IOException { this.channel = channel; this.header = new Header( channel ); @@ -181,7 +191,7 @@ public Elf( FileChannel channel ) throws IOException } Tag tag = Tag.valueOf( ( int )tagValue ); - entries.add( new DynamicEntry( tag, value ) ); + entries.add( new DynamicEntry( tag, value, this ) ); } dynamicTable = entries.toArray( new DynamicEntry[entries.size()] ); @@ -289,7 +299,7 @@ protected StringBuilder dumpSectionHeader( StringBuilder sb, SectionHeader shdr return sb; } - protected byte[] getDynamicStringTable() throws IOException + public byte[] getDynamicStringTable() throws IOException { SectionHeader dynStrHdr = getSectionHeaderByType( SectionType.STRTAB ); if ( dynStrHdr == null ) @@ -484,7 +494,7 @@ public String toString() } catch ( IOException exception ) { - throw new RuntimeException( "Unable to get dynamic string table!" ); + throw new RuntimeException( "Unable to get dynamic string table!", exception ); } } } diff --git a/src/test/java/nl/lxtreme/binutils/AbstractTestCase.java b/src/test/java/nl/lxtreme/binutils/AbstractTestCase.java new file mode 100644 index 0000000..2a95e43 --- /dev/null +++ b/src/test/java/nl/lxtreme/binutils/AbstractTestCase.java @@ -0,0 +1,29 @@ +package nl.lxtreme.binutils; + +import static org.junit.Assert.fail; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLDecoder; + +public abstract class AbstractTestCase { + + /** + * @param aName + * @return + * @throws URISyntaxException + */ + protected File getResource(String aName) throws Exception + { + URL url = getClass().getClassLoader().getResource(aName); + if ((url != null) && "file".equals(url.getProtocol())) + { + String path = URLDecoder.decode(url.getPath(), "UTF-8"); + return new File(path).getCanonicalFile(); + } + fail("Resource " + aName + " not found!"); + return null; // to keep compiler happy... + } + +} diff --git a/src/test/java/nl/lxtreme/binutils/ar/ARTest.java b/src/test/java/nl/lxtreme/binutils/ar/ARTest.java index 07f76be..8176265 100644 --- a/src/test/java/nl/lxtreme/binutils/ar/ARTest.java +++ b/src/test/java/nl/lxtreme/binutils/ar/ARTest.java @@ -12,19 +12,23 @@ package nl.lxtreme.binutils.ar; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import java.io.*; -import java.net.*; -import java.util.*; +import java.io.File; +import java.io.StringWriter; +import java.util.Collection; -import org.junit.*; +import org.junit.Test; + +import nl.lxtreme.binutils.AbstractTestCase; /** * Test cases for AR. */ -public class ARTest +public class ARTest extends AbstractTestCase { // METHODS @@ -126,19 +130,4 @@ public void testReadFile() throws Exception assertEquals(0, string.length()); } - /** - * @param aName - * @return - * @throws URISyntaxException - */ - private File getResource(String aName) throws Exception - { - URL url = getClass().getClassLoader().getResource(aName); - if ((url != null) && "file".equals(url.getProtocol())) - { - return new File(url.getPath()).getCanonicalFile(); - } - fail("Resource " + aName + " not found!"); - return null; // to keep compiler happy... - } } diff --git a/src/test/java/nl/lxtreme/binutils/coff/CoffTest.java b/src/test/java/nl/lxtreme/binutils/coff/CoffTest.java index 65b5671..40125d6 100644 --- a/src/test/java/nl/lxtreme/binutils/coff/CoffTest.java +++ b/src/test/java/nl/lxtreme/binutils/coff/CoffTest.java @@ -8,15 +8,14 @@ package nl.lxtreme.binutils.coff; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; -import java.io.*; -import java.net.*; +import org.junit.Test; -import org.junit.*; +import nl.lxtreme.binutils.AbstractTestCase; -public class CoffTest +public class CoffTest extends AbstractTestCase { @Test @@ -28,14 +27,4 @@ public void testReadECoffFile() throws Exception } } - private File getResource( String aName ) throws Exception - { - URL url = getClass().getClassLoader().getResource( aName ); - if ( ( url != null ) && "file".equals( url.getProtocol() ) ) - { - return new File( url.getPath() ).getCanonicalFile(); - } - fail( "Resource " + aName + " not found!" ); - return null; // to keep compiler happy... - } } diff --git a/src/test/java/nl/lxtreme/binutils/elf/ElfTest.java b/src/test/java/nl/lxtreme/binutils/elf/ElfTest.java index 32bc8c0..b42a4a7 100644 --- a/src/test/java/nl/lxtreme/binutils/elf/ElfTest.java +++ b/src/test/java/nl/lxtreme/binutils/elf/ElfTest.java @@ -8,18 +8,19 @@ package nl.lxtreme.binutils.elf; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; -import java.io.*; -import java.net.*; +import java.io.File; -import org.junit.*; +import org.junit.Test; + +import nl.lxtreme.binutils.AbstractTestCase; /** * Provides test cases for {@link Elf}. */ -public class ElfTest +public class ElfTest extends AbstractTestCase { // METHODS @@ -81,19 +82,4 @@ private void dumpProgramHeaders( ProgramHeader[] aProgramHeaders ) } } - /** - * @param aName - * @return - * @throws URISyntaxException - */ - private File getResource( String aName ) throws Exception - { - URL url = getClass().getClassLoader().getResource( aName ); - if ( ( url != null ) && "file".equals( url.getProtocol() ) ) - { - return new File( url.getPath() ).getCanonicalFile(); - } - fail( "Resource " + aName + " not found!" ); - return null; // to keep compiler happy... - } }