summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Brewer <ben.brewer@codethink.co.uk>2014-05-29 15:26:35 (GMT)
committerBen Brewer <ben.brewer@codethink.co.uk>2014-06-02 10:03:00 (GMT)
commitd1173fe9e53269a3e07499f3299019e464ab5774 (patch)
treeb38c682c0d7cecfb2b396b1da00f793e85d78089
parent83990c46886eaecd3b6ad3c4ebbc67757514f8ee (diff)
downloadtbdiff-d1173fe9e53269a3e07499f3299019e464ab5774.tar.gz
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.
-rw-r--r--tbdiff/tbdiff-io.c116
1 files 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 <tbdiff/tbdiff-stat.h>
+#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;
}