From d1173fe9e53269a3e07499f3299019e464ab5774 Mon Sep 17 00:00:00 2001 From: Ben Brewer Date: Thu, 29 May 2014 16:26:35 +0100 Subject: Simplify endian swapping in tbdiff-io.c Simplify the endian swapping by always calling the endian macro and only implementing it when the platform is big-endain. The endian swapping function has also been made a little more obvious and now uses a byteswap inline function to swap the bytes. --- tbdiff/tbdiff-io.c | 116 +++++++++++++++++++++++------------------------------ 1 file changed, 51 insertions(+), 65 deletions(-) diff --git a/tbdiff/tbdiff-io.c b/tbdiff/tbdiff-io.c index c386a38..db3b437 100644 --- a/tbdiff/tbdiff-io.c +++ b/tbdiff/tbdiff-io.c @@ -21,100 +21,96 @@ #include +#if __STDC_VERSION__ >= 199901L +#define RESTRICT restrict +#elif defined(__GNUC__) +#define RESTRICT __restrict__ +#else +#define RESTRICT +#endif + #if __BYTE_ORDER == __BIG_ENDIAN +static inline void byteswap(char *RESTRICT a, char *RESTRICT b) +{ + char swap = *a; + *a = *b; + *b = swap; +} + /*inverts the indices of an array of bytes. */ -static void byteswap(char* value, int size) { - char tmp; - int i; - for (i = 0; i < size/2; i++) { - tmp = value[i]; - value[i] = value[size-i-1]; - value[size-i-1] = tmp; - } +static inline void endianswap(void* value, size_t size) +{ + + char* cvalue = value; + int i, j; + for (i = 0, j = (size - 1); i < (size / 2); i++, j--) + byteswap(&cvalue[i], &cvalue[j]); } -#endif -size_t tbd_write_uint16_t(uint16_t value, FILE* stream) { -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&value, sizeof(value)); +#define ENDIANSWAP(v) endianswap(v, sizeof(*v)) +#else +#define ENDIANSWAP(v) #endif + +size_t tbd_write_uint16_t(uint16_t value, FILE *stream) { + ENDIANSWAP(&value); return fwrite(&value, sizeof(value), 1, stream); } -size_t tbd_write_uint32_t(uint32_t value, FILE* stream) { -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&value, sizeof(value)); -#endif +size_t tbd_write_uint32_t(uint32_t value, FILE *stream) { + ENDIANSWAP(&value); return fwrite(&value, sizeof(value), 1, stream); } -size_t tbd_write_uint64_t(uint64_t value, FILE* stream) { -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&value, sizeof(value)); -#endif +size_t tbd_write_uint64_t(uint64_t value, FILE *stream) { + ENDIANSWAP(&value); return fwrite(&value, sizeof(value), 1, stream); } -size_t tbd_write_time_t(time_t value, FILE* stream) { +size_t tbd_write_time_t(time_t value, FILE *stream) { uint64_t realv = value; -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&realv, sizeof(realv)); -#endif + ENDIANSWAP(&realv); return fwrite(&realv, sizeof(realv), 1, stream); } -size_t tbd_write_mode_t(mode_t value, FILE* stream) { -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&value, sizeof(value)); -#endif +size_t tbd_write_mode_t(mode_t value, FILE *stream) { + ENDIANSWAP(&value); return fwrite(&value, sizeof(value), 1, stream); } -size_t tbd_write_uid_t(uid_t value, FILE* stream) { -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&value, sizeof(value)); -#endif +size_t tbd_write_uid_t(uid_t value, FILE *stream) { + ENDIANSWAP(&value); return fwrite(&value, sizeof(value), 1, stream); } -size_t tbd_write_gid_t(gid_t value, FILE* stream) { -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&value, sizeof(value)); -#endif +size_t tbd_write_gid_t(gid_t value, FILE *stream) { + ENDIANSWAP(&value); return fwrite(&value, sizeof(value), 1, stream); } -size_t tbd_write_size_t(size_t value, FILE* stream) { -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&value, sizeof(value)); -#endif +size_t tbd_write_size_t(size_t value, FILE *stream) { + ENDIANSWAP(&value); return fwrite(&value, sizeof(value), 1, stream); } -size_t tbd_read_uint16_t(uint16_t *value, FILE* stream) { +size_t tbd_read_uint16_t(uint16_t *value, FILE *stream) { assert(value != NULL); size_t rval = fread(value, sizeof(*value), 1, stream); -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)value, sizeof(*value)); -#endif + ENDIANSWAP(value); return rval; } size_t tbd_read_uint32_t(uint32_t *value, FILE *stream) { assert(value != NULL); size_t rval = fread(value, sizeof(*value), 1, stream); -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)value, sizeof(*value)); -#endif + ENDIANSWAP(value); return rval; } size_t tbd_read_uint64_t(uint64_t *value, FILE *stream) { assert(value != NULL); size_t rval = fread(value, sizeof(*value), 1, stream); -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)value, sizeof(*value)); -#endif + ENDIANSWAP(value); return rval; } @@ -122,9 +118,7 @@ size_t tbd_read_time_t(time_t *value, FILE *stream) { assert(value != NULL); uint64_t realv; size_t rval = fread(&realv, sizeof(realv), 1, stream); -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)&realv, sizeof(realv)); -#endif + ENDIANSWAP(&realv); *value = realv; return rval; } @@ -132,35 +126,27 @@ size_t tbd_read_time_t(time_t *value, FILE *stream) { size_t tbd_read_mode_t(mode_t *value, FILE *stream) { assert(value != NULL); size_t rval = fread(value, sizeof(*value), 1, stream); -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)value, sizeof(*value)); -#endif + ENDIANSWAP(value); return rval; } size_t tbd_read_uid_t(uid_t *value, FILE *stream) { assert(value != NULL); size_t rval = fread(value, sizeof(*value), 1, stream); -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)value, sizeof(*value)); -#endif + ENDIANSWAP(value); return rval; } size_t tbd_read_gid_t(gid_t *value, FILE *stream) { assert(value != NULL); size_t rval = fread(value, sizeof(*value), 1, stream); -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)value, sizeof(*value)); -#endif + ENDIANSWAP(value); return rval; } size_t tbd_read_size_t(size_t *value, FILE *stream) { assert(value != NULL); size_t rval = fread(value, sizeof(*value), 1, stream); -#if __BYTE_ORDER == __BIG_ENDIAN - byteswap((char*)value, sizeof(*value)); -#endif + ENDIANSWAP(value); return rval; } -- cgit v1.2.1