summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2017-01-28 04:45:35 +0100
committerGravatar Nikias Bassen2017-01-28 04:45:35 +0100
commit962d4064b690af1c479f7676b3e36f08eaa593c6 (patch)
treefe49aaf13ef250651f3de8da20606756d0f805b3
parent458341fcc360db6e0bac0619e2512e6050f4e8c7 (diff)
downloadlibplist-962d4064b690af1c479f7676b3e36f08eaa593c6.tar.gz
libplist-962d4064b690af1c479f7676b3e36f08eaa593c6.tar.bz2
bplist: Improve writing of integer nodes
-rw-r--r--src/bplist.c33
1 files changed, 12 insertions, 21 deletions
diff --git a/src/bplist.c b/src/bplist.c
index a2fb9be..ed3c0b9 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -837,36 +837,27 @@ static void serialize_plist(node_t* node, void* data)
static void write_int(bytearray_t * bplist, uint64_t val)
{
- uint64_t size = get_needed_bytes(val);
- uint8_t *buff = NULL;
+ int size = get_needed_bytes(val);
+ uint8_t sz;
//do not write 3bytes int node
if (size == 3)
size++;
+ sz = BPLIST_UINT | Log2(size);
-#ifdef __BIG_ENDIAN__
- val = val << ((sizeof(uint64_t) - size) * 8);
-#endif
-
- buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
- buff[0] = BPLIST_UINT | Log2(size);
- memcpy(buff + 1, &val, size);
- byte_convert(buff + 1, size);
- byte_array_append(bplist, buff, sizeof(uint8_t) + size);
- free(buff);
+ val = be64toh(val);
+ byte_array_append(bplist, &sz, 1);
+ byte_array_append(bplist, (uint8_t*)&val + (8-size), size);
}
static void write_uint(bytearray_t * bplist, uint64_t val)
{
- uint64_t size = 16;
- uint8_t *buff = NULL;
+ uint8_t sz = BPLIST_UINT | 4;
+ uint64_t zero = 0;
- buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
- buff[0] = BPLIST_UINT | 4;
- memset(buff + 1, '\0', 8);
- memcpy(buff + 9, &val, 8);
- byte_convert(buff + 9, 8);
- byte_array_append(bplist, buff, sizeof(uint8_t) + size);
- free(buff);
+ val = be64toh(val);
+ byte_array_append(bplist, &sz, 1);
+ byte_array_append(bplist, &zero, sizeof(uint64_t));
+ byte_array_append(bplist, &val, sizeof(uint64_t));
}
static void write_real(bytearray_t * bplist, double val)