diff --git a/src/main/help/help/shared/Frontpage.css b/src/main/help/help/shared/Frontpage.css deleted file mode 100644 index 452bf6e..0000000 --- a/src/main/help/help/shared/Frontpage.css +++ /dev/null @@ -1,58 +0,0 @@ -/* ### - * IP: GHIDRA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - WARNING! - This file is copied to all help directories. If you change this file, you must copy it - to each src/main/help/help/shared directory. - - - Java Help Note: JavaHelp does not accept sizes (like in 'margin-top') in anything but - px (pixel) or with no type marking. - -*/ - -body { margin-bottom: 50px; margin-left: 10px; margin-right: 10px; margin-top: 10px; } /* some padding to improve readability */ -li { font-family:times new roman; font-size:14pt; } -h1 { color:#000080; font-family:times new roman; font-size:36pt; font-style:italic; font-weight:bold; text-align:center; } -h2 { margin: 10px; margin-top: 20px; color:#984c4c; font-family:times new roman; font-size:18pt; font-weight:bold; } -h3 { margin-left: 10px; margin-top: 20px; color:#0000ff; font-family:times new roman; font-size:14pt; font-weight:bold; } -h4 { margin-left: 10px; margin-top: 20px; font-family:times new roman; font-size:14pt; font-style:italic; } - -/* - P tag code. Most of the help files nest P tags inside of blockquote tags (the was the - way it had been done in the beginning). The net effect is that the text is indented. In - modern HTML we would use CSS to do this. We need to support the Ghidra P tags, nested in - blockquote tags, as well as naked P tags. The following two lines accomplish this. Note - that the 'blockquote p' definition will inherit from the first 'p' definition. -*/ -p { margin-left: 40px; font-family:times new roman; font-size:14pt; } -blockquote p { margin-left: 10px; } - -p.providedbyplugin { color:#7f7f7f; margin-left: 10px; font-size:14pt; margin-top:100px } -p.ProvidedByPlugin { color:#7f7f7f; margin-left: 10px; font-size:14pt; margin-top:100px } -p.relatedtopic { color:#800080; margin-left: 10px; font-size:14pt; } -p.RelatedTopic { color:#800080; margin-left: 10px; font-size:14pt; } - -/* - We wish for a tables to have space between it and the preceding element, so that text - is not too close to the top of the table. Also, nest the table a bit so that it is clear - the table relates to the preceding text. -*/ -table { margin-left: 20px; margin-top: 10px; width: 80%;} -td { font-family:times new roman; font-size:14pt; vertical-align: top; } -th { font-family:times new roman; font-size:14pt; font-weight:bold; background-color: #EDF3FE; } - -code { color: black; font-family: courier new; font-size: 14pt; } diff --git a/src/main/help/help/topics/WebAssembly/help.html b/src/main/help/help/topics/WebAssembly/help.html index ec997c4..7facdfb 100644 --- a/src/main/help/help/topics/WebAssembly/help.html +++ b/src/main/help/help/topics/WebAssembly/help.html @@ -6,7 +6,7 @@ WebAssembly - +

WebAssembly

diff --git a/src/main/java/ghidra/program/emulation/WasmEmulateInstructionStateModifier.java b/src/main/java/ghidra/program/emulation/WasmEmulateInstructionStateModifier.java index 7084b12..3c23e44 100644 --- a/src/main/java/ghidra/program/emulation/WasmEmulateInstructionStateModifier.java +++ b/src/main/java/ghidra/program/emulation/WasmEmulateInstructionStateModifier.java @@ -23,7 +23,6 @@ import ghidra.app.util.bin.ByteProvider; import ghidra.app.util.bin.ByteProviderWrapper; import ghidra.app.util.bin.MemBufferByteProvider; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.pcode.emulate.Emulate; import ghidra.pcode.emulate.EmulateInstructionStateModifier; import ghidra.pcode.emulate.EmulateMemoryStateBuffer; @@ -33,6 +32,7 @@ import ghidra.program.model.address.Address; import ghidra.program.model.address.AddressFactory; import ghidra.program.model.address.AddressSpace; +import ghidra.program.model.data.LEB128; import ghidra.program.model.lang.Register; import ghidra.program.model.lang.RegisterValue; import ghidra.program.model.pcode.PcodeOp; @@ -69,12 +69,12 @@ public WasmFunctionAnalysisProvider(AddressFactory addressFactory, MemoryState m while (true) { moduleSize = reader.getPointerIndex(); int id = reader.readNextUnsignedByte(); - LEB128 contentLength = LEB128.readUnsignedValue(reader); - reader.setPointerIndex(reader.getPointerIndex() + contentLength.asLong()); + long contentLength = reader.readNext(LEB128::unsigned); + reader.setPointerIndex(reader.getPointerIndex() + contentLength); // A custom section (id 0) must have a name, so a zero-length custom section // is invalid. This happens if we have two consecutive null bytes, which // suggests that we're at the end of the module. - if (id == 0 && contentLength.asLong() == 0) { + if (id == 0 && contentLength == 0) { break; } } diff --git a/src/main/java/wasm/WasmLoader.java b/src/main/java/wasm/WasmLoader.java index 1c409b3..0d03b52 100644 --- a/src/main/java/wasm/WasmLoader.java +++ b/src/main/java/wasm/WasmLoader.java @@ -344,7 +344,7 @@ private static void createCodeLengthData(Program program, MemoryBlock moduleBloc for (int i = 0; i < codeEntries.size(); i++) { WasmCodeEntry entry = codeEntries.get(i); StructureBuilder builder = new StructureBuilder("code_" + (i + imports.size())); - builder.add(entry.getCodeSizeLeb128(), "code_size"); + builder.addUnsignedLeb128(entry.getCodeSizeLeb128(), "code_size"); long offset = entry.getOffset() - entry.getCodeSizeLeb128().getLength(); createData(program, program.getListing(), moduleBlock.getStart().add(offset), builder.toStructure()); } @@ -435,12 +435,12 @@ private void loadTables(Program program, FileBytes fileBytes, WasmModule module, /** * Copy element segment to table. - * + * * This is public so that it can be called after loading, e.g. to load a passive * element segment once the dynamic table index and offset are known. * * For example, this could be called from a script as follows: - * + * * WasmLoader.loadElementsToTable(getCurrentProgram(), * WasmAnalysis.getState(getCurrentProgram()).getModule(), elemidx, tableidx, * offset, new ConsoleTaskMonitor()) @@ -520,12 +520,12 @@ private void loadMemories(Program program, FileBytes fileBytes, WasmModule modul /** * Copy data segment to memory. - * + * * This is public so that it can be called after loading, e.g. to load a passive * data segment once the dynamic memory index and offset are known. * * For example, this could be called from a script as follows: - * + * * WasmLoader.loadDataToMemory(getCurrentProgram(), * WasmAnalysis.getState(getCurrentProgram()).getModule(), dataidx, memidx, * offset, new ConsoleTaskMonitor()) diff --git a/src/main/java/wasm/analysis/WasmDWARFAnalyzer.java b/src/main/java/wasm/analysis/WasmDWARFAnalyzer.java index 8bd1935..62f7722 100644 --- a/src/main/java/wasm/analysis/WasmDWARFAnalyzer.java +++ b/src/main/java/wasm/analysis/WasmDWARFAnalyzer.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/src/main/java/wasm/analysis/WasmFunctionAnalysis.java b/src/main/java/wasm/analysis/WasmFunctionAnalysis.java index 673e1e1..55b5c1e 100644 --- a/src/main/java/wasm/analysis/WasmFunctionAnalysis.java +++ b/src/main/java/wasm/analysis/WasmFunctionAnalysis.java @@ -23,8 +23,8 @@ import java.util.Map; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.address.Address; +import ghidra.program.model.data.LEB128; import ghidra.program.model.lang.Register; import ghidra.program.model.lang.RegisterValue; import ghidra.program.model.listing.ContextChangeException; @@ -352,11 +352,11 @@ public StackEffect getStackEffect(Address address) { // #region BinaryReader utilities private static long readLeb128(BinaryReader reader) throws IOException { - return LEB128.readUnsignedValue(reader).asLong(); + return reader.readNext(LEB128::unsigned); } private static long readSignedLeb128(BinaryReader reader) throws IOException { - return LEB128.readSignedValue(reader).asLong(); + return reader.readNext(LEB128::signed); } // #endregion @@ -957,7 +957,7 @@ private void analyzeOpcode(WasmAnalysis analysis, Address instAddress, BinaryRea break; } case 0xFC: { - int opcode2 = LEB128.readAsUInt32(reader); + int opcode2 = reader.readNextUnsignedVarIntExact(LEB128::unsigned); switch (opcode2) { case 0x00: /* i32.trunc_sat_f32_s */ case 0x01: /* i32.trunc_sat_f32_u */ @@ -1047,7 +1047,7 @@ private void analyzeOpcode(WasmAnalysis analysis, Address instAddress, BinaryRea break; } case 0xFD: { - int opcode2 = LEB128.readAsUInt32(reader); + int opcode2 = reader.readNextUnsignedVarIntExact(LEB128::unsigned); switch (opcode2) { case 0x0: /* v128.load */ case 0x1: /* v128.load8x8_s */ diff --git a/src/main/java/wasm/analysis/WasmPreAnalyzer.java b/src/main/java/wasm/analysis/WasmPreAnalyzer.java index 1027338..5919281 100644 --- a/src/main/java/wasm/analysis/WasmPreAnalyzer.java +++ b/src/main/java/wasm/analysis/WasmPreAnalyzer.java @@ -25,7 +25,6 @@ import ghidra.app.services.AnalyzerType; import ghidra.app.util.bin.BinaryReader; import ghidra.app.util.bin.MemoryByteProvider; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.app.util.importer.MessageLog; import ghidra.framework.options.Options; import ghidra.program.disassemble.Disassembler; @@ -33,6 +32,7 @@ import ghidra.program.model.address.Address; import ghidra.program.model.address.AddressSet; import ghidra.program.model.address.AddressSetView; +import ghidra.program.model.data.LEB128; import ghidra.program.model.lang.Processor; import ghidra.program.model.listing.Function; import ghidra.program.model.listing.Program; @@ -80,10 +80,10 @@ public void optionsChanged(Options options, Program program) { private int guessCStackGlobalForFunction(Program program, Address funcAddress) throws IOException { BinaryReader codeReader = new BinaryReader(new MemoryByteProvider(program.getMemory(), funcAddress), true); - int localsCount = LEB128.readAsInt32(codeReader); + int localsCount = codeReader.readNextVarInt(LEB128::signed); for (int i = 0; i < localsCount; i++) { - LEB128.readAsInt32(codeReader); /* count */ - LEB128.readAsInt32(codeReader); /* type */ + codeReader.readNextVarInt(LEB128::signed); /* count */ + codeReader.readNextVarInt(LEB128::signed); /* type */ } /* @@ -92,7 +92,7 @@ private int guessCStackGlobalForFunction(Program program, Address funcAddress) t */ if (codeReader.readNextUnsignedByte() != 0x23) return -1; - return LEB128.readAsInt32(codeReader); + return codeReader.readNextVarInt(LEB128::signed); } private int guessCStackGlobal(Program program, List functions, TaskMonitor monitor) { diff --git a/src/main/java/wasm/analysis/WasmStackVariableAnalyzer.java b/src/main/java/wasm/analysis/WasmStackVariableAnalyzer.java index 9051931..318e539 100644 --- a/src/main/java/wasm/analysis/WasmStackVariableAnalyzer.java +++ b/src/main/java/wasm/analysis/WasmStackVariableAnalyzer.java @@ -1,3 +1,18 @@ +/* ### + * IP: GHIDRA + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package wasm.analysis; import java.util.ArrayList; diff --git a/src/main/java/wasm/format/StructureBuilder.java b/src/main/java/wasm/format/StructureBuilder.java index bf822bf..8765b0c 100644 --- a/src/main/java/wasm/format/StructureBuilder.java +++ b/src/main/java/wasm/format/StructureBuilder.java @@ -17,8 +17,8 @@ import java.io.IOException; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.ArrayDataType; import ghidra.program.model.data.CategoryPath; import ghidra.program.model.data.DataType; @@ -99,12 +99,12 @@ public void add(DataType dataType, String name) { add(dataType, name, null); } - public void add(LEB128 leb128, String name, String comment) { - addArray(StructConverter.BYTE, leb128.getLength(), name, comment); + public void addUnsignedLeb128(LEB128Info leb128, String name, String comment) { + add(StructConverter.ULEB128, leb128.getLength(), name, comment); } - public void add(LEB128 leb128, String name) { - add(leb128, name, null); + public void addUnsignedLeb128(LEB128Info leb128, String name) { + addUnsignedLeb128(leb128, name, null); } public void add(StructConverter converter, String name, String comment) throws DuplicateNameException, IOException { diff --git a/src/main/java/wasm/format/sections/WasmCodeSection.java b/src/main/java/wasm/format/sections/WasmCodeSection.java index 4940bfc..53940fa 100644 --- a/src/main/java/wasm/format/sections/WasmCodeSection.java +++ b/src/main/java/wasm/format/sections/WasmCodeSection.java @@ -21,19 +21,19 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.sections.structures.WasmCodeEntry; public class WasmCodeSection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List functions = new ArrayList(); public WasmCodeSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { functions.add(new WasmCodeEntry(reader)); } @@ -45,7 +45,7 @@ public List getFunctions() { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); // Do not add individual code entries to the structure: they need to be parsed // as code instead of data. } diff --git a/src/main/java/wasm/format/sections/WasmCustomSection.java b/src/main/java/wasm/format/sections/WasmCustomSection.java index 57b674f..4ebdf8c 100644 --- a/src/main/java/wasm/format/sections/WasmCustomSection.java +++ b/src/main/java/wasm/format/sections/WasmCustomSection.java @@ -18,7 +18,7 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.sections.structures.WasmName; @@ -37,7 +37,7 @@ public static WasmCustomSection create(BinaryReader reader) throws IOException { long initialOffset = reader.getPointerIndex(); /* skip section header: id + contentLength */ reader.readNextUnsignedByte(); - LEB128.readUnsignedValue(reader); + reader.readNext(LEB128Info::unsigned); String name = new WasmName(reader).getValue(); reader.setPointerIndex(initialOffset); diff --git a/src/main/java/wasm/format/sections/WasmDataSection.java b/src/main/java/wasm/format/sections/WasmDataSection.java index 18af527..f426a38 100644 --- a/src/main/java/wasm/format/sections/WasmDataSection.java +++ b/src/main/java/wasm/format/sections/WasmDataSection.java @@ -21,19 +21,19 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.sections.structures.WasmDataSegment; public class WasmDataSection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List dataSegments = new ArrayList(); public WasmDataSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { dataSegments.add(new WasmDataSegment(reader)); } @@ -45,7 +45,7 @@ public List getSegments() { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < dataSegments.size(); i++) { builder.add(dataSegments.get(i), "segment_" + i); } diff --git a/src/main/java/wasm/format/sections/WasmElementSection.java b/src/main/java/wasm/format/sections/WasmElementSection.java index f23aa1a..f2737f7 100644 --- a/src/main/java/wasm/format/sections/WasmElementSection.java +++ b/src/main/java/wasm/format/sections/WasmElementSection.java @@ -21,19 +21,19 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.sections.structures.WasmElementSegment; public class WasmElementSection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List elements = new ArrayList(); public WasmElementSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { elements.add(new WasmElementSegment(reader)); } @@ -45,7 +45,7 @@ public List getSegments() { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < elements.size(); i++) { builder.add(elements.get(i), "element_" + i); } diff --git a/src/main/java/wasm/format/sections/WasmExportSection.java b/src/main/java/wasm/format/sections/WasmExportSection.java index f0b1731..9d03610 100644 --- a/src/main/java/wasm/format/sections/WasmExportSection.java +++ b/src/main/java/wasm/format/sections/WasmExportSection.java @@ -23,7 +23,7 @@ import java.util.Map; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.WasmEnums.WasmExternalKind; @@ -31,13 +31,13 @@ public class WasmExportSection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List exportList = new ArrayList<>(); private Map> exports = new EnumMap<>(WasmExternalKind.class); public WasmExportSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { WasmExportEntry entry = new WasmExportEntry(reader); WasmExternalKind kind = entry.getKind(); @@ -64,7 +64,7 @@ public WasmExportEntry findEntry(WasmExternalKind kind, int id) { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < exportList.size(); i++) { builder.add(exportList.get(i), "export_" + i); } diff --git a/src/main/java/wasm/format/sections/WasmFunctionSection.java b/src/main/java/wasm/format/sections/WasmFunctionSection.java index 30b12cf..b3c010e 100644 --- a/src/main/java/wasm/format/sections/WasmFunctionSection.java +++ b/src/main/java/wasm/format/sections/WasmFunctionSection.java @@ -20,20 +20,20 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; public class WasmFunctionSection extends WasmSection { - private LEB128 count; - private List types = new ArrayList(); + private LEB128Info count; + private List types = new ArrayList(); public WasmFunctionSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { - types.add(LEB128.readUnsignedValue(reader)); + types.add(reader.readNext(LEB128Info::unsigned)); } } @@ -47,9 +47,9 @@ public int getTypeCount() { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < types.size(); i++) { - builder.add(types.get(i), "function_" + i); + builder.addUnsignedLeb128(types.get(i), "function_" + i); } } diff --git a/src/main/java/wasm/format/sections/WasmGlobalSection.java b/src/main/java/wasm/format/sections/WasmGlobalSection.java index c80715a..77b1e9e 100644 --- a/src/main/java/wasm/format/sections/WasmGlobalSection.java +++ b/src/main/java/wasm/format/sections/WasmGlobalSection.java @@ -21,19 +21,19 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.sections.structures.WasmGlobalEntry; public class WasmGlobalSection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List globals = new ArrayList<>(); public WasmGlobalSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { globals.add(new WasmGlobalEntry(reader)); } @@ -45,7 +45,7 @@ public List getEntries() { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < globals.size(); i++) { builder.add(globals.get(i), "global_" + i); } diff --git a/src/main/java/wasm/format/sections/WasmImportSection.java b/src/main/java/wasm/format/sections/WasmImportSection.java index 24a0142..e635cc1 100644 --- a/src/main/java/wasm/format/sections/WasmImportSection.java +++ b/src/main/java/wasm/format/sections/WasmImportSection.java @@ -23,7 +23,7 @@ import java.util.Map; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.WasmEnums.WasmExternalKind; @@ -31,13 +31,13 @@ public class WasmImportSection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List importList = new ArrayList<>(); private Map> imports = new EnumMap<>(WasmExternalKind.class); public WasmImportSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { WasmImportEntry entry = new WasmImportEntry(reader); WasmExternalKind kind = entry.getKind(); @@ -55,7 +55,7 @@ public List getImports(WasmExternalKind kind) { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < importList.size(); i++) { builder.add(importList.get(i), "import_" + i); } diff --git a/src/main/java/wasm/format/sections/WasmLinearMemorySection.java b/src/main/java/wasm/format/sections/WasmLinearMemorySection.java index 1c05286..7e0a044 100644 --- a/src/main/java/wasm/format/sections/WasmLinearMemorySection.java +++ b/src/main/java/wasm/format/sections/WasmLinearMemorySection.java @@ -21,19 +21,19 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.sections.structures.WasmResizableLimits; public class WasmLinearMemorySection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List limits = new ArrayList(); public WasmLinearMemorySection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { limits.add(new WasmResizableLimits(reader)); } @@ -45,7 +45,7 @@ public List getMemories() { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < limits.size(); i++) { builder.add(limits.get(i), "memory_type_" + i); } diff --git a/src/main/java/wasm/format/sections/WasmSection.java b/src/main/java/wasm/format/sections/WasmSection.java index c8701a0..724807e 100644 --- a/src/main/java/wasm/format/sections/WasmSection.java +++ b/src/main/java/wasm/format/sections/WasmSection.java @@ -18,8 +18,8 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; @@ -27,7 +27,7 @@ public abstract class WasmSection implements StructConverter { private WasmSectionId id; - private LEB128 contentLength; + private LEB128Info contentLength; private long sectionOffset; public enum WasmSectionId { @@ -48,7 +48,7 @@ public enum WasmSectionId { public static WasmSection createSection(BinaryReader reader) throws IOException { long sectionOffset = reader.getPointerIndex(); int id = reader.readNextUnsignedByte(); - LEB128 contentLength = LEB128.readUnsignedValue(reader); + LEB128Info contentLength = reader.readNext(LEB128Info::unsigned); reader.setPointerIndex(reader.getPointerIndex() + contentLength.asLong()); if(id >= WasmSectionId.values().length) @@ -89,14 +89,14 @@ public static WasmSection createSection(BinaryReader reader) throws IOException protected WasmSection(BinaryReader reader) throws IOException { sectionOffset = reader.getPointerIndex(); id = WasmSectionId.values()[reader.readNextUnsignedByte()]; - contentLength = LEB128.readUnsignedValue(reader); + contentLength = reader.readNext(LEB128Info::unsigned); } @Override public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder(getName()); builder.add(BYTE, "id"); - builder.add(contentLength, "size"); + builder.addUnsignedLeb128(contentLength, "size"); addToStructure(builder); return builder.toStructure(); } diff --git a/src/main/java/wasm/format/sections/WasmStartSection.java b/src/main/java/wasm/format/sections/WasmStartSection.java index 76c620e..303c834 100644 --- a/src/main/java/wasm/format/sections/WasmStartSection.java +++ b/src/main/java/wasm/format/sections/WasmStartSection.java @@ -18,17 +18,17 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; public class WasmStartSection extends WasmSection { - private LEB128 funcIdx; + private LEB128Info funcIdx; public WasmStartSection(BinaryReader reader) throws IOException { super(reader); - funcIdx = LEB128.readUnsignedValue(reader); + funcIdx = reader.readNext(LEB128Info::unsigned); } public long getStartFunctionIndex() { @@ -37,7 +37,7 @@ public long getStartFunctionIndex() { @Override public void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(funcIdx, "func"); + builder.addUnsignedLeb128(funcIdx, "func"); } @Override diff --git a/src/main/java/wasm/format/sections/WasmTableSection.java b/src/main/java/wasm/format/sections/WasmTableSection.java index 552a9ac..bd01cdd 100644 --- a/src/main/java/wasm/format/sections/WasmTableSection.java +++ b/src/main/java/wasm/format/sections/WasmTableSection.java @@ -21,19 +21,19 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.sections.structures.WasmTableType; public class WasmTableSection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List tables = new ArrayList(); public WasmTableSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { tables.add(new WasmTableType(reader)); } @@ -45,7 +45,7 @@ public List getTables() { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < tables.size(); i++) { builder.add(tables.get(i), "table_" + i); } diff --git a/src/main/java/wasm/format/sections/WasmTypeSection.java b/src/main/java/wasm/format/sections/WasmTypeSection.java index 2ca5352..13db1d9 100644 --- a/src/main/java/wasm/format/sections/WasmTypeSection.java +++ b/src/main/java/wasm/format/sections/WasmTypeSection.java @@ -20,19 +20,19 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; -import ghidra.app.util.bin.format.dwarf4.LEB128; +import ghidra.app.util.bin.LEB128Info; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; import wasm.format.sections.structures.WasmFuncType; public class WasmTypeSection extends WasmSection { - private LEB128 count; + private LEB128Info count; private List types = new ArrayList(); public WasmTypeSection(BinaryReader reader) throws IOException { super(reader); - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); ++i) { types.add(new WasmFuncType(reader)); } @@ -48,7 +48,7 @@ public int getNumTypes() { @Override protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException { - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < types.size(); i++) { builder.add(types.get(i), "type_" + i); } diff --git a/src/main/java/wasm/format/sections/structures/ConstantExpression.java b/src/main/java/wasm/format/sections/structures/ConstantExpression.java index 31b7fad..126f828 100644 --- a/src/main/java/wasm/format/sections/structures/ConstantExpression.java +++ b/src/main/java/wasm/format/sections/structures/ConstantExpression.java @@ -18,8 +18,8 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.address.Address; import ghidra.program.model.address.AddressFactory; import ghidra.program.model.data.DataType; @@ -50,7 +50,7 @@ followed by an explicit end byte (0x0b). public final class ConstantExpression implements StructConverter { private ConstantInstruction type; - private LEB128 opcode2; + private LEB128Info opcode2; private Object value; private enum ConstantInstruction { @@ -71,15 +71,15 @@ public ConstantExpression(BinaryReader reader) throws IOException, IllegalArgume switch (typeCode) { case 0x23: type = ConstantInstruction.GLOBAL_GET; - value = LEB128.readUnsignedValue(reader); + value = reader.readNext(LEB128Info::unsigned); break; case 0x41: type = ConstantInstruction.I32_CONST; - value = LEB128.readUnsignedValue(reader); + value = reader.readNext(LEB128Info::unsigned); break; case 0x42: type = ConstantInstruction.I64_CONST; - value = LEB128.readUnsignedValue(reader); + value = reader.readNext(LEB128Info::unsigned); break; case 0x43: type = ConstantInstruction.F32_CONST; @@ -103,10 +103,10 @@ public ConstantExpression(BinaryReader reader) throws IOException, IllegalArgume } case 0xD2: type = ConstantInstruction.REF_FUNC; - value = LEB128.readUnsignedValue(reader); + value = reader.readNext(LEB128Info::unsigned); break; case 0xFD: - opcode2 = LEB128.readUnsignedValue(reader); + opcode2 = reader.readNext(LEB128Info::unsigned); if (opcode2.asUInt32() == 0x0C) { type = ConstantInstruction.V128_CONST; value = reader.readNextByteArray(16); @@ -151,11 +151,11 @@ public static byte[] longToBytes(long value) { public byte[] asBytes(WasmModule module) { switch (type) { case I32_CONST: - return intToBytes((int) ((LEB128) value).asLong()); + return intToBytes((int) ((LEB128Info) value).asLong()); case I64_CONST: - return longToBytes(((LEB128) value).asLong()); + return longToBytes(((LEB128Info) value).asLong()); case REF_FUNC: - return intToBytes((int) WasmLoader.getFunctionAddressOffset(module, (int) ((LEB128) value).asLong())); + return intToBytes((int) WasmLoader.getFunctionAddressOffset(module, (int) ((LEB128Info) value).asLong())); case F32_CONST: case F64_CONST: case V128_CONST: @@ -172,21 +172,21 @@ public byte[] asBytes(WasmModule module) { public Address asAddress(AddressFactory addressFactory, WasmModule module) { if (type == ConstantInstruction.REF_FUNC) { - return WasmLoader.getFunctionAddress(addressFactory, module, (int) ((LEB128) value).asLong()); + return WasmLoader.getFunctionAddress(addressFactory, module, (int) ((LEB128Info) value).asLong()); } return null; } public Long asI32() { if (type == ConstantInstruction.I32_CONST) { - return ((LEB128) value).asLong(); + return ((LEB128Info) value).asLong(); } return null; } public Long asGlobalGet() { if (type == ConstantInstruction.GLOBAL_GET) { - return ((LEB128) value).asLong(); + return ((LEB128Info) value).asLong(); } return null; } @@ -200,7 +200,7 @@ public DataType toDataType() throws DuplicateNameException, IOException { case I64_CONST: case REF_FUNC: case GLOBAL_GET: - builder.add((LEB128) value, "value"); + builder.addUnsignedLeb128((LEB128Info) value, "value"); break; case F32_CONST: builder.add(Float4DataType.dataType, "value"); @@ -209,7 +209,7 @@ public DataType toDataType() throws DuplicateNameException, IOException { builder.add(Float8DataType.dataType, "value"); break; case V128_CONST: - builder.add((LEB128) opcode2, "opcode2"); + builder.addUnsignedLeb128((LEB128Info) opcode2, "opcode2"); builder.add(ValType.Undefined16, "value"); break; case REF_NULL_FUNCREF: diff --git a/src/main/java/wasm/format/sections/structures/WasmCodeEntry.java b/src/main/java/wasm/format/sections/structures/WasmCodeEntry.java index 315033d..df5ff06 100644 --- a/src/main/java/wasm/format/sections/structures/WasmCodeEntry.java +++ b/src/main/java/wasm/format/sections/structures/WasmCodeEntry.java @@ -21,8 +21,8 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; @@ -30,16 +30,16 @@ public class WasmCodeEntry implements StructConverter { - private LEB128 codeSize; + private LEB128Info codeSize; private List locals = new ArrayList(); - private LEB128 localCount; + private LEB128Info localCount; private long codeOffset; private byte[] instructions; public WasmCodeEntry(BinaryReader reader) throws IOException { - codeSize = LEB128.readUnsignedValue(reader); + codeSize = reader.readNext(LEB128Info::unsigned); codeOffset = reader.getPointerIndex(); - localCount = LEB128.readUnsignedValue(reader); + localCount = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < localCount.asLong(); ++i) { locals.add(new WasmLocalEntry(reader)); } @@ -47,7 +47,7 @@ public WasmCodeEntry(BinaryReader reader) throws IOException { reader.setPointerIndex(codeOffset + codeSize.asLong()); } - public LEB128 getCodeSizeLeb128() { + public LEB128Info getCodeSizeLeb128() { return codeSize; } @@ -80,7 +80,7 @@ public ValType[] getLocals() { @Override public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder("code_" + codeOffset); - builder.add(codeSize, "code_size"); + builder.addUnsignedLeb128(codeSize, "code_size"); builder.addArray(BYTE, instructions.length, "instructions"); return builder.toStructure(); } diff --git a/src/main/java/wasm/format/sections/structures/WasmDataSegment.java b/src/main/java/wasm/format/sections/structures/WasmDataSegment.java index 4e95a64..2ecec35 100644 --- a/src/main/java/wasm/format/sections/structures/WasmDataSegment.java +++ b/src/main/java/wasm/format/sections/structures/WasmDataSegment.java @@ -18,24 +18,24 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; public class WasmDataSegment implements StructConverter { - private LEB128 index; + private LEB128Info index; private ConstantExpression offsetExpr; private long fileOffset; - private LEB128 size; + private LEB128Info size; private byte[] data; public WasmDataSegment(BinaryReader reader) throws IOException { int mode = reader.readNextUnsignedByte(); if (mode == 2) { - index = LEB128.readUnsignedValue(reader); + index = reader.readNext(LEB128Info::unsigned); } else { /* for mode < 2, index defaults to 0 */ index = null; @@ -49,7 +49,7 @@ public WasmDataSegment(BinaryReader reader) throws IOException { offsetExpr = null; } - size = LEB128.readUnsignedValue(reader); + size = reader.readNext(LEB128Info::unsigned); fileOffset = reader.getPointerIndex(); data = reader.readNextByteArray((int) size.asLong()); } @@ -89,12 +89,12 @@ public DataType toDataType() throws DuplicateNameException, IOException { builder.add(BYTE, "mode"); if (index != null) { - builder.add(index, "index"); + builder.addUnsignedLeb128(index, "index"); } if (offsetExpr != null) { builder.add(offsetExpr, "offset"); } - builder.add(size, "size"); + builder.addUnsignedLeb128(size, "size"); if (data.length != 0) { builder.addArray(BYTE, data.length, "data"); } diff --git a/src/main/java/wasm/format/sections/structures/WasmElementSegment.java b/src/main/java/wasm/format/sections/structures/WasmElementSegment.java index 82870bd..ce49365 100644 --- a/src/main/java/wasm/format/sections/structures/WasmElementSegment.java +++ b/src/main/java/wasm/format/sections/structures/WasmElementSegment.java @@ -21,8 +21,8 @@ import java.util.List; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.address.Address; import ghidra.program.model.address.AddressFactory; import ghidra.program.model.data.DataType; @@ -37,12 +37,12 @@ public class WasmElementSegment implements StructConverter { private int flags; private ElementSegmentMode mode; - private LEB128 tableidx; /* if (flags & 3) == 2 */ + private LEB128Info tableidx; /* if (flags & 3) == 2 */ private ConstantExpression offset; /* if (flags & 1) == 0 */ - private LEB128 count; + private LEB128Info count; int elemkind; /* if (flags & 4) == 0 */ - private List funcidxs; /* if (flags & 4) == 0 */ + private List funcidxs; /* if (flags & 4) == 0 */ ValType elemtype; /* if (flags & 4) != 0 */ private List exprs; /* if (flags & 4) != 0 */ @@ -57,7 +57,7 @@ public WasmElementSegment(BinaryReader reader) throws IOException { flags = reader.readNextUnsignedByte(); if ((flags & 3) == 2) { /* active segment with explicit table index */ - tableidx = LEB128.readUnsignedValue(reader); + tableidx = reader.readNext(LEB128Info::unsigned); } else { /* tableidx defaults to 0 */ tableidx = null; @@ -89,12 +89,12 @@ public WasmElementSegment(BinaryReader reader) throws IOException { } } - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); if ((flags & 4) == 0) { /* vector of funcidx */ funcidxs = new ArrayList<>(); for (int i = 0; i < count.asLong(); i++) { - funcidxs.add(LEB128.readUnsignedValue(reader)); + funcidxs.add(reader.readNext(LEB128Info::unsigned)); } } else { /* vector of expr */ @@ -187,7 +187,7 @@ public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder("element_segment"); builder.add(BYTE, "flags"); if (tableidx != null) { - builder.add(tableidx, "tableidx"); + builder.addUnsignedLeb128(tableidx, "tableidx"); } if (offset != null) { builder.add(offset, "offset"); @@ -197,10 +197,10 @@ public DataType toDataType() throws DuplicateNameException, IOException { builder.add(BYTE, "element_type"); } - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); if (funcidxs != null) { for (int i = 0; i < funcidxs.size(); i++) { - builder.add(funcidxs.get(i), "element" + i); + builder.addUnsignedLeb128(funcidxs.get(i), "element" + i); } } if (exprs != null) { diff --git a/src/main/java/wasm/format/sections/structures/WasmExportEntry.java b/src/main/java/wasm/format/sections/structures/WasmExportEntry.java index 0f48f3d..8db0c77 100644 --- a/src/main/java/wasm/format/sections/structures/WasmExportEntry.java +++ b/src/main/java/wasm/format/sections/structures/WasmExportEntry.java @@ -18,8 +18,8 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; @@ -29,12 +29,12 @@ public class WasmExportEntry implements StructConverter { private WasmName name; private WasmExternalKind kind; - private LEB128 index; + private LEB128Info index; public WasmExportEntry(BinaryReader reader) throws IOException { name = new WasmName(reader); kind = WasmExternalKind.values()[reader.readNextByte()]; - index = LEB128.readUnsignedValue(reader); + index = reader.readNext(LEB128Info::unsigned); } public String getName() { @@ -54,7 +54,7 @@ public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder("export_" + getIndex()); builder.add(name, "name"); builder.add(BYTE, "kind"); - builder.add(index, "index"); + builder.addUnsignedLeb128(index, "index"); return builder.toStructure(); } } diff --git a/src/main/java/wasm/format/sections/structures/WasmFuncType.java b/src/main/java/wasm/format/sections/structures/WasmFuncType.java index 8fe1915..b0a4d0f 100644 --- a/src/main/java/wasm/format/sections/structures/WasmFuncType.java +++ b/src/main/java/wasm/format/sections/structures/WasmFuncType.java @@ -18,8 +18,8 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; @@ -29,16 +29,16 @@ public class WasmFuncType implements StructConverter { @SuppressWarnings("unused") private int form; /* always 0 in this version */ - private LEB128 paramCount; + private LEB128Info paramCount; private ValType[] paramTypes; - private LEB128 returnCount; + private LEB128Info returnCount; private ValType[] returnTypes; public WasmFuncType(BinaryReader reader) throws IOException { form = reader.readNextUnsignedByte(); - paramCount = LEB128.readUnsignedValue(reader); + paramCount = reader.readNext(LEB128Info::unsigned); paramTypes = ValType.fromBytes(reader.readNextByteArray((int) paramCount.asLong())); - returnCount = LEB128.readUnsignedValue(reader); + returnCount = reader.readNext(LEB128Info::unsigned); returnTypes = ValType.fromBytes(reader.readNextByteArray((int) returnCount.asLong())); } @@ -72,9 +72,9 @@ public String toString() { public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder("func_type_" + paramCount.asLong() + "_" + returnCount.asLong()); builder.add(BYTE, "form"); - builder.add(paramCount, "param_count"); + builder.addUnsignedLeb128(paramCount, "param_count"); builder.addArray(BYTE, (int) paramCount.asLong(), "param_types"); - builder.add(returnCount, "return_count"); + builder.addUnsignedLeb128(returnCount, "return_count"); builder.addArray(BYTE, (int) returnCount.asLong(), "return_types"); return builder.toStructure(); } diff --git a/src/main/java/wasm/format/sections/structures/WasmImportEntry.java b/src/main/java/wasm/format/sections/structures/WasmImportEntry.java index 90b0b21..26d7dba 100644 --- a/src/main/java/wasm/format/sections/structures/WasmImportEntry.java +++ b/src/main/java/wasm/format/sections/structures/WasmImportEntry.java @@ -18,8 +18,8 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; @@ -31,7 +31,7 @@ public class WasmImportEntry implements StructConverter { private WasmName field; private WasmExternalKind kind; - private LEB128 functionEntry; + private LEB128Info functionEntry; private WasmTableType tableEntry; private WasmResizableLimits memoryEntry; private WasmGlobalType globalEntry; @@ -45,7 +45,7 @@ public WasmImportEntry(BinaryReader reader) throws IOException { kind = WasmExternalKind.values()[reader.readNextByte()]; switch (kind) { case EXT_FUNCTION: - functionEntry = LEB128.readUnsignedValue(reader); + functionEntry = reader.readNext(LEB128Info::unsigned); break; case EXT_TABLE: tableEntry = new WasmTableType(reader); @@ -118,7 +118,7 @@ public DataType toDataType() throws DuplicateNameException, IOException { builder.add(BYTE, "kind"); switch (kind) { case EXT_FUNCTION: - builder.add(functionEntry, "type"); + builder.addUnsignedLeb128(functionEntry, "type"); break; case EXT_TABLE: builder.add(tableEntry, "type"); diff --git a/src/main/java/wasm/format/sections/structures/WasmLocalEntry.java b/src/main/java/wasm/format/sections/structures/WasmLocalEntry.java index 3ca7340..fbe91a9 100644 --- a/src/main/java/wasm/format/sections/structures/WasmLocalEntry.java +++ b/src/main/java/wasm/format/sections/structures/WasmLocalEntry.java @@ -18,19 +18,19 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; public class WasmLocalEntry implements StructConverter { - private LEB128 count; + private LEB128Info count; private int type; public WasmLocalEntry(BinaryReader reader) throws IOException { - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); type = reader.readNextUnsignedByte(); } @@ -45,7 +45,7 @@ public int getType() { @Override public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder("locals_" + count.asLong() + "_" + type); - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); builder.add(BYTE, "type"); return builder.toStructure(); } diff --git a/src/main/java/wasm/format/sections/structures/WasmName.java b/src/main/java/wasm/format/sections/structures/WasmName.java index dcd0163..d50b008 100644 --- a/src/main/java/wasm/format/sections/structures/WasmName.java +++ b/src/main/java/wasm/format/sections/structures/WasmName.java @@ -19,18 +19,18 @@ import java.nio.charset.StandardCharsets; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; public class WasmName implements StructConverter { - private LEB128 size; + private LEB128Info size; private String value; public WasmName(BinaryReader reader) throws IOException { - size = LEB128.readUnsignedValue(reader); + size = reader.readNext(LEB128Info::unsigned); byte[] data = reader.readNextByteArray((int) size.asLong()); value = new String(data, StandardCharsets.UTF_8); } @@ -46,7 +46,7 @@ public String getValue() { @Override public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder("name_" + size.asLong()); - builder.add(size, "size"); + builder.addUnsignedLeb128(size, "size"); builder.addString((int) size.asLong(), "value"); return builder.toStructure(); } diff --git a/src/main/java/wasm/format/sections/structures/WasmNameIndirectMap.java b/src/main/java/wasm/format/sections/structures/WasmNameIndirectMap.java index 8a5bb9c..45f96e0 100644 --- a/src/main/java/wasm/format/sections/structures/WasmNameIndirectMap.java +++ b/src/main/java/wasm/format/sections/structures/WasmNameIndirectMap.java @@ -22,27 +22,27 @@ import java.util.Map; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; public class WasmNameIndirectMap implements StructConverter { - private LEB128 count; + private LEB128Info count; private List entries = new ArrayList<>(); private Map map = new HashMap<>(); private static class WasmIndirectAssoc { - LEB128 idx; + LEB128Info idx; WasmNameMap nameMap; } public WasmNameIndirectMap(BinaryReader reader) throws IOException { - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); i++) { WasmIndirectAssoc assoc = new WasmIndirectAssoc(); - assoc.idx = LEB128.readUnsignedValue(reader); + assoc.idx = reader.readNext(LEB128Info::unsigned); assoc.nameMap = new WasmNameMap("namemap_func_" + i + "_locals", reader); entries.add(assoc); map.put(assoc.idx.asLong(), assoc.nameMap); @@ -60,10 +60,10 @@ public String getEntry(long idx1, long idx2) { @Override public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder("indirectnamemap"); - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < entries.size(); i++) { WasmIndirectAssoc assoc = entries.get(i); - builder.add(assoc.idx, "idx" + i); + builder.addUnsignedLeb128(assoc.idx, "idx" + i); builder.add(assoc.nameMap, "namemap" + i); } return builder.toStructure(); diff --git a/src/main/java/wasm/format/sections/structures/WasmNameMap.java b/src/main/java/wasm/format/sections/structures/WasmNameMap.java index 422a57a..67227a5 100644 --- a/src/main/java/wasm/format/sections/structures/WasmNameMap.java +++ b/src/main/java/wasm/format/sections/structures/WasmNameMap.java @@ -22,8 +22,8 @@ import java.util.Map; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; @@ -31,21 +31,21 @@ public class WasmNameMap implements StructConverter { // this is used to avoid structure name conflict private String structureName; - private LEB128 count; + private LEB128Info count; private List entries = new ArrayList<>(); private Map map = new HashMap<>(); private static class WasmAssoc { - LEB128 idx; + LEB128Info idx; WasmName name; } public WasmNameMap(String structureName, BinaryReader reader) throws IOException { this.structureName = structureName; - count = LEB128.readUnsignedValue(reader); + count = reader.readNext(LEB128Info::unsigned); for (int i = 0; i < count.asLong(); i++) { WasmAssoc assoc = new WasmAssoc(); - assoc.idx = LEB128.readUnsignedValue(reader); + assoc.idx = reader.readNext(LEB128Info::unsigned); assoc.name = new WasmName(reader); entries.add(assoc); map.put(assoc.idx.asLong(), assoc.name); @@ -62,10 +62,10 @@ public String getEntry(long idx) { @Override public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder(structureName); - builder.add(count, "count"); + builder.addUnsignedLeb128(count, "count"); for (int i = 0; i < entries.size(); i++) { WasmAssoc assoc = entries.get(i); - builder.add(assoc.idx, "idx" + i); + builder.addUnsignedLeb128(assoc.idx, "idx" + i); builder.add(assoc.name, "name" + i); } return builder.toStructure(); diff --git a/src/main/java/wasm/format/sections/structures/WasmNameSubsection.java b/src/main/java/wasm/format/sections/structures/WasmNameSubsection.java index fee8675..7e84cda 100644 --- a/src/main/java/wasm/format/sections/structures/WasmNameSubsection.java +++ b/src/main/java/wasm/format/sections/structures/WasmNameSubsection.java @@ -18,8 +18,8 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; @@ -27,7 +27,7 @@ public abstract class WasmNameSubsection implements StructConverter { protected int id; - private LEB128 contentLength; + private LEB128Info contentLength; private long sectionOffset; // see https://github.com/WebAssembly/extended-name-section/blob/main/proposals/extended-name-section/Overview.md @@ -47,7 +47,7 @@ public enum WasmNameSubsectionId { public static WasmNameSubsection createSubsection(BinaryReader reader) throws IOException { long sectionOffset = reader.getPointerIndex(); int id = reader.readNextUnsignedByte(); - LEB128 contentLength = LEB128.readUnsignedValue(reader); + LEB128Info contentLength = reader.readNext(LEB128Info::unsigned); reader.setPointerIndex(reader.getPointerIndex() + contentLength.asLong()); BinaryReader sectionReader = reader.clone(sectionOffset); @@ -86,14 +86,14 @@ public static WasmNameSubsection createSubsection(BinaryReader reader) throws IO protected WasmNameSubsection(BinaryReader reader) throws IOException { sectionOffset = reader.getPointerIndex(); id = reader.readNextUnsignedByte(); - contentLength = LEB128.readUnsignedValue(reader); + contentLength = reader.readNext(LEB128Info::unsigned); } @Override public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder(getName()); builder.add(BYTE, "id"); - builder.add(contentLength, "size"); + builder.addUnsignedLeb128(contentLength, "size"); addToStructure(builder); return builder.toStructure(); } diff --git a/src/main/java/wasm/format/sections/structures/WasmResizableLimits.java b/src/main/java/wasm/format/sections/structures/WasmResizableLimits.java index e694f24..281b541 100644 --- a/src/main/java/wasm/format/sections/structures/WasmResizableLimits.java +++ b/src/main/java/wasm/format/sections/structures/WasmResizableLimits.java @@ -18,8 +18,8 @@ import java.io.IOException; import ghidra.app.util.bin.BinaryReader; +import ghidra.app.util.bin.LEB128Info; import ghidra.app.util.bin.StructConverter; -import ghidra.app.util.bin.format.dwarf4.LEB128; import ghidra.program.model.data.DataType; import ghidra.util.exception.DuplicateNameException; import wasm.format.StructureBuilder; @@ -27,14 +27,14 @@ public class WasmResizableLimits implements StructConverter { private int flags; - private LEB128 initial; - private LEB128 maximum; + private LEB128Info initial; + private LEB128Info maximum; public WasmResizableLimits(BinaryReader reader) throws IOException { flags = reader.readNextUnsignedByte(); - initial = LEB128.readUnsignedValue(reader); + initial = reader.readNext(LEB128Info::unsigned); if (flags == 1) { - maximum = LEB128.readUnsignedValue(reader); + maximum = reader.readNext(LEB128Info::unsigned); } } @@ -53,9 +53,9 @@ public long getMaximum() { public DataType toDataType() throws DuplicateNameException, IOException { StructureBuilder builder = new StructureBuilder("limits"); builder.add(BYTE, "flags"); - builder.add(initial, "initial"); + builder.addUnsignedLeb128(initial, "initial"); if (maximum != null) { - builder.add(maximum, "maximum"); + builder.addUnsignedLeb128(maximum, "maximum"); } return builder.toStructure(); }