From 31c17856b8ba6c286cb86a2f7284bf4430eea01d Mon Sep 17 00:00:00 2001 From: irungentoo_trip Date: Wed, 22 Oct 2014 21:56:52 -0400 Subject: Load file portability code for big endian. Warning: only loads, doesn't save. --- toxcore/util.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/toxcore/util.c b/toxcore/util.c index ee4fa3b2..23e19290 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -84,6 +84,26 @@ void host_to_net(uint8_t *num, uint16_t numbytes) return; } +uint16_t lendian_to_host16(uint16_t lendian) +{ +#ifdef WORDS_BIGENDIAN + return (lendian << 8) | (lendian >> 8 ); +#else + return lendian; +#endif +} + +void lendian_to_host32(uint32_t *dest, const uint8_t *lendian) +{ + uint32_t d; + memcpy(&d, lendian, sizeof(uint32_t)); +#ifdef WORDS_BIGENDIAN + d = ((d << 8) & 0xFF00FF00 ) | ((d >> 8) & 0xFF00FF ); + d = (d << 16) | (d >> 16); +#endif + *dest = d; +} + /* state load/save */ int load_state(load_state_callback_func load_state_callback, void *outer, const uint8_t *data, uint32_t length, uint16_t cookie_inner) @@ -101,8 +121,8 @@ int load_state(load_state_callback_func load_state_callback, void *outer, uint32_t size_head = sizeof(uint32_t) * 2; while (length >= size_head) { - memcpy(&length_sub, data, sizeof(length_sub)); - memcpy(&cookie_type, data + sizeof(length_sub), sizeof(cookie_type)); + lendian_to_host32(&length_sub, data); + lendian_to_host32(&cookie_type, data + sizeof(length_sub)); data += size_head; length -= size_head; @@ -114,7 +134,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer, return -1; } - if ((cookie_type >> 16) != cookie_inner) { + if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) { /* something is not matching up in a bad way, give up */ #ifdef DEBUG fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner); @@ -122,7 +142,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer, return -1; } - type = cookie_type & 0xFFFF; + type = lendian_to_host16(cookie_type & 0xFFFF); if (-1 == load_state_callback(outer, data, length_sub, type)) return -1; -- cgit v1.2.3 From f73ad4ab05aaf35cd720dffbdc47c28d35d609e1 Mon Sep 17 00:00:00 2001 From: irungentoo_trip Date: Fri, 24 Oct 2014 18:04:27 -0700 Subject: Portability saving fixes for big endian systems. Saves should now be portable from big endian to little endian systems though I need someone to actually test it to be sure I didn't mess up. --- toxcore/Messenger.c | 5 ++--- toxcore/util.c | 9 +++++++++ toxcore/util.h | 5 +++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 4841cbd9..eaf87179 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2648,10 +2648,9 @@ uint32_t messenger_size(const Messenger *m) static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) { - memcpy(data, &len, sizeof(uint32_t)); + host_to_lendian32(data, len); data += sizeof(uint32_t); - uint32_t temp = (MESSENGER_STATE_COOKIE_TYPE << 16) | type; - memcpy(data, &temp, sizeof(uint32_t)); + host_to_lendian32(data, (host_tolendian16(MESSENGER_STATE_COOKIE_TYPE) << 16) | host_tolendian16(type)); data += sizeof(uint32_t); return data; } diff --git a/toxcore/util.c b/toxcore/util.c index 23e19290..3d444b07 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -93,6 +93,15 @@ uint16_t lendian_to_host16(uint16_t lendian) #endif } +void host_to_lendian32(uint8_t *dest, uint32_t num) +{ +#ifdef WORDS_BIGENDIAN + num = ((num << 8) & 0xFF00FF00 ) | ((num >> 8) & 0xFF00FF ); + num = (num << 16) | (num >> 16); +#endif + memcpy(dest, &num, sizeof(uint32_t)); +} + void lendian_to_host32(uint32_t *dest, const uint8_t *lendian) { uint32_t d; diff --git a/toxcore/util.h b/toxcore/util.h index 7992a985..e90eee0f 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -42,6 +42,11 @@ uint32_t id_copy(uint8_t *dest, const uint8_t *src); /* return value is CLIENT_I void host_to_net(uint8_t *num, uint16_t numbytes); #define net_to_host(x, y) host_to_net(x, y) +uint16_t lendian_to_host16(uint16_t lendian); +#define host_tolendian16(x) lendian_to_host16(x) + +void host_to_lendian32(uint8_t *dest, uint32_t num); + /* state load/save */ typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type); int load_state(load_state_callback_func load_state_callback, void *outer, -- cgit v1.2.3