summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bplist.c66
-rw-r--r--src/plist.h2
-rw-r--r--src/xplist.c91
3 files changed, 93 insertions, 66 deletions
diff --git a/src/bplist.c b/src/bplist.c
index ed381db..7a7225d 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -27,10 +27,10 @@
#include <string.h>
/* Magic marker and size. */
-#define BPLIST_MAGIC "bplist"
+#define BPLIST_MAGIC ((uint8_t*)"bplist")
#define BPLIST_MAGIC_SIZE 6
-#define BPLIST_VERSION "00"
+#define BPLIST_VERSION ((uint8_t*)"00")
#define BPLIST_VERSION_SIZE 2
@@ -59,11 +59,11 @@ enum {
BPLIST_MASK = 0xF0
};
-static void byte_convert(char *address, size_t size)
+static void byte_convert(uint8_t *address, size_t size)
{
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
uint8_t i = 0, j = 0;
- char tmp = '\0';
+ uint8_t tmp = 0;
for (i = 0; i < (size / 2); i++) {
tmp = address[i];
@@ -75,13 +75,18 @@ static void byte_convert(char *address, size_t size)
}
#define swap_n_bytes(x, n) \
- n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \
+ (n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \
(n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \
- (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : *(uint8_t *)(x) ))
+ (n == 2 ? GUINT16_FROM_BE( *(uint16_t *)(x) ) : \
+ *(uint8_t *)(x) )))
#define be64dec(x) GUINT64_FROM_BE( *(uint64_t*)(x) )
-#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= (uint64_t)1<<32 ? 4 : 8)))
+#define get_needed_bytes(x) \
+ ( ((uint64_t)x) < (1ULL << 8) ? 1 : \
+ ( ((uint64_t)x) < (1ULL << 16) ? 2 : \
+ ( ((uint64_t)x) < (1ULL << 32) ? 4 : 8)))
+
#define get_real_bytes(x) (x >> 32 ? 4 : 8)
@@ -118,12 +123,8 @@ static plist_t parse_real_node(char *bnode, uint8_t size)
size = 1 << size; // make length less misleading
switch (size) {
case sizeof(float):
- memcpy(&data->realval, bnode, size);
- byte_convert((char *) &data->realval, size);
- break;
case sizeof(double):
- memcpy(&data->realval, bnode, size);
- byte_convert((char *) &data->realval, size);
+ data->realval = swap_n_bytes(bnode, size);
break;
default:
free(data);
@@ -163,8 +164,8 @@ static plist_t parse_data_node(char *bnode, uint64_t size)
data->type = PLIST_DATA;
data->length = size;
- data->buff = (char *) malloc(sizeof(char) * size);
- memcpy(data->buff, bnode, sizeof(char) * size);
+ data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size);
+ memcpy(data->buff, bnode, sizeof(uint8_t) * size);
return g_node_new(data);
}
@@ -175,8 +176,8 @@ static plist_t parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size)
data->type = PLIST_DICT;
data->length = size;
- data->buff = (char *) malloc(sizeof(char) * size * ref_size * 2);
- memcpy(data->buff, bnode, sizeof(char) * size * ref_size * 2);
+ data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size * ref_size * 2);
+ memcpy(data->buff, bnode, sizeof(uint8_t) * size * ref_size * 2);
return g_node_new(data);
}
@@ -187,8 +188,8 @@ static plist_t parse_array_node(char *bnode, uint64_t size, uint32_t ref_size)
data->type = PLIST_ARRAY;
data->length = size;
- data->buff = (char *) malloc(sizeof(char) * size * ref_size);
- memcpy(data->buff, bnode, sizeof(char) * size * ref_size);
+ data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size * ref_size);
+ memcpy(data->buff, bnode, sizeof(uint8_t) * size * ref_size);
return g_node_new(data);
}
@@ -321,12 +322,12 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data)
break;
case PLIST_DATA:
case PLIST_ARRAY:
- dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length);
- memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length);
+ dstdata->buff = (uint8_t *) malloc(sizeof(uint8_t *) * srcdata->length);
+ memcpy(dstdata->buff, srcdata->buff, sizeof(uint8_t *) * srcdata->length);
break;
case PLIST_DICT:
- dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length * 2);
- memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length * 2);
+ dstdata->buff = (uint8_t *) malloc(sizeof(uint8_t *) * srcdata->length * 2);
+ memcpy(dstdata->buff, srcdata->buff, sizeof(uint8_t *) * srcdata->length * 2);
break;
default:
break;
@@ -561,7 +562,9 @@ static void serialize_plist(GNode * node, gpointer data)
return;
}
//insert new ref
- g_hash_table_insert(ser->ref_table, node, GUINT_TO_POINTER(current_index));
+ uint64_t* index_val = (uint64_t*) malloc(sizeof(uint64_t));
+ *index_val = current_index;
+ g_hash_table_insert(ser->ref_table, node, index_val);
//now append current node to object array
g_ptr_array_add(ser->objects, node);
@@ -571,6 +574,12 @@ static void serialize_plist(GNode * node, gpointer data)
return;
}
+static gboolean free_index (gpointer key, gpointer value, gpointer user_data)
+{
+ free((uint64_t*)value);
+ return TRUE;
+}
+
#define Log2(x) (x == 8 ? 3 : (x == 4 ? 2 : (x == 2 ? 1 : 0)))
static void write_int(GByteArray * bplist, uint64_t val)
@@ -619,7 +628,7 @@ static void write_data(GByteArray * bplist, uint8_t * val, uint64_t size)
static void write_string(GByteArray * bplist, char *val)
{
uint64_t size = strlen(val);
- write_raw_data(bplist, BPLIST_STRING, val, size);
+ write_raw_data(bplist, BPLIST_STRING, (uint8_t*) val, size);
}
static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)
@@ -640,7 +649,7 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl
GNode *cur = NULL;
uint64_t i = 0;
for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) {
- idx = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));
+ idx = *(uint64_t*)(g_hash_table_lookup(ref_table, cur));
memcpy(buff + i * dict_param_size, &idx, dict_param_size);
byte_convert(buff + i * dict_param_size, dict_param_size);
}
@@ -747,7 +756,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
//TODO
break;
case PLIST_DATA:
- write_data(bplist_buff, data->strval, data->length);
+ write_data(bplist_buff, data->buff, data->length);
case PLIST_ARRAY:
write_array(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
break;
@@ -762,6 +771,9 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
}
}
+ //free intermediate objects
+ g_hash_table_foreach_remove (ref_table, free_index, NULL);
+
//write offsets
offset_size = get_needed_bytes(bplist_buff->len);
offset_table_index = bplist_buff->len;
@@ -778,7 +790,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
root_object = GUINT64_FROM_BE(root_object);
offset_table_index = GUINT64_FROM_BE(offset_table_index);
- char trailer[BPLIST_TRL_SIZE];
+ uint8_t trailer[BPLIST_TRL_SIZE];
memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t));
memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t));
memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t));
diff --git a/src/plist.h b/src/plist.h
index a637118..8428597 100644
--- a/src/plist.h
+++ b/src/plist.h
@@ -43,7 +43,7 @@ struct plist_data_s {
double realval;
char *strval;
wchar_t *unicodeval;
- char *buff;
+ uint8_t *buff;
};
uint64_t length;
plist_type type;
diff --git a/src/xplist.c b/src/xplist.c
index abc448d..3487f96 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -32,6 +32,17 @@
#include <libxml/parser.h>
#include <libxml/tree.h>
+#define XPLIST_TEXT BAD_CAST("text")
+#define XPLIST_KEY BAD_CAST("key")
+#define XPLIST_FALSE BAD_CAST("false")
+#define XPLIST_TRUE BAD_CAST("true")
+#define XPLIST_INT BAD_CAST("integer")
+#define XPLIST_REAL BAD_CAST("real")
+#define XPLIST_DATE BAD_CAST("date")
+#define XPLIST_DATA BAD_CAST("data")
+#define XPLIST_STRING BAD_CAST("string")
+#define XPLIST_ARRAY BAD_CAST("array")
+#define XPLIST_DICT BAD_CAST("dict")
const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
@@ -125,56 +136,56 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
xmlNodePtr child_node = NULL;
char isStruct = FALSE;
- const gchar *tag = NULL;
- const gchar *val = NULL;
+ const xmlChar *tag = NULL;
+ gchar *val = NULL;
switch (node_data->type) {
case PLIST_BOOLEAN:
{
if (node_data->boolval)
- tag = "true";
+ tag = XPLIST_TRUE;
else
- tag = "false";
+ tag = XPLIST_FALSE;
}
break;
case PLIST_UINT:
- tag = "integer";
+ tag = XPLIST_INT;
val = g_strdup_printf("%lu", (long unsigned int) node_data->intval);
break;
case PLIST_REAL:
- tag = "real";
+ tag = XPLIST_REAL;
val = g_strdup_printf("%Lf", (long double) node_data->realval);
break;
case PLIST_STRING:
- tag = "string";
+ tag = XPLIST_STRING;
val = g_strdup(node_data->strval);
break;
case PLIST_UNICODE:
- tag = "string";
+ tag = XPLIST_STRING;
val = g_strdup((gchar *) node_data->unicodeval);
break;
case PLIST_KEY:
- tag = "key";
+ tag = XPLIST_KEY;
val = g_strdup((gchar *) node_data->strval);
break;
case PLIST_DATA:
- tag = "data";
+ tag = XPLIST_DATA;
gchar *valtmp = g_base64_encode(node_data->buff, node_data->length);
val = format_string(valtmp, 68, xstruct->depth);
g_free(valtmp);
break;
case PLIST_ARRAY:
- tag = "array";
+ tag = XPLIST_ARRAY;
isStruct = TRUE;
break;
case PLIST_DICT:
- tag = "dict";
+ tag = XPLIST_DICT;
isStruct = TRUE;
break;
case PLIST_DATE: //TODO : handle date tag
@@ -182,17 +193,17 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
break;
}
- int i = 0;
+ uint32_t i = 0;
for (i = 0; i < xstruct->depth; i++) {
- xmlNodeAddContent(xstruct->xml, "\t");
+ xmlNodeAddContent(xstruct->xml, BAD_CAST("\t"));
}
- child_node = xmlNewChild(xstruct->xml, NULL, tag, val);
- xmlNodeAddContent(xstruct->xml, "\n");
+ child_node = xmlNewChild(xstruct->xml, NULL, tag, BAD_CAST(val));
+ xmlNodeAddContent(xstruct->xml, BAD_CAST("\n"));
g_free(val);
//add return for structured types
if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT)
- xmlNodeAddContent(child_node, "\n");
+ xmlNodeAddContent(child_node, BAD_CAST("\n"));
if (isStruct) {
struct xml_node child = { child_node, xstruct->depth + 1 };
@@ -202,20 +213,20 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT) {
for (i = 0; i < xstruct->depth; i++) {
- xmlNodeAddContent(child_node, "\t");
+ xmlNodeAddContent(child_node, BAD_CAST("\t"));
}
}
return;
}
-void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
+static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
{
xmlNodePtr node = NULL;
for (node = xml_node->children; node; node = node->next) {
- while (node && !xmlStrcmp(node->name, "text"))
+ while (node && !xmlStrcmp(node->name, XPLIST_TEXT))
node = node->next;
if (!node)
break;
@@ -227,68 +238,68 @@ void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
else
*plist_node = subnode;
- if (!xmlStrcmp(node->name, "true")) {
- data->boolval = 1;
+ if (!xmlStrcmp(node->name, XPLIST_TRUE)) {
+ data->boolval = TRUE;
data->type = PLIST_BOOLEAN;
data->length = 1;
continue;
}
- if (!xmlStrcmp(node->name, "false")) {
- data->boolval = 0;
+ if (!xmlStrcmp(node->name, XPLIST_FALSE)) {
+ data->boolval = FALSE;
data->type = PLIST_BOOLEAN;
data->length = 1;
continue;
}
- if (!xmlStrcmp(node->name, "integer")) {
- char *strval = xmlNodeGetContent(node);
+ if (!xmlStrcmp(node->name, XPLIST_INT)) {
+ char *strval = (char*)xmlNodeGetContent(node);
data->intval = g_ascii_strtoull(strval, NULL, 0);
data->type = PLIST_UINT;
data->length = 8;
continue;
}
- if (!xmlStrcmp(node->name, "real")) {
- char *strval = xmlNodeGetContent(node);
+ if (!xmlStrcmp(node->name, XPLIST_REAL)) {
+ char *strval = (char*)xmlNodeGetContent(node);
data->realval = atof(strval);
data->type = PLIST_REAL;
data->length = 8;
continue;
}
- if (!xmlStrcmp(node->name, "date"))
+ if (!xmlStrcmp(node->name, XPLIST_DATE))
continue; //TODO : handle date tag
- if (!xmlStrcmp(node->name, "string")) {
- data->strval = strdup(xmlNodeGetContent(node));
+ if (!xmlStrcmp(node->name, XPLIST_STRING)) {
+ data->strval = strdup( (char*) xmlNodeGetContent(node));
data->type = PLIST_STRING;
data->length = strlen(data->strval);
continue;
}
- if (!xmlStrcmp(node->name, "key")) {
- data->strval = strdup(xmlNodeGetContent(node));
+ if (!xmlStrcmp(node->name, XPLIST_KEY)) {
+ data->strval = strdup( (char*) xmlNodeGetContent(node));
data->type = PLIST_KEY;
data->length = strlen(data->strval);
continue;
}
- if (!xmlStrcmp(node->name, "data")) {
+ if (!xmlStrcmp(node->name, XPLIST_DATA)) {
gsize size = 0;
- data->buff = g_base64_decode(xmlNodeGetContent(node), &size);
+ data->buff = g_base64_decode((char*)xmlNodeGetContent(node), &size);
data->length = size;
data->type = PLIST_DATA;
continue;
}
- if (!xmlStrcmp(node->name, "array")) {
+ if (!xmlStrcmp(node->name, XPLIST_ARRAY)) {
data->type = PLIST_ARRAY;
xml_to_node(node, &subnode);
continue;
}
- if (!xmlStrcmp(node->name, "dict")) {
+ if (!xmlStrcmp(node->name, XPLIST_DICT)) {
data->type = PLIST_DICT;
xml_to_node(node, &subnode);
continue;
@@ -306,7 +317,11 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
node_to_xml(plist, &root);
- xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length);
+ int size = 0;
+ xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size);
+ if (size >=0 )
+ *length = size;
+ free_plist(plist_doc);
}
void plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist)