diff options
author | Jonathan Beck | 2009-02-15 17:15:29 +0100 |
---|---|---|
committer | Jonathan Beck | 2009-02-15 17:15:29 +0100 |
commit | bb3097cb2266b55719b955c93d09a0e2d6f8eccb (patch) | |
tree | 478a6dd7b31b1640d79bc645052fc9843cc74ca1 /src | |
parent | 8e9eb83c2a8cd3b6a6d1943043f1d3b674e82de4 (diff) | |
download | libplist-bb3097cb2266b55719b955c93d09a0e2d6f8eccb.tar.gz libplist-bb3097cb2266b55719b955c93d09a0e2d6f8eccb.tar.bz2 |
Add more regression test and fix Integer and Real type handling.
Diffstat (limited to 'src')
-rw-r--r-- | src/bplist.c | 80 | ||||
-rw-r--r-- | src/plist.c | 67 | ||||
-rw-r--r-- | src/plist.h | 2 | ||||
-rw-r--r-- | src/xplist.c | 4 |
4 files changed, 86 insertions, 67 deletions
diff --git a/src/bplist.c b/src/bplist.c index 95070d7..cf9b9c6 100644 --- a/src/bplist.c +++ b/src/bplist.c @@ -87,7 +87,7 @@ static uint32_t uint24_from_be(char *buff) #define UINT_TO_HOST(x, n) \ (n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \ (n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \ - (n == 3 ? uint24_from_be( x ) : \ + (n == 3 ? uint24_from_be( (char*)x ) : \ (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : \ *(uint8_t *)(x) )))) @@ -99,10 +99,7 @@ static uint32_t uint24_from_be(char *buff) ( ((uint64_t)x) < (1ULL << 24) ? 3 : \ ( ((uint64_t)x) < (1ULL << 32) ? 4 : 8)))) -#define get_real_bytes(x) (x >> 32 ? 4 : 8) - - - +#define get_real_bytes(x) (x == (float) x ? 4 : 8) static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) @@ -133,12 +130,18 @@ static plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) static plist_t parse_real_node(char *bnode, uint8_t size) { plist_data_t data = plist_new_plist_data(); + float floatval = 0.0; size = 1 << size; // make length less misleading switch (size) { case sizeof(float): + floatval = *(float*)bnode; + byte_convert((uint8_t*)&floatval, sizeof(float)); + data->realval = floatval; + break; case sizeof(double): - data->intval = UINT_TO_HOST(bnode, size); //use the fact that we have an union to cheat byte swapping + data->realval = *(double*)bnode; + byte_convert((uint8_t*)&(data->realval), sizeof(double)); break; default: free(data); @@ -187,7 +190,7 @@ static plist_t parse_unicode_node(char *bnode, uint64_t size) data->unicodeval[sizeof(gunichar2) * size] = '\0'; data->length = size; for (i = 0; i <= size; i++) - byte_convert(data->unicodeval + i, sizeof(gunichar2)); + byte_convert((uint8_t*)(data->unicodeval + i), sizeof(gunichar2)); return g_node_new(data); } @@ -543,63 +546,7 @@ static guint plist_data_hash(gconstpointer key) return hash; } -static gboolean plist_data_compare(gconstpointer a, gconstpointer b) -{ - plist_data_t val_a = NULL; - plist_data_t val_b = NULL; - if (!a || !b) - return FALSE; - - if (!((GNode *) a)->data || !((GNode *) b)->data) - return FALSE; - - val_a = plist_get_data((plist_t) a); - val_b = plist_get_data((plist_t) b); - - if (val_a->type != val_b->type) - return FALSE; - - switch (val_a->type) { - case PLIST_BOOLEAN: - case PLIST_UINT: - case PLIST_REAL: - if (val_a->intval == val_b->intval) //it is an union so this is sufficient - return TRUE; - else - return FALSE; - - case PLIST_KEY: - case PLIST_STRING: - if (!strcmp(val_a->strval, val_b->strval)) - return TRUE; - else - return FALSE; - case PLIST_UNICODE: - if (!memcmp(val_a->unicodeval, val_b->unicodeval, val_a->length)) - return TRUE; - else - return FALSE; - - case PLIST_DATA: - case PLIST_ARRAY: - case PLIST_DICT: - //compare pointer - if (a == b) - return TRUE; - else - return FALSE; - break; - case PLIST_DATE: - if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(GTimeVal))) - return TRUE; - else - return FALSE; - default: - break; - } - return FALSE; -} struct serialize_s { GPtrArray *objects; @@ -655,7 +602,12 @@ static void write_real(GByteArray * bplist, double val) uint64_t size = get_real_bytes(*((uint64_t *) & val)); //cheat to know used space uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size); buff[0] = BPLIST_REAL | Log2(size); - memcpy(buff + 1, &val, size); + if (size == sizeof(double)) { + memcpy(buff + 1, &val, size); + } else if (size == sizeof(float)) { + float tmpval = (float) val; + memcpy(buff + 1, &tmpval, size); + } byte_convert(buff + 1, size); g_byte_array_append(bplist, buff, sizeof(uint8_t) + size); free(buff); diff --git a/src/plist.c b/src/plist.c index 0e73c4b..e800b5c 100644 --- a/src/plist.c +++ b/src/plist.c @@ -414,3 +414,70 @@ void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) *sec = val.tv_sec; *usec = val.tv_usec; } + +gboolean plist_data_compare(gconstpointer a, gconstpointer b) +{ + plist_data_t val_a = NULL; + plist_data_t val_b = NULL; + + if (!a || !b) + return FALSE; + + if (!((GNode *) a)->data || !((GNode *) b)->data) + return FALSE; + + val_a = plist_get_data((plist_t) a); + val_b = plist_get_data((plist_t) b); + + if (val_a->type != val_b->type) + return FALSE; + + switch (val_a->type) { + case PLIST_BOOLEAN: + case PLIST_UINT: + case PLIST_REAL: + if (val_a->intval == val_b->intval) //it is an union so this is sufficient + return TRUE; + else + return FALSE; + + case PLIST_KEY: + case PLIST_STRING: + if (!strcmp(val_a->strval, val_b->strval)) + return TRUE; + else + return FALSE; + case PLIST_UNICODE: + if (!memcmp(val_a->unicodeval, val_b->unicodeval, val_a->length)) + return TRUE; + else + return FALSE; + + case PLIST_DATA: + if (!memcmp(val_a->buff, val_b->buff, val_a->length)) + return TRUE; + else + return FALSE; + case PLIST_ARRAY: + case PLIST_DICT: + //compare pointer + if (a == b) + return TRUE; + else + return FALSE; + break; + case PLIST_DATE: + if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(GTimeVal))) + return TRUE; + else + return FALSE; + default: + break; + } + return FALSE; +} + +char plist_compare_node_value(plist_t node_l, plist_t node_r) +{ + return plist_data_compare( node_l , node_r ); +} diff --git a/src/plist.h b/src/plist.h index 4bb97e3..83c63bf 100644 --- a/src/plist.h +++ b/src/plist.h @@ -49,7 +49,7 @@ typedef struct plist_data_s *plist_data_t; plist_t plist_new_node(plist_data_t data); plist_data_t plist_get_data(const plist_t node); plist_data_t plist_new_plist_data(); - +gboolean plist_data_compare(gconstpointer a, gconstpointer b); #endif diff --git a/src/xplist.c b/src/xplist.c index e281b4f..c0849b6 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -152,12 +152,12 @@ static void node_to_xml(GNode * node, gpointer xml_struct) case PLIST_UINT: tag = XPLIST_INT; - val = g_strdup_printf("%lu", (long unsigned int) node_data->intval); + val = g_strdup_printf("%llu", node_data->intval); break; case PLIST_REAL: tag = XPLIST_REAL; - val = g_strdup_printf("%Lf", (long double) node_data->realval); + val = g_strdup_printf("%f", node_data->realval); break; case PLIST_STRING: |