diff options
-rw-r--r-- | project/headers.mk | 1 | ||||
-rw-r--r-- | src/internal/perk_endian_impl.h | 20 | ||||
-rw-r--r-- | src/internal/perk_reader_impl.h | 44 |
3 files changed, 38 insertions, 27 deletions
diff --git a/project/headers.mk b/project/headers.mk index 6cc3657..5ffbebe 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -9,6 +9,7 @@ API_HEADERS = \ INTERNAL_HEADERS = \ $(PROJECT_DIR)/src/internal/argv/argv.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_driver_impl.h \ + $(PROJECT_DIR)/src/internal/$(PACKAGE)_endian_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_output_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_reader_impl.h \ 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 <stdbool.h> + +#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 <stdint.h> -#include <endian.h> - -#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 |