diff options
| author | 2009-01-04 20:56:05 +0100 | |
|---|---|---|
| committer | 2009-01-04 20:56:05 +0100 | |
| commit | a835ed062afea72a2c50fc002b0467925f8d9c1b (patch) | |
| tree | 23a2bc7cfd487a568013c9cee7e78e46afa9ad02 /src | |
| parent | a3c6acf863f1a4deef76b0075c36f20de8d31197 (diff) | |
| download | libplist-a835ed062afea72a2c50fc002b0467925f8d9c1b.tar.gz libplist-a835ed062afea72a2c50fc002b0467925f8d9c1b.tar.bz2 | |
handle date tag.
Diffstat (limited to 'src')
| -rw-r--r-- | src/bplist.c | 43 | ||||
| -rw-r--r-- | src/plist.h | 1 | ||||
| -rw-r--r-- | src/xplist.c | 11 |
3 files changed, 43 insertions, 12 deletions
diff --git a/src/bplist.c b/src/bplist.c index 7a7225d..cbe53cd 100644 --- a/src/bplist.c +++ b/src/bplist.c | |||
| @@ -74,7 +74,7 @@ static void byte_convert(uint8_t *address, size_t size) | |||
| 74 | #endif | 74 | #endif |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | #define swap_n_bytes(x, n) \ | 77 | #define UINT_TO_HOST(x, n) \ |
| 78 | (n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \ | 78 | (n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \ |
| 79 | (n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \ | 79 | (n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \ |
| 80 | (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : \ | 80 | (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : \ |
| @@ -104,7 +104,7 @@ static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) | |||
| 104 | case sizeof(uint32_t): | 104 | case sizeof(uint32_t): |
| 105 | case sizeof(uint64_t): | 105 | case sizeof(uint64_t): |
| 106 | memcpy(&data->intval, bnode, size); | 106 | memcpy(&data->intval, bnode, size); |
| 107 | data->intval = swap_n_bytes(&data->intval, size); | 107 | data->intval = UINT_TO_HOST(&data->intval, size); |
| 108 | break; | 108 | break; |
| 109 | default: | 109 | default: |
| 110 | free(data); | 110 | free(data); |
| @@ -124,7 +124,7 @@ static plist_t parse_real_node(char *bnode, uint8_t size) | |||
| 124 | switch (size) { | 124 | switch (size) { |
| 125 | case sizeof(float): | 125 | case sizeof(float): |
| 126 | case sizeof(double): | 126 | case sizeof(double): |
| 127 | data->realval = swap_n_bytes(bnode, size); | 127 | data->intval = UINT_TO_HOST(bnode, size); //use the fact that we have an union to cheat byte swapping |
| 128 | break; | 128 | break; |
| 129 | default: | 129 | default: |
| 130 | free(data); | 130 | free(data); |
| @@ -134,6 +134,18 @@ static plist_t parse_real_node(char *bnode, uint8_t size) | |||
| 134 | return g_node_new(data); | 134 | return g_node_new(data); |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | static plist_t parse_date_node(char *bnode, uint8_t size) | ||
| 138 | { | ||
| 139 | plist_t node = parse_real_node(bnode, size); | ||
| 140 | plist_data_t data = plist_get_data(node); | ||
| 141 | |||
| 142 | double time_real = data->realval; | ||
| 143 | data->timeval.tv_sec = (glong)time_real; | ||
| 144 | data->timeval.tv_usec = (time_real - (glong)time_real) * G_USEC_PER_SEC; | ||
| 145 | data->type = PLIST_DATE; | ||
| 146 | return node; | ||
| 147 | } | ||
| 148 | |||
| 137 | static plist_t parse_string_node(char *bnode, uint8_t size) | 149 | static plist_t parse_string_node(char *bnode, uint8_t size) |
| 138 | { | 150 | { |
| 139 | plist_data_t data = plist_new_plist_data(); | 151 | plist_data_t data = plist_new_plist_data(); |
| @@ -241,7 +253,7 @@ static plist_t parse_bin_node(char *object, uint8_t dict_size, char **next_objec | |||
| 241 | if (3 != size) | 253 | if (3 != size) |
| 242 | return NULL; | 254 | return NULL; |
| 243 | else | 255 | else |
| 244 | return parse_real_node(object, size); | 256 | return parse_date_node(object, size); |
| 245 | 257 | ||
| 246 | case BPLIST_DATA: | 258 | case BPLIST_DATA: |
| 247 | if (0x0F == size) { | 259 | if (0x0F == size) { |
| @@ -378,7 +390,7 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) | |||
| 378 | uint64_t current_offset = 0; | 390 | uint64_t current_offset = 0; |
| 379 | const char *offset_table = plist_bin + offset_table_index; | 391 | const char *offset_table = plist_bin + offset_table_index; |
| 380 | for (i = 0; i < num_objects; i++) { | 392 | for (i = 0; i < num_objects; i++) { |
| 381 | current_offset = swap_n_bytes(offset_table + i * offset_size, offset_size); | 393 | current_offset = UINT_TO_HOST(offset_table + i * offset_size, offset_size); |
| 382 | 394 | ||
| 383 | log_debug_msg("parse_nodes: current_offset = %i\n", current_offset); | 395 | log_debug_msg("parse_nodes: current_offset = %i\n", current_offset); |
| 384 | char *obj = plist_bin + current_offset; | 396 | char *obj = plist_bin + current_offset; |
| @@ -402,8 +414,8 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) | |||
| 402 | str_i = j * dict_param_size; | 414 | str_i = j * dict_param_size; |
| 403 | str_j = (j + data->length) * dict_param_size; | 415 | str_j = (j + data->length) * dict_param_size; |
| 404 | 416 | ||
| 405 | index1 = swap_n_bytes(data->buff + str_i, dict_param_size); | 417 | index1 = UINT_TO_HOST(data->buff + str_i, dict_param_size); |
| 406 | index2 = swap_n_bytes(data->buff + str_j, dict_param_size); | 418 | index2 = UINT_TO_HOST(data->buff + str_j, dict_param_size); |
| 407 | 419 | ||
| 408 | //first one is actually a key | 420 | //first one is actually a key |
| 409 | plist_get_data(nodeslist[index1])->type = PLIST_KEY; | 421 | plist_get_data(nodeslist[index1])->type = PLIST_KEY; |
| @@ -430,7 +442,7 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) | |||
| 430 | log_debug_msg("parse_nodes: array found\n"); | 442 | log_debug_msg("parse_nodes: array found\n"); |
| 431 | for (j = 0; j < data->length; j++) { | 443 | for (j = 0; j < data->length; j++) { |
| 432 | str_j = j * dict_param_size; | 444 | str_j = j * dict_param_size; |
| 433 | index1 = swap_n_bytes(data->buff + str_j, dict_param_size); | 445 | index1 = UINT_TO_HOST(data->buff + str_j, dict_param_size); |
| 434 | 446 | ||
| 435 | if (index1 < num_objects) { | 447 | if (index1 < num_objects) { |
| 436 | if (G_NODE_IS_ROOT(nodeslist[index1])) | 448 | if (G_NODE_IS_ROOT(nodeslist[index1])) |
| @@ -463,7 +475,7 @@ static guint plist_data_hash(gconstpointer key) | |||
| 463 | case PLIST_BOOLEAN: | 475 | case PLIST_BOOLEAN: |
| 464 | case PLIST_UINT: | 476 | case PLIST_UINT: |
| 465 | case PLIST_REAL: | 477 | case PLIST_REAL: |
| 466 | buff = (char *) &data->intval; | 478 | buff = (char *) &data->intval; //works also for real as we use an union |
| 467 | size = 8; | 479 | size = 8; |
| 468 | break; | 480 | break; |
| 469 | case PLIST_KEY: | 481 | case PLIST_KEY: |
| @@ -604,6 +616,17 @@ static void write_real(GByteArray * bplist, double val) | |||
| 604 | free(buff); | 616 | free(buff); |
| 605 | } | 617 | } |
| 606 | 618 | ||
| 619 | static void write_date(GByteArray * bplist, double val) | ||
| 620 | { | ||
| 621 | uint64_t size = 8; //dates always use 8 bytes | ||
| 622 | uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); | ||
| 623 | buff[0] = BPLIST_DATE | Log2(size); | ||
| 624 | memcpy(buff + 1, &val, size); | ||
| 625 | byte_convert(buff + 1, size); | ||
| 626 | g_byte_array_append(bplist, buff, sizeof(uint8_t) + size); | ||
| 627 | free(buff); | ||
| 628 | } | ||
| 629 | |||
| 607 | static void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uint64_t size) | 630 | static void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uint64_t size) |
| 608 | { | 631 | { |
| 609 | uint8_t marker = mark | (size < 15 ? size : 0xf); | 632 | uint8_t marker = mark | (size < 15 ? size : 0xf); |
| @@ -764,7 +787,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) | |||
| 764 | write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); | 787 | write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size); |
| 765 | break; | 788 | break; |
| 766 | case PLIST_DATE: | 789 | case PLIST_DATE: |
| 767 | //TODO | 790 | write_date(bplist_buff, data->timeval.tv_sec + (double)data->timeval.tv_usec / G_USEC_PER_SEC ); |
| 768 | break; | 791 | break; |
| 769 | default: | 792 | default: |
| 770 | break; | 793 | break; |
diff --git a/src/plist.h b/src/plist.h index 8428597..bd12e3a 100644 --- a/src/plist.h +++ b/src/plist.h | |||
| @@ -44,6 +44,7 @@ struct plist_data_s { | |||
| 44 | char *strval; | 44 | char *strval; |
| 45 | wchar_t *unicodeval; | 45 | wchar_t *unicodeval; |
| 46 | uint8_t *buff; | 46 | uint8_t *buff; |
| 47 | GTimeVal timeval; | ||
| 47 | }; | 48 | }; |
| 48 | uint64_t length; | 49 | uint64_t length; |
| 49 | plist_type type; | 50 | plist_type type; |
diff --git a/src/xplist.c b/src/xplist.c index 3487f96..29d4e46 100644 --- a/src/xplist.c +++ b/src/xplist.c | |||
| @@ -188,7 +188,10 @@ static void node_to_xml(GNode * node, gpointer xml_struct) | |||
| 188 | tag = XPLIST_DICT; | 188 | tag = XPLIST_DICT; |
| 189 | isStruct = TRUE; | 189 | isStruct = TRUE; |
| 190 | break; | 190 | break; |
| 191 | case PLIST_DATE: //TODO : handle date tag | 191 | case PLIST_DATE: |
| 192 | tag = XPLIST_DATE; | ||
| 193 | val = g_time_val_to_iso8601(&node_data->timeval); | ||
| 194 | break; | ||
| 192 | default: | 195 | default: |
| 193 | break; | 196 | break; |
| 194 | } | 197 | } |
| @@ -268,8 +271,12 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) | |||
| 268 | continue; | 271 | continue; |
| 269 | } | 272 | } |
| 270 | 273 | ||
| 271 | if (!xmlStrcmp(node->name, XPLIST_DATE)) | 274 | if (!xmlStrcmp(node->name, XPLIST_DATE)) { |
| 275 | g_time_val_from_iso8601((char*)xmlNodeGetContent(node), &data->timeval); | ||
| 276 | data->type = PLIST_DATE; | ||
| 277 | data->length = sizeof(GTimeVal); | ||
| 272 | continue; //TODO : handle date tag | 278 | continue; //TODO : handle date tag |
| 279 | } | ||
| 273 | 280 | ||
| 274 | if (!xmlStrcmp(node->name, XPLIST_STRING)) { | 281 | if (!xmlStrcmp(node->name, XPLIST_STRING)) { |
| 275 | data->strval = strdup( (char*) xmlNodeGetContent(node)); | 282 | data->strval = strdup( (char*) xmlNodeGetContent(node)); |
