change integer stuff, stuff

This commit is contained in:
sirjonasxx 2020-11-10 05:06:42 +01:00
parent e049dc8480
commit 573e5af2e2
33 changed files with 164 additions and 649 deletions

View File

@ -17,20 +17,11 @@ public class Main {
InputStream targetStream = new FileInputStream(unityWasmCode); InputStream targetStream = new FileInputStream(unityWasmCode);
BufferedInputStream in = new BufferedInputStream(targetStream); 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(); long time_before = System.currentTimeMillis();
Module module = new Module(in); Module module = new Module(in);
long time_after = System.currentTimeMillis(); long time_after = System.currentTimeMillis();
System.out.println(String.format("%d", time_after - time_before)); System.out.println(String.format("%d", time_after - time_before));
System.out.println("test"); System.out.println("test");
} }

View File

@ -1,57 +0,0 @@
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

@ -1,7 +1,7 @@
package disassembly.conventions; package disassembly.conventions;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.values.old.OldWUnsignedInt; import disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -15,9 +15,9 @@ public class CustomVector<B> {
private Assembler<B> assembler; private Assembler<B> assembler;
public CustomVector(BufferedInputStream in, Creator<B> creator, Assembler<B> assembler) throws IOException, InvalidOpCodeException { public CustomVector(BufferedInputStream in, Creator<B> creator, Assembler<B> assembler) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(in, 32); long length = WUnsignedInt.read(in, 32);
elements = new ArrayList<>(); elements = new ArrayList<>();
for (int i = 0; i < length.getUnsignedInt(); i++) { for (int i = 0; i < length; i++) {
elements.add(creator.create(in)); elements.add(creator.create(in));
} }
this.assembler = assembler; this.assembler = assembler;
@ -29,8 +29,7 @@ public class CustomVector<B> {
} }
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException { public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(elements.size()); WUnsignedInt.write(elements.size(), out, 32);
length.assemble(out);
for (B b : elements) { for (B b : elements) {
assembler.assemble(b, out); assembler.assemble(b, out);
} }

View File

@ -2,7 +2,7 @@ package disassembly.conventions;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode; import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt; import disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -15,9 +15,9 @@ public class Vector<B extends WASMOpCode> extends WASMOpCode {
private List<B> elements; private List<B> elements;
public Vector(BufferedInputStream in, Creator<B> creator) throws IOException, InvalidOpCodeException { public Vector(BufferedInputStream in, Creator<B> creator) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(in, 32); long length = WUnsignedInt.read(in, 32);
elements = new ArrayList<>(); elements = new ArrayList<>();
for (int i = 0; i < length.getUnsignedInt(); i++) { for (int i = 0; i < length; i++) {
elements.add(creator.create(in)); elements.add(creator.create(in));
} }
} }
@ -27,8 +27,7 @@ public class Vector<B extends WASMOpCode> extends WASMOpCode {
} }
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException { public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(elements.size()); WUnsignedInt.write(elements.size(), out, 32);
length.assemble(out);
for (B b : elements) { for (B b : elements) {
b.assemble(out); b.assemble(out);
} }

View File

@ -3,7 +3,7 @@ package disassembly.instructions.control;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode; import disassembly.WASMOpCode;
import disassembly.types.ValType; import disassembly.types.ValType;
import disassembly.values.old.OldWSignedInt; import disassembly.values.WSignedLong;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -25,7 +25,7 @@ public class BlockType extends WASMOpCode {
} }
else { else {
in.reset(); in.reset();
value = new OldWSignedInt(in, 33); value = WSignedLong.read(in, 33);
} }
} }
@ -41,8 +41,8 @@ public class BlockType extends WASMOpCode {
else if (value instanceof ValType) { else if (value instanceof ValType) {
out.write(((ValType)value).val); out.write(((ValType)value).val);
} }
else if (value instanceof OldWSignedInt) { else if (value instanceof Long) {
((OldWSignedInt)value).assemble(out); WSignedLong.write((Long)value, out, 33);
} }
else { else {
throw new InvalidOpCodeException("Invalid block type"); throw new InvalidOpCodeException("Invalid block type");
@ -66,6 +66,6 @@ public class BlockType extends WASMOpCode {
} }
public boolean isSignedInteger() { public boolean isSignedInteger() {
return value != null && value instanceof OldWSignedInt; return value != null && value instanceof Long;
} }
} }

View File

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

View File

@ -3,7 +3,7 @@ package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr; import disassembly.instructions.Instr;
import disassembly.instructions.InstrType; import disassembly.instructions.InstrType;
import disassembly.values.old.OldWFloatingPoint; import disassembly.values.WFloat;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -11,28 +11,28 @@ import java.io.OutputStream;
public class NumericF32ConstInstr extends Instr { public class NumericF32ConstInstr extends Instr {
private OldWFloatingPoint float32; private float constValue;
public NumericF32ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException { public NumericF32ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType); super(instrType);
float32 = new OldWFloatingPoint(in, 32); constValue = WFloat.read(in);
} }
public NumericF32ConstInstr(InstrType instrType, OldWFloatingPoint float32) throws IOException { public NumericF32ConstInstr(InstrType instrType, float constValue) throws IOException {
super(instrType); super(instrType);
this.float32 = float32; this.constValue = constValue;
} }
@Override @Override
protected void assemble2(OutputStream out) throws IOException { protected void assemble2(OutputStream out) throws IOException {
float32.assemble(out); WFloat.write(constValue, out);
} }
public OldWFloatingPoint getFloat32() { public float getConstValue() {
return float32; return constValue;
} }
public void setFloat32(OldWFloatingPoint float32) { public void setConstValue(float constValue) {
this.float32 = float32; this.constValue = constValue;
} }
} }

View File

@ -3,7 +3,7 @@ package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr; import disassembly.instructions.Instr;
import disassembly.instructions.InstrType; import disassembly.instructions.InstrType;
import disassembly.values.old.OldWFloatingPoint; import disassembly.values.WDouble;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -11,28 +11,28 @@ import java.io.OutputStream;
public class NumericF64ConstInstr extends Instr { public class NumericF64ConstInstr extends Instr {
private OldWFloatingPoint float64; private double constValue;
public NumericF64ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException { public NumericF64ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType); super(instrType);
float64 = new OldWFloatingPoint(in, 64); constValue = WDouble.read(in);
} }
public NumericF64ConstInstr(InstrType instrType, OldWFloatingPoint floatingPoint) throws IOException { public NumericF64ConstInstr(InstrType instrType, float constValue) throws IOException {
super(instrType); super(instrType);
this.float64 = floatingPoint; this.constValue = constValue;
} }
@Override @Override
protected void assemble2(OutputStream out) throws IOException { protected void assemble2(OutputStream out) throws IOException {
float64.assemble(out); WDouble.write(constValue, out);
} }
public OldWFloatingPoint getFloat64() { public double getConstValue() {
return float64; return constValue;
} }
public void setFloat64(OldWFloatingPoint float64) { public void setConstValue(double constValue) {
this.float64 = float64; this.constValue = constValue;
} }
} }

View File

@ -3,7 +3,7 @@ package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr; import disassembly.instructions.Instr;
import disassembly.instructions.InstrType; import disassembly.instructions.InstrType;
import disassembly.values.old.OldWSignedInt; import disassembly.values.WSignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -11,28 +11,28 @@ import java.io.OutputStream;
public class NumericI32ConstInstr extends Instr { public class NumericI32ConstInstr extends Instr {
private OldWSignedInt signedInt32; private int constValue;
public NumericI32ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException { public NumericI32ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType); super(instrType);
signedInt32 = new OldWSignedInt(in, 32); constValue = WSignedInt.read(in, 32);
} }
public NumericI32ConstInstr(InstrType instrType, OldWSignedInt signedInt32) throws IOException { public NumericI32ConstInstr(InstrType instrType, int constValue) throws IOException {
super(instrType); super(instrType);
this.signedInt32 = signedInt32; this.constValue = constValue;
} }
@Override @Override
protected void assemble2(OutputStream out) throws IOException { protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
signedInt32.assemble(out); WSignedInt.write(constValue, out, 32);
} }
public OldWSignedInt getSignedInt32() { public int getConstValue() {
return signedInt32; return constValue;
} }
public void setSignedInt32(OldWSignedInt signedInt32) { public void setConstValue(int constValue) {
this.signedInt32 = signedInt32; this.constValue = constValue;
} }
} }

View File

@ -3,7 +3,7 @@ package disassembly.instructions.numeric;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.instructions.Instr; import disassembly.instructions.Instr;
import disassembly.instructions.InstrType; import disassembly.instructions.InstrType;
import disassembly.values.old.OldWSignedInt; import disassembly.values.WSignedLong;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -11,28 +11,28 @@ import java.io.OutputStream;
public class NumericI64ConstInstr extends Instr { public class NumericI64ConstInstr extends Instr {
private OldWSignedInt signedInt64; private long constValue;
public NumericI64ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException { public NumericI64ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
super(instrType); super(instrType);
signedInt64 = new OldWSignedInt(in, 64); constValue = WSignedLong.read(in, 64);
} }
public NumericI64ConstInstr(InstrType instrType, OldWSignedInt signedInt64) throws IOException { public NumericI64ConstInstr(InstrType instrType, long constValue) throws IOException {
super(instrType); super(instrType);
this.signedInt64 = signedInt64; this.constValue = constValue;
} }
@Override @Override
protected void assemble2(OutputStream out) throws IOException { protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
signedInt64.assemble(out); WSignedLong.write(constValue, out, 64);
} }
public OldWSignedInt getSignedInt64() { public long getConstValue() {
return signedInt64; return constValue;
} }
public void setSignedInt64(OldWSignedInt signedInt64) { public void setConstValue(long constValue) {
this.signedInt64 = signedInt64; this.constValue = constValue;
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,7 @@ package disassembly.modules.sections.code;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode; import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt; import disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -14,7 +14,7 @@ public class Code extends WASMOpCode {
private Func code; private Func code;
public Code(BufferedInputStream in) throws IOException, InvalidOpCodeException { public Code(BufferedInputStream in) throws IOException, InvalidOpCodeException {
OldWUnsignedInt sizeInBytes = new OldWUnsignedInt(in, 32); // don't use long sizeInBytes = WUnsignedInt.read(in, 32); // don't use
code = new Func(in); code = new Func(in);
} }
@ -27,7 +27,7 @@ public class Code extends WASMOpCode {
ByteArrayOutputStream codeBuffer = new ByteArrayOutputStream(); ByteArrayOutputStream codeBuffer = new ByteArrayOutputStream();
code.assemble(codeBuffer); code.assemble(codeBuffer);
byte[] codeInBytes = codeBuffer.toByteArray(); byte[] codeInBytes = codeBuffer.toByteArray();
new OldWUnsignedInt(codeInBytes.length).assemble(out); WUnsignedInt.write(codeInBytes.length, out, 32);
out.write(codeInBytes); out.write(codeInBytes);
} }

View File

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

View File

@ -3,7 +3,6 @@ package disassembly.modules.sections.custom;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.modules.sections.Section; import disassembly.modules.sections.Section;
import disassembly.values.WName; import disassembly.values.WName;
import disassembly.values.old.OldWUnsignedInt;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -15,7 +14,7 @@ public abstract class CustomSection extends Section {
private WName name; private WName name;
public CustomSection(OldWUnsignedInt size, WName name) throws IOException, InvalidOpCodeException { public CustomSection(long size, WName name) throws IOException, InvalidOpCodeException {
super(CUSTOM_SECTION_ID, size); super(CUSTOM_SECTION_ID, size);
this.name = name; this.name = name;
} }

View File

@ -2,7 +2,7 @@ package disassembly.modules.sections.custom;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.values.WName; import disassembly.values.WName;
import disassembly.values.old.OldWUnsignedInt; import disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -10,7 +10,7 @@ import java.io.IOException;
public class CustomSectionFactory { public class CustomSectionFactory {
public static CustomSection get(BufferedInputStream in) throws IOException, InvalidOpCodeException { public static CustomSection get(BufferedInputStream in) throws IOException, InvalidOpCodeException {
OldWUnsignedInt size = new OldWUnsignedInt(in, 32); long size = WUnsignedInt.read(in, 32);
WName name = new WName(in); WName name = new WName(in);
// select implementation // select implementation

View File

@ -2,7 +2,6 @@ package disassembly.modules.sections.custom;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.values.WName; import disassembly.values.WName;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -13,9 +12,9 @@ public class UnImplementedCustomSection extends CustomSection {
private byte[] bytes; private byte[] bytes;
public UnImplementedCustomSection(BufferedInputStream in, OldWUnsignedInt size, WName name) throws IOException, InvalidOpCodeException { public UnImplementedCustomSection(BufferedInputStream in, long size, WName name) throws IOException, InvalidOpCodeException {
super(size, name); super(size, name);
bytes = new byte[(int)size.getUnsignedInt() - name.getValue().getBytes(StandardCharsets.UTF_8).length]; bytes = new byte[(int)size - name.getValue().getBytes(StandardCharsets.UTF_8).length];
in.read(bytes); in.read(bytes);
} }

View File

@ -4,7 +4,7 @@ import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode; import disassembly.WASMOpCode;
import disassembly.instructions.Expression; import disassembly.instructions.Expression;
import disassembly.modules.indices.MemIdx; import disassembly.modules.indices.MemIdx;
import disassembly.values.old.OldWUnsignedInt; import disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -20,8 +20,8 @@ public class Data extends WASMOpCode {
public Data(BufferedInputStream in) throws IOException, InvalidOpCodeException { public Data(BufferedInputStream in) throws IOException, InvalidOpCodeException {
dataMemId = new MemIdx(in); dataMemId = new MemIdx(in);
offset = new Expression(in); offset = new Expression(in);
OldWUnsignedInt length = new OldWUnsignedInt(in, 32); long length = WUnsignedInt.read(in, 32);
data = new byte[(int)length.getUnsignedInt()]; data = new byte[(int)length];
in.read(data); in.read(data);
} }
@ -35,7 +35,7 @@ public class Data extends WASMOpCode {
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException { public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
dataMemId.assemble(out); dataMemId.assemble(out);
offset.assemble(out); offset.assemble(out);
new OldWUnsignedInt(data.length).assemble(out); WUnsignedInt.write(data.length, out, 32);
out.write(data); out.write(data);
} }

View File

@ -2,7 +2,7 @@ package disassembly.types;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode; import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt; import disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -16,12 +16,12 @@ public class Limits extends WASMOpCode {
public Limits(BufferedInputStream in) throws IOException, InvalidOpCodeException { public Limits(BufferedInputStream in) throws IOException, InvalidOpCodeException {
int flag = in.read(); int flag = in.read();
if (flag == 0x00) { if (flag == 0x00) {
min = new OldWUnsignedInt(in, 32).getUnsignedInt(); min = WUnsignedInt.read(in, 32);
max = -1; max = -1;
} }
else if (flag == 0x01) { else if (flag == 0x01) {
min = new OldWUnsignedInt(in, 32).getUnsignedInt(); min = WUnsignedInt.read(in, 32);
max = new OldWUnsignedInt(in, 32).getUnsignedInt(); max = WUnsignedInt.read(in, 32);
} }
else throw new InvalidOpCodeException("Function types must be encoded with 0x00 or 0x01"); else throw new InvalidOpCodeException("Function types must be encoded with 0x00 or 0x01");
} }
@ -33,9 +33,9 @@ public class Limits extends WASMOpCode {
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException { public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(max == -1 ? 0x00 : 0x01); out.write(max == -1 ? 0x00 : 0x01);
new OldWUnsignedInt(min).assemble(out); WUnsignedInt.write(min, out, 32);
if (max != -1) { if (max != -1) {
new OldWUnsignedInt(max).assemble(out); WUnsignedInt.write(max, out, 32);
} }
} }

View File

@ -2,7 +2,6 @@ package disassembly.values;
import disassembly.InvalidOpCodeException; import disassembly.InvalidOpCodeException;
import disassembly.WASMOpCode; import disassembly.WASMOpCode;
import disassembly.values.old.OldWUnsignedInt;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -14,8 +13,8 @@ public class WName extends WASMOpCode {
private String value; private String value;
public WName(BufferedInputStream in) throws IOException, InvalidOpCodeException { public WName(BufferedInputStream in) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(in, 32); long length = WUnsignedInt.read(in, 32);
byte[] arr = new byte[(int)length.getUnsignedInt()]; byte[] arr = new byte[(int)length];
in.read(arr); in.read(arr);
value = new String(arr, StandardCharsets.UTF_8); value = new String(arr, StandardCharsets.UTF_8);
} }
@ -25,9 +24,8 @@ public class WName extends WASMOpCode {
} }
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException { public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
OldWUnsignedInt length = new OldWUnsignedInt(value.length());
length.assemble(out);
byte[] bytes = value.getBytes(StandardCharsets.UTF_8); byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
WUnsignedInt.write(bytes.length, out, 32);
out.write(bytes); out.write(bytes);
} }

View File

@ -11,7 +11,7 @@ public class WSignedInt {
// everything with N <= 32 // everything with N <= 32
public static int read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException { public static int read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException {
if (N <= 32) throw new InvalidOpCodeException("Invalid integer size"); if (N > 32) throw new InvalidOpCodeException("Invalid integer size");
int result = 0; int result = 0;
int cur; int cur;
@ -40,7 +40,7 @@ public class WSignedInt {
} }
public static void write(int value, OutputStream out, int N) throws InvalidOpCodeException, IOException { public static void write(int value, OutputStream out, int N) throws InvalidOpCodeException, IOException {
if (N <= 64) throw new InvalidOpCodeException("Invalid integer size"); if (N > 32) throw new InvalidOpCodeException("Invalid integer size");
int remaining = value >> 7; int remaining = value >> 7;
boolean hasMore = true; boolean hasMore = true;

View File

@ -12,8 +12,6 @@ public class WSignedLong {
// everything with N > 32 && N <= 64 // everything with N > 32 && N <= 64
public static long read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException { public static long read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException {
if (N <= 64) throw new InvalidOpCodeException("Invalid integer size");
long result = 0; long result = 0;
long cur; long cur;
int count = 0; int count = 0;
@ -41,8 +39,6 @@ public class WSignedLong {
} }
public static void write(long value, OutputStream out, int N) throws InvalidOpCodeException, IOException { 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; long remaining = value >> 7;
boolean hasMore = true; boolean hasMore = true;
long end = ((value & Long.MIN_VALUE) == 0) ? 0 : -1; long end = ((value & Long.MIN_VALUE) == 0) ? 0 : -1;

View File

@ -10,7 +10,7 @@ public class WUnsignedInt {
// everything with N <= 63 // everything with N <= 63
public static long read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException, IOException { public static long read(BufferedInputStream in, int N) throws InvalidOpCodeException, IOException, IOException {
if (N <= 63) throw new InvalidOpCodeException("Invalid integer size"); if (N >= 64) throw new InvalidOpCodeException("Invalid integer size");
long result = 0; long result = 0;
long cur; long cur;
@ -31,7 +31,7 @@ public class WUnsignedInt {
} }
public static void write(long value, OutputStream out, int N) throws InvalidOpCodeException, IOException { public static void write(long value, OutputStream out, int N) throws InvalidOpCodeException, IOException {
if (N <= 63) throw new InvalidOpCodeException("Invalid integer size"); if (N >= 64) throw new InvalidOpCodeException("Invalid integer size");
long remaining = value >>> 7; long remaining = value >>> 7;

View File

@ -1,78 +0,0 @@
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

@ -1,169 +0,0 @@
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

@ -1,160 +0,0 @@
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());
}
}