Skip to content

Commit

Permalink
Use new rz_buf_read API
Browse files Browse the repository at this point in the history
  • Loading branch information
Basstorm committed Aug 23, 2021
1 parent ab71d6f commit b79f900
Show file tree
Hide file tree
Showing 7 changed files with 694 additions and 301 deletions.
143 changes: 80 additions & 63 deletions librz/bin/pdb/dbi.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,39 @@ RZ_IPI void rz_bin_pdb_free_dbi_stream(DbiStream *stream) {
rz_list_free(stream->ex_hdrs);
}

static void parse_dbi_stream_header(DbiStream *s, RzBuffer *buf) {
s->hdr.version_signature = rz_buf_read_le32(buf);
s->hdr.version_header = rz_buf_read_le32(buf);
s->hdr.age = rz_buf_read_le32(buf);
s->hdr.global_stream_index = rz_buf_read_le16(buf);
s->hdr.build_number = rz_buf_read_le16(buf);
s->hdr.public_stream_index = rz_buf_read_le16(buf);
s->hdr.pdb_dll_version = rz_buf_read_le16(buf);
s->hdr.sym_record_stream = rz_buf_read_le16(buf);
s->hdr.pdb_dll_rbld = rz_buf_read_le16(buf);
s->hdr.mod_info_size = rz_buf_read_le32(buf);
s->hdr.section_contribution_size = rz_buf_read_le32(buf);
s->hdr.section_map_size = rz_buf_read_le32(buf);
s->hdr.source_info_size = rz_buf_read_le32(buf);
s->hdr.type_server_map_size = rz_buf_read_le32(buf);
s->hdr.mfc_type_server_index = rz_buf_read_le32(buf);
s->hdr.optional_dbg_header_size = rz_buf_read_le32(buf);
s->hdr.ec_substream_size = rz_buf_read_le32(buf);
s->hdr.flags = rz_buf_read_le16(buf);
s->hdr.machine = rz_buf_read_le16(buf);
s->hdr.padding = rz_buf_read_le32(buf);
static bool parse_dbi_stream_header(DbiStream *s, RzBuffer *buf) {
return rz_buf_read_le32(buf, &s->hdr.version_signature) &&
rz_buf_read_le32(buf, &s->hdr.version_header) &&
rz_buf_read_le32(buf, &s->hdr.age) &&
rz_buf_read_le16(buf, &s->hdr.global_stream_index) &&
rz_buf_read_le16(buf, &s->hdr.build_number) &&
rz_buf_read_le16(buf, &s->hdr.public_stream_index) &&
rz_buf_read_le16(buf, &s->hdr.pdb_dll_version) &&
rz_buf_read_le16(buf, &s->hdr.sym_record_stream) &&
rz_buf_read_le16(buf, &s->hdr.pdb_dll_rbld) &&
rz_buf_read_le32(buf, &s->hdr.mod_info_size) &&
rz_buf_read_le32(buf, &s->hdr.section_contribution_size) &&
rz_buf_read_le32(buf, &s->hdr.section_map_size) &&
rz_buf_read_le32(buf, &s->hdr.source_info_size) &&
rz_buf_read_le32(buf, &s->hdr.type_server_map_size) &&
rz_buf_read_le32(buf, &s->hdr.mfc_type_server_index) &&
rz_buf_read_le32(buf, &s->hdr.optional_dbg_header_size) &&
rz_buf_read_le32(buf, &s->hdr.ec_substream_size) &&
rz_buf_read_le16(buf, &s->hdr.flags) &&
rz_buf_read_le16(buf, &s->hdr.machine) &&
rz_buf_read_le32(buf, &s->hdr.padding);
}

static ut32 parse_dbi_stream_section_entry(DbiStreamExHdr *hdr, RzBuffer *buf) {
hdr->SectionContr.Section = rz_buf_read_le16(buf);
*(ut16 *)hdr->SectionContr.Padding1 = rz_buf_read_le16(buf);
hdr->SectionContr.Offset = rz_buf_read_le32(buf);
hdr->SectionContr.Size = rz_buf_read_le32(buf);
hdr->SectionContr.Characteristics = rz_buf_read_le32(buf);
hdr->SectionContr.ModuleIndex = rz_buf_read_le16(buf);
*(ut16 *)hdr->SectionContr.Padding2 = rz_buf_read_le16(buf);
hdr->SectionContr.DataCrc = rz_buf_read_le32(buf);
hdr->SectionContr.RelocCrc = rz_buf_read_le32(buf);
return sizeof(hdr->SectionContr);
static bool parse_dbi_stream_section_entry(DbiStreamExHdr *hdr, RzBuffer *buf) {
return rz_buf_read_le16(buf, &hdr->sec_con.Section) &&
rz_buf_read_le16(buf, &hdr->sec_con.Padding1) &&
rz_buf_read_le32(buf, &hdr->sec_con.Offset) &&
rz_buf_read_le32(buf, &hdr->sec_con.Size) &&
rz_buf_read_le32(buf, &hdr->sec_con.Characteristics) &&
rz_buf_read_le16(buf, &hdr->sec_con.ModuleIndex) &&
rz_buf_read_le16(buf, &hdr->sec_con.Padding2) &&
rz_buf_read_le32(buf, &hdr->sec_con.DataCrc) &&
rz_buf_read_le32(buf, &hdr->sec_con.RelocCrc);
}

static bool parse_dbi_stream_ex_header(DbiStream *s, RzBuffer *buf) {
Expand All @@ -64,22 +63,35 @@ static bool parse_dbi_stream_ex_header(DbiStream *s, RzBuffer *buf) {
if (!hdr) {
return false;
}
hdr->unknown = rz_buf_read_le32(buf);
if (!rz_buf_read_le32(buf, &hdr->unknown)) {
return false;
}
read_len += sizeof(ut32);
read_len += parse_dbi_stream_section_entry(hdr, buf);
hdr->Flags = rz_buf_read_le16(buf);
hdr->ModuleSymStream = rz_buf_read_le16(buf);
if (!parse_dbi_stream_section_entry(hdr, buf)) {
return false;
}
read_len += sizeof(SectionContr);
if (!rz_buf_read_le16(buf, &hdr->Flags) ||
!rz_buf_read_le16(buf, &hdr->ModuleSymStream)) {
return false;
}
read_len += sizeof(ut16) * 2;
hdr->SymByteSize = rz_buf_read_le32(buf);
hdr->C11ByteSize = rz_buf_read_le32(buf);
hdr->C13ByteSize = rz_buf_read_le32(buf);
if (!rz_buf_read_le32(buf, &hdr->SymByteSize) ||
!rz_buf_read_le32(buf, &hdr->C11ByteSize) ||
!rz_buf_read_le32(buf, &hdr->C13ByteSize)) {
return false;
}
read_len += sizeof(ut32) * 3;
hdr->SourceFileCount = rz_buf_read_le16(buf);
*(ut16 *)hdr->Padding = rz_buf_read_le16(buf);
if (!rz_buf_read_le16(buf, &hdr->SourceFileCount) ||
!rz_buf_read_le16(buf, &hdr->Padding)) {
return false;
}
read_len += sizeof(ut16) * 2;
hdr->Unused2 = rz_buf_read_le32(buf);
hdr->SourceFileNameIndex = rz_buf_read_le32(buf);
hdr->PdbFilePathNameIndex = rz_buf_read_le32(buf);
if (!rz_buf_read_le32(buf, &hdr->Unused2) ||
!rz_buf_read_le32(buf, &hdr->SourceFileNameIndex) ||
!rz_buf_read_le32(buf, &hdr->PdbFilePathNameIndex)) {
return false;
}
read_len += sizeof(ut32) * 3;

hdr->ModuleName = rz_buf_get_string(buf, rz_buf_tell(buf));
Expand All @@ -105,18 +117,21 @@ static bool parse_dbi_stream_ex_header(DbiStream *s, RzBuffer *buf) {
return true;
}

static void parse_dbi_dbg_header(DbiStream *s, RzBuffer *buf) {
s->dbg_hdr.sn_fpo = rz_buf_read_le16(buf);
s->dbg_hdr.sn_exception = rz_buf_read_le16(buf);
s->dbg_hdr.sn_fixup = rz_buf_read_le16(buf);
s->dbg_hdr.sn_omap_to_src = rz_buf_read_le16(buf);
s->dbg_hdr.sn_omap_from_src = rz_buf_read_le16(buf);
s->dbg_hdr.sn_section_hdr = rz_buf_read_le16(buf);
s->dbg_hdr.sn_token_rid_map = rz_buf_read_le16(buf);
s->dbg_hdr.sn_xdata = rz_buf_read_le16(buf);
s->dbg_hdr.sn_pdata = rz_buf_read_le16(buf);
s->dbg_hdr.sn_new_fpo = rz_buf_read_le16(buf);
s->dbg_hdr.sn_section_hdr_orig = rz_buf_read_le16(buf);
static bool parse_dbi_dbg_header(DbiStream *s, RzBuffer *buf) {
if (!rz_buf_read_le16(buf, &s->dbg_hdr.sn_fpo) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_exception) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_fixup) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_omap_to_src) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_omap_from_src) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_section_hdr) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_token_rid_map) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_xdata) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_pdata) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_new_fpo) ||
!rz_buf_read_le16(buf, &s->dbg_hdr.sn_section_hdr_orig)) {
return false;
}
return true;
}

RZ_IPI bool parse_dbi_stream(RzPdb *pdb, MsfStream *stream) {
Expand All @@ -127,15 +142,17 @@ RZ_IPI bool parse_dbi_stream(RzPdb *pdb, MsfStream *stream) {
DbiStream *s = pdb->s_dbi;
RzBuffer *buf = stream->stream_data;
// parse header
parse_dbi_stream_header(s, buf);
parse_dbi_stream_ex_header(s, buf);
if (!parse_dbi_stream_header(s, buf) || !parse_dbi_stream_ex_header(s, buf)) {
return false;
}

// skip these streams
ut64 seek = s->hdr.section_contribution_size +
s->hdr.section_map_size +
s->hdr.source_info_size +
s->hdr.type_server_map_size +
ut64 seek = s->hdr.section_contribution_size + s->hdr.section_map_size +
s->hdr.source_info_size + s->hdr.type_server_map_size +
s->hdr.ec_substream_size;
rz_buf_seek(buf, rz_buf_tell(buf) + seek, RZ_BUF_SET);
parse_dbi_dbg_header(s, buf);
if (!parse_dbi_dbg_header(s, buf)) {
return false;
}
return true;
}
24 changes: 13 additions & 11 deletions librz/bin/pdb/dbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ typedef struct dbi_stream_header_t {
ut32 padding;
} DbiStreamHdr;

typedef struct SectionContribEntry {
ut16 Section;
char Padding1[2];
st32 Offset;
st32 Size;
ut32 Characteristics;
ut16 ModuleIndex;
char Padding2[2];
ut32 DataCrc;
ut32 RelocCrc;
} SectionContr;

typedef struct dbi_stream_ex_header_t {
ut32 unknown;
struct SectionContribEntry {
ut16 Section;
char Padding1[2];
st32 Offset;
st32 Size;
ut32 Characteristics;
ut16 ModuleIndex;
char Padding2[2];
ut32 DataCrc;
ut32 RelocCrc;
} SectionContr;
SectionContr sec_con;
ut16 Flags;
ut16 ModuleSymStream;
ut32 SymByteSize;
Expand Down
31 changes: 23 additions & 8 deletions librz/bin/pdb/gdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

#include "pdb.h"

static void parse_gdata_global(GDataGlobal *global, RzBuffer *buf, ut32 *read_len) {
global->symtype = rz_buf_read_le32(buf);
global->offset = rz_buf_read_le32(buf);
static bool parse_gdata_global(GDataGlobal *global, RzBuffer *buf, ut32 *read_len) {
if (!rz_buf_read_le32(buf, &global->symtype) ||
!rz_buf_read_le32(buf, &global->offset)) {
return false;
}
*read_len += sizeof(ut32) * 2;
global->segment = rz_buf_read_le16(buf);
if (!rz_buf_read_le16(buf, &global->segment)) {
return false;
}
*read_len += sizeof(ut16);
if (global->leaf_type == 0x110E) {
global->name = rz_buf_get_string(buf, rz_buf_tell(buf));
Expand All @@ -17,14 +21,17 @@ static void parse_gdata_global(GDataGlobal *global, RzBuffer *buf, ut32 *read_le
rz_buf_seek(buf, rz_buf_tell(buf) + len, RZ_BUF_SET);
*read_len += len;
} else {
global->name_len = rz_buf_read8(buf);
if (!rz_buf_read8(buf, &global->name_len)) {
return false;
}
*read_len += sizeof(ut8);
}
if ((*read_len % 4)) {
ut16 remain = 4 - (*read_len % 4);
rz_buf_seek(buf, rz_buf_tell(buf) + remain, RZ_BUF_SET);
read_len += remain;
}
return true;
}

RZ_IPI bool parse_gdata_stream(RzPdb *pdb, MsfStream *stream) {
Expand All @@ -41,20 +48,28 @@ RZ_IPI bool parse_gdata_stream(RzPdb *pdb, MsfStream *stream) {
ut16 len;
while (true) {
ut32 read_len = 0;
len = rz_buf_read_le16(buf);
if (!rz_buf_read_le16(buf, &len)) {
break;
}
read_len += sizeof(ut16);
if (len == 0 || len == UT16_MAX) {
break;
}
ut16 leaf_type = rz_buf_read_le16(buf);
ut16 leaf_type;
if (!rz_buf_read_le16(buf, &leaf_type)) {
return false;
}
read_len += sizeof(ut16);
if (leaf_type == 0x110E || leaf_type == 0x1009) {
GDataGlobal *global = RZ_NEW0(GDataGlobal);
if (!global) {
goto skip;
}
global->leaf_type = leaf_type;
parse_gdata_global(global, buf, &read_len);
if (!parse_gdata_global(global, buf, &read_len)) {
RZ_FREE(global);
return false;
}
rz_list_append(s->global_list, global);
continue;
}
Expand Down
10 changes: 5 additions & 5 deletions librz/bin/pdb/omap.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ RZ_IPI bool parse_omap_stream(RzPdb *pdb, MsfStream *stream) {
rz_list_free(s->entries);
return false;
}
entry->from = rz_buf_read_le32(buf);
entry->to = rz_buf_read_le32(buf);
if (!rz_buf_read_le32(buf, &entry->from) ||
!rz_buf_read_le32(buf, &entry->to)) {
return false;
}
read_len += sizeof(ut32) * 2;
rz_list_append(s->entries, entry);
}
Expand All @@ -35,9 +37,7 @@ RZ_IPI void free_omap_stream(OmapStream *stream) {
OmapEntry *entry;

RzListIter *it;
rz_list_foreach (stream->entries, it, entry) {
RZ_FREE(entry);
}
rz_list_foreach (stream->entries, it, entry) { RZ_FREE(entry); }
rz_list_free(stream->entries);
}

Expand Down
Loading

0 comments on commit b79f900

Please sign in to comment.