Skip to content

Added Maven support and made string values from dynamic entries accessible #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
6 changes: 6 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.lxtreme</groupId>
<artifactId>binutils</artifactId>
<version>1.0.3</version>
<name>Java Binary Utilities</name>
<description>Utilities for parsing various executable formats</description>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
10 changes: 9 additions & 1 deletion src/main/java/nl/lxtreme/binutils/elf/DynamicEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/nl/lxtreme/binutils/elf/Elf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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()] );
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
}
}
}
29 changes: 29 additions & 0 deletions src/test/java/nl/lxtreme/binutils/AbstractTestCase.java
Original file line number Diff line number Diff line change
@@ -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...
}

}
31 changes: 10 additions & 21 deletions src/test/java/nl/lxtreme/binutils/ar/ARTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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...
}
}
19 changes: 4 additions & 15 deletions src/test/java/nl/lxtreme/binutils/coff/CoffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...
}
}
26 changes: 6 additions & 20 deletions src/test/java/nl/lxtreme/binutils/elf/ElfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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...
}
}