From dd2de65b85cdc7a7744145214ecf8ea5ebe46c25 Mon Sep 17 00:00:00 2001 From: rimio Date: Thu, 1 Mar 2018 21:24:07 +0200 Subject: Added endianness conversion; Added first 10 answer files; fixed test case; fixed small issue in describe tool --- src/ecbor.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/ecbor_decoder.c | 9 ++++++--- src/ecbor_describe.c | 4 ++-- src/ecbor_internal.h | 14 ++++++++++++++ 4 files changed, 73 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ecbor.c b/src/ecbor.c index 9e8404f..dbbed96 100644 --- a/src/ecbor.c +++ b/src/ecbor.c @@ -6,6 +6,57 @@ */ #include "ecbor.h" +#include "ecbor_internal.h" + +uint16_t +ecbor_uint16_from_big_endian (uint16_t value) +{ +#if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) + return value; +#elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) + /* TODO: ARM optimization */ + return ((value << 8) & 0xff00) + | ((value >> 8) & 0x00ff); +#else + #error "Endianness not supported!" +#endif +} + +uint32_t +ecbor_uint32_from_big_endian (uint32_t value) +{ +#if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) + return value; +#elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) + /* TODO: ARM optimization */ + return ((value << 8) & 0x00ff0000) + | ((value >> 8) & 0x0000ff00) + | ((value << 24) & 0xff000000) + | ((value >> 24) & 0x000000ff); +#else + #error "Endianness not supported!" +#endif +} + +uint64_t +ecbor_uint64_from_big_endian (uint64_t value) +{ +#if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) + return value; +#elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) + /* TODO: ARM optimization */ + return ((value << 8) & 0x000000ff00000000) + | ((value >> 8) & 0x00000000ff000000) + | ((value << 24) & 0x0000ff0000000000) + | ((value >> 24) & 0x0000000000ff0000) + | ((value << 40) & 0x00ff000000000000) + | ((value >> 40) & 0x000000000000ff00) + | ((value << 56) & 0xff00000000000000) + | ((value >> 56) & 0x00000000000000ff); +#else + #error "Endianness not supported!" +#endif +} extern ecbor_major_type_t ecbor_get_type (ecbor_item_t *item) diff --git a/src/ecbor_decoder.c b/src/ecbor_decoder.c index dfd3cad..d9cea78 100644 --- a/src/ecbor_decoder.c +++ b/src/ecbor_decoder.c @@ -116,15 +116,18 @@ ecbor_decode_uint (ecbor_decode_context_t *context, break; case ECBOR_ADDITIONAL_2BYTE: - (*value) = *((uint16_t *) context->in_position); + (*value) = + ecbor_uint16_from_big_endian (*((uint16_t *) context->in_position)); break; case ECBOR_ADDITIONAL_4BYTE: - (*value) = *((uint32_t *) context->in_position); + (*value) = + ecbor_uint32_from_big_endian (*((uint32_t *) context->in_position)); break; case ECBOR_ADDITIONAL_8BYTE: - (*value) = *((uint64_t *) context->in_position); + (*value) = + ecbor_uint64_from_big_endian (*((uint64_t *) context->in_position)); break; default: diff --git a/src/ecbor_describe.c b/src/ecbor_describe.c index 1f5dd5a..de1025c 100644 --- a/src/ecbor_describe.c +++ b/src/ecbor_describe.c @@ -67,7 +67,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix) return rc; } - printf ("[NINT] value %d\n", (int)val); + printf ("[NINT] value %lld\n", (long long int)val); } break; @@ -80,7 +80,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix) return rc; } - printf ("[UINT] value %u\n", (unsigned int) val); + printf ("[UINT] value %llu\n", (unsigned long long int) val); } break; diff --git a/src/ecbor_internal.h b/src/ecbor_internal.h index f0ec24b..b783eb1 100644 --- a/src/ecbor_internal.h +++ b/src/ecbor_internal.h @@ -8,6 +8,8 @@ #ifndef _ECBOR_INTERNAL_H_ #define _ECBOR_INTERNAL_H_ +#include + /* Don't rely on for this */ #define NULL 0 #define false 0 @@ -23,4 +25,16 @@ enum { ECBOR_ADDITIONAL_INDEFINITE = 31 }; +/* + * Endianness + */ +extern uint16_t +ecbor_uint16_from_big_endian (uint16_t value); + +extern uint32_t +ecbor_uint32_from_big_endian (uint32_t value); + +extern uint64_t +ecbor_uint64_from_big_endian (uint64_t value); + #endif \ No newline at end of file -- cgit v1.2.3