summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2016-05-06 08:55:29 -0400
committermidipix <writeonce@midipix.org>2016-11-10 23:35:43 -0500
commit8216f99bc4dfeb9dc079694e528566b8bf89438f (patch)
tree3f4590e3a21e8514d48b51b1718d33ff53db4c96
parent60f46a42ac0b7e87a53e7cdd84186449abca2117 (diff)
downloadperk-8216f99bc4dfeb9dc079694e528566b8bf89438f.tar.bz2
perk-8216f99bc4dfeb9dc079694e528566b8bf89438f.tar.xz
internals: pe_little_endian_host(): initial implementation and integration.
-rw-r--r--project/headers.mk1
-rw-r--r--src/internal/perk_endian_impl.h20
-rw-r--r--src/internal/perk_reader_impl.h44
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