summaryrefslogtreecommitdiff
path: root/toxcore/util.c
diff options
context:
space:
mode:
authorirungentoo_trip <irungentoo@gmail.com>2014-10-22 21:56:52 -0400
committerirungentoo_trip <irungentoo@gmail.com>2014-10-22 21:56:52 -0400
commit31c17856b8ba6c286cb86a2f7284bf4430eea01d (patch)
tree7fbc6f2bbc5aeba8ed564ce03fe1d6e74ab21025 /toxcore/util.c
parent3c874bcf62ab4e453d745547512bd8a0835d3bcb (diff)
Load file portability code for big endian.
Warning: only loads, doesn't save.
Diffstat (limited to 'toxcore/util.c')
-rw-r--r--toxcore/util.c28
1 files 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)
84 return; 84 return;
85} 85}
86 86
87uint16_t lendian_to_host16(uint16_t lendian)
88{
89#ifdef WORDS_BIGENDIAN
90 return (lendian << 8) | (lendian >> 8 );
91#else
92 return lendian;
93#endif
94}
95
96void lendian_to_host32(uint32_t *dest, const uint8_t *lendian)
97{
98 uint32_t d;
99 memcpy(&d, lendian, sizeof(uint32_t));
100#ifdef WORDS_BIGENDIAN
101 d = ((d << 8) & 0xFF00FF00 ) | ((d >> 8) & 0xFF00FF );
102 d = (d << 16) | (d >> 16);
103#endif
104 *dest = d;
105}
106
87/* state load/save */ 107/* state load/save */
88int load_state(load_state_callback_func load_state_callback, void *outer, 108int load_state(load_state_callback_func load_state_callback, void *outer,
89 const uint8_t *data, uint32_t length, uint16_t cookie_inner) 109 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,
101 uint32_t size_head = sizeof(uint32_t) * 2; 121 uint32_t size_head = sizeof(uint32_t) * 2;
102 122
103 while (length >= size_head) { 123 while (length >= size_head) {
104 memcpy(&length_sub, data, sizeof(length_sub)); 124 lendian_to_host32(&length_sub, data);
105 memcpy(&cookie_type, data + sizeof(length_sub), sizeof(cookie_type)); 125 lendian_to_host32(&cookie_type, data + sizeof(length_sub));
106 data += size_head; 126 data += size_head;
107 length -= size_head; 127 length -= size_head;
108 128
@@ -114,7 +134,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
114 return -1; 134 return -1;
115 } 135 }
116 136
117 if ((cookie_type >> 16) != cookie_inner) { 137 if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) {
118 /* something is not matching up in a bad way, give up */ 138 /* something is not matching up in a bad way, give up */
119#ifdef DEBUG 139#ifdef DEBUG
120 fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner); 140 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,
122 return -1; 142 return -1;
123 } 143 }
124 144
125 type = cookie_type & 0xFFFF; 145 type = lendian_to_host16(cookie_type & 0xFFFF);
126 146
127 if (-1 == load_state_callback(outer, data, length_sub, type)) 147 if (-1 == load_state_callback(outer, data, length_sub, type))
128 return -1; 148 return -1;