Commit fa4a22dd authored by Jonathan Beck's avatar Jonathan Beck

Make it compile on MSVC 2005.

parent ff1fa73f
...@@ -26,7 +26,20 @@ ...@@ -26,7 +26,20 @@
extern "C" { extern "C" {
#endif #endif
#ifdef _MSC_VER
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h> #include <stdint.h>
#endif
#include <sys/types.h> #include <sys/types.h>
/** /**
......
...@@ -30,6 +30,11 @@ ...@@ -30,6 +30,11 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
FILE *iplist = NULL;
plist_t root_node = NULL;
char *plist_out = NULL;
int size = 0;
char *plist_entire = NULL;
struct stat *filestats = (struct stat *) malloc(sizeof(struct stat)); struct stat *filestats = (struct stat *) malloc(sizeof(struct stat));
Options *options = parse_arguments(argc, argv); Options *options = parse_arguments(argc, argv);
...@@ -38,19 +43,17 @@ int main(int argc, char *argv[]) ...@@ -38,19 +43,17 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
//read input file //read input file
FILE *iplist = fopen(options->in_file, "r"); iplist = fopen(options->in_file, "rb");
if (!iplist) if (!iplist)
return 1; return 1;
stat(options->in_file, filestats); stat(options->in_file, filestats);
char *plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1)); plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1));
fread(plist_entire, sizeof(char), filestats->st_size, iplist); fread(plist_entire, sizeof(char), filestats->st_size, iplist);
fclose(iplist); fclose(iplist);
//convert one format to another //convert one format to another
plist_t root_node = NULL;
char *plist_out = NULL;
int size = 0;
if (memcmp(plist_entire, "bplist00", 8) == 0) { if (memcmp(plist_entire, "bplist00", 8) == 0) {
plist_from_bin(plist_entire, filestats->st_size, &root_node); plist_from_bin(plist_entire, filestats->st_size, &root_node);
......
...@@ -75,6 +75,7 @@ static void byte_convert(uint8_t * address, size_t size) ...@@ -75,6 +75,7 @@ static void byte_convert(uint8_t * address, size_t size)
#endif #endif
} }
#define UINT_TO_HOST(x, n) \ #define UINT_TO_HOST(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 == 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) ...@@ -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) 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) if (!object)
return NULL; return NULL;
uint16_t type = *object & 0xF0; type = (*object) & 0xF0;
uint64_t size = *object & 0x0F; size = (*object) & 0x0F;
object++; object++;
switch (type) { switch (type) {
...@@ -364,6 +368,22 @@ static gpointer copy_plist_data(gconstpointer src, gpointer data) ...@@ -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) 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 //first check we have enough data
if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE)) if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE))
return; return;
...@@ -375,39 +395,34 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) ...@@ -375,39 +395,34 @@ void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist)
return; return;
//now parse trailer //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]; offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX];
uint8_t dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX]; dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX];
uint64_t num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX); num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX);
uint64_t root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX); root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX);
uint64_t offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX); offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX);
if (num_objects == 0) if (num_objects == 0)
return; return;
//allocate serialized array of nodes //allocate serialized array of nodes
plist_t *nodeslist = NULL;
nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects); nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects);
if (!nodeslist) if (!nodeslist)
return; return;
//parse serialized nodes //parse serialized nodes
uint64_t i = 0; offset_table = plist_bin + offset_table_index;
uint64_t current_offset = 0;
const char *offset_table = plist_bin + offset_table_index;
for (i = 0; i < num_objects; i++) { for (i = 0; i < num_objects; i++) {
char *obj = NULL;
current_offset = UINT_TO_HOST(offset_table + i * offset_size, offset_size); 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); nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj);
} }
//setup children for structured types //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++) { for (i = 0; i < num_objects; i++) {
plist_data_t data = plist_get_data(nodeslist[i]); plist_data_t data = plist_get_data(nodeslist[i]);
...@@ -514,14 +529,17 @@ static guint plist_data_hash(gconstpointer key) ...@@ -514,14 +529,17 @@ static guint plist_data_hash(gconstpointer key)
static gboolean plist_data_compare(gconstpointer a, gconstpointer b) static gboolean plist_data_compare(gconstpointer a, gconstpointer b)
{ {
plist_data_t val_a = NULL;
plist_data_t val_b = NULL;
if (!a || !b) if (!a || !b)
return FALSE; return FALSE;
if (!((GNode *) a)->data || !((GNode *) b)->data) if (!((GNode *) a)->data || !((GNode *) b)->data)
return FALSE; return FALSE;
plist_data_t val_a = plist_get_data((plist_t) a); val_a = plist_get_data((plist_t) a);
plist_data_t val_b = plist_get_data((plist_t) b); val_b = plist_get_data((plist_t) b);
if (val_a->type != val_b->type) if (val_a->type != val_b->type)
return FALSE; return FALSE;
...@@ -574,6 +592,7 @@ struct serialize_s { ...@@ -574,6 +592,7 @@ struct serialize_s {
static void serialize_plist(GNode * node, gpointer data) static void serialize_plist(GNode * node, gpointer data)
{ {
uint64_t *index_val = NULL;
struct serialize_s *ser = (struct serialize_s *) data; struct serialize_s *ser = (struct serialize_s *) data;
uint64_t current_index = ser->objects->len; uint64_t current_index = ser->objects->len;
...@@ -584,7 +603,7 @@ static void serialize_plist(GNode * node, gpointer data) ...@@ -584,7 +603,7 @@ static void serialize_plist(GNode * node, gpointer data)
return; return;
} }
//insert new ref //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; *index_val = current_index;
g_hash_table_insert(ser->ref_table, node, index_val); g_hash_table_insert(ser->ref_table, node, index_val);
...@@ -639,6 +658,7 @@ static void write_date(GByteArray * bplist, double 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) 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); uint8_t marker = mark | (size < 15 ? size : 0xf);
g_byte_array_append(bplist, &marker, sizeof(uint8_t)); g_byte_array_append(bplist, &marker, sizeof(uint8_t));
if (size >= 15) { if (size >= 15) {
...@@ -647,7 +667,7 @@ static void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uin ...@@ -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_append(bplist, int_buff->data, int_buff->len);
g_byte_array_free(int_buff, TRUE); g_byte_array_free(int_buff, TRUE);
} }
uint8_t *buff = (uint8_t *) malloc(size); buff = (uint8_t *) malloc(size);
memcpy(buff, val, size); memcpy(buff, val, size);
g_byte_array_append(bplist, buff, size); g_byte_array_append(bplist, buff, size);
free(buff); free(buff);
...@@ -677,6 +697,12 @@ static void write_unicode(GByteArray * bplist, gunichar2 * val, uint64_t size) ...@@ -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) 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); uint64_t size = g_node_n_children(node);
uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf); uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf);
g_byte_array_append(bplist, &marker, sizeof(uint8_t)); g_byte_array_append(bplist, &marker, sizeof(uint8_t));
...@@ -687,11 +713,8 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl ...@@ -687,11 +713,8 @@ static void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_tabl
g_byte_array_free(int_buff, TRUE); g_byte_array_free(int_buff, TRUE);
} }
uint64_t idx = 0; buff = (uint8_t *) malloc(size * dict_param_size);
uint8_t *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++) { for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) {
idx = *(uint64_t *) (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); 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 ...@@ -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) 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; uint64_t size = g_node_n_children(node) / 2;
uint8_t marker = BPLIST_DICT | (size < 15 ? size : 0xf); uint8_t marker = BPLIST_DICT | (size < 15 ? size : 0xf);
g_byte_array_append(bplist, &marker, sizeof(uint8_t)); g_byte_array_append(bplist, &marker, sizeof(uint8_t));
...@@ -716,12 +746,8 @@ static void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table ...@@ -716,12 +746,8 @@ static void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table
g_byte_array_free(int_buff, TRUE); g_byte_array_free(int_buff, TRUE);
} }
uint64_t idx1 = 0; buff = (uint8_t *) malloc(size * 2 * dict_param_size);
uint64_t idx2 = 0;
uint8_t *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++) { 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)); idx1 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));
memcpy(buff + i * dict_param_size, &idx1, dict_param_size); 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 ...@@ -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) 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 //check for valid input
if (!plist || !plist_bin || *plist_bin || !length) if (!plist || !plist_bin || *plist_bin || !length)
return; return;
//list of objects //list of objects
GPtrArray *objects = g_ptr_array_new(); objects = g_ptr_array_new();
//hashtable to write only once same nodes //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 //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); serialize_plist(plist, &ser_s);
//now stream to output buffer //now stream to output buffer
uint8_t offset_size = 0; //unknown yet offset_size = 0; //unknown yet
uint8_t dict_param_size = get_needed_bytes(objects->len); dict_param_size = get_needed_bytes(objects->len);
uint64_t num_objects = objects->len; num_objects = objects->len;
uint64_t root_object = 0; //root is first in list root_object = 0; //root is first in list
uint64_t offset_table_index = 0; //unknown yet offset_table_index = 0; //unknown yet
//setup a dynamic bytes array to store bplist in //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 //set magic number and version
g_byte_array_append(bplist_buff, BPLIST_MAGIC, BPLIST_MAGIC_SIZE); g_byte_array_append(bplist_buff, BPLIST_MAGIC, BPLIST_MAGIC_SIZE);
g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE); g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE);
//write objects and table //write objects and table
uint64_t i = 0; offsets = (uint64_t *) malloc(num_objects * sizeof(uint64_t));
uint8_t *buff = NULL;
uint64_t offsets[num_objects];
for (i = 0; i < num_objects; i++) { 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)); plist_data_t data = plist_get_data(g_ptr_array_index(objects, i));
offsets[i] = bplist_buff->len;
switch (data->type) { switch (data->type) {
case PLIST_BOOLEAN: case PLIST_BOOLEAN:
...@@ -830,7 +870,6 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) ...@@ -830,7 +870,6 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
} }
//experimental pad to reflect apple's files //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); g_byte_array_append(bplist_buff, pad, 6);
//setup trailer //setup trailer
...@@ -838,7 +877,6 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) ...@@ -838,7 +877,6 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
root_object = GUINT64_FROM_BE(root_object); root_object = GUINT64_FROM_BE(root_object);
offset_table_index = GUINT64_FROM_BE(offset_table_index); 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_OFFSIZE_IDX, &offset_size, sizeof(uint8_t));
memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_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)); 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) ...@@ -853,4 +891,5 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
*length = bplist_buff->len; *length = bplist_buff->len;
g_byte_array_free(bplist_buff, TRUE); g_byte_array_free(bplist_buff, TRUE);
free(offsets);
} }
...@@ -77,15 +77,17 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void * ...@@ -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) { if (node_type == PLIST_DICT || node_type == PLIST_ARRAY) {
//only structured types are allowed to have nulll value //only structured types are allowed to have nulll value
if (value || (!value && (type == PLIST_DICT || type == PLIST_ARRAY))) { 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 len = 0;
glong items_read = 0; glong items_read = 0;
glong items_written = 0; glong items_written = 0;
GError *error = NULL; 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) { switch (type) {
case PLIST_BOOLEAN: case PLIST_BOOLEAN:
...@@ -119,7 +121,7 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void * ...@@ -119,7 +121,7 @@ static plist_t plist_add_sub_element(plist_t node, plist_type type, const void *
break; break;
} }
plist_t subnode = plist_new_node(data); subnode = plist_new_node(data);
if (node) if (node)
g_node_append(node, subnode); g_node_append(node, subnode);
return subnode; return subnode;
...@@ -185,10 +187,11 @@ static char compare_node_value(plist_type type, plist_data_t data, const void *v ...@@ -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) static plist_t plist_find_node(plist_t plist, plist_type type, const void *value, uint64_t length)
{ {
plist_t current = NULL;
if (!plist) if (!plist)
return NULL; return NULL;
plist_t current = NULL;
for (current = plist_get_first_child(plist); current; current = plist_get_next_sibling(current)) { for (current = plist_get_first_child(plist); current; current = plist_get_next_sibling(current)) {
plist_data_t data = plist_get_data(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) ...@@ -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) static void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length)
{ {
if (!node)
return;
//for unicode //for unicode
glong len = 0; glong len = 0;
glong items_read = 0; glong items_read = 0;
glong items_written = 0; glong items_written = 0;
GError *error = NULL; GError *error = NULL;
plist_data_t data = NULL;
plist_data_t data = plist_get_data(node); if (!node)
return;
data = plist_get_data(node);
*type = data->type; *type = data->type;
*length = data->length; *length = data->length;
......
...@@ -24,11 +24,9 @@ ...@@ -24,11 +24,9 @@
#include "plist/plist.h" #include "plist/plist.h"
#include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h>
#include <glib.h> #include <glib.h>
......
...@@ -102,7 +102,7 @@ struct xml_node { ...@@ -102,7 +102,7 @@ struct xml_node {
static xmlDocPtr new_xml_plist() static xmlDocPtr new_xml_plist()
{ {
char *plist = strdup(plist_base); 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) if (!plist_xml)
return NULL; return NULL;
...@@ -126,11 +126,8 @@ static void free_plist(xmlDocPtr plist) ...@@ -126,11 +126,8 @@ static void free_plist(xmlDocPtr plist)
static void node_to_xml(GNode * node, gpointer xml_struct) static void node_to_xml(GNode * node, gpointer xml_struct)
{ {
if (!node) struct xml_node *xstruct = NULL;
return; plist_data_t node_data = NULL;
struct xml_node *xstruct = (struct xml_node *) xml_struct;
plist_data_t node_data = plist_get_data(node);
xmlNodePtr child_node = NULL; xmlNodePtr child_node = NULL;
char isStruct = FALSE; char isStruct = FALSE;
...@@ -138,12 +135,23 @@ static void node_to_xml(GNode * node, gpointer xml_struct) ...@@ -138,12 +135,23 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
const xmlChar *tag = NULL; const xmlChar *tag = NULL;
gchar *val = NULL; gchar *val = NULL;
//for base64
gchar *valtmp = NULL;
//for unicode //for unicode
glong len = 0; glong len = 0;
glong items_read = 0; glong items_read = 0;
glong items_written = 0; glong items_written = 0;
GError *error = NULL; 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) { switch (node_data->type) {
case PLIST_BOOLEAN: case PLIST_BOOLEAN:
{ {
...@@ -182,7 +190,7 @@ static void node_to_xml(GNode * node, gpointer xml_struct) ...@@ -182,7 +190,7 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
case PLIST_DATA: case PLIST_DATA:
tag = XPLIST_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); val = format_string(valtmp, 60, xstruct->depth);
g_free(valtmp); g_free(valtmp);
break; break;
...@@ -202,7 +210,6 @@ static void node_to_xml(GNode * node, gpointer xml_struct) ...@@ -202,7 +210,6 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
break; break;
} }
uint32_t i = 0;
for (i = 0; i < xstruct->depth; i++) { for (i = 0; i < xstruct->depth; i++) {
xmlNodeAddContent(xstruct->xml, BAD_CAST("\t")); xmlNodeAddContent(xstruct->xml, BAD_CAST("\t"));
} }
...@@ -232,6 +239,16 @@ static void node_to_xml(GNode * node, gpointer xml_struct) ...@@ -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) static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
{ {
xmlNodePtr node = NULL; 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) if (!xml_node)
return; return;
...@@ -243,8 +260,8 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) ...@@ -243,8 +260,8 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
if (!node) if (!node)
break; break;
plist_data_t data = plist_new_plist_data(); data = plist_new_plist_data();
plist_t subnode = plist_new_node(data); subnode = plist_new_node(data);
if (*plist_node) if (*plist_node)
g_node_append(*plist_node, subnode); g_node_append(*plist_node, subnode);
else else
...@@ -289,12 +306,12 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) ...@@ -289,12 +306,12 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
if (!xmlStrcmp(node->name, XPLIST_STRING)) { if (!xmlStrcmp(node->name, XPLIST_STRING)) {
unsigned char *tmp = xmlNodeGetContent(node); tmp = xmlNodeGetContent(node);
glong len = strlen((char *) tmp); len = strlen((char *) tmp);
glong items_read = 0; items_read = 0;
glong items_written = 0; items_written = 0;
GError *error = NULL; error = NULL;
int type = xmlDetectCharEncoding(tmp, len); type = xmlDetectCharEncoding(tmp, len);
if (XML_CHAR_ENCODING_UTF8 == type) { if (XML_CHAR_ENCODING_UTF8 == type) {
data->unicodeval = g_utf8_to_utf16((char *) tmp, len, &items_read, &items_written, &error); 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) ...@@ -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) 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) if (!plist || !plist_xml || *plist_xml)
return; return;
xmlDocPtr plist_doc = new_xml_plist(); plist_doc = new_xml_plist();
xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); root_node = xmlDocGetRootElement(plist_doc);
struct xml_node root = { root_node, 0 }; root.xml = root_node;
node_to_xml(plist, &root); node_to_xml(plist, &root);
int size = 0;
xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size); xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, &size);
if (size >= 0) if (size >= 0)
*length = size; *length = size;
...@@ -356,7 +377,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) ...@@ -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) 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); xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);
xml_to_node(root_node, plist); xml_to_node(root_node, plist);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment