From 9093cd4aba11a8175a71824d475d8501c2a19712 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 21 Sep 2015 11:37:31 +0200 Subject: [PATCH] (libretro-db) Use retro_endianness.h functions completely --- libretro-common/include/retro_endianness.h | 42 ++++++++++++++++++++++ libretro-db/libretrodb.c | 6 ++-- libretro-db/libretrodb_endian.h | 23 ------------ libretro-db/rmsgpack.c | 13 +++---- 4 files changed, 52 insertions(+), 32 deletions(-) delete mode 100644 libretro-db/libretrodb_endian.h diff --git a/libretro-common/include/retro_endianness.h b/libretro-common/include/retro_endianness.h index 2c952c04d1..74f29821cd 100644 --- a/libretro-common/include/retro_endianness.h +++ b/libretro-common/include/retro_endianness.h @@ -43,6 +43,16 @@ )) #endif +#define SWAP64(val) \ + ((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \ + | (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \ + | (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \ + | (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \ + | (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \ + | (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \ + | (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \ + | (((uint64_t)(val) & 0xff00000000000000ULL) >> 56)) + /** * is_little_endian: * @@ -69,6 +79,22 @@ static INLINE uint8_t is_little_endian(void) #endif } +/** + * swap_if_big64: + * @val : unsigned 64-bit value + * + * Byteswap unsigned 64-bit value if system is big-endian. + * + * Returns: Byteswapped value in case system is big-endian, + * otherwise returns same value. + **/ +static INLINE uint64_t swap_if_big64(uint64_t val) +{ + if (is_little_endian()) + return val; + return SWAP64(val); +} + /** * swap_if_big32: * @val : unsigned 32-bit value @@ -85,6 +111,22 @@ static INLINE uint32_t swap_if_big32(uint32_t val) return SWAP32(val); } +/** + * swap_if_little64: + * @val : unsigned 64-bit value + * + * Byteswap unsigned 64-bit value if system is little-endian. + * + * Returns: Byteswapped value in case system is little-endian, + * otherwise returns same value. + **/ +static INLINE uint64_t swap_if_little64(uint64_t val) +{ + if (is_little_endian()) + return SWAP64(val); + return val; +} + /** * swap_if_little32: * @val : unsigned 32-bit value diff --git a/libretro-db/libretrodb.c b/libretro-db/libretrodb.c index 5e2e5843b3..194aff50c1 100644 --- a/libretro-db/libretrodb.c +++ b/libretro-db/libretrodb.c @@ -12,13 +12,13 @@ #include #include +#include #include #include "libretrodb.h" #include "rmsgpack_dom.h" #include "rmsgpack.h" #include "bintree.h" -#include "libretrodb_endian.h" #include "query.h" #include "libretrodb.h" @@ -149,7 +149,7 @@ int libretrodb_create(RFILE *fd, libretrodb_value_provider value_provider, if ((rv = rmsgpack_dom_write(fd, &sentinal)) < 0) goto clean; - header.metadata_offset = httobe64(retro_fseek(fd, 0, SEEK_CUR)); + header.metadata_offset = swap_if_little64(retro_fseek(fd, 0, SEEK_CUR)); md.count = item_count; libretrodb_write_metadata(fd, &md); retro_fseek(fd, root, SEEK_SET); @@ -211,7 +211,7 @@ int libretrodb_open(const char *path, libretrodb_t *db) goto error; } - header.metadata_offset = betoht64(header.metadata_offset); + header.metadata_offset = swap_if_little64(header.metadata_offset); retro_fseek(fd, (ssize_t)header.metadata_offset, SEEK_SET); if (libretrodb_read_metadata(fd, &md) < 0) diff --git a/libretro-db/libretrodb_endian.h b/libretro-db/libretrodb_endian.h deleted file mode 100644 index 8ba508f226..0000000000 --- a/libretro-db/libretrodb_endian.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __LIBRETRODB_MSGPACK_ENDIAN_H -#define __LIBRETRODB_MSGPACK_ENDIAN_H - -#include -#include - -#ifndef swap64 -#define swap64(val) \ - ((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \ - | (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \ - | (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \ - | (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \ - | (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \ - | (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \ - | (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \ - | (((uint64_t)(val) & 0xff00000000000000ULL) >> 56)) -#endif - -#define httobe64(x) (is_little_endian() ? swap64(x) : (x)) - -#define betoht64(x) httobe64(x) - -#endif diff --git a/libretro-db/rmsgpack.c b/libretro-db/rmsgpack.c index ce7bbfe751..36d82c3bfd 100644 --- a/libretro-db/rmsgpack.c +++ b/libretro-db/rmsgpack.c @@ -9,7 +9,6 @@ * For more information http://msgpack.org/ */ -#include "rmsgpack.h" #include #ifdef _WIN32 @@ -21,7 +20,9 @@ #include #include -#include "libretrodb_endian.h" +#include + +#include "rmsgpack.h" #define _MPF_FIXMAP 0x80 #define _MPF_MAP16 0xde @@ -327,7 +328,7 @@ int rmsgpack_write_int(RFILE *fd, int64_t value) if (retro_fwrite(fd, &MPF_INT64, sizeof(MPF_INT64)) == -1) goto error; - value = httobe64(value); + value = swap_if_little64(value); if (retro_fwrite(fd, &value, sizeof(int64_t)) == -1) goto error; written += sizeof(int64_t); @@ -379,7 +380,7 @@ int rmsgpack_write_uint(RFILE *fd, uint64_t value) if (retro_fwrite(fd, &MPF_UINT64, sizeof(MPF_UINT64)) == -1) goto error; - value = httobe64(value); + value = swap_if_little64(value); if (retro_fwrite(fd, &value, sizeof(uint64_t)) == -1) goto error; written += sizeof(uint64_t); @@ -409,7 +410,7 @@ static int read_uint(RFILE *fd, uint64_t *out, size_t size) *out = swap_if_little32(tmp); break; case 8: - *out = betoht64(tmp); + *out = swap_if_little64(tmp); break; } return 0; @@ -441,7 +442,7 @@ static int read_int(RFILE *fd, int64_t *out, size_t size) *out = *((int32_t *)(&tmp32)); break; case 8: - tmp64 = betoht64(tmp64); + tmp64 = swap_if_little64(tmp64); *out = *((int64_t *)(&tmp64)); break; }