summaryrefslogtreecommitdiff
path: root/src/reader
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2016-05-06 09:52:08 -0400
committermidipix <writeonce@midipix.org>2016-11-10 23:35:44 -0500
commit2d6c77772c5d23f8d005dbb7d9749b9a84d6606a (patch)
tree254b686ab17249de101c17f250b967ae54144334 /src/reader
parentff23632ddaf5f3acc893f110cbd4eabb72b75de0 (diff)
downloadperk-2d6c77772c5d23f8d005dbb7d9749b9a84d6606a.tar.bz2
perk-2d6c77772c5d23f8d005dbb7d9749b9a84d6606a.tar.xz
pe_read_optional_header(): integrate runtime little endian test.
Diffstat (limited to 'src/reader')
-rw-r--r--src/reader/pe_read_optional_header.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/reader/pe_read_optional_header.c b/src/reader/pe_read_optional_header.c
index 3d0d835..48e7fce 100644
--- a/src/reader/pe_read_optional_header.c
+++ b/src/reader/pe_read_optional_header.c
@@ -4,19 +4,17 @@
/* Released under GPLv2 and GPLv3; see COPYING.PERK. */
/***************************************************************/
-#include <endian.h>
#include <string.h>
#include <perk/perk.h>
+#include "perk_endian_impl.h"
#include "perk_reader_impl.h"
-int pe_read_optional_header(const union pe_opt_hdr * p, struct pe_meta_opt_hdr * m)
+static int pe_read_optional_header_little_endian(const union pe_opt_hdr * p, struct pe_meta_opt_hdr * m)
{
m->std.magic = pe_read_short(p->opt_hdr_32.magic);
memset(&m->dirs,0,sizeof(m->dirs));
- #if (BYTE_ORDER == LITTLE_ENDIAN)
-
memcpy(&m->std,p,sizeof(struct pe_meta_opt_hdr_std));
switch (m->std.magic) {
@@ -50,8 +48,11 @@ int pe_read_optional_header(const union pe_opt_hdr * p, struct pe_meta_opt_hdr *
return PERK_BAD_IMAGE_TYPE;
}
- #else
+ return 0;
+}
+static int pe_read_optional_header_big_endian(const union pe_opt_hdr * p, struct pe_meta_opt_hdr * m)
+{
struct pe_opt_hdr_std * astd;
struct pe_opt_hdr_vers * avers;
struct pe_opt_hdr_align * aalign;
@@ -60,6 +61,9 @@ int pe_read_optional_header(const union pe_opt_hdr * p, struct pe_meta_opt_hdr *
struct pe_opt_hdr_dirs * adirs;
size_t sdirs;
+ m->std.magic = pe_read_short(p->opt_hdr_32.magic);
+ memset(&m->dirs,0,sizeof(m->dirs));
+
astd = (struct pe_opt_hdr_std *)p;
switch (m->std.magic) {
@@ -119,7 +123,20 @@ int pe_read_optional_header(const union pe_opt_hdr * p, struct pe_meta_opt_hdr *
m->ldr.loader_flags = pe_read_long(aldr->loader_flags);
m->ldr.rva_and_sizes = pe_read_long(aldr->rva_and_sizes);
- #endif
+ return 0;
+}
+
+int pe_read_optional_header(const union pe_opt_hdr * p, struct pe_meta_opt_hdr * m)
+{
+ int ret;
+
+ if (PERK_LITTLE_ENDIAN) {
+ if ((ret = pe_read_optional_header_little_endian(p,m)))
+ return ret;
+ } else {
+ if ((ret = pe_read_optional_header_big_endian(p,m)))
+ return ret;
+ }
switch (m->std.magic) {
case PE_MAGIC_PE32: