GP-5742 Cleanup preferred CommentType enum use. Changed SARIF data component comment JSON serialization from int to String.

This commit is contained in:
ghidra1
2025-06-06 17:58:07 -04:00
parent 4a65e9af3b
commit 8c441250f5
211 changed files with 4627 additions and 4860 deletions
@@ -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.
@@ -154,8 +154,6 @@ public class MergeTwoProgramsScript extends GhidraScript {
private void mergeComments(Program currProgram, Program otherProgram) throws Exception {
monitor.setMessage("Merging comments...");
int[] commentTypes = { CodeUnit.EOL_COMMENT, CodeUnit.PRE_COMMENT, CodeUnit.POST_COMMENT,
CodeUnit.PLATE_COMMENT, CodeUnit.REPEATABLE_COMMENT, };
Listing currentListing = currProgram.getListing();
Listing otherListing = otherProgram.getListing();
CodeUnitIterator otherCodeUnits = otherListing.getCodeUnits(true);
@@ -164,7 +162,7 @@ public class MergeTwoProgramsScript extends GhidraScript {
break;
}
CodeUnit otherCodeUnit = otherCodeUnits.next();
for (int commentType : commentTypes) {
for (CommentType commentType : CommentType.values()) {
if (monitor.isCancelled()) {
break;
}
@@ -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.
@@ -29,8 +29,7 @@ import ghidra.file.formats.android.dex.util.DexUtil;
import ghidra.framework.model.DomainObject;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.PointerDataType;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.listing.Program;
import ghidra.program.model.listing.*;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.symbol.*;
import ghidra.util.exception.InvalidInputException;
@@ -139,11 +138,9 @@ public class DexLoader extends AbstractProgramWrapperLoader {
protected void createMethods(Program program, DexHeader header, ClassDefItem item,
List<EncodedMethod> methods, TaskMonitor monitor, MessageLog log) throws Exception {
for (int i = 0; i < methods.size(); ++i) {
for (EncodedMethod encodedMethod : methods) {
monitor.checkCancelled();
EncodedMethod encodedMethod = methods.get(i);
CodeItem codeItem = encodedMethod.getCodeItem();
Address methodIndexAddress =
@@ -165,8 +162,7 @@ public class DexLoader extends AbstractProgramWrapperLoader {
}
protected void markupMethodLookup(Program program, DexHeader header, TaskMonitor monitor,
MessageLog log)
throws Exception {
MessageLog log) throws Exception {
monitor.setMessage("DEX: processing methods");
monitor.setMaximum(header.getMethodIdsSize());
@@ -189,7 +185,7 @@ public class DexLoader extends AbstractProgramWrapperLoader {
if (program.getMemory().getInt(methodIndexAddress) == -1) {
program.getListing()
.setComment(methodIndexAddress, CodeUnit.PLATE_COMMENT, builder.toString());
.setComment(methodIndexAddress, CommentType.PLATE, builder.toString());
// Add placeholder symbol for external functions
String methodName = DexUtil.convertToString(header, item.getNameIndex());
@@ -198,9 +194,8 @@ public class DexLoader extends AbstractProgramWrapperLoader {
DexUtil.createNameSpaceFromMangledClassName(program, className);
if (classNameSpace != null) {
Address externalAddress = DexUtil.toLookupAddress(program, methodIndex);
Symbol methodSymbol =
createMethodSymbol(program, externalAddress, methodName, classNameSpace,
log);
Symbol methodSymbol = createMethodSymbol(program, externalAddress, methodName,
classNameSpace, log);
if (methodSymbol != null) {
String externalName = methodSymbol.getName(true);
program.getReferenceManager()
@@ -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.
@@ -77,8 +77,8 @@ public abstract class FileFormatAnalyzer implements Analyzer {
}
@Override
public boolean removed(Program program, AddressSetView set, TaskMonitor monitor,
MessageLog log) throws CancelledException {
public boolean removed(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
throws CancelledException {
return false;
}
@@ -154,7 +154,7 @@ public abstract class FileFormatAnalyzer implements Analyzer {
}
protected ProgramFragment createFragment(Program program, String fragmentName, Address start,
Address end) throws DuplicateNameException, NotFoundException {
Address end) throws DuplicateNameException, NotFoundException {
ProgramModule module = program.getListing().getDefaultRootModule();
ProgramFragment fragment = getFragment(module, fragmentName);
if (fragment == null) {
@@ -190,7 +190,8 @@ public abstract class FileFormatAnalyzer implements Analyzer {
return program.getAddressFactory().getDefaultAddressSpace().getAddress(offset);
}
protected Data createData(Program program, Address address, DataType datatype) throws CodeUnitInsertionException {
protected Data createData(Program program, Address address, DataType datatype)
throws CodeUnitInsertionException {
if (datatype instanceof StringDataType) {
CreateStringCmd cmd = new CreateStringCmd(address);
if (!cmd.applyTo(program)) {
@@ -207,7 +208,7 @@ public abstract class FileFormatAnalyzer implements Analyzer {
}
protected boolean setPlateComment(Program program, Address address, String comment) {
SetCommentCmd cmd = new SetCommentCmd(address, CodeUnit.PLATE_COMMENT, comment);
SetCommentCmd cmd = new SetCommentCmd(address, CommentType.PLATE, comment);
return cmd.applyTo(program);
}
@@ -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.
@@ -181,7 +181,7 @@ public abstract class ArtImageSections {
program.getMinAddress().getNewAddress(header.getImageBegin() + section.getOffset());
program.getSymbolTable().createLabel(address, name, SourceType.ANALYSIS);
program.getListing()
.setComment(address, CodeUnit.PLATE_COMMENT, "Size: " + section.getSize());
.setComment(address, CommentType.PLATE, "Size: " + section.getSize());
createFragment(program, address, section, name, monitor);
}
@@ -202,15 +202,14 @@ public abstract class ArtImageSections {
Address address =
program.getMinAddress().getNewAddress(header.getImageBegin() + section.getOffset());
for (int i = 0; i < fieldList.size(); ++i) {
for (ArtField field : fieldList) {
monitor.checkCancelled();
ArtField field = fieldList.get(i);
DataType dataType = field.toDataType();
program.getListing().createData(address, dataType);
String comment =
"Declaring Class: 0x" + Integer.toHexString(field.getDeclaringClass());
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, comment);
program.getListing().setComment(address, CommentType.PLATE, comment);
address = address.add(dataType.getLength());
@@ -221,16 +220,15 @@ public abstract class ArtImageSections {
if (section.getSize() > 0) {
Address address =
program.getMinAddress().getNewAddress(header.getImageBegin() + section.getOffset());
for (int i = 0; i < fieldGroupList.size(); ++i) {
for (ArtFieldGroup fieldGroup : fieldGroupList) {
monitor.checkCancelled();
ArtFieldGroup fieldGroup = fieldGroupList.get(i);
DataType dataType = fieldGroup.toDataType();
program.getListing().createData(address, dataType);
if (fieldGroup.getFieldCount() > 0) {
ArtField artField = fieldGroup.getFieldList().get(0);
String comment =
"Declaring Class: 0x" + Integer.toHexString(artField.getDeclaringClass());
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, comment);
program.getListing().setComment(address, CommentType.PLATE, comment);
}
address = address.add(dataType.getLength());
@@ -253,15 +251,14 @@ public abstract class ArtImageSections {
if (section.getSize() > 0) {
Address address =
program.getMinAddress().getNewAddress(header.getImageBegin() + section.getOffset());
for (int i = 0; i < methodList.size(); ++i) {
for (ArtMethod method : methodList) {
monitor.checkCancelled();
ArtMethod method = methodList.get(i);
DataType dataType = method.toDataType();
program.getListing().createData(address, dataType);
String comment =
"Declaring Class: 0x" + Integer.toHexString(method.getDeclaringClass());
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, comment);
program.getListing().setComment(address, CommentType.PLATE, comment);
address = address.add(dataType.getLength());
@@ -272,17 +269,16 @@ public abstract class ArtImageSections {
if (section.getSize() > 0) {
Address address =
program.getMinAddress().getNewAddress(header.getImageBegin() + section.getOffset());
for (int i = 0; i < methodGroupList.size(); ++i) {
for (ArtMethodGroup methodGroup : methodGroupList) {
monitor.checkCancelled();
ArtMethodGroup methodGroup = methodGroupList.get(i);
DataType dataType = methodGroup.toDataType();
program.getListing().createData(address, dataType);
if (methodGroup.getMethodCount() > 0) {
ArtMethod artMethod = methodGroup.getMethodList().get(0);
String comment =
"Declaring Class: 0x" + Integer.toHexString(artMethod.getDeclaringClass());
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, comment);
program.getListing().setComment(address, CommentType.PLATE, comment);
}
address = address.add(dataType.getLength());
@@ -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.
@@ -255,8 +255,7 @@ public class DexMarkupInstructionsAnalyzer extends FileFormatAnalyzer {
String className = stringItem.getStringDataItem().getString();
setEquate(program, instruction.getMinAddress(), operand, className, classTypeIndex);
program.getListing()
.setComment(instruction.getMinAddress(), CodeUnit.EOL_COMMENT, className);
program.getListing().setComment(instruction.getMinAddress(), CommentType.EOL, className);
}
private void processString(Program program, Instruction instruction, int operand,
@@ -300,7 +299,6 @@ public class DexMarkupInstructionsAnalyzer extends FileFormatAnalyzer {
String valueName = format(className, fieldName);
setEquate(program, instruction.getMinAddress(), operand, fieldName, fieldIndex);
program.getListing()
.setComment(instruction.getMinAddress(), CodeUnit.EOL_COMMENT, valueName);
program.getListing().setComment(instruction.getMinAddress(), CommentType.EOL, valueName);
}
}
@@ -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.
@@ -20,9 +20,7 @@ import ghidra.app.util.importer.MessageLog;
import ghidra.program.flatapi.FlatProgramAPI;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.listing.Data;
import ghidra.program.model.listing.Program;
import ghidra.program.model.listing.*;
import ghidra.program.model.symbol.RefType;
import ghidra.util.task.TaskMonitor;
@@ -99,7 +97,8 @@ public abstract class FBPK_Partition implements StructConverter {
* @param log the message log
* @throws Exception if any exception occurs during markup
*/
public void markup(Program program, Address address, TaskMonitor monitor, MessageLog log) throws Exception {
public void markup(Program program, Address address, TaskMonitor monitor, MessageLog log)
throws Exception {
FlatProgramAPI api = new FlatProgramAPI(program);
DataType partitionDataType = toDataType();
@@ -112,8 +111,7 @@ public abstract class FBPK_Partition implements StructConverter {
}
program.getListing()
.setComment(address, CodeUnit.PLATE_COMMENT,
getName() + " - " + getPartitionIndex());
.setComment(address, CommentType.PLATE, getName() + " - " + getPartitionIndex());
api.createFragment(getName(), address, partitionDataType.getLength());
@@ -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.
@@ -31,7 +31,8 @@ public abstract class FBPT implements StructConverter {
public abstract List<FBPT_Entry> getEntries();
public void processFBPT(Program program, Address address, TaskMonitor monitor, MessageLog log) throws Exception {
public void processFBPT(Program program, Address address, TaskMonitor monitor, MessageLog log)
throws Exception {
FlatProgramAPI api = new FlatProgramAPI(program);
DataType fbptDataType = toDataType();
@@ -41,14 +42,15 @@ public abstract class FBPT implements StructConverter {
return;
}
String comment = "FBPT" + "\n" + "Num of entries: " + getEntries().size();
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, comment);
program.getListing().setComment(address, CommentType.PLATE, comment);
api.createFragment(FBPK_Constants.FBPT, address, fbptDataType.getLength());
address = address.add(fbptDataType.getLength());
processFbPtEntries(program, address, monitor, log);
}
private void processFbPtEntries(Program program, Address address, TaskMonitor monitor, MessageLog log) throws Exception {
private void processFbPtEntries(Program program, Address address, TaskMonitor monitor,
MessageLog log) throws Exception {
int i = 0;
FlatProgramAPI api = new FlatProgramAPI(program);
for (FBPT_Entry entry : getEntries()) {
@@ -59,7 +61,8 @@ public abstract class FBPT implements StructConverter {
log.appendMsg("Unable to apply FBPT Entry data, stopping - " + address);
return;
}
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, entry.getName() + " - " + i++);
program.getListing()
.setComment(address, CommentType.PLATE, entry.getName() + " - " + i++);
api.createFragment(FBPK_Constants.FBPT, address, entryDataType.getLength());
address = address.add(entryDataType.getLength());
}
@@ -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.
@@ -25,24 +25,12 @@ import ghidra.file.formats.android.dex.format.DexHeader;
import ghidra.file.formats.android.oat.oatdexfile.OatDexFile;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.Array;
import ghidra.program.model.data.ArrayDataType;
import ghidra.program.model.data.DWordDataType;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.Undefined1DataType;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.listing.Data;
import ghidra.program.model.listing.Program;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.mem.Memory;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.scalar.Scalar;
import ghidra.program.model.symbol.Equate;
import ghidra.program.model.symbol.EquateTable;
import ghidra.program.model.symbol.RefType;
import ghidra.program.model.symbol.ReferenceManager;
import ghidra.program.model.symbol.SourceType;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.SymbolTable;
import ghidra.program.model.symbol.*;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.TaskMonitor;
@@ -199,7 +187,7 @@ public class OatHeaderAnalyzer extends FileFormatAnalyzer {
Data data = createData(program, address, dataType);
Scalar scalar = data.getScalar(0);
Address toAddr = destinationBlock.getStart().add(scalar.getUnsignedValue());
program.getListing().setComment(address, CodeUnit.EOL_COMMENT, "->" + toAddr);
program.getListing().setComment(address, CommentType.EOL, "->" + toAddr);
}
catch (Exception e) {
log.appendException(e);
@@ -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.
@@ -59,8 +59,7 @@ public class OatDexFile_S_T extends OatDexFile {
private long _offset;
private DexHeader dexHeader;
public OatDexFile_S_T(BinaryReader reader, OatBundle bundle)
throws IOException {
public OatDexFile_S_T(BinaryReader reader, OatBundle bundle) throws IOException {
_offset = reader.getPointerIndex();
@@ -120,7 +119,7 @@ public class OatDexFile_S_T extends OatDexFile {
Address dataAddress = address.add(_offset);
program.getListing().setComment(dataAddress, CodeUnit.PLATE_COMMENT, getDexFileLocation());
program.getListing().setComment(dataAddress, CommentType.PLATE, getDexFileLocation());
program.getListing().clearCodeUnits(dataAddress, dataAddress, false, monitor);
Data data = program.getListing().createData(dataAddress, toDataType());
@@ -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.
@@ -15,9 +15,7 @@
*/
package ghidra.file.formats.android.vdex;
import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.bin.MemoryByteProvider;
import ghidra.app.util.bin.*;
import ghidra.app.util.importer.MessageLog;
import ghidra.file.analyzers.FileFormatAnalyzer;
import ghidra.file.formats.android.dex.format.DexHeader;
@@ -25,11 +23,8 @@ import ghidra.file.formats.android.oat.OatUtilities;
import ghidra.file.formats.android.vdex.sections.DexSectionHeader_002;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.ArrayDataType;
import ghidra.program.model.data.ByteDataType;
import ghidra.program.model.data.DWordDataType;
import ghidra.program.model.data.DataType;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.CommentType;
import ghidra.program.model.listing.Program;
import ghidra.util.task.TaskMonitor;
@@ -86,16 +81,14 @@ public class VdexHeaderAnalyzer extends FileFormatAnalyzer {
address = address.add(vdexHeaderDataType.getLength());
if (!vdexHeader.isDexHeaderEmbeddedInDataType()) {
for (int i = 0; i < vdexHeader.getDexHeaderList().size(); ++i) {
for (DexHeader dexHeader : vdexHeader.getDexHeaderList()) {
monitor.checkCancelled();
program.getListing()
.setComment(address, CodeUnit.PLATE_COMMENT,
"quicken info table entry");
.setComment(address, CommentType.PLATE, "quicken info table entry");
createData(program, address, new DWordDataType());
address = address.add(4);
DexHeader dexHeader = vdexHeader.getDexHeaderList().get(i);
DataType dexHeaderDataType = dexHeader.toDataType();
createData(program, address, dexHeaderDataType);
@@ -130,7 +123,7 @@ public class VdexHeaderAnalyzer extends FileFormatAnalyzer {
if (sectionHeader != null) {
int dexSharedDataSize = sectionHeader.getDexSharedDataSize();
String comment = "dex_shared_data_size_ : 0x" + Integer.toHexString(dexSharedDataSize);
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, comment);
program.getListing().setComment(address, CommentType.PLATE, comment);
DataType array = new ArrayDataType(BYTE, dexSharedDataSize, BYTE.getLength());
createData(program, address, array);
address = address.add(dexSharedDataSize);
@@ -146,7 +139,7 @@ public class VdexHeaderAnalyzer extends FileFormatAnalyzer {
String comment =
"verifier_deps_size_ : 0x" + Integer.toHexString(vdexHeader.getVerifierDepsSize());
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, comment);
program.getListing().setComment(address, CommentType.PLATE, comment);
int remainderSize = vdexHeader.getVerifierDepsSize();
@@ -183,7 +176,7 @@ public class VdexHeaderAnalyzer extends FileFormatAnalyzer {
}
String comment = "quickening_info_size_ : 0x" + Integer.toHexString(quickeningInfoSize);
program.getListing().setComment(address, CodeUnit.PLATE_COMMENT, comment);
program.getListing().setComment(address, CommentType.PLATE, comment);
address = address.add(quickeningInfoSize);
@@ -25,7 +25,7 @@ import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.DataType;
import ghidra.program.model.lang.Language;
import ghidra.program.model.lang.Processor;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.listing.CommentType;
import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.util.exception.CancelledException;
@@ -76,8 +76,7 @@ public class CramFsAnalyzer extends AbstractAnalyzer {
DataType dataType = cramFsSuper.toDataType();
program.getListing().createData(minAddress, dataType);
program.getListing()
.setComment(minAddress, CodeUnit.PLATE_COMMENT,
cramFsSuper.getRoot().toString());
.setComment(minAddress, CommentType.PLATE, cramFsSuper.getRoot().toString());
int offset = cramFsSuper.getRoot().getOffsetAdjusted();
for (int i = 0; i < cramFsSuper.getFsid().getFiles() - 1; i++) {
@@ -90,7 +89,7 @@ public class CramFsAnalyzer extends AbstractAnalyzer {
if (newInode.isFile()) {
Address inodeDataAddress = minAddress.add(newInode.getOffsetAdjusted());
program.getListing()
.setComment(inodeDataAddress, CodeUnit.PLATE_COMMENT,
.setComment(inodeDataAddress, CommentType.PLATE,
newInode.getName() + " Data/Bytes\n");
}
@@ -98,7 +97,7 @@ public class CramFsAnalyzer extends AbstractAnalyzer {
program.getListing().createData(inodeAddress, inodeDataType);
program.getListing()
.setComment(inodeAddress, CodeUnit.PLATE_COMMENT,
.setComment(inodeAddress, CommentType.PLATE,
newInode.getName() + "\n" + newInode.toString());
offset += inodeDataType.getLength();
@@ -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.
@@ -23,13 +23,13 @@ import ghidra.file.analyzers.FileFormatAnalyzer;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.DataType;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.listing.CommentType;
import ghidra.program.model.listing.Program;
import ghidra.util.exception.DuplicateNameException;
import ghidra.util.task.TaskMonitor;
public class Ext4Analyzer extends FileFormatAnalyzer {
private int blockSize;
@Override
@@ -53,18 +53,19 @@ public class Ext4Analyzer extends FileFormatAnalyzer {
MemoryByteProvider.createDefaultAddressSpaceByteProvider(program, false);
BinaryReader reader = new BinaryReader(provider, true);
int start = getSuperBlockStart(reader);
if( start == -1 ) {
if (start == -1) {
return false;
}
reader.setPointerIndex(start + 0x38);
short magic = -1;
try {
magic = reader.readNextShort();
} catch (IOException e ) {
}
catch (IOException e) {
// ignore
}
if( magic != (short)0xef53 ) {
if (magic != (short) 0xef53) {
return false;
}
return true;
@@ -76,8 +77,8 @@ public class Ext4Analyzer extends FileFormatAnalyzer {
}
@Override
public boolean analyze(Program program, AddressSetView set,
TaskMonitor monitor, MessageLog log) throws Exception {
public boolean analyze(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
throws Exception {
ByteProvider provider =
MemoryByteProvider.createDefaultAddressSpaceByteProvider(program, false);
BinaryReader reader = new BinaryReader(provider, true);
@@ -86,67 +87,74 @@ public class Ext4Analyzer extends FileFormatAnalyzer {
reader.setPointerIndex(start);
Ext4SuperBlock superBlock = new Ext4SuperBlock(reader);
createData(program, toAddr(program, start), superBlock.toDataType());
boolean is64Bit = (superBlock.getS_desc_size() > 32) && ((superBlock.getS_feature_incompat() & 0x80) > 0);
long numBytes = program.getMaxAddress().getOffset() - program.getMinAddress().getOffset() + 1;
int groupSize = calculateGroupSize( superBlock );
int numGroups = (int)numBytes / groupSize;
if( numBytes % groupSize != 0) {
boolean is64Bit =
(superBlock.getS_desc_size() > 32) && ((superBlock.getS_feature_incompat() & 0x80) > 0);
long numBytes =
program.getMaxAddress().getOffset() - program.getMinAddress().getOffset() + 1;
int groupSize = calculateGroupSize(superBlock);
int numGroups = (int) numBytes / groupSize;
if (numBytes % groupSize != 0) {
numGroups++;
}
long groupDescOffset = groupStart + blockSize;
Address groupDescAddress = toAddr(program, groupDescOffset);
reader.setPointerIndex(groupDescOffset);
Ext4GroupDescriptor groupDescriptors[] = new Ext4GroupDescriptor[numGroups];
monitor.setMessage("Creating group descriptors...");
monitor.setMaximum(numGroups);
for( int i = 0; i < numGroups; i++ ) {
for (int i = 0; i < numGroups; i++) {
monitor.checkCancelled();
groupDescriptors[i] = new Ext4GroupDescriptor(reader, is64Bit);
DataType groupDescDataType = groupDescriptors[i].toDataType();
createData(program, groupDescAddress, groupDescDataType);
groupDescAddress = groupDescAddress.add(groupDescDataType.getLength());
groupDescAddress = groupDescAddress.add(groupDescDataType.getLength());
monitor.incrementProgress(1);
}
boolean isSparseSuper = (superBlock.getS_feature_ro_compat() & 1) != 0;
createSuperBlockCopies(program, reader, groupSize, numGroups, is64Bit, isSparseSuper, monitor);
createSuperBlockCopies(program, reader, groupSize, numGroups, is64Bit, isSparseSuper,
monitor);
createInodeTables(program, reader, superBlock, groupDescriptors, is64Bit, monitor);
// test(program, reader);
return true;
}
private void createInodeTables(Program program, BinaryReader reader, Ext4SuperBlock superBlock,
Ext4GroupDescriptor[] groupDescriptors, boolean is64Bit, TaskMonitor monitor) throws DuplicateNameException, Exception {
Ext4GroupDescriptor[] groupDescriptors, boolean is64Bit, TaskMonitor monitor)
throws DuplicateNameException, Exception {
int inodeCount = superBlock.getS_inodes_count();
Ext4Inode inodes[] = new Ext4Inode[inodeCount];
for( int i = 0; i < groupDescriptors.length; i++ ) {
for (int i = 0; i < groupDescriptors.length; i++) {
monitor.checkCancelled();
long inodeTableBlockOffset = groupDescriptors[i].getBg_inode_table_lo() & 0xffffffffL;
if( is64Bit ) {
inodeTableBlockOffset = (((long) groupDescriptors[i].getBg_inode_table_hi()) << 32) | inodeTableBlockOffset;
if (is64Bit) {
inodeTableBlockOffset =
(((long) groupDescriptors[i].getBg_inode_table_hi()) << 32) |
inodeTableBlockOffset;
}
long offset = inodeTableBlockOffset * blockSize;
reader.setPointerIndex(offset);
Address address = null;
try {
address = toAddr(program, offset);
} catch (Exception e ) {
}
catch (Exception e) {
throw new IOException("offset " + offset + " not in program.");
}
int inodesPerGroup = superBlock.getS_inodes_per_group();
monitor.setMessage("Creating inode table " + i + " of " + (groupDescriptors.length - 1) + "...");
monitor.setMessage(
"Creating inode table " + i + " of " + (groupDescriptors.length - 1) + "...");
monitor.setMaximum(inodesPerGroup);
monitor.setProgress(0);
for( int j = 0; j < inodesPerGroup; j++ ) {
if( i == 0 && j == 0) {
for (int j = 0; j < inodesPerGroup; j++) {
if (i == 0 && j == 0) {
//inode 0 does not exist
continue;
}
@@ -154,44 +162,48 @@ public class Ext4Analyzer extends FileFormatAnalyzer {
Ext4Inode inode = new Ext4Inode(reader);
DataType dataType = inode.toDataType();
createData(program, address, dataType);
program.getListing().setComment(address, CodeUnit.EOL_COMMENT, "0x" + (Integer.toHexString(inodesPerGroup * i + j )));
program.getListing()
.setComment(address, CommentType.EOL,
"0x" + (Integer.toHexString(inodesPerGroup * i + j)));
address = address.add(superBlock.getS_inode_size());
reader.setPointerIndex(address.getOffset());
monitor.incrementProgress(1);
inodes[inodesPerGroup * i + j] = inode;
}
}
processInodes( program, reader, superBlock, inodes, monitor);
processInodes(program, reader, superBlock, inodes, monitor);
}
private void processInodes(Program program, BinaryReader reader,
Ext4SuperBlock superBlock, Ext4Inode[] inodes, TaskMonitor monitor) throws Exception {
private void processInodes(Program program, BinaryReader reader, Ext4SuperBlock superBlock,
Ext4Inode[] inodes, TaskMonitor monitor) throws Exception {
//first 0xa inodes are reserved (0 doesn't exist)
for( int i = 0x1; i < inodes.length; i++ ) {
for (int i = 0x1; i < inodes.length; i++) {
monitor.checkCancelled();
Ext4Inode inode = inodes[i];
short mode = inode.getI_mode();
if( (mode & Ext4Constants.S_IFDIR) != 0 ) {
if ((mode & Ext4Constants.S_IFDIR) != 0) {
processDirectory(program, reader, superBlock, inode, monitor);
} else if( (mode & Ext4Constants.S_IFREG) != 0 ) {
}
else if ((mode & Ext4Constants.S_IFREG) != 0) {
processFile(program, reader, superBlock, inode, monitor);
}
}
}
}
private void processFile(Program program, BinaryReader reader,
Ext4SuperBlock superBlock, Ext4Inode inode, TaskMonitor monitor) {
private void processFile(Program program, BinaryReader reader, Ext4SuperBlock superBlock,
Ext4Inode inode, TaskMonitor monitor) {
// TODO?
}
private void processDirectory(Program program, BinaryReader reader,
Ext4SuperBlock superBlock, Ext4Inode inode, TaskMonitor monitor) throws Exception {
if( (inode.getI_flags() & Ext4Constants.EXT4_INDEX_FL) != 0 ) {
private void processDirectory(Program program, BinaryReader reader, Ext4SuperBlock superBlock,
Ext4Inode inode, TaskMonitor monitor) throws Exception {
if ((inode.getI_flags() & Ext4Constants.EXT4_INDEX_FL) != 0) {
processHashTreeDirectory(program, reader, superBlock, inode, monitor);
}
boolean isDirEntry2 = (superBlock.getS_feature_incompat() & Ext4Constants.INCOMPAT_FILETYPE) != 0;
boolean isDirEntry2 =
(superBlock.getS_feature_incompat() & Ext4Constants.INCOMPAT_FILETYPE) != 0;
// if uses extents
if( (inode.getI_flags() & Ext4Constants.EXT4_EXTENTS_FL) != 0 ) {
if ((inode.getI_flags() & Ext4Constants.EXT4_EXTENTS_FL) != 0) {
// Ext4IBlock i_block = inode.getI_block();
// Ext4ExtentHeader header = i_block.getHeader();
// if( header.getEh_depth() == 0 ) {
@@ -214,7 +226,7 @@ public class Ext4Analyzer extends FileFormatAnalyzer {
//
// }
}
}
private void processHashTreeDirectory(Program program, BinaryReader reader,
@@ -222,13 +234,14 @@ public class Ext4Analyzer extends FileFormatAnalyzer {
// TODO?
}
private void createSuperBlockCopies(Program program, BinaryReader reader,
int groupSize, int numGroups, boolean is64Bit, boolean isSparseSuper, TaskMonitor monitor) throws DuplicateNameException, IOException, Exception {
private void createSuperBlockCopies(Program program, BinaryReader reader, int groupSize,
int numGroups, boolean is64Bit, boolean isSparseSuper, TaskMonitor monitor)
throws DuplicateNameException, IOException, Exception {
monitor.setMessage("Creating super block and group descriptor copies...");
monitor.setMaximum(numGroups);
for( int i = 1; i < numGroups; i++ ) {
for (int i = 1; i < numGroups; i++) {
monitor.checkCancelled();
if( isSparseSuper && (!isXpowerOfY(i, 3) && !isXpowerOfY(i, 5) && !isXpowerOfY(i, 7)) ) {
if (isSparseSuper && (!isXpowerOfY(i, 3) && !isXpowerOfY(i, 5) && !isXpowerOfY(i, 7))) {
continue;
}
int offset = groupSize * i;
@@ -236,27 +249,26 @@ public class Ext4Analyzer extends FileFormatAnalyzer {
reader.setPointerIndex(offset);
Ext4SuperBlock superBlock = new Ext4SuperBlock(reader);
createData(program, address, superBlock.toDataType());
long groupDescOffset = offset + blockSize;
Address groupDescAddress = toAddr(program, groupDescOffset);
reader.setPointerIndex(groupDescOffset);
for( int j = 0; j < numGroups; j++ ) {
for (int j = 0; j < numGroups; j++) {
Ext4GroupDescriptor groupDesc = new Ext4GroupDescriptor(reader, is64Bit);
DataType groupDescDataType = groupDesc.toDataType();
createData(program, groupDescAddress, groupDescDataType);
groupDescAddress = groupDescAddress.add(groupDescDataType.getLength());
groupDescAddress = groupDescAddress.add(groupDescDataType.getLength());
}
monitor.incrementProgress(1);
}
}
private boolean isXpowerOfY( int x, int y ) {
if( x == 0 ) {
private boolean isXpowerOfY(int x, int y) {
if (x == 0) {
return false;
}
while( x % y == 0 ) {
while (x % y == 0) {
x = x / y;
}
return x == 1;
@@ -274,23 +286,25 @@ public class Ext4Analyzer extends FileFormatAnalyzer {
int padding = -1;
int padStart = 0;
boolean isPadding = false;
while( padStart < 1024 ) {
if( !isPadding ) {
padStart = (int)reader.getPointerIndex();
while (padStart < 1024) {
if (!isPadding) {
padStart = (int) reader.getPointerIndex();
}
padding = reader.readNextInt();
if( padding == 0 ) {
if( isPadding ) {
if (padding == 0) {
if (isPadding) {
return padStart + 0x400;
}
isPadding = true;
} else {
}
else {
isPadding = false;
}
}
} catch (Exception e) {
}
catch (Exception e) {
}
return -1;
}
}
@@ -103,16 +103,17 @@ public class CFStringAnalyzer extends AbstractAnalyzer {
String comment = makeComment(cFString);
program.getListing().setComment(currentAddress, CodeUnit.REPEATABLE_COMMENT,
"\"" + comment + "\",00");
program.getListing()
.setComment(currentAddress, CommentType.REPEATABLE,
"\"" + comment + "\",00");
if (program.getSymbolTable().getGlobalSymbol(symbolString,
currentAddress) != null) {
if (program.getSymbolTable()
.getGlobalSymbol(symbolString, currentAddress) != null) {
continue;
}
Symbol mine = program.getSymbolTable().createLabel(currentAddress, symbolString,
SourceType.ANALYSIS);
Symbol mine = program.getSymbolTable()
.createLabel(currentAddress, symbolString, SourceType.ANALYSIS);
mine.setPrimary();
}
catch (CodeUnitInsertionException e) {
@@ -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.
@@ -64,8 +64,9 @@ public class TestAnalyzer extends AbstractAnalyzer {
//don't need to check this..
}
program.getListing().setComment(currentAddress, CodeUnit.PLATE_COMMENT,
"Address = " + currentAddress.toString());
program.getListing()
.setComment(currentAddress, CommentType.PLATE,
"Address = " + currentAddress.toString());
currentAddress = currentAddress.add(data.getLength());
}