This commit is contained in:
sirjonasxx 2020-11-10 03:43:22 +01:00
commit e049dc8480
95 changed files with 4427 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# intelliJ
.idea/
*.iml
out/
# maven
bin/
**/target/

1
README.md Normal file
View File

@ -0,0 +1 @@
# G-Wasm

12
pom.xml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>G-Earth</groupId>
<artifactId>G-Wasm</artifactId>
<version>1.0-SNAPSHOT</version>
</project>

37
src/main/java/Main.java Normal file
View File

@ -0,0 +1,37 @@
import disassembly.InvalidOpCodeException;
import disassembly.modules.Magic;
import disassembly.modules.Module;
import disassembly.modules.Version;
import disassembly.modules.sections.SectionFactory;
import disassembly.modules.sections.type.TypeSection;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, InvalidOpCodeException {
File unityWasmCode = new File("C:\\Users\\jonas\\Desktop\\Projects\\Jznnp\\S\\habbo2020\\rawfiles\\habbo2020-global-prod.wasm.code.unityweb");
InputStream targetStream = new FileInputStream(unityWasmCode);
BufferedInputStream in = new BufferedInputStream(targetStream);
//
// SectionFactory factory = new SectionFactory(in);
//
// Magic magic = new Magic(in);
// Version version = new Version(in);
//
// TypeSection section = (TypeSection) factory.get();
long time_before = System.currentTimeMillis();
Module module = new Module(in);
long time_after = System.currentTimeMillis();
System.out.println(String.format("%d", time_after - time_before));
System.out.println("test");
}
}

57
src/main/java/Test.java Normal file
View File

@ -0,0 +1,57 @@
import disassembly.InvalidOpCodeException;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main2(String[] args) throws InvalidOpCodeException, IOException {
List<OldWUnsignedInt> values = new ArrayList<>();
ByteArrayOutputStream tempFill = new ByteArrayOutputStream();
for (int i = 0; i < 12000000; i++) {
OldWUnsignedInt oldWUnsignedInt = new OldWUnsignedInt(i);
oldWUnsignedInt.assemble(tempFill);
}
BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(tempFill.toByteArray()));
long time_before = System.currentTimeMillis();
for (int i = 0; i < 12000000; i++) {
values.add(new OldWUnsignedInt(in, 32));
}
long time_after = System.currentTimeMillis();
System.out.println(String.format("%d", time_after - time_before));
System.out.println("hi");
}
public static void main(String[] args) throws InvalidOpCodeException {
List<Long> values = new ArrayList<>();
long time_before = System.currentTimeMillis();
for (int i = 0; i < 12000000; i++) {
OldWUnsignedInt oldWUnsignedInt = new OldWUnsignedInt(i);
values.add(oldWUnsignedInt.getUnsignedInt());
}
long time_after = System.currentTimeMillis();
System.out.println(String.format("%d", time_after - time_before));
}
}

View File

@ -0,0 +1,8 @@
package disassembly;
public class InvalidOpCodeException extends Exception {
public InvalidOpCodeException(String message) {
super(message);
}
}

View File

@ -0,0 +1,9 @@
package disassembly;
import java.io.*;
public abstract class WASMOpCode {
public abstract void assemble(OutputStream out) throws IOException, InvalidOpCodeException;
}

View File

@ -0,0 +1,10 @@
package disassembly.conventions;
import java.io.IOException;
import java.io.OutputStream;
public interface Assembler<B> {
void assemble(B b, OutputStream out) throws IOException;
}

View File

@ -0,0 +1,13 @@
package disassembly.conventions;
import disassembly.InvalidOpCodeException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public interface Creator<B> {
B create(BufferedInputStream in) throws IOException, InvalidOpCodeException;
}

View File

@ -0,0 +1,46 @@
package disassembly.conventions;
import disassembly.InvalidOpCodeException;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class CustomVector<B> {
private List<B> elements;
private Assembler<B> assembler;
public CustomVector(BufferedInputStream in, Creator<B> creator, Assembler<B> assembler) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(in, 32);
elements = new ArrayList<>();
for (int i = 0; i < length.getUnsignedInt(); i++) {
elements.add(creator.create(in));
}
this.assembler = assembler;
}
public CustomVector(List<B> elements, Assembler<B> assembler) {
this.elements = elements;
this.assembler = assembler;
}
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(elements.size());
length.assemble(out);
for (B b : elements) {
assembler.assemble(b, out);
}
}
public List<B> getElements() {
return elements;
}
public void setElements(List<B> elements) {
this.elements = elements;
}
}

View File

@ -0,0 +1,44 @@
package disassembly.conventions;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Vector<B extends WASMOpCode> extends WASMOpCode {
private List<B> elements;
public Vector(BufferedInputStream in, Creator<B> creator) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(in, 32);
elements = new ArrayList<>();
for (int i = 0; i < length.getUnsignedInt(); i++) {
elements.add(creator.create(in));
}
}
public Vector(List<B> elements) {
this.elements = elements;
}
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(elements.size());
length.assemble(out);
for (B b : elements) {
b.assemble(out);
}
}
public List<B> getElements() {
return elements;
}
public void setElements(List<B> elements) {
this.elements = elements;
}
}

View File

@ -0,0 +1,43 @@
package disassembly.instructions;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Expression extends WASMOpCode {
private List<Instr> instructions;
public Expression(BufferedInputStream in) throws IOException, InvalidOpCodeException {
instructions = new ArrayList<>();
InstrType type;
while ((type = InstrFactory.disassembleType(in)) != InstrType.END) {
instructions.add(InstrFactory.disassemble(in, type));
}
}
public Expression(List<Instr> instructions) {
this.instructions = instructions;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
for(Instr instr : instructions) {
instr.assemble(out);
}
out.write(InstrType.END.val);
}
public List<Instr> getInstructions() {
return instructions;
}
public void setInstructions(List<Instr> instructions) {
this.instructions = instructions;
}
}

View File

@ -0,0 +1,33 @@
package disassembly.instructions;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public abstract class Instr extends WASMOpCode {
private InstrType instrType;
public Instr(InstrType instrType) throws IOException {
this.instrType = instrType;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(instrType.val);
assemble2(out);
}
protected abstract void assemble2(OutputStream out) throws IOException, InvalidOpCodeException;
public InstrType getInstrType() {
return instrType;
}
public void setInstrType(InstrType instrType) {
this.instrType = instrType;
}
}

View File

@ -0,0 +1,85 @@
package disassembly.instructions;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.control.*;
import disassembly.instructions.memory.Mem0Instr;
import disassembly.instructions.memory.MemInstr;
import disassembly.instructions.misc.SingleByteInstr;
import disassembly.instructions.numeric.*;
import disassembly.instructions.variable.GlobalVariableInstr;
import disassembly.instructions.variable.LocalVariableInstr;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class InstrFactory {
public static InstrType disassembleType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
InstrType type = InstrType.from_val(in.read());
if (type == null) {
throw new InvalidOpCodeException("Invalid instruction prefix");
}
return type;
}
private final static Map<InstrType, InstrSupplier> map;
static {
map = new HashMap<>();
// control instructions
map.put(InstrType.UNREACHABLE, SingleByteInstr::new);
map.put(InstrType.NOP, SingleByteInstr::new);
map.put(InstrType.BLOCK, BlockInstr::new);
map.put(InstrType.LOOP, BlockInstr::new);
map.put(InstrType.IF, IfElseInstr::new);
map.put(InstrType.BR, BranchInstr::new);
map.put(InstrType.BR_IF, BranchInstr::new);
map.put(InstrType.BR_TABLE, BranchTableInstr::new);
map.put(InstrType.RETURN, SingleByteInstr::new);
map.put(InstrType.CALL, CallInstr::new);
map.put(InstrType.CALL_INDIRECT, CallIndirectInstr::new);
// parametric instructions
map.put(InstrType.DROP, SingleByteInstr::new);
map.put(InstrType.SELECT, SingleByteInstr::new);
// variable instructions
for (int i = 0x20; i <= 0x22; i++) map.put(InstrType.from_val(i), LocalVariableInstr::new);
for (int i = 0x23; i <= 0x24; i++) map.put(InstrType.from_val(i), GlobalVariableInstr::new);
// memory instructions
for (int i = 0x28; i <= 0x3E; i++) map.put(InstrType.from_val(i), MemInstr::new);
for (int i = 0x3F; i <= 0x40; i++) map.put(InstrType.from_val(i), Mem0Instr::new);
// numeric instructions
map.put(InstrType.I32_CONST, NumericI32ConstInstr::new);
map.put(InstrType.I64_CONST, NumericI64ConstInstr::new);
map.put(InstrType.F32_CONST, NumericF32ConstInstr::new);
map.put(InstrType.F64_CONST, NumericF64ConstInstr::new);
for (int i = 0x45; i <= 0xC4; i++) map.put(InstrType.from_val(i), NumericInstr::new);
map.put(InstrType.IXX_TRUNC_SAT_FXX_SU, TruncSatInstr::new);
}
public static Instr disassemble(BufferedInputStream in, InstrType instrType) throws InvalidOpCodeException, IOException {
if (instrType == InstrType.END || instrType == InstrType.ELSE) {
throw new InvalidOpCodeException("Instruction invalid as a standalone instruction");
}
if (instrType == null) {
throw new InvalidOpCodeException("Invalid instruction prefix");
}
return map.get(instrType).get(in, instrType);
}
}

View File

@ -0,0 +1,12 @@
package disassembly.instructions;
import disassembly.InvalidOpCodeException;
import java.io.BufferedInputStream;
import java.io.IOException;
public interface InstrSupplier {
Instr get(BufferedInputStream in, InstrType type) throws IOException, InvalidOpCodeException;
}

View File

@ -0,0 +1,235 @@
package disassembly.instructions;
import disassembly.types.ElemType;
import java.util.HashMap;
import java.util.Map;
public enum InstrType {
// control instructions
UNREACHABLE(0x00),
NOP(0x01),
BLOCK(0x02),
LOOP(0x03),
IF(0x04),
ELSE(0x05),
END(0x0B),
BR(0x0C),
BR_IF(0x0D),
BR_TABLE(0x0E),
RETURN(0x0F),
CALL(0x10),
CALL_INDIRECT(0x11),
// parametric instructions
DROP(0x1A),
SELECT(0x1B),
// variable instructions
LOCAL_GET(0x20),
LOCAL_SET(0x21),
LOCAL_TEE(0x22),
GLOBAL_GET(0x23),
GLOBAL_SET(0x24),
// memory instructions
I32_LOAD(0x28),
I64_LOAD(0x29),
F32_LOAD(0x2A),
F64_LOAD(0x2B),
I32_LOAD8_S(0x2C),
I32_LOAD8_U(0x2D),
I32_LOAD16_S(0x2E),
I32_LOAD16_U(0x2F),
I64_LOAD8_S(0x30),
I64_LOAD8_U(0x31),
I64_LOAD16_S(0x32),
I64_LOAD16_U(0x33),
I64_LOAD32_S(0x34),
I64_LOAD32_U(0x35),
I32_STORE(0x36),
I64_STORE(0x37),
F32_STORE(0x38),
F64_STORE(0x39),
I32_STORE8(0x3A),
I32_STORE16(0x3B),
I64_STORE8(0X3C),
I64_STORE16(0x3D),
I64_STORE32(0x3E),
MEMORY_SIZE(0x3F),
MEMORY_GROW(0x40),
// numeric instructions
I32_CONST(0x41),
I64_CONST(0x42),
F32_CONST(0x43),
F64_CONST(0x44),
I32_EQZ(0x45),
I32_EQ(0x46),
I32_NE(0x47),
I32_LT_S(0x48),
I32_LT_U(0x49),
I32_GT_S(0x4A),
I32_GT_U(0x4B),
I32_LE_S(0x4C),
I32_LE_U(0x4D),
I32_GE_S(0x4E),
I32_GE_U(0x4F),
I64_EQZ(0x50),
I64_EQ(0x51),
I64_NE(0x52),
I64_LT_S(0x53),
I64_LT_U(0x54),
I64_GT_S(0x55),
I64_GT_U(0x56),
I64_LE_S(0x57),
I64_LE_U(0x58),
I64_GE_S(0x59),
I64_GE_U(0x5A),
F32_EQ(0x5B),
F32_NE(0x5C),
F32_LT(0x5D),
F32_GT(0x5E),
F32_LE(0x5F),
F32_GE(0x60),
F64_EQ(0x61),
F64_NE(0x62),
F64_LT(0x63),
F64_GT(0x64),
F64_LE(0x65),
F64_GE(0x66),
I32_CLZ(0x67),
I32_CTZ(0x68),
I32_POPCNT(0x69),
I32_ADD(0x6A),
I32_SUB(0x6B),
I32_MUL(0x6C),
I32_DIV_S(0x6D),
I32_DIV_U(0x6E),
I32_REM_S(0x6F),
I32_REM_U(0x70),
I32_AND(0x71),
I32_OR(0x72),
I32_XOR(0x73),
I32_SHL(0x74),
I32_SHR_S(0x75),
I32_SHR_U(0x76),
I32_ROTL(0x77),
I32_ROTR(0x78),
I64_CLZ(0x79),
I64_CTZ(0x7A),
I64_POPCNT(0x7B),
I64_ADD(0x7C),
I64_SUB(0x7D),
I64_MUL(0x7E),
I64_DIV_S(0x7F),
I64_DIV_U(0x80),
I64_REM_S(0x81),
I64_REM_U(0x82),
I64_AND(0x83),
I64_OR(0x84),
I64_XOR(0x85),
I64_SHL(0x86),
I64_SHR_S(0x87),
I64_SHR_U(0x88),
I64_ROTL(0x89),
I64_ROTR(0x8A),
F32_ABS(0x8B),
F32_NEG(0x8C),
F32_CEIL(0x8D),
F32_FLOOR(0x8E),
F32_TRUNC(0x8F),
F32_NEAREST(0x90),
F32_SQRT(0x91),
F32_ADD(0x92),
F32_SUB(0x93),
F32_MUL(0x94),
F32_DIV(0x95),
F32_MIN(0x96),
F32_MAX(0x97),
F32_COPYSIGN(0x98),
F64_ABS(0x99),
F64_NEG(0x9A),
F64_CEIL(0x9B),
F64_FLOOR(0x9C),
F64_TRUNC(0x9D),
F64_NEAREST(0x9E),
F64_SQRT(0x9F),
F64_ADD(0xA0),
F64_SUB(0xA1),
F64_MUL(0xA2),
F64_DIV(0xA3),
F64_MIN(0xA4),
F64_MAX(0xA5),
F64_COPYSIGN(0xA6),
I32_WRAP_I64(0xA7),
I32_TRUNC_F32_S(0xA8),
I32_TRUNC_F32_U(0xA9),
I32_TRUNC_F64_S(0xAA),
I32_TRUNC_F64_U(0xAB),
I64_EXTEND_I32_S(0xAC),
I64_EXTEND_I32_U(0xAD),
I64_TRUNC_F32_S(0xAE),
I64_TRUNC_F32_U(0xAF),
I64_TRUNC_F64_S(0xB0),
I64_TRUNC_F64_U(0xB1),
F32_CONVERT_I32_S(0xB2),
F32_CONVERT_I32_U(0xB3),
F32_CONVERT_I64_S(0xB4),
F32_CONVERT_I64_U(0xB5),
F32_DEMOTE_F64(0xB6),
F64_CONVERT_I32_S(0xB7),
F64_CONVERT_I32_U(0xB8),
F64_CONVERT_I64_S(0xB9),
F64_CONVERT_I64_U(0xBA),
F64_PROMOTE_F32(0xBB),
I32_REINTERPRET_F32(0xBC),
I64_REINTERPRET_F64(0xBD),
F32_REINTERPRET_I32(0xBE),
F64_REINTERPRET_I64(0xBF),
I32_EXTEND8_S(0xC0),
I32_EXTENDS16_S(0xC1),
I64_EXTEND8_S(0xC2),
I64_EXTENDS16_6(0xC3),
I64_EXTENDS32_S(0xC4),
IXX_TRUNC_SAT_FXX_SU(0xFC);
public int val;
InstrType(int val) {
this.val = val;
}
private static Map<Integer, InstrType> map = new HashMap<>();
static {
for (InstrType valType : InstrType.values()) {
map.put(valType.val, valType);
}
}
public static InstrType from_val(int val) {
return map.get(val);
}
}

View File

@ -0,0 +1,62 @@
package disassembly.instructions.control;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrFactory;
import disassembly.instructions.InstrType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class BlockInstr extends Instr {
private List<Instr> blockInstructions;
private BlockType blockType;
public BlockInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
blockType = new BlockType(in);
blockInstructions = new ArrayList<>();
InstrType type;
while ((type = InstrFactory.disassembleType(in)) != InstrType.END) {
blockInstructions.add(InstrFactory.disassemble(in, type));
}
}
public BlockInstr(InstrType instrType, List<Instr> blockInstructions, BlockType blockType) throws IOException {
super(instrType);
this.blockInstructions = blockInstructions;
this.blockType = blockType;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
blockType.assemble(out);
for(Instr instr : blockInstructions) {
instr.assemble(out);
}
out.write(InstrType.END.val);
}
public List<Instr> getBlockInstructions() {
return blockInstructions;
}
public void setBlockInstructions(List<Instr> blockInstructions) {
this.blockInstructions = blockInstructions;
}
public BlockType getBlockType() {
return blockType;
}
public void setBlockType(BlockType blockType) {
this.blockType = blockType;
}
}

View File

@ -0,0 +1,71 @@
package disassembly.instructions.control;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.types.ValType;
import disassembly.values.old.OldWSignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class BlockType extends WASMOpCode {
private Object value;
public BlockType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
in.mark(1);
int first = in.read();
if (first == 0x40) {
value = null;
}
else if (ValType.from_val(first) != null) {
value = ValType.from_val(first);
}
else {
in.reset();
value = new OldWSignedInt(in, 33);
}
}
public BlockType(Object value) {
this.value = value;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
if (value == null) {
out.write(0x40);
}
else if (value instanceof ValType) {
out.write(((ValType)value).val);
}
else if (value instanceof OldWSignedInt) {
((OldWSignedInt)value).assemble(out);
}
else {
throw new InvalidOpCodeException("Invalid block type");
}
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public boolean isEmpty() {
return value == null;
}
public boolean isValueType() {
return value != null && value instanceof ValType;
}
public boolean isSignedInteger() {
return value != null && value instanceof OldWSignedInt;
}
}

View File

@ -0,0 +1,38 @@
package disassembly.instructions.control;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.modules.indices.LabelIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class BranchInstr extends Instr {
private LabelIdx labelIdx;
public BranchInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
labelIdx = new LabelIdx(in);
}
public BranchInstr(InstrType instrType, LabelIdx labelIdx) throws IOException {
super(instrType);
this.labelIdx = labelIdx;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
labelIdx.assemble(out);
}
public LabelIdx getLabelIdx() {
return labelIdx;
}
public void setLabelIdx(LabelIdx labelIdx) {
this.labelIdx = labelIdx;
}
}

View File

@ -0,0 +1,52 @@
package disassembly.instructions.control;
import disassembly.InvalidOpCodeException;
import disassembly.conventions.Creator;
import disassembly.conventions.Vector;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.modules.indices.LabelIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class BranchTableInstr extends Instr {
private Vector<LabelIdx> table;
private LabelIdx labelIdx;
public BranchTableInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
table = new Vector<>(in, LabelIdx::new);
labelIdx = new LabelIdx(in);
}
public BranchTableInstr(InstrType instrType, Vector<LabelIdx> table, LabelIdx labelIdx) throws IOException {
super(instrType);
this.table = table;
this.labelIdx = labelIdx;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
table.assemble(out);
labelIdx.assemble(out);
}
public Vector<LabelIdx> getTable() {
return table;
}
public void setTable(Vector<LabelIdx> table) {
this.table = table;
}
public LabelIdx getLabelIdx() {
return labelIdx;
}
public void setLabelIdx(LabelIdx labelIdx) {
this.labelIdx = labelIdx;
}
}

View File

@ -0,0 +1,43 @@
package disassembly.instructions.control;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.modules.indices.FuncIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CallIndirectInstr extends Instr {
private FuncIdx funcIdx;
public CallIndirectInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
funcIdx = new FuncIdx(in);
if (in.read() != 0x00) {
throw new InvalidOpCodeException("Unexpected non-zero byte");
}
}
public CallIndirectInstr(InstrType instrType, FuncIdx funcIdx) throws IOException {
super(instrType);
this.funcIdx = funcIdx;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
funcIdx.assemble(out);
out.write(0x00);
}
public FuncIdx getFuncIdx() {
return funcIdx;
}
public void setFuncIdx(FuncIdx funcIdx) {
this.funcIdx = funcIdx;
}
}

View File

@ -0,0 +1,38 @@
package disassembly.instructions.control;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.modules.indices.FuncIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CallInstr extends Instr {
private FuncIdx funcIdx;
public CallInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
funcIdx = new FuncIdx(in);
}
public CallInstr(InstrType instrType, FuncIdx funcIdx) throws IOException {
super(instrType);
this.funcIdx = funcIdx;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
funcIdx.assemble(out);
}
public FuncIdx getFuncIdx() {
return funcIdx;
}
public void setFuncIdx(FuncIdx funcIdx) {
this.funcIdx = funcIdx;
}
}

View File

@ -0,0 +1,87 @@
package disassembly.instructions.control;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrFactory;
import disassembly.instructions.InstrType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class IfElseInstr extends Instr {
private List<Instr> ifInstructions;
private List<Instr> elseInstructions;
private BlockType blockType;
public IfElseInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
blockType = new BlockType(in);
ifInstructions = new ArrayList<>();
elseInstructions = null;
List<Instr> currentBlock = ifInstructions;
InstrType type;
while ((type = InstrFactory.disassembleType(in)) != InstrType.END) {
if (type == InstrType.ELSE) {
elseInstructions = new ArrayList<>();
currentBlock = elseInstructions;
}
else {
currentBlock.add(InstrFactory.disassemble(in, type));
}
}
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
blockType.assemble(out);
for(Instr instr : ifInstructions) {
instr.assemble(out);
}
if (elseInstructions != null) {
out.write(InstrType.ELSE.val);
for(Instr instr : elseInstructions) {
instr.assemble(out);
}
}
out.write(InstrType.END.val);
}
public IfElseInstr(InstrType instrType, List<Instr> ifInstructions, List<Instr> elseInstructions, BlockType blockType) throws IOException {
super(instrType);
this.ifInstructions = ifInstructions;
this.elseInstructions = elseInstructions;
this.blockType = blockType;
}
public List<Instr> getIfInstructions() {
return ifInstructions;
}
public void setIfInstructions(List<Instr> ifInstructions) {
this.ifInstructions = ifInstructions;
}
public List<Instr> getElseInstructions() {
return elseInstructions;
}
public void setElseInstructions(List<Instr> elseInstructions) {
this.elseInstructions = elseInstructions;
}
public BlockType getBlockType() {
return blockType;
}
public void setBlockType(BlockType blockType) {
this.blockType = blockType;
}
}

View File

@ -0,0 +1,23 @@
package disassembly.instructions.memory;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Mem0Instr extends Instr {
public Mem0Instr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
if (in.read() != 0x00) {
throw new InvalidOpCodeException("Unexpected non-zero byte");
}
}
@Override
protected void assemble2(OutputStream out) throws IOException {
out.write(0x00);
}
}

View File

@ -0,0 +1,47 @@
package disassembly.instructions.memory;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MemArg extends WASMOpCode {
private OldWUnsignedInt align;
private OldWUnsignedInt offset;
public MemArg(BufferedInputStream in) throws IOException, InvalidOpCodeException {
align = new OldWUnsignedInt(in, 32);
offset = new OldWUnsignedInt(in, 32);
}
public MemArg(OldWUnsignedInt align, OldWUnsignedInt offset) {
this.align = align;
this.offset = offset;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
align.assemble(out);
offset.assemble(out);
}
public OldWUnsignedInt getAlign() {
return align;
}
public void setAlign(OldWUnsignedInt align) {
this.align = align;
}
public OldWUnsignedInt getOffset() {
return offset;
}
public void setOffset(OldWUnsignedInt offset) {
this.offset = offset;
}
}

View File

@ -0,0 +1,37 @@
package disassembly.instructions.memory;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MemInstr extends Instr {
private MemArg memArg;
public MemInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
memArg = new MemArg(in);
}
public MemInstr(InstrType instrType, MemArg memArg) throws IOException {
super(instrType);
this.memArg = memArg;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
memArg.assemble(out);
}
public MemArg getMemArg() {
return memArg;
}
public void setMemArg(MemArg memArg) {
this.memArg = memArg;
}
}

View File

@ -0,0 +1,19 @@
package disassembly.instructions.misc;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SingleByteInstr extends Instr {
public SingleByteInstr(BufferedInputStream in, InstrType instrType) throws IOException {
super(instrType);
}
@Override
protected void assemble2(OutputStream out) {
// do nothing
}
}

View File

@ -0,0 +1,38 @@
package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.values.old.OldWFloatingPoint;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class NumericF32ConstInstr extends Instr {
private OldWFloatingPoint float32;
public NumericF32ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
float32 = new OldWFloatingPoint(in, 32);
}
public NumericF32ConstInstr(InstrType instrType, OldWFloatingPoint float32) throws IOException {
super(instrType);
this.float32 = float32;
}
@Override
protected void assemble2(OutputStream out) throws IOException {
float32.assemble(out);
}
public OldWFloatingPoint getFloat32() {
return float32;
}
public void setFloat32(OldWFloatingPoint float32) {
this.float32 = float32;
}
}

View File

@ -0,0 +1,38 @@
package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.values.old.OldWFloatingPoint;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class NumericF64ConstInstr extends Instr {
private OldWFloatingPoint float64;
public NumericF64ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
float64 = new OldWFloatingPoint(in, 64);
}
public NumericF64ConstInstr(InstrType instrType, OldWFloatingPoint floatingPoint) throws IOException {
super(instrType);
this.float64 = floatingPoint;
}
@Override
protected void assemble2(OutputStream out) throws IOException {
float64.assemble(out);
}
public OldWFloatingPoint getFloat64() {
return float64;
}
public void setFloat64(OldWFloatingPoint float64) {
this.float64 = float64;
}
}

View File

@ -0,0 +1,38 @@
package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.values.old.OldWSignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class NumericI32ConstInstr extends Instr {
private OldWSignedInt signedInt32;
public NumericI32ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
signedInt32 = new OldWSignedInt(in, 32);
}
public NumericI32ConstInstr(InstrType instrType, OldWSignedInt signedInt32) throws IOException {
super(instrType);
this.signedInt32 = signedInt32;
}
@Override
protected void assemble2(OutputStream out) throws IOException {
signedInt32.assemble(out);
}
public OldWSignedInt getSignedInt32() {
return signedInt32;
}
public void setSignedInt32(OldWSignedInt signedInt32) {
this.signedInt32 = signedInt32;
}
}

View File

@ -0,0 +1,38 @@
package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.values.old.OldWSignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class NumericI64ConstInstr extends Instr {
private OldWSignedInt signedInt64;
public NumericI64ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
signedInt64 = new OldWSignedInt(in, 64);
}
public NumericI64ConstInstr(InstrType instrType, OldWSignedInt signedInt64) throws IOException {
super(instrType);
this.signedInt64 = signedInt64;
}
@Override
protected void assemble2(OutputStream out) throws IOException {
signedInt64.assemble(out);
}
public OldWSignedInt getSignedInt64() {
return signedInt64;
}
public void setSignedInt64(OldWSignedInt signedInt64) {
this.signedInt64 = signedInt64;
}
}

View File

@ -0,0 +1,20 @@
package disassembly.instructions.numeric;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class NumericInstr extends Instr {
public NumericInstr(BufferedInputStream in, InstrType instrType) throws IOException {
super(instrType);
}
@Override
protected void assemble2(OutputStream out) {
// nothing
}
}

View File

@ -0,0 +1,42 @@
package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TruncSatInstr extends Instr {
private OldWUnsignedInt type;
public TruncSatInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
type = new OldWUnsignedInt(in, 32);
long value = type.getUnsignedInt();
if (value < 0 || value > 7) {
throw new InvalidOpCodeException("Invalid opcode");
}
}
public TruncSatInstr(InstrType instrType, OldWUnsignedInt type) throws IOException {
super(instrType);
this.type = type;
}
@Override
protected void assemble2(OutputStream out) throws IOException {
type.assemble(out);
}
public OldWUnsignedInt getType() {
return type;
}
public void setType(OldWUnsignedInt type) {
this.type = type;
}
}

View File

@ -0,0 +1,38 @@
package disassembly.instructions.variable;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.modules.indices.GlobalIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class GlobalVariableInstr extends Instr {
private GlobalIdx globalIdx;
public GlobalVariableInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
globalIdx = new GlobalIdx(in);
}
public GlobalVariableInstr(InstrType instrType, GlobalIdx globalIdx) throws IOException {
super(instrType);
this.globalIdx = globalIdx;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
globalIdx.assemble(out);
}
public GlobalIdx getGlobalIdx() {
return globalIdx;
}
public void setGlobalIdx(GlobalIdx globalIdx) {
this.globalIdx = globalIdx;
}
}

View File

@ -0,0 +1,38 @@
package disassembly.instructions.variable;
import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr;
import disassembly.instructions.InstrType;
import disassembly.modules.indices.LocalIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class LocalVariableInstr extends Instr {
private LocalIdx localIdx;
public LocalVariableInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType);
localIdx = new LocalIdx(in);
}
public LocalVariableInstr(InstrType instrType, LocalIdx localIdx) throws IOException {
super(instrType);
this.localIdx = localIdx;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
localIdx.assemble(out);
}
public LocalIdx getLocalIdx() {
return localIdx;
}
public void setLocalIdx(LocalIdx localIdx) {
this.localIdx = localIdx;
}
}

View File

@ -0,0 +1,31 @@
package disassembly.modules;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
public class Magic extends WASMOpCode {
private static final byte[] EXPECTED_MAGIC = new byte[]{0x00, 0x61, 0x73, 0x6D};
public Magic(BufferedInputStream in) throws IOException, InvalidOpCodeException {
byte[] magic = new byte[4];
in.read(magic);
if (!Arrays.equals(magic, EXPECTED_MAGIC)) {
throw new InvalidOpCodeException("Invalid magic");
}
}
public Magic() {}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(EXPECTED_MAGIC);
}
}

View File

@ -0,0 +1,143 @@
package disassembly.modules;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.modules.indices.TypeIdx;
import disassembly.modules.sections.Section;
import disassembly.modules.sections.SectionFactory;
import disassembly.modules.sections.code.CodeSection;
import disassembly.modules.sections.custom.CustomSection;
import disassembly.modules.sections.custom.CustomSectionFactory;
import disassembly.modules.sections.data.DataSection;
import disassembly.modules.sections.element.ElementSection;
import disassembly.modules.sections.export.ExportSection;
import disassembly.modules.sections.function.FunctionSection;
import disassembly.modules.sections.global.GlobalSection;
import disassembly.modules.sections.imprt.ImportSection;
import disassembly.modules.sections.memory.MemorySection;
import disassembly.modules.sections.start.StartSection;
import disassembly.modules.sections.table.TableSection;
import disassembly.modules.sections.type.TypeSection;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Module extends WASMOpCode {
private Magic magic;
private Version version;
private TypeSection typeSection;
private ImportSection importSection;
private FunctionSection functionSection;
private TableSection tableSection;
private MemorySection memorySection;
private GlobalSection globalSection;
private ExportSection exportSection;
private StartSection startSection;
private ElementSection elementSection;
private CodeSection codeSection;
private DataSection dataSection;
private List<List<CustomSection>> customSectionsList;
public Module(BufferedInputStream in) throws IOException, InvalidOpCodeException {
customSectionsList = new ArrayList<>();
magic = new Magic(in);
version = new Version(in);
disassembleCustomSections(in);
typeSection = new TypeSection(in);
disassembleCustomSections(in);
importSection = new ImportSection(in);
disassembleCustomSections(in);
functionSection = new FunctionSection(in);
disassembleCustomSections(in);
// tableSection = new TableSection(in);
// disassembleCustomSections(in);
// memorySection = new MemorySection(in);
// disassembleCustomSections(in);
globalSection = new GlobalSection(in);
disassembleCustomSections(in);
exportSection = new ExportSection(in);
disassembleCustomSections(in);
// startSection = new StartSection(in);
// disassembleCustomSections(in);
elementSection = new ElementSection(in);
disassembleCustomSections(in);
codeSection = new CodeSection(in);
disassembleCustomSections(in);
dataSection = new DataSection(in);
disassembleCustomSections(in);
}
public Module(Magic magic, Version version, TypeSection typeSection, ImportSection importSection, FunctionSection functionSection, TableSection tableSection, MemorySection memorySection, GlobalSection globalSection, ExportSection exportSection, StartSection startSection, ElementSection elementSection, CodeSection codeSection, DataSection dataSection) {
this.magic = magic;
this.version = version;
this.typeSection = typeSection;
this.importSection = importSection;
this.functionSection = functionSection;
this.tableSection = tableSection;
this.memorySection = memorySection;
this.globalSection = globalSection;
this.exportSection = exportSection;
this.startSection = startSection;
this.elementSection = elementSection;
this.codeSection = codeSection;
this.dataSection = dataSection;
}
public Module(TypeSection typeSection, ImportSection importSection, FunctionSection functionSection, TableSection tableSection, MemorySection memorySection, GlobalSection globalSection, ExportSection exportSection, StartSection startSection, ElementSection elementSection, CodeSection codeSection, DataSection dataSection, List<List<CustomSection>> customSectionsList) {
this.magic = new Magic();
this.version = new Version(new byte[]{1, 0, 0, 0});
this.typeSection = typeSection;
this.importSection = importSection;
this.functionSection = functionSection;
this.tableSection = tableSection;
this.memorySection = memorySection;
this.globalSection = globalSection;
this.exportSection = exportSection;
this.startSection = startSection;
this.elementSection = elementSection;
this.codeSection = codeSection;
this.dataSection = dataSection;
this.customSectionsList = customSectionsList;
}
private void disassembleCustomSections(BufferedInputStream in) throws IOException, InvalidOpCodeException {
List<CustomSection> customSections = new ArrayList<>();
int nextSectionId = SectionFactory.readSectionId(in);
if (nextSectionId == 0) {
customSections.add(CustomSectionFactory.get(in));
}
customSectionsList.add(customSections);
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
Section[] sections = new Section[]{typeSection, importSection, functionSection, tableSection,
memorySection, globalSection, exportSection, startSection, elementSection, codeSection,
dataSection};
for (int i = 0; i < 12; i++) {
assembleCustomSections(out, i);
sections[i].assemble(out);
}
assembleCustomSections(out, 11);
}
private void assembleCustomSections(OutputStream out, int location) throws IOException, InvalidOpCodeException {
for(CustomSection section : customSectionsList.get(location)) {
section.assemble(out);
}
}
}

View File

@ -0,0 +1,36 @@
package disassembly.modules;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Version extends WASMOpCode {
byte[] version;
public Version(BufferedInputStream in) throws IOException {
version = new byte[4];
in.read(version);
}
public Version(byte[] version) {
this.version = version;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(version);
}
public byte[] getVersion() {
return version;
}
public void setVersion(byte[] version) {
this.version = version;
}
}

View File

@ -0,0 +1,35 @@
package disassembly.modules.indices;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FuncIdx extends WASMOpCode {
private OldWUnsignedInt x;
public FuncIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
x = new OldWUnsignedInt(in, 32);
}
public FuncIdx(OldWUnsignedInt x) {
this.x = x;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
x.assemble(out);
}
public OldWUnsignedInt getX() {
return x;
}
public void setX(OldWUnsignedInt x) {
this.x = x;
}
}

View File

@ -0,0 +1,35 @@
package disassembly.modules.indices;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class GlobalIdx extends WASMOpCode {
private OldWUnsignedInt x;
public GlobalIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
x = new OldWUnsignedInt(in, 32);
}
public GlobalIdx(OldWUnsignedInt x) {
this.x = x;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
x.assemble(out);
}
public OldWUnsignedInt getX() {
return x;
}
public void setX(OldWUnsignedInt x) {
this.x = x;
}
}

View File

@ -0,0 +1,35 @@
package disassembly.modules.indices;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class LabelIdx extends WASMOpCode {
private OldWUnsignedInt l;
public LabelIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
l = new OldWUnsignedInt(in, 32);
}
public LabelIdx(OldWUnsignedInt l) {
this.l = l;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
l.assemble(out);
}
public OldWUnsignedInt getL() {
return l;
}
public void setL(OldWUnsignedInt l) {
this.l = l;
}
}

View File

@ -0,0 +1,35 @@
package disassembly.modules.indices;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class LocalIdx extends WASMOpCode {
private OldWUnsignedInt x;
public LocalIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
x = new OldWUnsignedInt(in, 32);
}
public LocalIdx(OldWUnsignedInt x) {
this.x = x;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
x.assemble(out);
}
public OldWUnsignedInt getX() {
return x;
}
public void setX(OldWUnsignedInt x) {
this.x = x;
}
}

View File

@ -0,0 +1,35 @@
package disassembly.modules.indices;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MemIdx extends WASMOpCode {
private OldWUnsignedInt x;
public MemIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
x = new OldWUnsignedInt(in, 32);
}
public MemIdx(OldWUnsignedInt x) {
this.x = x;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
x.assemble(out);
}
public OldWUnsignedInt getX() {
return x;
}
public void setX(OldWUnsignedInt x) {
this.x = x;
}
}

View File

@ -0,0 +1,35 @@
package disassembly.modules.indices;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TableIdx extends WASMOpCode {
private OldWUnsignedInt x;
public TableIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
x = new OldWUnsignedInt(in, 32);
}
public TableIdx(OldWUnsignedInt x) {
this.x = x;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
x.assemble(out);
}
public OldWUnsignedInt getX() {
return x;
}
public void setX(OldWUnsignedInt x) {
this.x = x;
}
}

View File

@ -0,0 +1,35 @@
package disassembly.modules.indices;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TypeIdx extends WASMOpCode {
private OldWUnsignedInt x;
public TypeIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
x = new OldWUnsignedInt(in, 32);
}
public TypeIdx(OldWUnsignedInt x) {
this.x = x;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
x.assemble(out);
}
public OldWUnsignedInt getX() {
return x;
}
public void setX(OldWUnsignedInt x) {
this.x = x;
}
}

View File

@ -0,0 +1,52 @@
package disassembly.modules.sections;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public abstract class Section extends WASMOpCode {
private int sectionId;
private OldWUnsignedInt size;
public Section(BufferedInputStream in, int sectionId) throws IOException, InvalidOpCodeException {
this.sectionId = sectionId;
size = new OldWUnsignedInt(in, 32);
}
public Section(int sectionId, OldWUnsignedInt size) {
this.sectionId = sectionId;
this.size = size;
}
public Section(int sectionId) {
this(sectionId, null);
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(sectionId);
ByteArrayOutputStream fakeOutputStream = new ByteArrayOutputStream();
assemble2(fakeOutputStream);
byte[] asbytes = fakeOutputStream.toByteArray();
new OldWUnsignedInt(asbytes.length).assemble(out);
out.write(asbytes);
}
protected abstract void assemble2(OutputStream out) throws IOException, InvalidOpCodeException;
public int getSectionId() {
return sectionId;
}
public OldWUnsignedInt getSize() {
return size;
}
}

View File

@ -0,0 +1,75 @@
package disassembly.modules.sections;
import disassembly.InvalidOpCodeException;
import disassembly.modules.sections.code.CodeSection;
import disassembly.modules.sections.custom.CustomSectionFactory;
import disassembly.modules.sections.data.DataSection;
import disassembly.modules.sections.element.ElementSection;
import disassembly.modules.sections.export.ExportSection;
import disassembly.modules.sections.function.FunctionSection;
import disassembly.modules.sections.global.GlobalSection;
import disassembly.modules.sections.imprt.ImportSection;
import disassembly.modules.sections.memory.MemorySection;
import disassembly.modules.sections.start.StartSection;
import disassembly.modules.sections.table.TableSection;
import disassembly.modules.sections.type.TypeSection;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class SectionFactory {
private static Map<Integer, SectionSupplier> allSections;
static {
allSections = new HashMap<>();
allSections.put(0, CustomSectionFactory::get);
allSections.put(1, TypeSection::new);
allSections.put(2, ImportSection::new);
allSections.put(3, FunctionSection::new);
allSections.put(4, TableSection::new);
allSections.put(5, MemorySection::new);
allSections.put(6, GlobalSection::new);
allSections.put(7, ExportSection::new);
allSections.put(8, StartSection::new);
allSections.put(9, ElementSection::new);
allSections.put(10, CodeSection::new);
allSections.put(11, DataSection::new);
}
private int minimumSectionId = 1;
private BufferedInputStream in;
public SectionFactory(BufferedInputStream in) {
this.in = in;
}
// returns null if none left
public Section get() throws IOException, InvalidOpCodeException {
int sectionType = in.read();
if (sectionType == -1) {
return null;
}
if (sectionType != 0) {
if (sectionType < minimumSectionId) {
throw new InvalidOpCodeException("Unexpected WASM section");
}
minimumSectionId = sectionType + 1;
}
return allSections.get(sectionType).get(in);
}
public static int readSectionId(BufferedInputStream in) throws IOException {
// in.mark(1);
int id = in.read();
// in.reset();
return id;
}
}

View File

@ -0,0 +1,12 @@
package disassembly.modules.sections;
import disassembly.InvalidOpCodeException;
import java.io.BufferedInputStream;
import java.io.IOException;
public interface SectionSupplier {
Section get(BufferedInputStream in) throws IOException, InvalidOpCodeException;
}

View File

@ -0,0 +1,41 @@
package disassembly.modules.sections.code;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Code extends WASMOpCode {
private Func code;
public Code(BufferedInputStream in) throws IOException, InvalidOpCodeException {
OldWUnsignedInt sizeInBytes = new OldWUnsignedInt(in, 32); // don't use
code = new Func(in);
}
public Code(Func code) {
this.code = code;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
ByteArrayOutputStream codeBuffer = new ByteArrayOutputStream();
code.assemble(codeBuffer);
byte[] codeInBytes = codeBuffer.toByteArray();
new OldWUnsignedInt(codeInBytes.length).assemble(out);
out.write(codeInBytes);
}
public Func getCode() {
return code;
}
public void setCode(Func code) {
this.code = code;
}
}

View File

@ -0,0 +1,44 @@
package disassembly.modules.sections.code;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class CodeSection extends Section {
public static final int CODE_SECTION_ID = 10;
private Vector<Code> codesEntries;
public CodeSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, CODE_SECTION_ID);
codesEntries = new Vector<>(in, Code::new);
}
public CodeSection(List<Code> codesEntries) {
super(CODE_SECTION_ID);
this.codesEntries = new Vector<>(codesEntries);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
codesEntries.assemble(out);
}
public List<Code> getCodesEntries() {
return codesEntries.getElements();
}
public void setCodesEntries(List<Code> codesEntries) {
this.codesEntries = new Vector<>(codesEntries);
}
}

View File

@ -0,0 +1,50 @@
package disassembly.modules.sections.code;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.instructions.Expression;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class Func extends WASMOpCode {
private Vector<Locals> localss; // intended double s
private Expression expression;
public Func(BufferedInputStream in) throws IOException, InvalidOpCodeException {
localss = new Vector<>(in, Locals::new);
expression = new Expression(in);
}
public Func(List<Locals> localss, Expression expression) {
this.localss = new Vector<>(localss);
this.expression = expression;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
localss.assemble(out);
expression.assemble(out);
}
public List<Locals> getLocalss() {
return localss.getElements();
}
public void setLocalss(List<Locals> localss) {
this.localss = new Vector<>(localss);
}
public Expression getExpression() {
return expression;
}
public void setExpression(Expression expression) {
this.expression = expression;
}
}

View File

@ -0,0 +1,49 @@
package disassembly.modules.sections.code;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.types.ValType;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Locals extends WASMOpCode {
private OldWUnsignedInt amount;
private ValType valType;
public Locals(BufferedInputStream in) throws IOException, InvalidOpCodeException {
amount = new OldWUnsignedInt(in, 32);
valType = ValType.from_val(in.read());
}
public Locals(long amount, ValType valType) throws InvalidOpCodeException {
this.amount = new OldWUnsignedInt(amount);
this.valType = valType;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
amount.assemble(out);
out.write(valType.val);
}
public OldWUnsignedInt getAmount() {
return amount;
}
public void setAmount(OldWUnsignedInt amount) {
this.amount = amount;
}
public ValType getValType() {
return valType;
}
public void setValType(ValType valType) {
this.valType = valType;
}
}

View File

@ -0,0 +1,38 @@
package disassembly.modules.sections.custom;
import disassembly.InvalidOpCodeException;
import disassembly.modules.sections.Section;
import disassembly.values.WName;
import disassembly.values.old.OldWUnsignedInt;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public abstract class CustomSection extends Section {
public static final int CUSTOM_SECTION_ID = 0;
private WName name;
public CustomSection(OldWUnsignedInt size, WName name) throws IOException, InvalidOpCodeException {
super(CUSTOM_SECTION_ID, size);
this.name = name;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(name.getValue().getBytes(StandardCharsets.UTF_8));
assemble3(out);
}
protected abstract void assemble3(OutputStream out) throws IOException, InvalidOpCodeException;
public WName getName() {
return name;
}
public void setName(WName name) {
this.name = name;
}
}

View File

@ -0,0 +1,20 @@
package disassembly.modules.sections.custom;
import disassembly.InvalidOpCodeException;
import disassembly.values.WName;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
public class CustomSectionFactory {
public static CustomSection get(BufferedInputStream in) throws IOException, InvalidOpCodeException {
OldWUnsignedInt size = new OldWUnsignedInt(in, 32);
WName name = new WName(in);
// select implementation
return new UnImplementedCustomSection(in, size, name);
}
}

View File

@ -0,0 +1,34 @@
package disassembly.modules.sections.custom;
import disassembly.InvalidOpCodeException;
import disassembly.values.WName;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class UnImplementedCustomSection extends CustomSection {
private byte[] bytes;
public UnImplementedCustomSection(BufferedInputStream in, OldWUnsignedInt size, WName name) throws IOException, InvalidOpCodeException {
super(size, name);
bytes = new byte[(int)size.getUnsignedInt() - name.getValue().getBytes(StandardCharsets.UTF_8).length];
in.read(bytes);
}
@Override
protected void assemble3(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(bytes);
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
}

View File

@ -0,0 +1,66 @@
package disassembly.modules.sections.data;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.instructions.Expression;
import disassembly.modules.indices.MemIdx;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Data extends WASMOpCode {
private MemIdx dataMemId;
private Expression offset;
private byte[] data;
public Data(BufferedInputStream in) throws IOException, InvalidOpCodeException {
dataMemId = new MemIdx(in);
offset = new Expression(in);
OldWUnsignedInt length = new OldWUnsignedInt(in, 32);
data = new byte[(int)length.getUnsignedInt()];
in.read(data);
}
public Data(MemIdx dataMemId, Expression offset, byte[] data) {
this.dataMemId = dataMemId;
this.offset = offset;
this.data = data;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
dataMemId.assemble(out);
offset.assemble(out);
new OldWUnsignedInt(data.length).assemble(out);
out.write(data);
}
public MemIdx getDataMemId() {
return dataMemId;
}
public void setDataMemId(MemIdx dataMemId) {
this.dataMemId = dataMemId;
}
public Expression getOffset() {
return offset;
}
public void setOffset(Expression offset) {
this.offset = offset;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}

View File

@ -0,0 +1,44 @@
package disassembly.modules.sections.data;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class DataSection extends Section {
public static final int DATA_SECTION_ID = 11;
private Vector<Data> dataSegments;
public DataSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, DATA_SECTION_ID);
dataSegments = new Vector<>(in, Data::new);
}
public DataSection(List<Data> dataSegments) {
super(DATA_SECTION_ID);
this.dataSegments = new Vector<>(dataSegments);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
dataSegments.assemble(out);
}
public List<Data> getDataSegments() {
return dataSegments.getElements();
}
public void setDataSegments(List<Data> dataSegments) {
this.dataSegments = new Vector<>(dataSegments);
}
}

View File

@ -0,0 +1,62 @@
package disassembly.modules.sections.element;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.instructions.Expression;
import disassembly.modules.indices.FuncIdx;
import disassembly.modules.indices.TableIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Elem extends WASMOpCode {
private TableIdx tableIdx;
private Expression offset;
private Vector<FuncIdx> init;
public Elem(BufferedInputStream in) throws IOException, InvalidOpCodeException {
tableIdx = new TableIdx(in);
offset = new Expression(in);
init = new Vector<>(in, FuncIdx::new);
}
public Elem(TableIdx tableIdx, Expression offset, Vector<FuncIdx> init) {
this.tableIdx = tableIdx;
this.offset = offset;
this.init = init;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
tableIdx.assemble(out);
offset.assemble(out);
init.assemble(out);
}
public TableIdx getTableIdx() {
return tableIdx;
}
public void setTableIdx(TableIdx tableIdx) {
this.tableIdx = tableIdx;
}
public Expression getOffset() {
return offset;
}
public void setOffset(Expression offset) {
this.offset = offset;
}
public Vector<FuncIdx> getInit() {
return init;
}
public void setInit(Vector<FuncIdx> init) {
this.init = init;
}
}

View File

@ -0,0 +1,42 @@
package disassembly.modules.sections.element;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class ElementSection extends Section {
public static final int ELEMENT_SECTION_ID = 9;
private Vector<Elem> elementSegments;
public ElementSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, ELEMENT_SECTION_ID);
elementSegments = new Vector<>(in, Elem::new);
}
public ElementSection(List<Elem> elementSegments) {
super(ELEMENT_SECTION_ID);
this.elementSegments = new Vector<>(elementSegments);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
elementSegments.assemble(out);
}
public List<Elem> getElementSegments() {
return elementSegments.getElements();
}
public void setElementSegments(List<Elem> elementSegments) {
this.elementSegments = new Vector<>(elementSegments);
}
}

View File

@ -0,0 +1,48 @@
package disassembly.modules.sections.export;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.WName;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Export extends WASMOpCode {
private WName name;
private ExportDesc exportDesc;
public Export(BufferedInputStream in) throws IOException, InvalidOpCodeException {
name = new WName(in);
exportDesc = new ExportDesc(in);
}
public Export(WName name, ExportDesc exportDesc) {
this.name = name;
this.exportDesc = exportDesc;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
name.assemble(out);
exportDesc.assemble(out);
}
public WName getName() {
return name;
}
public void setName(WName name) {
this.name = name;
}
public ExportDesc getExportDesc() {
return exportDesc;
}
public void setExportDesc(ExportDesc exportDesc) {
this.exportDesc = exportDesc;
}
}

View File

@ -0,0 +1,75 @@
package disassembly.modules.sections.export;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.modules.indices.FuncIdx;
import disassembly.modules.indices.GlobalIdx;
import disassembly.modules.indices.MemIdx;
import disassembly.modules.indices.TableIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ExportDesc extends WASMOpCode {
private WASMOpCode exportValue;
private int exportType;
public ExportDesc(BufferedInputStream in) throws InvalidOpCodeException, IOException {
exportType = in.read();
if (exportType < 0x00 || exportType > 0x03) {
throw new InvalidOpCodeException("invalid importdesc type");
}
exportValue = exportType == 0x00 ? new FuncIdx(in) :
(exportType == 0x01 ? new TableIdx(in) :
(exportType == 0x02 ? new MemIdx(in) :
new GlobalIdx(in)));
}
public ExportDesc(WASMOpCode exportValue, int exportType) {
this.exportValue = exportValue;
this.exportType = exportType;
}
public ExportDesc(FuncIdx funcIdx) {
this(funcIdx, 0x00);
}
public ExportDesc(TableIdx tableIdx) {
this(tableIdx, 0x01);
}
public ExportDesc(MemIdx memIdx) {
this(memIdx, 0x02);
}
public ExportDesc(GlobalIdx globalIdx) {
this(globalIdx, 0x03);
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(exportType);
exportValue.assemble(out);
}
public WASMOpCode getExportValue() {
return exportValue;
}
public void setExportValue(WASMOpCode exportValue) {
this.exportValue = exportValue;
}
public int getExportType() {
return exportType;
}
public void setExportType(int exportType) {
this.exportType = exportType;
}
}

View File

@ -0,0 +1,42 @@
package disassembly.modules.sections.export;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class ExportSection extends Section {
public static final int EXPORT_SECTION_ID = 7;
private Vector<Export> exports;
public ExportSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, EXPORT_SECTION_ID);
exports = new Vector<>(in, Export::new);
}
public ExportSection(List<Export> exports) {
super(EXPORT_SECTION_ID);
this.exports = new Vector<>(exports);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
exports.assemble(out);
}
public List<Export> getExports() {
return exports.getElements();
}
public void setExports(List<Export> exports) {
this.exports = new Vector<>(exports);
}
}

View File

@ -0,0 +1,43 @@
package disassembly.modules.sections.function;
import disassembly.InvalidOpCodeException;
import disassembly.conventions.Vector;
import disassembly.modules.indices.TypeIdx;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class FunctionSection extends Section {
public static final int FUNCTION_SECTION_ID = 3;
private Vector<TypeIdx> typeIdxVector;
public FunctionSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, FUNCTION_SECTION_ID);
typeIdxVector = new Vector<>(in, TypeIdx::new);
}
public FunctionSection(List<TypeIdx> typeIdxList) {
super(FUNCTION_SECTION_ID);
this.typeIdxVector = new Vector<>(typeIdxList);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
typeIdxVector.assemble(out);
}
public List<TypeIdx> getTypeIdxVector() {
return typeIdxVector.getElements();
}
public void setTypeIdxVector(List<TypeIdx> typeIdxVector) {
this.typeIdxVector = new Vector<>(typeIdxVector);
}
}

View File

@ -0,0 +1,48 @@
package disassembly.modules.sections.global;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.instructions.Expression;
import disassembly.types.GlobalType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Global extends WASMOpCode {
private GlobalType globalType;
private Expression expression;
public Global(BufferedInputStream in) throws IOException, InvalidOpCodeException {
globalType = new GlobalType(in);
expression = new Expression(in);
}
public Global(GlobalType globalType, Expression expression) {
this.globalType = globalType;
this.expression = expression;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
globalType.assemble(out);
expression.assemble(out);
}
public GlobalType getGlobalType() {
return globalType;
}
public void setGlobalType(GlobalType globalType) {
this.globalType = globalType;
}
public Expression getExpression() {
return expression;
}
public void setExpression(Expression expression) {
this.expression = expression;
}
}

View File

@ -0,0 +1,43 @@
package disassembly.modules.sections.global;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class GlobalSection extends Section {
public static final int GLOBAL_SECTION_ID = 6;
private Vector<Global> globals;
public GlobalSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, GLOBAL_SECTION_ID);
globals = new Vector<>(in, Global::new);
}
public GlobalSection(List<Global> globals) {
super(GLOBAL_SECTION_ID);
this.globals = new Vector<>(globals);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
globals.assemble(out);
}
public List<Global> getGlobals() {
return globals.getElements();
}
public void setGlobals(List<Global> globals) {
this.globals = new Vector<>(globals);
}
}

View File

@ -0,0 +1,60 @@
package disassembly.modules.sections.imprt;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.modules.Module;
import disassembly.values.WName;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Import extends WASMOpCode {
private WName module;
private WName name;
private ImportDesc importDescription;
public Import(BufferedInputStream in) throws IOException, InvalidOpCodeException {
module = new WName(in);
name = new WName(in);
importDescription = new ImportDesc(in);
}
public Import(WName module, WName name, ImportDesc importDescription) {
this.module = module;
this.name = name;
this.importDescription = importDescription;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
module.assemble(out);
name.assemble(out);
importDescription.assemble(out);
}
public WName getModule() {
return module;
}
public void setModule(WName module) {
this.module = module;
}
public WName getName() {
return name;
}
public void setName(WName name) {
this.name = name;
}
public ImportDesc getImportDescription() {
return importDescription;
}
public void setImportDescription(ImportDesc importDescription) {
this.importDescription = importDescription;
}
}

View File

@ -0,0 +1,73 @@
package disassembly.modules.sections.imprt;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.modules.indices.TypeIdx;
import disassembly.types.GlobalType;
import disassembly.types.MemType;
import disassembly.types.TableType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ImportDesc extends WASMOpCode {
private WASMOpCode importValue;
private int importType;
public ImportDesc(BufferedInputStream in) throws IOException, InvalidOpCodeException {
importType = in.read();
if (importType < 0x00 || importType > 0x03) {
throw new InvalidOpCodeException("invalid importdesc type");
}
importValue = importType == 0x00 ? new TypeIdx(in) :
(importType == 0x01 ? new TableType(in) :
(importType == 0x02 ? new MemType(in) :
new GlobalType(in)));
}
public ImportDesc(WASMOpCode importValue, int importType) {
this.importValue = importValue;
this.importType = importType;
}
public ImportDesc(TypeIdx typeIdx) {
this(typeIdx, 0x00);
}
public ImportDesc(TableType tableType) {
this(tableType, 0x01);
}
public ImportDesc(MemType memType) {
this(memType, 0x02);
}
public ImportDesc(GlobalType globalType) {
this(globalType, 0x03);
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(importType);
importValue.assemble(out);
}
public WASMOpCode getImportValue() {
return importValue;
}
public void setImportValue(WASMOpCode importValue) {
this.importValue = importValue;
}
public int getImportType() {
return importType;
}
public void setImportType(int importType) {
this.importType = importType;
}
}

View File

@ -0,0 +1,43 @@
package disassembly.modules.sections.imprt;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class ImportSection extends Section {
public static final int IMPORT_SECTION_ID = 2;
private Vector<Import> functionTypes;
public ImportSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, IMPORT_SECTION_ID);
functionTypes = new Vector<>(in, Import::new);
}
public ImportSection(List<Import> functionTypes) {
super(IMPORT_SECTION_ID);
this.functionTypes = new Vector<>(functionTypes);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
functionTypes.assemble(out);
}
public List<Import> getFunctionTypes() {
return functionTypes.getElements();
}
public void setFunctionTypes(List<Import> functionTypes) {
this.functionTypes = new Vector<>(functionTypes);
}
}

View File

@ -0,0 +1,36 @@
package disassembly.modules.sections.memory;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.types.MemType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Mem extends WASMOpCode {
private MemType memType;
public Mem(BufferedInputStream in) throws IOException, InvalidOpCodeException {
memType = new MemType(in);
}
public Mem(MemType memType) {
this.memType = memType;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
memType.assemble(out);
}
public MemType getMemType() {
return memType;
}
public void setMemType(MemType memType) {
this.memType = memType;
}
}

View File

@ -0,0 +1,44 @@
package disassembly.modules.sections.memory;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class MemorySection extends Section {
public static final int MEMORY_SECTION_ID = 5;
private Vector<Mem> memories;
public MemorySection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, MEMORY_SECTION_ID);
memories = new Vector<>(in, Mem::new);
}
public MemorySection(List<Mem> memories) {
super(MEMORY_SECTION_ID);
this.memories = new Vector<>(memories);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
memories.assemble(out);
}
public List<Mem> getMemories() {
return memories.getElements();
}
public void setMemories(List<Mem> memories) {
this.memories = new Vector<>(memories);
}
}

View File

@ -0,0 +1,36 @@
package disassembly.modules.sections.start;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.modules.indices.FuncIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Start extends WASMOpCode {
private FuncIdx funcIdx;
public Start(BufferedInputStream in) throws IOException, InvalidOpCodeException {
funcIdx = new FuncIdx(in);
}
public Start(FuncIdx funcIdx) {
this.funcIdx = funcIdx;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
funcIdx.assemble(out);
}
public FuncIdx getFuncIdx() {
return funcIdx;
}
public void setFuncIdx(FuncIdx funcIdx) {
this.funcIdx = funcIdx;
}
}

View File

@ -0,0 +1,41 @@
package disassembly.modules.sections.start;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StartSection extends Section {
public static final int START_SECTION_ID = 8;
private Start start;
public StartSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, START_SECTION_ID);
start = new Start(in);
}
public StartSection(Start start) {
super(START_SECTION_ID);
this.start = start;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
start.assemble(out);
}
public Start getStart() {
return start;
}
public void setStart(Start start) {
this.start = start;
}
}

View File

@ -0,0 +1,35 @@
package disassembly.modules.sections.table;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.types.TableType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Table extends WASMOpCode {
private TableType tableType;
public Table(BufferedInputStream in) throws IOException, InvalidOpCodeException {
tableType = new TableType(in);
}
public Table(TableType tableType) {
this.tableType = tableType;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
tableType.assemble(out);
}
public TableType getTableType() {
return tableType;
}
public void setTableType(TableType tableType) {
this.tableType = tableType;
}
}

View File

@ -0,0 +1,45 @@
package disassembly.modules.sections.table;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.Creator;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import disassembly.types.TableType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class TableSection extends Section {
public static final int TABLE_SECTION_ID = 4;
private Vector<Table> tables;
public TableSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, TABLE_SECTION_ID);
tables = new Vector<>(in, Table::new);
}
public TableSection(List<Table> tables) {
super(TABLE_SECTION_ID);
this.tables = new Vector<>(tables);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
tables.assemble(out);
}
public List<Table> getTables() {
return tables.getElements();
}
public void setTables(List<Table> tables) {
this.tables = new Vector<>(tables);
}
}

View File

@ -0,0 +1,42 @@
package disassembly.modules.sections.type;
import disassembly.InvalidOpCodeException;
import disassembly.conventions.Vector;
import disassembly.modules.sections.Section;
import disassembly.types.FuncType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class TypeSection extends Section {
public static final int TYPE_SECTION_ID = 1;
private Vector<FuncType> functionTypes;
public TypeSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
super(in, TYPE_SECTION_ID);
functionTypes = new Vector<>(in, FuncType::new);
}
public TypeSection(List<FuncType> functionTypes) {
super(TYPE_SECTION_ID);
this.functionTypes = new Vector<>(functionTypes);
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
functionTypes.assemble(out);
}
public List<FuncType> getFunctionTypes() {
return functionTypes.getElements();
}
public void setFunctionTypes(List<FuncType> functionTypes) {
this.functionTypes = new Vector<>(functionTypes);
}
}

View File

@ -0,0 +1,26 @@
package disassembly.types;
import java.util.HashMap;
import java.util.Map;
public enum ElemType {
FUNCREF(0x70);
public int val;
ElemType(int val) {
this.val = val;
}
private static Map<Integer, ElemType> map = new HashMap<>();
static {
for (ElemType valType : ElemType.values()) {
map.put(valType.val, valType);
}
}
public static ElemType from_val(int val) {
return map.get(val);
}
}

View File

@ -0,0 +1,50 @@
package disassembly.types;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FuncType extends WASMOpCode {
private ResultType parameterType;
private ResultType resultType;
public FuncType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
if (in.read() != 0x60) throw new InvalidOpCodeException("Function types must be encoded with 0x60");
parameterType = new ResultType(in);
resultType = new ResultType(in);
}
public FuncType(ResultType parameterType, ResultType resultType) {
this.parameterType = parameterType;
this.resultType = resultType;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(0x60);
parameterType.assemble(out);
resultType.assemble(out);
}
public ResultType getParameterType() {
return parameterType;
}
public void setParameterType(ResultType parameterType) {
this.parameterType = parameterType;
}
public ResultType getResultType() {
return resultType;
}
public void setResultType(ResultType resultType) {
this.resultType = resultType;
}
}

View File

@ -0,0 +1,47 @@
package disassembly.types;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class GlobalType extends WASMOpCode {
private ValType valType;
private Mutability mutability;
public GlobalType(BufferedInputStream in) throws IOException {
valType = ValType.from_val(in.read());
mutability = Mutability.from_val(in.read());
}
public GlobalType(ValType valType, Mutability mutability) {
this.valType = valType;
this.mutability = mutability;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(valType.val);
out.write(mutability.val);
}
public ValType getValType() {
return valType;
}
public void setValType(ValType valType) {
this.valType = valType;
}
public Mutability getMutability() {
return mutability;
}
public void setMutability(Mutability mutability) {
this.mutability = mutability;
}
}

View File

@ -0,0 +1,61 @@
package disassembly.types;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Limits extends WASMOpCode {
private long min;
private long max;
public Limits(BufferedInputStream in) throws IOException, InvalidOpCodeException {
int flag = in.read();
if (flag == 0x00) {
min = new OldWUnsignedInt(in, 32).getUnsignedInt();
max = -1;
}
else if (flag == 0x01) {
min = new OldWUnsignedInt(in, 32).getUnsignedInt();
max = new OldWUnsignedInt(in, 32).getUnsignedInt();
}
else throw new InvalidOpCodeException("Function types must be encoded with 0x00 or 0x01");
}
public Limits(long min, long max) {
this.min = min;
this.max = max;
}
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(max == -1 ? 0x00 : 0x01);
new OldWUnsignedInt(min).assemble(out);
if (max != -1) {
new OldWUnsignedInt(max).assemble(out);
}
}
public long getMax() {
return max;
}
public long getMin() {
return min;
}
public boolean hasMax() {
return max != -1;
}
public void setMin(long min) {
this.min = min;
}
public void setMax(long max) {
this.max = max;
}
}

View File

@ -0,0 +1,34 @@
package disassembly.types;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MemType extends WASMOpCode {
private Limits limits;
public MemType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
limits = new Limits(in);
}
public MemType(Limits limits) {
this.limits = limits;
}
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
limits.assemble(out);
}
public Limits getLimits() {
return limits;
}
public void setLimits(Limits limits) {
this.limits = limits;
}
}

View File

@ -0,0 +1,26 @@
package disassembly.types;
import java.util.HashMap;
import java.util.Map;
public enum Mutability {
CONST(0x00),
VAR(0x01);
public int val;
Mutability(int val) {
this.val = val;
}
private static Map<Integer, Mutability> map = new HashMap<>();
static {
for (Mutability valType : Mutability.values()) {
map.put(valType.val, valType);
}
}
public static Mutability from_val(int val) {
return map.get(val);
}
}

View File

@ -0,0 +1,52 @@
package disassembly.types;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.conventions.CustomVector;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
public class ResultType extends WASMOpCode {
private CustomVector<ValType> vector;
public ResultType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
vector = new CustomVector<>(
in,
in1 -> ValType.from_val(in1.read()),
(valType, out) -> out.write(valType.val)
);
}
public ResultType(List<ValType> valTypes) {
vector = new CustomVector<>(
valTypes,
(valType, out) -> out.write(valType.val)
);
}
public ResultType(CustomVector<ValType> vector) {
this.vector = vector;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
vector.assemble(out);
}
public List<ValType> typeList() {
return vector.getElements();
}
public CustomVector<ValType> getVector() {
return vector;
}
public void setVector(CustomVector<ValType> vector) {
this.vector = vector;
}
}

View File

@ -0,0 +1,50 @@
package disassembly.types;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TableType extends WASMOpCode {
private ElemType elemType;
private Limits limits;
public TableType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
elemType = ElemType.from_val(in.read());
if (elemType == null) {
throw new InvalidOpCodeException("No such element type");
}
limits = new Limits(in);
}
public TableType(ElemType elemType, Limits limits) {
this.elemType = elemType;
this.limits = limits;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(elemType.val);
limits.assemble(out);
}
public ElemType getElemType() {
return elemType;
}
public void setElemType(ElemType elemType) {
this.elemType = elemType;
}
public Limits getLimits() {
return limits;
}
public void setLimits(Limits limits) {
this.limits = limits;
}
}

View File

@ -0,0 +1,31 @@
package disassembly.types;
import disassembly.WASMOpCode;
import java.util.HashMap;
import java.util.Map;
public enum ValType {
I32(0x7f),
I64(0x7e),
F32(0x7d),
F64(0x7c);
public int val;
ValType(int val) {
this.val = val;
}
private static Map<Integer, ValType> map = new HashMap<>();
static {
for (ValType valType : ValType.values()) {
map.put(valType.val, valType);
}
}
public static ValType from_val(int val) {
return map.get(val);
}
}

View File

@ -0,0 +1,33 @@
package disassembly.values;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class WByte extends WASMOpCode {
private int b;
public WByte(BufferedInputStream inputStream) throws IOException {
b = inputStream.read();
}
public WByte(int b) {
this.b = b;
}
public void assemble(OutputStream out) throws IOException {
out.write(b);
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}

View File

@ -0,0 +1,22 @@
package disassembly.values;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
public class WDouble {
public static double read(BufferedInputStream in) throws IOException {
byte[] bytes = new byte[8];
in.read(bytes);
return ByteBuffer.wrap(bytes).getDouble();
}
public static void write(double value, OutputStream out) throws IOException {
byte[] bytes = new byte[8];
ByteBuffer.wrap(bytes).putDouble(value);
out.write(bytes);
}
}

View File

@ -0,0 +1,23 @@
package disassembly.values;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
public class WFloat {
public static float read(BufferedInputStream in) throws IOException {
byte[] bytes = new byte[4];
in.read(bytes);
return ByteBuffer.wrap(bytes).getFloat();
}
public static void write(float value, OutputStream out) throws IOException {
byte[] bytes = new byte[4];
ByteBuffer.wrap(bytes).putFloat(value);
out.write(bytes);
}
}

View File

@ -0,0 +1,41 @@
package disassembly.values;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class WName extends WASMOpCode {
private String value;
public WName(BufferedInputStream in) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(in, 32);
byte[] arr = new byte[(int)length.getUnsignedInt()];
in.read(arr);
value = new String(arr, StandardCharsets.UTF_8);
}
public WName(String value) {
this.value = value;
}
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(value.length());
length.assemble(out);
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
out.write(bytes);
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,59 @@
package disassembly.values;
import disassembly.InvalidOpCodeException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class WSignedInt {
// everything with N <= 32
public static int read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException {
if (N <= 32) throw new InvalidOpCodeException("Invalid integer size");
int result = 0;
int cur;
int count = 0;
int signBits = -1;
int limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
cur = (in.read()) & 0xff;
result |= (cur & 0x7f) << (count * 7);
signBits <<= 7;
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
// Sign extend if appropriate
if (((signBits >> 1) & result) != 0 ) {
result |= signBits;
}
return result;
}
public static void write(int value, OutputStream out, int N) throws InvalidOpCodeException, IOException {
if (N <= 64) throw new InvalidOpCodeException("Invalid integer size");
int remaining = value >> 7;
boolean hasMore = true;
int end = ((value & Integer.MIN_VALUE) == 0) ? 0 : -1;
while (hasMore) {
hasMore = (remaining != end)
|| ((remaining & 1) != ((value >> 6) & 1));
out.write((byte)((value & 0x7f) | (hasMore ? 0x80 : 0)));
value = remaining;
remaining >>= 7;
}
}
}

View File

@ -0,0 +1,60 @@
package disassembly.values;
import disassembly.InvalidOpCodeException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class WSignedLong {
// everything with N > 32 && N <= 64
public static long read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException {
if (N <= 64) throw new InvalidOpCodeException("Invalid integer size");
long result = 0;
long cur;
int count = 0;
long signBits = -1;
long limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
cur = (in.read()) & 0xff;
result |= (cur & 0x7f) << (count * 7);
signBits <<= 7;
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
// Sign extend if appropriate
if (((signBits >> 1) & result) != 0 ) {
result |= signBits;
}
return result;
}
public static void write(long value, OutputStream out, int N) throws InvalidOpCodeException, IOException {
if (N <= 64) throw new InvalidOpCodeException("Invalid integer size");
long remaining = value >> 7;
boolean hasMore = true;
long end = ((value & Long.MIN_VALUE) == 0) ? 0 : -1;
while (hasMore) {
hasMore = (remaining != end)
|| ((remaining & 1) != ((value >> 6) & 1));
out.write((byte)((value & 0x7f) | (hasMore ? 0x80 : 0)));
value = remaining;
remaining >>= 7;
}
}
}

View File

@ -0,0 +1,47 @@
package disassembly.values;
import disassembly.InvalidOpCodeException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class WUnsignedInt {
// everything with N <= 63
public static long read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException, IOException {
if (N <= 63) throw new InvalidOpCodeException("Invalid integer size");
long result = 0;
long cur;
int count = 0;
int limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
cur = in.read() & 0xff;
result |= (cur & 0x7f) << (count * 7);
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
return result;
}
public static void write(long value, OutputStream out, int N) throws InvalidOpCodeException, IOException {
if (N <= 63) throw new InvalidOpCodeException("Invalid integer size");
long remaining = value >>> 7;
while (remaining != 0) {
out.write((byte)((value & 0x7f) | 0x80));
value = remaining;
remaining >>>= 7;
}
out.write((byte)(value & 0x7f));
}
}

View File

@ -0,0 +1,60 @@
package disassembly.values;
import disassembly.InvalidOpCodeException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class WUnsignedLong {
// everything with N > 63
public static BigInteger read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException, IOException {
BigInteger result = BigInteger.ZERO;
long cur;
int count = 0;
int limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
cur = in.read() & 0xff;
result = result.or(BigInteger.valueOf(cur & 0x7f).shiftLeft(count * 7));
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
return result;
}
public static void write(BigInteger value, OutputStream out, int N) throws InvalidOpCodeException, IOException {
long remaining = value.shiftRight(7).longValueExact();
long l_value = -1;
boolean first = true;
while (remaining != 0) {
if (first) {
out.write((byte) ((value.longValue() & 0x7f) | 0x80));
first = false;
}
else {
out.write((byte) ((l_value & 0x7f) | 0x80));
}
l_value = remaining;
remaining >>>= 7;
}
if (first) {
out.write((byte) (value.longValue() & 0x7f));
}
else {
out.write((byte) (l_value & 0x7f));
}
}
}

View File

@ -0,0 +1,78 @@
package disassembly.values.old;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
public class OldWFloatingPoint extends WASMOpCode {
private final int N;
private byte[] bytes;
public OldWFloatingPoint(BufferedInputStream in, int N) throws IOException, InvalidOpCodeException {
this.N = N;
if (N != 64 && N != 32) {
throw new InvalidOpCodeException("Invalid floating point size");
}
bytes = new byte[N/8];
in.read(bytes);
}
public OldWFloatingPoint(float v) {
this.N = 32;
bytes = new byte[4];
ByteBuffer.wrap(bytes).putFloat(v);
}
public OldWFloatingPoint(double v) {
this.N = 64;
bytes = new byte[8];
ByteBuffer.wrap(bytes).putDouble(v);
}
public void assemble(OutputStream out) throws IOException {
out.write(bytes);
}
public double get() throws InvalidOpCodeException {
if (N == 32) {
return getFloat();
}
else if (N == 64) {
return getDouble();
}
else {
throw new InvalidOpCodeException("Invalid floating point size");
}
}
public float getFloat() throws InvalidOpCodeException {
if (N != 32) throw new InvalidOpCodeException("Invalid floating point size");
return ByteBuffer.wrap(bytes).getFloat();
}
public double getDouble() throws InvalidOpCodeException {
if (N != 64) throw new InvalidOpCodeException("Invalid floating point size");
return ByteBuffer.wrap(bytes).getDouble();
}
public void put(float v) throws InvalidOpCodeException {
if (N != 32) throw new InvalidOpCodeException("Invalid floating point size");
ByteBuffer.wrap(bytes).putFloat(v);
}
public void put(double v) throws InvalidOpCodeException {
if (N != 64) throw new InvalidOpCodeException("Invalid floating point size");
ByteBuffer.wrap(bytes).putDouble(v);
}
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,169 @@
package disassembly.values.old;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class OldWSignedInt extends WASMOpCode {
private final int N;
private byte[] bytes;
public OldWSignedInt(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException {
int cur;
int count = 0;
int limit = N/7 + (N%7 != 0 ? 1 : 0);
List<Byte> buffer = new ArrayList<>();
do {
int read = in.read();
buffer.add((byte)read);
cur = read & 0xff;
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
this.N = N;
bytes = new byte[buffer.size()];
for (int i = 0; i < buffer.size(); i++) {
bytes[i] = buffer.get(i);
}
}
public OldWSignedInt(int value) throws InvalidOpCodeException {
N = 32;
put(value);
}
public OldWSignedInt(long value) throws InvalidOpCodeException {
N = 64;
put(value);
}
public void assemble(OutputStream out) throws IOException {
out.write(bytes);
}
public int getInt() throws InvalidOpCodeException {
if (N <= 32) throw new InvalidOpCodeException("Invalid integer size");
int result = 0;
int cur;
int count = 0;
int signBits = -1;
int limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
cur = bytes[count] & 0xff;
result |= (cur & 0x7f) << (count * 7);
signBits <<= 7;
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
// Sign extend if appropriate
if (((signBits >> 1) & result) != 0 ) {
result |= signBits;
}
return result;
}
public long getLong() throws InvalidOpCodeException {
if (N <= 64) throw new InvalidOpCodeException("Invalid integer size");
long result = 0;
long cur;
int count = 0;
long signBits = -1;
long limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
cur = bytes[count] & 0xff;
result |= (cur & 0x7f) << (count * 7);
signBits <<= 7;
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
// Sign extend if appropriate
if (((signBits >> 1) & result) != 0 ) {
result |= signBits;
}
return result;
}
public void put(int value) throws InvalidOpCodeException {
if (N <= 32) throw new InvalidOpCodeException("Invalid integer size");
int remaining = value >> 7;
boolean hasMore = true;
int end = ((value & Integer.MIN_VALUE) == 0) ? 0 : -1;
List<Byte> buffer = new ArrayList<>();
while (hasMore) {
hasMore = (remaining != end)
|| ((remaining & 1) != ((value >> 6) & 1));
buffer.add((byte) ((value & 0x7f) | (hasMore ? 0x80 : 0)));
value = remaining;
remaining >>= 7;
}
bytes = new byte[buffer.size()];
for (int i = 0; i < buffer.size(); i++) {
bytes[i] = buffer.get(i);
}
}
public void put(long value) throws InvalidOpCodeException {
if (N <= 64) throw new InvalidOpCodeException("Invalid integer size");
long remaining = value >> 7;
boolean hasMore = true;
long end = ((value & Long.MIN_VALUE) == 0) ? 0 : -1;
List<Byte> buffer = new ArrayList<>();
while (hasMore) {
hasMore = (remaining != end)
|| ((remaining & 1) != ((value >> 6) & 1));
buffer.add((byte) ((value & 0x7f) | (hasMore ? 0x80 : 0)));
value = remaining;
remaining >>= 7;
}
bytes = new byte[buffer.size()];
for (int i = 0; i < buffer.size(); i++) {
bytes[i] = buffer.get(i);
}
}
public static void main(String[] args) throws InvalidOpCodeException {
long t = Long.MIN_VALUE;
OldWSignedInt s = new OldWSignedInt(t);
System.out.println(s.getLong());
}
}

View File

@ -0,0 +1,160 @@
package disassembly.values.old;
import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class OldWUnsignedInt extends WASMOpCode {
private final int N;
private byte[] bytes;
public OldWUnsignedInt(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException {
List<Byte> buffer = new ArrayList<>();
int cur;
int count = 0;
int limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
int read = in.read();
buffer.add((byte)read);
cur = read & 0xff;
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
this.N = N;
bytes = new byte[buffer.size()];
for (int i = 0; i < buffer.size(); i++) {
bytes[i] = buffer.get(i);
}
}
public OldWUnsignedInt(long v) throws InvalidOpCodeException {
N = 32;
put(v);
}
public OldWUnsignedInt(BigInteger v) throws InvalidOpCodeException {
N = 64;
put(v);
}
public void assemble(OutputStream out) throws IOException {
out.write(bytes);
}
public long getUnsignedInt() throws InvalidOpCodeException {
if (N != 32) throw new InvalidOpCodeException("Invalid integer size");
long result = 0;
long cur;
int count = 0;
int limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
cur = bytes[count] & 0xff;
result |= (cur & 0x7f) << (count * 7);
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
return result;
}
public BigInteger getUnsignedLong() throws InvalidOpCodeException {
if (N != 64) throw new InvalidOpCodeException("Invalid integer size");
BigInteger result = BigInteger.ZERO;
long cur;
int count = 0;
int limit = N/7 + (N%7 != 0 ? 1 : 0);
do {
cur = bytes[count] & 0xff;
result = result.or(BigInteger.valueOf(cur & 0x7f).shiftLeft(count * 7));
count++;
} while (((cur & 0x80) == 0x80) && count < limit);
if ((cur & 0x80) == 0x80) {
throw new InvalidOpCodeException("invalid LEB128 sequence");
}
return result;
}
public void put(long value) throws InvalidOpCodeException {
if (N != 32) throw new InvalidOpCodeException("Invalid integer size");
long remaining = value >>> 7;
List<Byte> buffer = new ArrayList<>();
while (remaining != 0) {
buffer.add((byte) ((value & 0x7f) | 0x80));
value = remaining;
remaining >>>= 7;
}
buffer.add((byte) (value & 0x7f));
bytes = new byte[buffer.size()];
for (int i = 0; i < buffer.size(); i++) {
bytes[i] = buffer.get(i);
}
}
public void put(BigInteger value) throws InvalidOpCodeException {
if (N != 64) throw new InvalidOpCodeException("Invalid integer size");
long remaining = value.shiftRight(7).longValueExact();
long l_value = -1;
List<Byte> buffer = new ArrayList<>();
while (remaining != 0) {
if (buffer.size() == 0) {
buffer.add((byte) ((value.longValue() & 0x7f) | 0x80));
}
else {
buffer.add((byte) ((l_value & 0x7f) | 0x80));
}
l_value = remaining;
remaining >>>= 7;
}
if (buffer.size() == 0) {
buffer.add((byte) (value.longValue() & 0x7f));
}
else {
buffer.add((byte) (l_value & 0x7f));
}
bytes = new byte[buffer.size()];
for (int i = 0; i < buffer.size(); i++) {
bytes[i] = buffer.get(i);
}
}
public static void main(String[] args) throws InvalidOpCodeException {
OldWUnsignedInt u = new OldWUnsignedInt(45454);
System.out.println(u.getUnsignedInt());
BigInteger test = BigInteger.valueOf(8424526546848754651L).multiply(BigInteger.valueOf(2L));
System.out.println(test);
OldWUnsignedInt u2 = new OldWUnsignedInt(test);
System.out.println(u2.getUnsignedLong().toString());
}
}