Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
yetmorecode committed Jul 23, 2021
1 parent 8d8bc34 commit 7d19f61
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 12 deletions.
101 changes: 101 additions & 0 deletions src/yetmorecode/file/BinaryFileInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package yetmorecode.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class BinaryFileInputStream extends FileInputStream {

private ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN;

public BinaryFileInputStream(String name) throws FileNotFoundException {
super(name);
}

public BinaryFileInputStream(String name, ByteOrder order) throws FileNotFoundException {
super(name);
byteOrder = order;
}

public BinaryFileInputStream(File file) throws FileNotFoundException {
super(file);
}

public BinaryFileInputStream(File file, ByteOrder order) throws FileNotFoundException {
this(file);
byteOrder = order;
}

public int readByte(long offset) throws IOException {
var old = position(offset);
int ret = readByte();
position(old);
return ret;
}

public int readByte() throws IOException {
return read();
}

public short readShort(long offset) throws IOException {
var old = position(offset);
var i = readShort();
position(old);
return i;
}

public short readShort() throws IOException {
byte[] bytes = readNBytes(2);
ByteBuffer bb = ByteBuffer.wrap(bytes);
bb.order(byteOrder);
return bb.getShort();
}

public int readInt(long offset) throws IOException {
var old = position(offset);
var i = readInt();
position(old);
return i;
}

public int readInt() throws IOException {
byte[] bytes = readNBytes(4);
ByteBuffer bb = ByteBuffer.wrap(bytes);
bb.order(byteOrder);
return bb.getInt();
}

public float readFloat(long offset) throws IOException {
var old = position(offset);
var i = readFloat();
position(old);
return i;
}

public float readFloat() throws IOException {
byte[] bytes = readNBytes(4);
ByteBuffer bb = ByteBuffer.wrap(bytes);
bb.order(byteOrder);
return bb.getFloat();
}

public String readString(long offset, int size) throws IOException {
var old = position(offset);
var i = readString(size);
position(old);
return i;
}

public String readString(int size) throws IOException {
return new String(readNBytes(size));
}

public long position(long offset) throws IOException {
var old = getChannel().position();
getChannel().position(offset);
return old;
}
}
12 changes: 12 additions & 0 deletions src/yetmorecode/file/BinaryFileOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package yetmorecode.file;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class BinaryFileOutputStream extends FileOutputStream {

public BinaryFileOutputStream(String name) throws FileNotFoundException {
super(name);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.exception;
package yetmorecode.file.exception;

/**
* An exception class to handle encountering
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.lx;
package yetmorecode.file.format.lx;

/**
* The Object page table provides information about a logical page in an object.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package yetmorecode.format.lx;
package yetmorecode.file.format.lx;

import java.util.ArrayList;

import yetmorecode.format.mz.MzHeader;
import yetmorecode.file.format.mz.MzHeader;

public class LxExecutable {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.lx;
package yetmorecode.file.format.lx;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.lx;
package yetmorecode.file.format.lx;

/**
* LX/LE/LC executable module header
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.lx;
package yetmorecode.file.format.lx;

/**
* The Object page table provides information about a logical page in an object.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.lx;
package yetmorecode.file.format.lx;

public interface ObjectPageTableEntry {
public int getOffset();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.lx;
package yetmorecode.file.format.lx;

/**
* LX Executable Object Table Entry<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.mz;
package yetmorecode.file.format.mz;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package yetmorecode.format.mz;
package yetmorecode.file.format.mz;

import java.io.IOException;

import yetmorecode.file.BinaryFileInputStream;

/**
* MS-DOS MZ header<br>
Expand Down Expand Up @@ -148,4 +152,30 @@ public class MzHeader {
* DOS-stub bytes
*/
public byte [] stubBytes;

public static MzHeader fromStream(BinaryFileInputStream input, long offset) throws IOException {
var header = new MzHeader();
var old = input.position(offset);
header.signature = input.readShort();
header.bytesOnLastBlock = input.readShort();
header.blockCount = input.readShort();
header.relocations = input.readShort();
header.headerSize = input.readShort();
header.minExtraParagraphs = input.readShort();
header.maxExtraParagraphs = input.readShort();
header.ss = input.readShort();
header.sp = input.readShort();
header.checksum = input.readShort();
header.ip = input.readShort();
header.cs = input.readShort();
header.relocationTableOffset = input.readShort();
header.overlayNumber = input.readShort();
input.skip(8);
header.oemId = input.readShort();
header.oemInfo = input.readShort();
input.skip(20);
header.fileAddressNewExe = input.readInt();
input.position(old);
return header;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package yetmorecode.format.mz;
package yetmorecode.file.format.mz;


/**
Expand Down

0 comments on commit 7d19f61

Please sign in to comment.