summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/perk/perk.h2
-rw-r--r--src/driver/pe_unit_ctx.c2
-rw-r--r--src/logic/pe_get_image_meta.c39
3 files changed, 28 insertions, 15 deletions
diff --git a/include/perk/perk.h b/include/perk/perk.h
index c5e0cff..be5c079 100644
--- a/include/perk/perk.h
+++ b/include/perk/perk.h
@@ -191,7 +191,7 @@ perk_api int pe_output_import_libraries(const struct pe_image_meta *, const str
perk_api int pe_map_raw_image (int fd, const char * path, int prot, struct pe_raw_image *);
perk_api int pe_unmap_raw_image (struct pe_raw_image *);
-perk_api int pe_get_image_meta (const struct pe_raw_image *, struct pe_image_meta **);
+perk_api int pe_get_image_meta (const struct pe_driver_ctx *, const struct pe_raw_image *, struct pe_image_meta **);
perk_api void pe_free_image_meta (struct pe_image_meta *);
perk_api int pe_get_named_section_index(const struct pe_image_meta *, const char * name);
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 <perk/perk.h>
#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; i<m->coff.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; i<m->summary.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; j<m->idata[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: