1 |
From 962d4064b690af1c479f7676b3e36f08eaa593c6 Mon Sep 17 00:00:00 2001 |
2 |
From: Nikias Bassen <nikias@gmx.li> |
3 |
Date: Sat, 28 Jan 2017 04:45:35 +0100 |
4 |
Subject: [PATCH] bplist: Improve writing of integer nodes |
5 |
|
6 |
--- |
7 |
src/bplist.c | 33 ++++++++++++--------------------- |
8 |
1 file changed, 12 insertions(+), 21 deletions(-) |
9 |
|
10 |
diff --git a/src/bplist.c b/src/bplist.c |
11 |
index a2fb9be..ed3c0b9 100644 |
12 |
--- a/src/bplist.c |
13 |
+++ b/src/bplist.c |
14 |
@@ -837,36 +837,27 @@ static void serialize_plist(node_t* node, void* data) |
15 |
|
16 |
static void write_int(bytearray_t * bplist, uint64_t val) |
17 |
{ |
18 |
- uint64_t size = get_needed_bytes(val); |
19 |
- uint8_t *buff = NULL; |
20 |
+ int size = get_needed_bytes(val); |
21 |
+ uint8_t sz; |
22 |
//do not write 3bytes int node |
23 |
if (size == 3) |
24 |
size++; |
25 |
+ sz = BPLIST_UINT | Log2(size); |
26 |
|
27 |
-#ifdef __BIG_ENDIAN__ |
28 |
- val = val << ((sizeof(uint64_t) - size) * 8); |
29 |
-#endif |
30 |
- |
31 |
- buff = (uint8_t *) malloc(sizeof(uint8_t) + size); |
32 |
- buff[0] = BPLIST_UINT | Log2(size); |
33 |
- memcpy(buff + 1, &val, size); |
34 |
- byte_convert(buff + 1, size); |
35 |
- byte_array_append(bplist, buff, sizeof(uint8_t) + size); |
36 |
- free(buff); |
37 |
+ val = be64toh(val); |
38 |
+ byte_array_append(bplist, &sz, 1); |
39 |
+ byte_array_append(bplist, (uint8_t*)&val + (8-size), size); |
40 |
} |
41 |
|
42 |
static void write_uint(bytearray_t * bplist, uint64_t val) |
43 |
{ |
44 |
- uint64_t size = 16; |
45 |
- uint8_t *buff = NULL; |
46 |
+ uint8_t sz = BPLIST_UINT | 4; |
47 |
+ uint64_t zero = 0; |
48 |
|
49 |
- buff = (uint8_t *) malloc(sizeof(uint8_t) + size); |
50 |
- buff[0] = BPLIST_UINT | 4; |
51 |
- memset(buff + 1, '\0', 8); |
52 |
- memcpy(buff + 9, &val, 8); |
53 |
- byte_convert(buff + 9, 8); |
54 |
- byte_array_append(bplist, buff, sizeof(uint8_t) + size); |
55 |
- free(buff); |
56 |
+ val = be64toh(val); |
57 |
+ byte_array_append(bplist, &sz, 1); |
58 |
+ byte_array_append(bplist, &zero, sizeof(uint64_t)); |
59 |
+ byte_array_append(bplist, &val, sizeof(uint64_t)); |
60 |
} |
61 |
|
62 |
static void write_real(bytearray_t * bplist, double val) |