diff options
author | irungentoo_trip <irungentoo@gmail.com> | 2014-10-24 18:05:11 -0700 |
---|---|---|
committer | irungentoo_trip <irungentoo@gmail.com> | 2014-10-24 18:05:11 -0700 |
commit | c8812d4734bc92be34df076accb3a8e416a94d9d (patch) | |
tree | 6667d39cb2fcbd9c8e5174e3768ac185fb5c0a35 /toxcore/util.c | |
parent | 9878b441b1d2b175b20d28cc41406280e3cada31 (diff) | |
parent | f73ad4ab05aaf35cd720dffbdc47c28d35d609e1 (diff) |
Merge branch 'big_endian_save'
Diffstat (limited to 'toxcore/util.c')
-rw-r--r-- | toxcore/util.c | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/toxcore/util.c b/toxcore/util.c index ee4fa3b2..3d444b07 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -84,6 +84,35 @@ void host_to_net(uint8_t *num, uint16_t numbytes) | |||
84 | return; | 84 | return; |
85 | } | 85 | } |
86 | 86 | ||
87 | uint16_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 | |||
96 | void host_to_lendian32(uint8_t *dest, uint32_t num) | ||
97 | { | ||
98 | #ifdef WORDS_BIGENDIAN | ||
99 | num = ((num << 8) & 0xFF00FF00 ) | ((num >> 8) & 0xFF00FF ); | ||
100 | num = (num << 16) | (num >> 16); | ||
101 | #endif | ||
102 | memcpy(dest, &num, sizeof(uint32_t)); | ||
103 | } | ||
104 | |||
105 | void lendian_to_host32(uint32_t *dest, const uint8_t *lendian) | ||
106 | { | ||
107 | uint32_t d; | ||
108 | memcpy(&d, lendian, sizeof(uint32_t)); | ||
109 | #ifdef WORDS_BIGENDIAN | ||
110 | d = ((d << 8) & 0xFF00FF00 ) | ((d >> 8) & 0xFF00FF ); | ||
111 | d = (d << 16) | (d >> 16); | ||
112 | #endif | ||
113 | *dest = d; | ||
114 | } | ||
115 | |||
87 | /* state load/save */ | 116 | /* state load/save */ |
88 | int load_state(load_state_callback_func load_state_callback, void *outer, | 117 | int load_state(load_state_callback_func load_state_callback, void *outer, |
89 | const uint8_t *data, uint32_t length, uint16_t cookie_inner) | 118 | const uint8_t *data, uint32_t length, uint16_t cookie_inner) |
@@ -101,8 +130,8 @@ int load_state(load_state_callback_func load_state_callback, void *outer, | |||
101 | uint32_t size_head = sizeof(uint32_t) * 2; | 130 | uint32_t size_head = sizeof(uint32_t) * 2; |
102 | 131 | ||
103 | while (length >= size_head) { | 132 | while (length >= size_head) { |
104 | memcpy(&length_sub, data, sizeof(length_sub)); | 133 | lendian_to_host32(&length_sub, data); |
105 | memcpy(&cookie_type, data + sizeof(length_sub), sizeof(cookie_type)); | 134 | lendian_to_host32(&cookie_type, data + sizeof(length_sub)); |
106 | data += size_head; | 135 | data += size_head; |
107 | length -= size_head; | 136 | length -= size_head; |
108 | 137 | ||
@@ -114,7 +143,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer, | |||
114 | return -1; | 143 | return -1; |
115 | } | 144 | } |
116 | 145 | ||
117 | if ((cookie_type >> 16) != cookie_inner) { | 146 | if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) { |
118 | /* something is not matching up in a bad way, give up */ | 147 | /* something is not matching up in a bad way, give up */ |
119 | #ifdef DEBUG | 148 | #ifdef DEBUG |
120 | fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner); | 149 | fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner); |
@@ -122,7 +151,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer, | |||
122 | return -1; | 151 | return -1; |
123 | } | 152 | } |
124 | 153 | ||
125 | type = cookie_type & 0xFFFF; | 154 | type = lendian_to_host16(cookie_type & 0xFFFF); |
126 | 155 | ||
127 | if (-1 == load_state_callback(outer, data, length_sub, type)) | 156 | if (-1 == load_state_callback(outer, data, length_sub, type)) |
128 | return -1; | 157 | return -1; |