From 0e3fd6a8154ca4121f9eb958903e9e9ef9a4eb99 Mon Sep 17 00:00:00 2001 From: midipix Date: Mon, 24 Oct 2016 20:15:06 -0400 Subject: pe_get_image_meta(): added error trace support. --- src/driver/pe_unit_ctx.c | 2 +- src/logic/pe_get_image_meta.c | 39 ++++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/driver/pe_unit_ctx.c b/src/driver/pe_unit_ctx.c index 2b05707..76ee376 100644 --- a/src/driver/pe_unit_ctx.c +++ b/src/driver/pe_unit_ctx.c @@ -42,7 +42,7 @@ int pe_get_unit_ctx( if (pe_map_raw_image(dctx->cctx->ioctx->fdin,path,prot,&ctx->map)) return pe_free_unit_ctx_impl(ctx,-1); - if (pe_get_image_meta(&ctx->map,&ctx->meta)) + if (pe_get_image_meta(dctx,&ctx->map,&ctx->meta)) return pe_free_unit_ctx_impl(ctx,-1); memcpy(&ctx->cctx,dctx->cctx, diff --git a/src/logic/pe_get_image_meta.c b/src/logic/pe_get_image_meta.c index 8da2c90..8d90353 100644 --- a/src/logic/pe_get_image_meta.c +++ b/src/logic/pe_get_image_meta.c @@ -11,8 +11,9 @@ #include #include "perk_reader_impl.h" +#include "perk_errinfo_impl.h" -static int pe_free_image_meta_impl(struct pe_image_meta * meta, int status) +static int pe_free_image_meta_impl(struct pe_image_meta * meta, int ret) { int i; @@ -25,7 +26,7 @@ static int pe_free_image_meta_impl(struct pe_image_meta * meta, int status) free(meta); } - return status; + return ret; } void pe_free_image_meta(struct pe_image_meta * meta) @@ -58,7 +59,10 @@ int pe_get_block_section_index(const struct pe_image_meta * m, const struct pe_b return -1; } -int pe_get_image_meta(const struct pe_raw_image * image, struct pe_image_meta ** meta) +int pe_get_image_meta( + const struct pe_driver_ctx * dctx, + const struct pe_raw_image * image, + struct pe_image_meta ** meta) { int i,s,status; unsigned j; @@ -66,27 +70,31 @@ int pe_get_image_meta(const struct pe_raw_image * image, struct pe_image_meta ** char * base = image->addr; if (!(m = calloc(1,sizeof(*m)))) - return -1; + return PERK_SYSTEM_ERROR(dctx); m->ados = (struct pe_image_dos_hdr *)base; if ((status = (pe_read_dos_header(m->ados,&m->dos)))) - return pe_free_image_meta_impl(m,status); + return pe_free_image_meta_impl(m, + PERK_CUSTOM_ERROR(dctx,status)); m->acoff = (struct pe_coff_file_hdr *)(base + m->dos.dos_lfanew); if ((status = (pe_read_coff_header(m->acoff,&m->coff)))) - return pe_free_image_meta_impl(m,status); + return pe_free_image_meta_impl(m, + PERK_CUSTOM_ERROR(dctx,status)); m->aopt = (union pe_opt_hdr *)((char *)m->acoff + sizeof(m->coff)); if ((status = (pe_read_optional_header(m->aopt,&m->opt)))) - return pe_free_image_meta_impl(m,status); + return pe_free_image_meta_impl(m, + PERK_CUSTOM_ERROR(dctx,status)); m->asectbl = (struct pe_sec_hdr *)((char *)m->aopt + m->coff.size_of_opt_hdr); if (!(m->sectbl = calloc(m->coff.num_of_sections,sizeof(*(m->sectbl))))) - return pe_free_image_meta_impl(m,status); + return pe_free_image_meta_impl(m, + PERK_SYSTEM_ERROR(dctx)); for (i=0; icoff.num_of_sections; i++) pe_read_section_header(&m->asectbl[i],&m->sectbl[i]); @@ -96,7 +104,8 @@ int pe_get_image_meta(const struct pe_raw_image * image, struct pe_image_meta ** s = pe_get_block_section_index(m,&m->opt.dirs.export_tbl); if ((i >= 0) && (i != s)) - return pe_free_image_meta_impl(m,PERK_MALFORMED_IMAGE); + return pe_free_image_meta_impl(m, + PERK_CUSTOM_ERROR(dctx,PERK_MALFORMED_IMAGE)); if (s >= 0) { m->hedata = &m->sectbl[s]; @@ -118,7 +127,8 @@ int pe_get_image_meta(const struct pe_raw_image * image, struct pe_image_meta ** s = pe_get_block_section_index(m,&m->opt.dirs.import_tbl); if ((i >= 0) && (i != s)) - return pe_free_image_meta_impl(m,PERK_MALFORMED_IMAGE); + return pe_free_image_meta_impl(m, + PERK_CUSTOM_ERROR(dctx,PERK_MALFORMED_IMAGE)); if (s >= 0) { m->hidata = &m->sectbl[s]; @@ -136,7 +146,8 @@ int pe_get_image_meta(const struct pe_raw_image * image, struct pe_image_meta ** /* import headers */ if (!(m->idata = calloc(m->summary.nimplibs,sizeof(*(m->idata))))) - return pe_free_image_meta_impl(m,status); + return pe_free_image_meta_impl(m, + PERK_SYSTEM_ERROR(dctx)); for (i=0; isummary.nimplibs; i++) { pe_read_import_header(&m->aidata[i],&m->idata[i]); @@ -160,7 +171,8 @@ int pe_get_image_meta(const struct pe_raw_image * image, struct pe_image_meta ** m->idata[i].count++; if (!(m->idata[i].items = calloc(m->idata[i].count,sizeof(*(m->idata[i].items))))) - return pe_free_image_meta_impl(m,status); + return pe_free_image_meta_impl(m, + PERK_SYSTEM_ERROR(dctx)); } for (j=0; jidata[i].count; j++) { @@ -168,7 +180,8 @@ int pe_get_image_meta(const struct pe_raw_image * image, struct pe_image_meta ** &(m->idata[i].aitems[j]), &(m->idata[i].items[j]), m->opt.std.magic))) - return pe_free_image_meta_impl(m,status); + return pe_free_image_meta_impl(m, + PERK_CUSTOM_ERROR(dctx,status)); switch (m->opt.std.magic) { case PE_MAGIC_PE32: -- cgit v1.2.3