Skip to content
Permalink
Browse files

Fix alignment issues in loaders, get rid of endian.h build flag

  • Loading branch information...
PJK committed Jun 23, 2019
1 parent c745f6b commit eeab3607e4043bfcbca72d08ca2b564dd9eac8a7
Showing with 10 additions and 26 deletions.
  1. +0 −7 CMakeLists.txt
  2. +10 −19 src/cbor/internal/loaders.c
@@ -10,13 +10,6 @@ SET(CBOR_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATC
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
include(CheckIncludeFiles)

check_include_files(endian.h HAVE_ENDIAN_H)
if(HAVE_ENDIAN_H)
# BSD source needed or else GNU-y extensions will not be visible
add_definitions(-D_BSD_SOURCE)
add_definitions(-DHAVE_ENDIAN_H)
endif()

include(TestBigEndian)
test_big_endian(BIG_ENDIAN)
if(BIG_ENDIAN)
@@ -7,45 +7,37 @@

#include "loaders.h"
#include <math.h>

#ifdef HAVE_ENDIAN_H
#include <endian.h>
#endif
#include <string.h>

uint8_t _cbor_load_uint8(cbor_data source) { return (uint8_t)*source; }

uint16_t _cbor_load_uint16(const unsigned char *source) {
#ifdef HAVE_ENDIAN_H
return be16toh(*(uint16_t *)source);
#else
#ifdef IS_BIG_ENDIAN
return *(uint16_t *)source;
uint16_t result;
memcpy(&result, source, 2);
return result;
#else
return ((uint16_t) * (source + 0) << 8) + (uint8_t) * (source + 1);
#endif
#endif
}

uint32_t _cbor_load_uint32(const unsigned char *source) {
#ifdef HAVE_ENDIAN_H
return be32toh(*(uint32_t *)source);
#else
#ifdef IS_BIG_ENDIAN
return *(uint32_t *)source;
uint32_t result;
memcpy(&result, source, 4);
return result;
#else
return ((uint32_t) * (source + 0) << 0x18) +
((uint32_t) * (source + 1) << 0x10) +
((uint16_t) * (source + 2) << 0x08) + (uint8_t) * (source + 3);
#endif
#endif
}

uint64_t _cbor_load_uint64(const unsigned char *source) {
#ifdef HAVE_ENDIAN_H
return be64toh(*(uint64_t *)source);
#else
#ifdef IS_BIG_ENDIAN
return *(uint64_t *)source;
uint64_t result;
memcpy(&result, source, 8);
return result;
#else
return ((uint64_t) * (source + 0) << 0x38) +
((uint64_t) * (source + 1) << 0x30) +
@@ -55,7 +47,6 @@ uint64_t _cbor_load_uint64(const unsigned char *source) {
((uint32_t) * (source + 5) << 0x10) +
((uint16_t) * (source + 6) << 0x08) + (uint8_t) * (source + 7);
#endif
#endif
}

/* As per http://tools.ietf.org/html/rfc7049#appendix-D */

0 comments on commit eeab360

Please sign in to comment.
You can’t perform that action at this time.