summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Duncan Ogilvie2024-11-28 15:39:51 +0100
committerGravatar Duncan Ogilvie2024-11-28 15:39:51 +0100
commitb611aa62b8373f6d47bf3528782550a2667cc0f0 (patch)
treee877dd21834b35a880a9306b6c3ba4223ea573a3 /src
parent8f24c4876a32b4f19e459bedd1fdbbc54cb0daa9 (diff)
downloadlibplist-b611aa62b8373f6d47bf3528782550a2667cc0f0.tar.gz
libplist-b611aa62b8373f6d47bf3528782550a2667cc0f0.tar.bz2
Fix compilation on MSVC
Diffstat (limited to 'src')
-rw-r--r--src/bplist.c66
1 files changed, 57 insertions, 9 deletions
diff --git a/src/bplist.c b/src/bplist.c
index 1216974..e543b63 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -87,6 +87,28 @@ union plist_uint_ptr
87 uint64_t *u64ptr; 87 uint64_t *u64ptr;
88}; 88};
89 89
90#ifdef _MSC_VER
91uint64_t get_unaligned_64(uint64_t *ptr)
92{
93 uint64_t temp;
94 memcpy(&temp, ptr, sizeof(temp));
95 return temp;
96}
97
98uint32_t get_unaligned_32(uint32_t *ptr)
99{
100 uint32_t temp;
101 memcpy(&temp, ptr, sizeof(temp));
102 return temp;
103}
104
105uint16_t get_unaligned_16(uint16_t *ptr)
106{
107 uint16_t temp;
108 memcpy(&temp, ptr, sizeof(temp));
109 return temp;
110}
111#else
90#define get_unaligned(ptr) \ 112#define get_unaligned(ptr) \
91 ({ \ 113 ({ \
92 struct __attribute__((packed)) { \ 114 struct __attribute__((packed)) { \
@@ -94,6 +116,7 @@ union plist_uint_ptr
94 } *__p = (void *) (ptr); \ 116 } *__p = (void *) (ptr); \
95 __p->__v; \ 117 __p->__v; \
96 }) 118 })
119#endif
97 120
98 121
99#ifndef bswap16 122#ifndef bswap16
@@ -148,17 +171,31 @@ union plist_uint_ptr
148#define beNtoh(x,n) be64toh((x) << ((8-(n)) << 3)) 171#define beNtoh(x,n) be64toh((x) << ((8-(n)) << 3))
149#endif 172#endif
150 173
174#ifdef _MSC_VER
175static uint64_t UINT_TO_HOST(const void* x, uint8_t n)
176{
177 union plist_uint_ptr __up;
178 __up.src = (n > 8) ? (const char*)x + (n - 8) : x;
179 return (n >= 8 ? be64toh( get_unaligned_64(__up.u64ptr) ) :
180 (n == 4 ? be32toh( get_unaligned_32(__up.u32ptr) ) :
181 (n == 2 ? be16toh( get_unaligned_16(__up.u16ptr) ) :
182 (n == 1 ? *__up.u8ptr :
183 beNtoh( get_unaligned_64(__up.u64ptr), n)
184 ))));
185}
186#else
151#define UINT_TO_HOST(x, n) \ 187#define UINT_TO_HOST(x, n) \
152 ({ \ 188 ({ \
153 union plist_uint_ptr __up; \ 189 union plist_uint_ptr __up; \
154 __up.src = ((n) > 8) ? (x) + ((n) - 8) : (x); \ 190 __up.src = ((n) > 8) ? (const char*)(x) + ((n) - 8) : (x); \
155 ((n) >= 8 ? be64toh( get_unaligned(__up.u64ptr) ) : \ 191 ((n) >= 8 ? be64toh( get_unaligned(__up.u64ptr) ) : \
156 ((n) == 4 ? be32toh( get_unaligned(__up.u32ptr) ) : \ 192 ((n) == 4 ? be32toh( get_unaligned(__up.u32ptr) ) : \
157 ((n) == 2 ? be16toh( get_unaligned(__up.u16ptr) ) : \ 193 ((n) == 2 ? be16toh( get_unaligned(__up.u16ptr) ) : \
158 ((n) == 1 ? *__up.u8ptr : \ 194 ((n) == 1 ? *__up.u8ptr : \
159 beNtoh( get_unaligned(__up.u64ptr), n) \ 195 beNtoh( get_unaligned(__up.u64ptr), n) \
160 )))); \ 196 )))); \
161 }) 197 })
198#endif
162 199
163#define get_needed_bytes(x) \ 200#define get_needed_bytes(x) \
164 ( ((uint64_t)(x)) < (1ULL << 8) ? 1 : \ 201 ( ((uint64_t)(x)) < (1ULL << 8) ? 1 : \
@@ -269,19 +306,30 @@ static plist_t parse_int_node(const char **bnode, uint8_t size)
269static plist_t parse_real_node(const char **bnode, uint8_t size) 306static plist_t parse_real_node(const char **bnode, uint8_t size)
270{ 307{
271 plist_data_t data = plist_new_plist_data(); 308 plist_data_t data = plist_new_plist_data();
272 uint8_t buf[8];
273 309
274 size = 1 << size; // make length less misleading 310 size = 1 << size; // make length less misleading
275 switch (size) 311 switch (size)
276 { 312 {
277 case sizeof(uint32_t): 313 case sizeof(uint32_t):
278 *(uint32_t*)buf = float_bswap32(get_unaligned((uint32_t*)*bnode)); 314 {
279 data->realval = *(float *) buf; 315 uint32_t ival;
280 break; 316 memcpy(&ival, *bnode, sizeof(uint32_t));
317 ival = float_bswap32(ival);
318 float fval;
319 memcpy(&fval, &ival, sizeof(float));
320 data->realval = fval;
321 }
322 break;
323
281 case sizeof(uint64_t): 324 case sizeof(uint64_t):
282 *(uint64_t*)buf = float_bswap64(get_unaligned((uint64_t*)*bnode)); 325 {
283 data->realval = *(double *) buf; 326 uint64_t ival;
327 memcpy(&ival, *bnode, sizeof(uint64_t));
328 ival = float_bswap64(ival);
329 memcpy(&data->realval, &ival, sizeof(double));
284 break; 330 break;
331 }
332
285 default: 333 default:
286 free(data); 334 free(data);
287 PLIST_BIN_ERR("%s: Invalid byte size for real node\n", __func__); 335 PLIST_BIN_ERR("%s: Invalid byte size for real node\n", __func__);
@@ -341,7 +389,7 @@ static char *plist_utf16be_to_utf8(uint16_t *unistr, long len, long *items_read,
341 } 389 }
342 390
343 while (i < len) { 391 while (i < len) {
344 wc = be16toh(get_unaligned(unistr + i)); 392 wc = UINT_TO_HOST(unistr + i, sizeof(wc));
345 i++; 393 i++;
346 if (wc >= 0xD800 && wc <= 0xDBFF) { 394 if (wc >= 0xD800 && wc <= 0xDBFF) {
347 if (!read_lead_surrogate) { 395 if (!read_lead_surrogate) {