diff options
author | midipix <writeonce@midipix.org> | 2016-05-06 08:55:29 -0400 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2016-11-10 23:35:43 -0500 |
commit | 8216f99bc4dfeb9dc079694e528566b8bf89438f (patch) | |
tree | 3f4590e3a21e8514d48b51b1718d33ff53db4c96 /src/internal | |
parent | 60f46a42ac0b7e87a53e7cdd84186449abca2117 (diff) | |
download | perk-8216f99bc4dfeb9dc079694e528566b8bf89438f.tar.bz2 perk-8216f99bc4dfeb9dc079694e528566b8bf89438f.tar.xz |
internals: pe_little_endian_host(): initial implementation and integration.
Diffstat (limited to 'src/internal')
-rw-r--r-- | src/internal/perk_endian_impl.h | 20 | ||||
-rw-r--r-- | src/internal/perk_reader_impl.h | 44 |
2 files changed, 37 insertions, 27 deletions
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 |