(libretro-db) Use retro_endianness.h functions completely

This commit is contained in:
twinaphex 2015-09-21 11:37:31 +02:00
parent 0d16768688
commit 9093cd4aba
4 changed files with 52 additions and 32 deletions

View File

@ -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

View File

@ -12,13 +12,13 @@
#include <stdlib.h>
#include <retro_file.h>
#include <retro_endianness.h>
#include <compat/strl.h>
#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)

View File

@ -1,23 +0,0 @@
#ifndef __LIBRETRODB_MSGPACK_ENDIAN_H
#define __LIBRETRODB_MSGPACK_ENDIAN_H
#include <stdint.h>
#include <retro_endianness.h>
#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

View File

@ -9,7 +9,6 @@
* For more information http://msgpack.org/
*/
#include "rmsgpack.h"
#include <stdlib.h>
#ifdef _WIN32
@ -21,7 +20,9 @@
#include <stdint.h>
#include <string.h>
#include "libretrodb_endian.h"
#include <retro_endianness.h>
#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;
}