From 8216f99bc4dfeb9dc079694e528566b8bf89438f Mon Sep 17 00:00:00 2001 From: midipix Date: Fri, 6 May 2016 08:55:29 -0400 Subject: internals: pe_little_endian_host(): initial implementation and integration. --- src/internal/perk_endian_impl.h | 20 +++++++++++++++++++ src/internal/perk_reader_impl.h | 44 ++++++++++++++++------------------------- 2 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 src/internal/perk_endian_impl.h (limited to 'src/internal') diff --git a/src/internal/perk_endian_impl.h b/src/internal/perk_endian_impl.h new file mode 100644 index 0000000..030dfa9 --- /dev/null +++ b/src/internal/perk_endian_impl.h @@ -0,0 +1,20 @@ +/***************************************************************/ +/* perk: PE Resource Kit */ +/* Copyright (C) 2015--2016 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.PERK. */ +/***************************************************************/ + +#ifndef PERK_ENDIAN_IMPL_H +#define PERK_ENDIAN_IMPL_H + +#include + +#define PERK_LITTLE_ENDIAN pe_little_endian_host() + +static inline bool pe_little_endian_host(void) +{ + const long test = 1; + return *((char *)&test); +} + +#endif diff --git a/src/internal/perk_reader_impl.h b/src/internal/perk_reader_impl.h index 24fc38c..fc5e372 100644 --- a/src/internal/perk_reader_impl.h +++ b/src/internal/perk_reader_impl.h @@ -8,31 +8,16 @@ #define PERK_READER_IMPL_H #include -#include - -#if (BYTE_ORDER == LITTLE_ENDIAN) - -static inline uint16_t pe_read_short(const unsigned char * raw) -{ - return *(uint16_t *)raw; -} - -static inline uint32_t pe_read_long(const unsigned char * raw) -{ - return *(uint32_t *)raw; -} - -static inline uint64_t pe_read_quad(const unsigned char * raw) -{ - return *(uint64_t *)raw; -} - -#else +#include "perk_endian_impl.h" static inline uint16_t pe_read_short(const unsigned char * raw) { - uint16_t x = *(uint16_t *)raw; - return x<<8 | x>>8; + if (PERK_LITTLE_ENDIAN) { + return *(uint16_t *)raw; + } else { + uint16_t x = *(uint16_t *)raw; + return x<<8 | x>>8; + } } static inline uint32_t pe_swap_long(uint32_t x) @@ -42,15 +27,20 @@ static inline uint32_t pe_swap_long(uint32_t x) static inline uint32_t pe_read_long(const unsigned char * raw) { - return pe_swap_long(*(uint32_t *)raw); + if (PERK_LITTLE_ENDIAN) + return *(uint32_t *)raw; + else + return pe_swap_long(*(uint32_t *)raw); } static inline uint64_t pe_read_quad(const unsigned char * raw) { - uint64_t x = *(uint64_t *)raw; - return ((uint64_t)pe_swap_long(x)<<32) | pe_swap_long(x>>32); + if (PERK_LITTLE_ENDIAN) { + return *(uint64_t *)raw; + } else { + uint64_t x = *(uint64_t *)raw; + return ((uint64_t)pe_swap_long(x)<<32) | pe_swap_long(x>>32); + } } #endif - -#endif -- cgit v1.2.3