diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bplist.c | 71 |
1 files changed, 17 insertions, 54 deletions
diff --git a/src/bplist.c b/src/bplist.c index f1bfcc0..2121ef9 100644 --- a/src/bplist.c +++ b/src/bplist.c | |||
| @@ -916,83 +916,46 @@ static void write_unicode(bytearray_t * bplist, uint16_t * val, uint64_t size) | |||
| 916 | 916 | ||
| 917 | static void write_array(bytearray_t * bplist, node_t* node, hashtable_t* ref_table, uint8_t ref_size) | 917 | static void write_array(bytearray_t * bplist, node_t* node, hashtable_t* ref_table, uint8_t ref_size) |
| 918 | { | 918 | { |
| 919 | uint64_t idx = 0; | ||
| 920 | uint8_t *buff = NULL; | ||
| 921 | |||
| 922 | node_t* cur = NULL; | 919 | node_t* cur = NULL; |
| 923 | uint64_t i = 0; | 920 | uint64_t i = 0; |
| 924 | 921 | ||
| 925 | uint64_t size = node_n_children(node); | 922 | uint64_t size = node_n_children(node); |
| 926 | uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf); | 923 | uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf); |
| 927 | byte_array_append(bplist, &marker, sizeof(uint8_t)); | 924 | byte_array_append(bplist, &marker, sizeof(uint8_t)); |
| 928 | if (size >= 15) | 925 | if (size >= 15) { |
| 929 | { | 926 | write_int(bplist, size); |
| 930 | bytearray_t *int_buff = byte_array_new(); | ||
| 931 | write_int(int_buff, size); | ||
| 932 | byte_array_append(bplist, int_buff->data, int_buff->len); | ||
| 933 | byte_array_free(int_buff); | ||
| 934 | } | 927 | } |
| 935 | 928 | ||
| 936 | buff = (uint8_t *) malloc(size * ref_size); | 929 | for (i = 0, cur = node_first_child(node); cur && i < size; cur = node_next_sibling(cur), i++) { |
| 937 | 930 | uint64_t idx = *(uint64_t *) (hash_table_lookup(ref_table, cur)); | |
| 938 | for (i = 0, cur = node_first_child(node); cur && i < size; cur = node_next_sibling(cur), i++) | 931 | idx = be64toh(idx); |
| 939 | { | 932 | byte_array_append(bplist, (uint8_t*)&idx + (sizeof(uint64_t) - ref_size), ref_size); |
| 940 | idx = *(uint64_t *) (hash_table_lookup(ref_table, cur)); | ||
| 941 | #ifdef __BIG_ENDIAN__ | ||
| 942 | idx = idx << ((sizeof(uint64_t) - ref_size) * 8); | ||
| 943 | #endif | ||
| 944 | memcpy(buff + i * ref_size, &idx, ref_size); | ||
| 945 | byte_convert(buff + i * ref_size, ref_size); | ||
| 946 | } | 933 | } |
| 947 | |||
| 948 | //now append to bplist | ||
| 949 | byte_array_append(bplist, buff, size * ref_size); | ||
| 950 | free(buff); | ||
| 951 | |||
| 952 | } | 934 | } |
| 953 | 935 | ||
| 954 | static void write_dict(bytearray_t * bplist, node_t* node, hashtable_t* ref_table, uint8_t ref_size) | 936 | static void write_dict(bytearray_t * bplist, node_t* node, hashtable_t* ref_table, uint8_t ref_size) |
| 955 | { | 937 | { |
| 956 | uint64_t idx1 = 0; | ||
| 957 | uint64_t idx2 = 0; | ||
| 958 | uint8_t *buff = NULL; | ||
| 959 | |||
| 960 | node_t* cur = NULL; | 938 | node_t* cur = NULL; |
| 961 | uint64_t i = 0; | 939 | uint64_t i = 0; |
| 962 | 940 | ||
| 963 | uint64_t size = node_n_children(node) / 2; | 941 | uint64_t size = node_n_children(node) / 2; |
| 964 | uint8_t marker = BPLIST_DICT | (size < 15 ? size : 0xf); | 942 | uint8_t marker = BPLIST_DICT | (size < 15 ? size : 0xf); |
| 965 | byte_array_append(bplist, &marker, sizeof(uint8_t)); | 943 | byte_array_append(bplist, &marker, sizeof(uint8_t)); |
| 966 | if (size >= 15) | 944 | if (size >= 15) { |
| 967 | { | 945 | write_int(bplist, size); |
| 968 | bytearray_t *int_buff = byte_array_new(); | ||
| 969 | write_int(int_buff, size); | ||
| 970 | byte_array_append(bplist, int_buff->data, int_buff->len); | ||
| 971 | byte_array_free(int_buff); | ||
| 972 | } | 946 | } |
| 973 | 947 | ||
| 974 | buff = (uint8_t *) malloc(size * 2 * ref_size); | 948 | for (i = 0, cur = node_first_child(node); cur && i < size; cur = node_next_sibling(node_next_sibling(cur)), i++) { |
| 975 | for (i = 0, cur = node_first_child(node); cur && i < size; cur = node_next_sibling(node_next_sibling(cur)), i++) | 949 | uint64_t idx1 = *(uint64_t *) (hash_table_lookup(ref_table, cur)); |
| 976 | { | 950 | idx1 = be64toh(idx1); |
| 977 | idx1 = *(uint64_t *) (hash_table_lookup(ref_table, cur)); | 951 | byte_array_append(bplist, (uint8_t*)&idx1 + (sizeof(uint64_t) - ref_size), ref_size); |
| 978 | #ifdef __BIG_ENDIAN__ | ||
| 979 | idx1 = idx1 << ((sizeof(uint64_t) - ref_size) * 8); | ||
| 980 | #endif | ||
| 981 | memcpy(buff + i * ref_size, &idx1, ref_size); | ||
| 982 | byte_convert(buff + i * ref_size, ref_size); | ||
| 983 | |||
| 984 | idx2 = *(uint64_t *) (hash_table_lookup(ref_table, cur->next)); | ||
| 985 | #ifdef __BIG_ENDIAN__ | ||
| 986 | idx2 = idx2 << ((sizeof(uint64_t) - ref_size) * 8); | ||
| 987 | #endif | ||
| 988 | memcpy(buff + (i + size) * ref_size, &idx2, ref_size); | ||
| 989 | byte_convert(buff + (i + size) * ref_size, ref_size); | ||
| 990 | } | 952 | } |
| 991 | 953 | ||
| 992 | //now append to bplist | 954 | for (i = 0, cur = node_first_child(node); cur && i < size; cur = node_next_sibling(node_next_sibling(cur)), i++) { |
| 993 | byte_array_append(bplist, buff, size * 2 * ref_size); | 955 | uint64_t idx2 = *(uint64_t *) (hash_table_lookup(ref_table, cur->next)); |
| 994 | free(buff); | 956 | idx2 = be64toh(idx2); |
| 995 | 957 | byte_array_append(bplist, (uint8_t*)&idx2 + (sizeof(uint64_t) - ref_size), ref_size); | |
| 958 | } | ||
| 996 | } | 959 | } |
| 997 | 960 | ||
| 998 | static void write_uid(bytearray_t * bplist, uint64_t val) | 961 | static void write_uid(bytearray_t * bplist, uint64_t val) |
