diff options
| author | 2024-11-28 15:39:51 +0100 | |
|---|---|---|
| committer | 2024-11-28 15:39:51 +0100 | |
| commit | b611aa62b8373f6d47bf3528782550a2667cc0f0 (patch) | |
| tree | e877dd21834b35a880a9306b6c3ba4223ea573a3 | |
| parent | 8f24c4876a32b4f19e459bedd1fdbbc54cb0daa9 (diff) | |
| download | libplist-b611aa62b8373f6d47bf3528782550a2667cc0f0.tar.gz libplist-b611aa62b8373f6d47bf3528782550a2667cc0f0.tar.bz2 | |
Fix compilation on MSVC
| -rw-r--r-- | src/bplist.c | 66 | ||||
| -rw-r--r-- | tools/plistutil.c | 3 |
2 files changed, 60 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 | ||
| 91 | uint64_t get_unaligned_64(uint64_t *ptr) | ||
| 92 | { | ||
| 93 | uint64_t temp; | ||
| 94 | memcpy(&temp, ptr, sizeof(temp)); | ||
| 95 | return temp; | ||
| 96 | } | ||
| 97 | |||
| 98 | uint32_t get_unaligned_32(uint32_t *ptr) | ||
| 99 | { | ||
| 100 | uint32_t temp; | ||
| 101 | memcpy(&temp, ptr, sizeof(temp)); | ||
| 102 | return temp; | ||
| 103 | } | ||
| 104 | |||
| 105 | uint16_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 | ||
| 175 | static 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) | |||
| 269 | static plist_t parse_real_node(const char **bnode, uint8_t size) | 306 | static 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) { |
diff --git a/tools/plistutil.c b/tools/plistutil.c index 8121a7d..98b440e 100644 --- a/tools/plistutil.c +++ b/tools/plistutil.c | |||
| @@ -32,10 +32,13 @@ | |||
| 32 | #include <string.h> | 32 | #include <string.h> |
| 33 | #include <sys/stat.h> | 33 | #include <sys/stat.h> |
| 34 | #include <errno.h> | 34 | #include <errno.h> |
| 35 | #ifndef _MSC_VER | ||
| 35 | #include <unistd.h> | 36 | #include <unistd.h> |
| 37 | #endif | ||
| 36 | 38 | ||
| 37 | #ifdef _MSC_VER | 39 | #ifdef _MSC_VER |
| 38 | #pragma warning(disable:4996) | 40 | #pragma warning(disable:4996) |
| 41 | #define STDIN_FILENO _fileno(stdin) | ||
| 39 | #endif | 42 | #endif |
| 40 | 43 | ||
| 41 | typedef struct _options | 44 | typedef struct _options |
