Skip to content

Commit

Permalink
dumper swap logic-error prone selections functions out
Browse files Browse the repository at this point in the history
dd86k committed May 11, 2024
1 parent 7a9a7fe commit 90392ec
Showing 15 changed files with 42 additions and 70 deletions.
31 changes: 7 additions & 24 deletions dumper/dumper.d
Original file line number Diff line number Diff line change
@@ -86,30 +86,13 @@ const(char)* opt_section_name;
long opt_baseaddress;
const(char)* opt_extractfile;

int selected_headers() { return opt_selected & Select.headers; }
int selected_sections() { return opt_selected & Select.sections; }
int selected_relocs() { return opt_selected & Select.relocs; }
int selected_exports() { return opt_selected & Select.exports; }
int selected_imports() { return opt_selected & Select.imports; }
int selected_rsrc() { return opt_selected & Select.rsrc; }
int selected_debug() { return opt_selected & Select.debug_; }
int selected_dirs() { return opt_selected & Select.dirs; }
int selected_loadcfg() { return opt_selected & Select.loadcfg; }

int setting_blob() { return opt_settings & Setting.blob; }
int setting_hexdump() { return opt_settings & Setting.hexdump; }
int setting_extract() { return opt_settings & Setting.extract; }
int setting_extract_any() { return opt_settings & Setting.extractAny; }

int setting_disasm() { return opt_settings & Setting.disasm; }
int setting_disasm_all() { return opt_settings & Setting.disasmAll; }
int setting_disasm_stats() { return opt_settings & Setting.disasmStats; }
int setting_disasm_any() { return opt_settings & Setting.disasmAny; }
int SELECTED(Select selection) { return opt_selected & selection; }
int SETTING(Setting setting) { return opt_settings & setting; }

/// Dump given file to stdout.
/// Returns: Error code if non-zero
int dump(const(char)* path) {
if (setting_blob()) {
if (SETTING(Setting.blob)) {
// NOTE: Program exits and memory is free'ds by OS
size_t size = void;
ubyte *buffer = readall(path, &size);
@@ -134,7 +117,7 @@ int dump(const(char)* path) {
// If anything was selected to dump specifically
if (opt_selected) {
// If not in any "extract" mode, print file info
if (setting_extract_any() == 0) {
if (SETTING(Setting.extractAny) == 0) {
print_string("filename", path);
print_u64("filesize", o.file_size);
print_string("format", adbg_object_format_name(o));
@@ -360,10 +343,10 @@ void print_reloc16(uint index, ushort seg, ushort off) {
}

void print_data(const(char)* name, void *data, size_t size, ulong baseaddress = 0) {
if (setting_hexdump())
if (SETTING(Setting.hexdump))
hexdump(name, data, size, baseaddress);

if (setting_extract())
if (SETTING(Setting.extract))
rawdump(opt_extractfile, data, size, baseaddress);
}

@@ -461,7 +444,7 @@ int dump_disassemble(AdbgMachine machine, void* data, ulong size, ulong base_add
adbg_dis_start(dis, data, cast(size_t)size, base_address);

// stats mode
if (setting_disasm_stats()) {
if (SETTING(Setting.disasmStats)) {
uint stat_avg; /// instruction average size
uint stat_min = uint.max; /// smallest instruction size
uint stat_max; /// longest instruction size
6 changes: 3 additions & 3 deletions dumper/format/ar.d
Original file line number Diff line number Diff line change
@@ -18,9 +18,9 @@ import common.utils : realstring;
extern (C):

int dump_archive(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_archive_firstheader(o);
if (selected_exports())
if (SELECTED(Select.exports))
dump_archive_allheaders(o);
return 0;
}
@@ -55,7 +55,7 @@ void dump_archive_firstheader(adbg_object_t *o) {
}

void dump_archive_memberdata(adbg_object_t *o, ar_member_header *member) {
if (setting_extract_any() == false)
if (SETTING(Setting.extractAny) == false)
return;

ar_member_data m = adbg_object_ar_data(o, member);
2 changes: 1 addition & 1 deletion dumper/format/coff.d
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ import dumper;
import common.utils : realstring;

int dump_coff(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_coff_hdr(o);

return 0;
2 changes: 1 addition & 1 deletion dumper/format/dmp.d
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import dumper;
extern (C):

int dump_dmp(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_dmp_header(o);

return 0;
14 changes: 7 additions & 7 deletions dumper/format/elf.d
Original file line number Diff line number Diff line change
@@ -17,18 +17,18 @@ import common.utils : realchar, hexstr;
extern (C):

int dump_elf(adbg_object_t *o) {
if (selected_headers()) {
if (SELECTED(Select.headers)) {
dump_elf_ehdr(o);
dump_elf_phdr(o);
}

if (selected_sections())
if (SELECTED(Select.sections))
dump_elf_sections(o);

if (selected_exports())
if (SELECTED(Select.exports))
dump_elf_exports(o);

if (setting_disasm_any())
if (SETTING(Setting.disasmAny))
dump_elf_disasm(o);

return 0;
@@ -439,7 +439,7 @@ void dump_elf_sections(adbg_object_t *o) {
if (opt_section_name && strncmp(sname, opt_section_name, SNMLEN))
continue;

if (setting_extract_any()) {
if (SETTING(Setting.extractAny)) {
void *data = o.buffer + shdr.sh_offset;
print_data(opt_section_name, data, shdr.sh_size, shdr.sh_offset);
return;
@@ -481,7 +481,7 @@ void dump_elf_sections(adbg_object_t *o) {
if (opt_section_name && strncmp(sname, opt_section_name, SNMLEN))
continue;

if (setting_extract_any()) {
if (SETTING(Setting.extractAny)) {
void *data = o.buffer + shdr.sh_offset;
print_data(opt_section_name, data, cast(uint)shdr.sh_size, shdr.sh_offset);
return;
@@ -615,7 +615,7 @@ void dump_elf_exports(adbg_object_t *o) {
void dump_elf_disasm(adbg_object_t *o) {
print_header("Disassembly");

int all = setting_disasm_all(); /// dump all
int all = SETTING(Setting.disasmAll); /// dump all

switch (o.i.elf32.ehdr.e_ident[ELF_EI_CLASS]) {
case ELF_CLASS_32:
2 changes: 1 addition & 1 deletion dumper/format/lx.d
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import dumper;
extern (C):

int dump_lx(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_lx_hdr(o);
return 0;
}
2 changes: 1 addition & 1 deletion dumper/format/macho.d
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import adbg.object.format.macho;
import dumper;

int dump_macho(adbg_object_t *o) {
if (selected_headers)
if (SELECTED(Select.headers))
dump_macho_hdr(o);

return 0;
4 changes: 2 additions & 2 deletions dumper/format/mdmp.d
Original file line number Diff line number Diff line change
@@ -15,10 +15,10 @@ import dumper;
import common.utils : realstring;

int dump_minidump(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_minidump_headers(o);

//if (selected_debug())
//if (SELECTED(Select.debug_))
// dump_minidump_debug(o);

return 0;
2 changes: 1 addition & 1 deletion dumper/format/mscoff.d
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import adbg.utils.uid;
import dumper;

int dump_mscoff(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_mscoff_hdr(o);

return 0;
9 changes: 3 additions & 6 deletions dumper/format/mz.d
Original file line number Diff line number Diff line change
@@ -19,15 +19,12 @@ extern (C):
/// o = Object instance.
/// Returns: Non-zero on error.
int dump_mz(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_mz_hdr(o);

if (selected_relocs())
if (SELECTED(Select.relocs))
dump_mz_relocs(o);

if (setting_disasm_any())
if (SETTING(Setting.disasmAny))
dump_mz_disasm(o);

return 0;
}

2 changes: 1 addition & 1 deletion dumper/format/ne.d
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import dumper;
extern (C):

int dump_ne(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_ne_hdr(o);
return 0;
}
4 changes: 2 additions & 2 deletions dumper/format/omf.d
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@ import dumper;
extern (C):

int dump_omf(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_omf_hdr(o);
if (selected_debug())
if (SELECTED(Select.debug_))
dump_omf_debug(o);
return 0;
}
2 changes: 1 addition & 1 deletion dumper/format/pdb20.d
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import dumper;
extern (C):

int dump_pdb20(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_pdb20_header(o);

return 0;
6 changes: 2 additions & 4 deletions dumper/format/pdb70.d
Original file line number Diff line number Diff line change
@@ -18,12 +18,10 @@ import dumper;
extern (C):

int dump_pdb70(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_pdb70_header(o);

if (selected_debug())
if (SELECTED(Select.debug_))
dump_pdb70_debug(o);

return 0;
}

24 changes: 9 additions & 15 deletions dumper/format/pe.d
Original file line number Diff line number Diff line change
@@ -23,32 +23,26 @@ extern (C):
/// o = Object instance.
/// Returns: Non-zero on error.
int dump_pe(adbg_object_t *o) {
if (selected_headers())
if (SELECTED(Select.headers))
dump_pe_hdr(o);

if (selected_sections())
if (SELECTED(Select.sections))
dump_pe_sections(o);

if (selected_exports())
if (SELECTED(Select.exports))
dump_pe_exports(o);

if (selected_imports())
if (SELECTED(Select.imports))
dump_pe_imports(o);

if (selected_debug())
if (SELECTED(Select.debug_))
dump_pe_debug(o);

if (setting_disasm_any())
if (SETTING(Setting.disasmAny))
dump_pe_disasm(o);

return 0;
}

private:

// Returns true if the machine value is unknown
void dump_pe_hdr(adbg_object_t *o) {
if (setting_extract_any()) {
if (SETTING(Setting.extractAny)) {
print_data("Header", o.i.pe.header, PE_HEADER.sizeof, mz_hdr_ext.sizeof);
return;
}
@@ -241,7 +235,7 @@ void dump_pe_sections(adbg_object_t *o) {
if (opt_section_name && strncmp(Name.ptr, opt_section_name, Name.sizeof))
continue;

if (setting_extract_any()) {
if (SETTING(Setting.extractAny)) {
void *data = o.buffer + PointerToRawData;
print_data(opt_section_name, data, SizeOfRawData, PointerToRawData);
}
@@ -812,7 +806,7 @@ void dump_pe_debug(adbg_object_t *o) {
void dump_pe_disasm(adbg_object_t *o) {
print_header("Disassembly");

int all = setting_disasm_all();
int all = SETTING(Setting.disasmAll);
PE_SECTION_ENTRY *section = void;
size_t i;
while ((section = adbg_object_pe_section(o, i++)) != null) with (section) {

0 comments on commit 90392ec

Please sign in to comment.