diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bplist.c | 127 | ||||
| -rw-r--r-- | src/plist.c | 24 | ||||
| -rw-r--r-- | src/plist.h | 2 | ||||
| -rw-r--r-- | src/xplist.c | 63 | 
4 files changed, 139 insertions, 77 deletions
| diff --git a/src/bplist.c b/src/bplist.c index 6e00f39..fb24a1e 100644 --- a/src/bplist.c +++ b/src/bplist.c @@ -75,6 +75,7 @@ static void byte_convert(uint8_t * address, size_t size)  #endif  } +  #define UINT_TO_HOST(x, n) \  		(n == 8 ? GUINT64_FROM_BE( *(uint64_t *)(x) ) : \  		(n == 4 ? GUINT32_FROM_BE( *(uint32_t *)(x) ) : \ @@ -220,11 +221,14 @@ static plist_t parse_array_node(char *bnode, uint64_t size, uint32_t ref_size)  static plist_t parse_bin_node(char *object, uint8_t dict_size, char **next_object)  { +	uint16_t type = 0; +	uint64_t size = 0; +  	if (!object)  		return NULL; -	uint16_t type = *object & 0xF0; -	uint64_t size = *object & 0x0F; +	type = (*object) & 0xF0; +	size = (*object) & 0x0F;  	object++;  	switch (type) { @@ -364,6 +368,22 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data)  void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist)  { +	char *trailer = NULL; + +	uint8_t offset_size = 0; +	uint8_t dict_param_size = 0; +	uint64_t num_objects = 0; +	uint64_t root_object = 0; +	uint64_t offset_table_index = 0; + +	plist_t *nodeslist = NULL; +	uint64_t i = 0; +	uint64_t current_offset = 0; +	char *offset_table = NULL; +	uint32_t j = 0, str_i = 0, str_j = 0; +	uint32_t index1 = 0, index2 = 0; + +  	//first check we have enough data  	if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE))  		return; @@ -375,39 +395,34 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist)  		return;  	//now parse trailer -	const char *trailer = plist_bin + (length - BPLIST_TRL_SIZE); +	trailer = plist_bin + (length - BPLIST_TRL_SIZE); -	uint8_t offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX]; -	uint8_t dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX]; -	uint64_t num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX); -	uint64_t root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX); -	uint64_t offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX); +	offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX]; +	dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX]; +	num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX); +	root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX); +	offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX);  	if (num_objects == 0)  		return;  	//allocate serialized array of nodes -	plist_t *nodeslist = NULL;  	nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects);  	if (!nodeslist)  		return;  	//parse serialized nodes -	uint64_t i = 0; -	uint64_t current_offset = 0; -	const char *offset_table = plist_bin + offset_table_index; +	offset_table = plist_bin + offset_table_index;  	for (i = 0; i < num_objects; i++) { +		char *obj = NULL;  		current_offset = UINT_TO_HOST(offset_table + i * offset_size, offset_size); -		char *obj = plist_bin + current_offset; +		obj = plist_bin + current_offset;  		nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj);  	}  	//setup children for structured types -	uint32_t j = 0, str_i = 0, str_j = 0; -	uint32_t index1 = 0, index2 = 0; -  	for (i = 0; i < num_objects; i++) {  		plist_data_t data = plist_get_data(nodeslist[i]); @@ -514,14 +529,17 @@ static guint plist_data_hash(gconstpointer key)  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; -	plist_data_t val_a = plist_get_data((plist_t) a); -	plist_data_t val_b = plist_get_data((plist_t) b); +	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; @@ -574,6 +592,7 @@ struct serialize_s {  static void serialize_plist(GNode * node, gpointer data)  { +	uint64_t *index_val = NULL;  	struct serialize_s *ser = (struct serialize_s *) data;  	uint64_t current_index = ser->objects->len; @@ -584,7 +603,7 @@ static void serialize_plist(GNode * node, gpointer data)  		return;  	}  	//insert new ref -	uint64_t *index_val = (uint64_t *) malloc(sizeof(uint64_t)); +	index_val = (uint64_t *) malloc(sizeof(uint64_t));  	*index_val = current_index;  	g_hash_table_insert(ser->ref_table, node, index_val); @@ -639,6 +658,7 @@ static void write_date(GByteArray * bplist, double val)  static void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uint64_t size)  { +	uint8_t *buff = NULL;  	uint8_t marker = mark | (size < 15 ? size : 0xf);  	g_byte_array_append(bplist, &marker, sizeof(uint8_t));  	if (size >= 15) { @@ -647,7 +667,7 @@ static void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uin  		g_byte_array_append(bplist, int_buff->data, int_buff->len);  		g_byte_array_free(int_buff, TRUE);  	} -	uint8_t *buff = (uint8_t *) malloc(size); +	buff = (uint8_t *) malloc(size);  	memcpy(buff, val, size);  	g_byte_array_append(bplist, buff, size);  	free(buff); @@ -677,6 +697,12 @@ static void write_unicode(GByteArray * bplist, gunichar2 * val, uint64_t size)  static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)  { +	uint64_t idx = 0; +	uint8_t *buff = NULL; + +	GNode *cur = NULL; +	uint64_t i = 0; +  	uint64_t size = g_node_n_children(node);  	uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf);  	g_byte_array_append(bplist, &marker, sizeof(uint8_t)); @@ -687,11 +713,8 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl  		g_byte_array_free(int_buff, TRUE);  	} -	uint64_t idx = 0; -	uint8_t *buff = (uint8_t *) malloc(size * dict_param_size); +	buff = (uint8_t *) malloc(size * dict_param_size); -	GNode *cur = NULL; -	uint64_t i = 0;  	for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) {  		idx = *(uint64_t *) (g_hash_table_lookup(ref_table, cur));  		memcpy(buff + i * dict_param_size, &idx, dict_param_size); @@ -706,6 +729,13 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl  static void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)  { +	uint64_t idx1 = 0; +	uint64_t idx2 = 0; +	uint8_t *buff = NULL; + +	GNode *cur = NULL; +	uint64_t i = 0; +  	uint64_t size = g_node_n_children(node) / 2;  	uint8_t marker = BPLIST_DICT | (size < 15 ? size : 0xf);  	g_byte_array_append(bplist, &marker, sizeof(uint8_t)); @@ -716,12 +746,8 @@ static void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table  		g_byte_array_free(int_buff, TRUE);  	} -	uint64_t idx1 = 0; -	uint64_t idx2 = 0; -	uint8_t *buff = (uint8_t *) malloc(size * 2 * dict_param_size); +	buff = (uint8_t *) malloc(size * 2 * dict_param_size); -	GNode *cur = NULL; -	uint64_t i = 0;  	for (i = 0, cur = node->children; cur && i < size; cur = cur->next->next, i++) {  		idx1 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));  		memcpy(buff + i * dict_param_size, &idx1, dict_param_size); @@ -740,41 +766,55 @@ static void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table  void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)  { +	GPtrArray *objects = NULL; +	GHashTable *ref_table = NULL; +	struct serialize_s ser_s; +	uint8_t offset_size = 0; +	uint8_t dict_param_size = 0; +	uint64_t num_objects = 0; +	uint64_t root_object = 0; +	uint64_t offset_table_index = 0; +	GByteArray *bplist_buff = NULL; +	uint64_t i = 0; +	uint8_t *buff = NULL; +	uint64_t *offsets = NULL; +	uint8_t pad[6] = { 0, 0, 0, 0, 0, 0 }; +	uint8_t trailer[BPLIST_TRL_SIZE]; +  	//check for valid input  	if (!plist || !plist_bin || *plist_bin || !length)  		return;  	//list of objects -	GPtrArray *objects = g_ptr_array_new(); +	objects = g_ptr_array_new();  	//hashtable to write only once same nodes -	GHashTable *ref_table = g_hash_table_new(plist_data_hash, plist_data_compare); +	ref_table = g_hash_table_new(plist_data_hash, plist_data_compare);  	//serialize plist -	struct serialize_s ser_s = { objects, ref_table }; +	ser_s.objects = objects; +	ser_s.ref_table = ref_table;  	serialize_plist(plist, &ser_s);  	//now stream to output buffer -	uint8_t offset_size = 0;	//unknown yet -	uint8_t dict_param_size = get_needed_bytes(objects->len); -	uint64_t num_objects = objects->len; -	uint64_t root_object = 0;	//root is first in list -	uint64_t offset_table_index = 0;	//unknown yet +	offset_size = 0;			//unknown yet +	dict_param_size = get_needed_bytes(objects->len); +	num_objects = objects->len; +	root_object = 0;			//root is first in list +	offset_table_index = 0;		//unknown yet  	//setup a dynamic bytes array to store bplist in -	GByteArray *bplist_buff = g_byte_array_new(); +	bplist_buff = g_byte_array_new();  	//set magic number and version  	g_byte_array_append(bplist_buff, BPLIST_MAGIC, BPLIST_MAGIC_SIZE);  	g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE);  	//write objects and table -	uint64_t i = 0; -	uint8_t *buff = NULL; -	uint64_t offsets[num_objects]; +	offsets = (uint64_t *) malloc(num_objects * sizeof(uint64_t));  	for (i = 0; i < num_objects; i++) { -		offsets[i] = bplist_buff->len;  		plist_data_t data = plist_get_data(g_ptr_array_index(objects, i)); +		offsets[i] = bplist_buff->len;  		switch (data->type) {  		case PLIST_BOOLEAN: @@ -830,7 +870,6 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)  	}  	//experimental pad to reflect apple's files -	uint8_t pad[6] = { 0, 0, 0, 0, 0, 0 };  	g_byte_array_append(bplist_buff, pad, 6);  	//setup trailer @@ -838,7 +877,6 @@ 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); -	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)); @@ -853,4 +891,5 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)  	*length = bplist_buff->len;  	g_byte_array_free(bplist_buff, TRUE); +	free(offsets);  } diff --git a/src/plist.c b/src/plist.c index 758fe53..87a1de5 100644 --- a/src/plist.c +++ b/src/plist.c @@ -77,15 +77,17 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *  	if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) {  		//only structured types are allowed to have nulll value  		if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { -			//now handle value -			plist_data_t data = plist_new_plist_data(); -			data->type = type; -			data->length = length;  			glong len = 0;  			glong items_read = 0;  			glong items_written = 0;  			GError *error = NULL; +			plist_t subnode = NULL; + +			//now handle value +			plist_data_t data = plist_new_plist_data(); +			data->type = type; +			data->length = length;  			switch (type) {  			case PLIST_BOOLEAN: @@ -119,7 +121,7 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *  				break;  			} -			plist_t subnode = plist_new_node(data); +			subnode = plist_new_node(data);  			if (node)  				g_node_append(node, subnode);  			return subnode; @@ -185,10 +187,11 @@ static char compare_node_value(plist_type type, plist_data_t data, const void *v  static plist_t plist_find_node(plist_t plist, plist_type type, const void *value, uint64_t length)  { +	plist_t current = NULL; +  	if (!plist)  		return NULL; -	plist_t current = NULL;  	for (current = plist_get_first_child(plist); current; current = plist_get_next_sibling(current)) {  		plist_data_t data = plist_get_data(current); @@ -217,16 +220,17 @@ plist_t plist_find_node_by_string(plist_t plist, const char *value)  static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length)  { -	if (!node) -		return; -  	//for unicode  	glong len = 0;  	glong items_read = 0;  	glong items_written = 0;  	GError *error = NULL; +	plist_data_t data = NULL; + +	if (!node) +		return; -	plist_data_t data = plist_get_data(node); +	data = plist_get_data(node);  	*type = data->type;  	*length = data->length; diff --git a/src/plist.h b/src/plist.h index 830da75..48b66f1 100644 --- a/src/plist.h +++ b/src/plist.h @@ -24,11 +24,9 @@  #include "plist/plist.h" -#include <stdint.h>  #include <sys/types.h>  #include <sys/stat.h> -#include <unistd.h>  #include <glib.h> diff --git a/src/xplist.c b/src/xplist.c index 20b2795..b404e79 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -102,7 +102,7 @@ struct xml_node {  static xmlDocPtr new_xml_plist()  {  	char *plist = strdup(plist_base); -	xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); +	xmlDocPtr plist_xml = xmlParseMemory(plist, strlen(plist));  	if (!plist_xml)  		return NULL; @@ -126,11 +126,8 @@ static void free_plist(xmlDocPtr plist)  static void node_to_xml(GNode * node, gpointer xml_struct)  { -	if (!node) -		return; - -	struct xml_node *xstruct = (struct xml_node *) xml_struct; -	plist_data_t node_data = plist_get_data(node); +	struct xml_node *xstruct = NULL; +	plist_data_t node_data = NULL;  	xmlNodePtr child_node = NULL;  	char isStruct = FALSE; @@ -138,12 +135,23 @@ static void node_to_xml(GNode * node, gpointer xml_struct)  	const xmlChar *tag = NULL;  	gchar *val = NULL; +	//for base64 +	gchar *valtmp = NULL; +  	//for unicode  	glong len = 0;  	glong items_read = 0;  	glong items_written = 0;  	GError *error = NULL; +	uint32_t i = 0; + +	if (!node) +		return; + +	xstruct = (struct xml_node *) xml_struct; +	node_data = plist_get_data(node); +  	switch (node_data->type) {  	case PLIST_BOOLEAN:  		{ @@ -182,7 +190,7 @@ static void node_to_xml(GNode * node, gpointer xml_struct)  	case PLIST_DATA:  		tag = XPLIST_DATA; -		gchar *valtmp = g_base64_encode(node_data->buff, node_data->length); +		valtmp = g_base64_encode(node_data->buff, node_data->length);  		val = format_string(valtmp, 60, xstruct->depth);  		g_free(valtmp);  		break; @@ -202,7 +210,6 @@ static void node_to_xml(GNode * node, gpointer xml_struct)  		break;  	} -	uint32_t i = 0;  	for (i = 0; i < xstruct->depth; i++) {  		xmlNodeAddContent(xstruct->xml, BAD_CAST("\t"));  	} @@ -232,6 +239,16 @@ static void node_to_xml(GNode * node, gpointer xml_struct)  static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)  {  	xmlNodePtr node = NULL; +	plist_data_t data = NULL; +	plist_t subnode = NULL; + +	//for string +	unsigned char *tmp = NULL; +	glong len = 0; +	glong items_read = 0; +	glong items_written = 0; +	GError *error = NULL; +	int type = 0;  	if (!xml_node)  		return; @@ -243,8 +260,8 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)  		if (!node)  			break; -		plist_data_t data = plist_new_plist_data(); -		plist_t subnode = plist_new_node(data); +		data = plist_new_plist_data(); +		subnode = plist_new_node(data);  		if (*plist_node)  			g_node_append(*plist_node, subnode);  		else @@ -289,12 +306,12 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)  		if (!xmlStrcmp(node->name, XPLIST_STRING)) { -			unsigned char *tmp = xmlNodeGetContent(node); -			glong len = strlen((char *) tmp); -			glong items_read = 0; -			glong items_written = 0; -			GError *error = NULL; -			int type = xmlDetectCharEncoding(tmp, len); +			tmp = xmlNodeGetContent(node); +			len = strlen((char *) tmp); +			items_read = 0; +			items_written = 0; +			error = NULL; +			type = xmlDetectCharEncoding(tmp, len);  			if (XML_CHAR_ENCODING_UTF8 == type) {  				data->unicodeval = g_utf8_to_utf16((char *) tmp, len, &items_read, &items_written, &error); @@ -339,15 +356,19 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)  void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)  { +	xmlDocPtr plist_doc = NULL; +	xmlNodePtr root_node = NULL; +	struct xml_node root = { NULL, 0 }; +	int size = 0; +  	if (!plist || !plist_xml || *plist_xml)  		return; -	xmlDocPtr plist_doc = new_xml_plist(); -	xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); -	struct xml_node root = { root_node, 0 }; +	plist_doc = new_xml_plist(); +	root_node = xmlDocGetRootElement(plist_doc); +	root.xml = root_node;  	node_to_xml(plist, &root); -	int size = 0;  	xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size);  	if (size >= 0)  		*length = size; @@ -356,7 +377,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)  void plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist)  { -	xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0); +	xmlDocPtr plist_doc = xmlParseMemory(plist_xml, length);  	xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);  	xml_to_node(root_node, plist); | 
