From e220e2cf08809a6a8853a8c9c7b06cef4e90cb57 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sat, 13 Dec 2008 13:56:55 +0100 Subject: Add plutil and do some cleaning. --- src/bplist.c | 52 +++++++++++++++--------------- src/plist.c | 103 +++++++++++++++++++++++++++++++++++++++++------------------ src/plist.h | 41 ++++++------------------ src/xplist.c | 6 ++-- 4 files changed, 110 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/bplist.c b/src/bplist.c index a5b1c9b..48b996d 100644 --- a/src/bplist.c +++ b/src/bplist.c @@ -83,9 +83,9 @@ void byte_convert(char *address, size_t size) #define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= 1<<32 ? 4 : 8))) #define get_real_bytes(x) (x >> 32 ? 4 : 8) -GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object) +plist_t parse_uint_node(char *bnode, uint8_t size, char **next_object) { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); size = 1 << size; // make length less misleading switch (size) { @@ -114,9 +114,9 @@ GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object) return g_node_new(data); } -GNode *parse_real_node(char *bnode, uint8_t size) +plist_t parse_real_node(char *bnode, uint8_t size) { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); size = 1 << size; // make length less misleading switch (size) { @@ -136,9 +136,9 @@ GNode *parse_real_node(char *bnode, uint8_t size) return g_node_new(data); } -GNode *parse_string_node(char *bnode, uint8_t size) +plist_t parse_string_node(char *bnode, uint8_t size) { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_STRING; data->strval = (char *) malloc(sizeof(char) * (size + 1)); @@ -148,9 +148,9 @@ GNode *parse_string_node(char *bnode, uint8_t size) return g_node_new(data); } -GNode *parse_unicode_node(char *bnode, uint8_t size) +plist_t parse_unicode_node(char *bnode, uint8_t size) { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_UNICODE; data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1)); @@ -160,9 +160,9 @@ GNode *parse_unicode_node(char *bnode, uint8_t size) return g_node_new(data); } -GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size) +plist_t parse_data_node(char *bnode, uint64_t size, uint32_t ref_size) { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_DATA; data->length = size; @@ -172,9 +172,9 @@ GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size) return g_node_new(data); } -GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size) +plist_t parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size) { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_DICT; data->length = size; @@ -184,9 +184,9 @@ GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size) return g_node_new(data); } -GNode *parse_array_node(char *bnode, uint64_t size, uint32_t ref_size) +plist_t parse_array_node(char *bnode, uint64_t size, uint32_t ref_size) { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_ARRAY; data->length = size; @@ -198,7 +198,7 @@ GNode *parse_array_node(char *bnode, uint64_t size, uint32_t ref_size) -GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) +plist_t parse_bin_node(char *object, uint8_t dict_size, char **next_object) { if (!object) return NULL; @@ -214,7 +214,7 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) case BPLIST_TRUE: { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_BOOLEAN; data->boolval = TRUE; return g_node_new(data); @@ -222,7 +222,7 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) case BPLIST_FALSE: { - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_BOOLEAN; data->boolval = FALSE; return g_node_new(data); @@ -299,8 +299,8 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) gpointer copy_plist_data(gconstpointer src, gpointer data) { - struct plist_data *srcdata = (struct plist_data *) src; - struct plist_data *dstdata = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t srcdata = (plist_data_t) src; + plist_data_t dstdata = plist_new_plist_data(); dstdata->type = srcdata->type; dstdata->length = srcdata->length; @@ -336,7 +336,7 @@ gpointer copy_plist_data(gconstpointer src, gpointer data) return dstdata; } -void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) +void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) { //first check we have enough data if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE)) @@ -393,7 +393,7 @@ void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) for (i = 0; i < num_objects; i++) { log_debug_msg("parse_nodes: on node %i\n", i); - struct plist_data *data = (struct plist_data *) nodeslist[i]->data; + plist_data_t data = plist_get_data(nodeslist[i]); switch (data->type) { case PLIST_DICT: @@ -406,7 +406,7 @@ void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) index2 = swap_n_bytes(data->buff + str_j, dict_param_size); //first one is actually a key - ((struct plist_data *) nodeslist[index1]->data)->type = PLIST_KEY; + plist_get_data(nodeslist[index1])->type = PLIST_KEY; if (G_NODE_IS_ROOT(nodeslist[index1])) g_node_append(nodeslist[i], nodeslist[index1]); @@ -446,7 +446,7 @@ void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) guint plist_data_hash(gconstpointer key) { - struct plist_data *data = (struct plist_data *) ((GNode *) key)->data; + plist_data_t data = plist_get_data(key); guint hash = data->type; guint i = 0; @@ -497,8 +497,8 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) if (!((GNode *) a)->data || !((GNode *) b)->data) return FALSE; - struct plist_data *val_a = (struct plist_data *) ((GNode *) a)->data; - struct plist_data *val_b = (struct plist_data *) ((GNode *) b)->data; + plist_data_t val_a = plist_get_data(a); + plist_data_t val_b = plist_get_data(b); if (val_a->type != val_b->type) return FALSE; @@ -718,7 +718,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) for (i = 0; i < num_objects; i++) { offsets[i] = bplist_buff->len; - struct plist_data *data = (struct plist_data *) ((GNode *) g_ptr_array_index(objects, i))->data; + plist_data_t data = plist_get_data(g_ptr_array_index(objects, i)); switch (data->type) { case PLIST_BOOLEAN: diff --git a/src/plist.c b/src/plist.c index 932ea5e..172eceb 100644 --- a/src/plist.c +++ b/src/plist.c @@ -28,23 +28,45 @@ #include #include +plist_t plist_new_node(plist_data_t data) +{ + return (plist_t)g_node_new(data); +} + +plist_data_t plist_get_data(plist_t node) +{ + if (!node) + return NULL; + return ((GNode*)node)->data; +} + +plist_data_t plist_new_plist_data() +{ + plist_data_t data = (plist_data_t) calloc(sizeof(struct plist_data_s), 1); + return data; +} + +void plist_free_plist_data(plist_data_t data) +{ + free(data); +} void plist_new_dict(plist_t * plist) { if (*plist != NULL) return; - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_DICT; - *plist = g_node_new(data); + *plist = plist_new_node(data); } void plist_new_array(plist_t * plist) { if (*plist != NULL) return; - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_ARRAY; - *plist = g_node_new(data); + *plist = plist_new_node(data); } void plist_new_dict_in_plist(plist_t plist, plist_t * dict) @@ -52,9 +74,9 @@ void plist_new_dict_in_plist(plist_t plist, plist_t * dict) if (!plist || *dict) return; - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_DICT; - *dict = g_node_new(data); + *dict = plist_new_node(data); g_node_append(plist, *dict); } @@ -72,14 +94,14 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu if (!dict || !key || !value) return; - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); data->type = PLIST_KEY; data->strval = strdup(key); - GNode *keynode = g_node_new(data); + plist_t keynode = plist_new_node(data); g_node_append(dict, keynode); //now handle value - struct plist_data *val = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t val = plist_new_plist_data(); val->type = type; val->length = length; @@ -108,7 +130,7 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu default: break; } - GNode *valnode = g_node_new(val); + plist_t valnode = plist_new_node(val); g_node_append(dict, valnode); } @@ -117,24 +139,41 @@ void plist_free(plist_t plist) g_node_destroy(plist); } -plist_t find_query_node(plist_t plist, char *key, char *request) +plist_t plist_get_first_child(plist_t node) +{ + return (plist_t)g_node_first_child( (GNode*)node ); +} + +plist_t plist_get_next_sibling(plist_t node) +{ + return (plist_t)g_node_next_sibling( (GNode*)node ); +} + +plist_t plist_get_prev_sibling(plist_t node) +{ + return (plist_t)g_node_prev_sibling( (GNode*)node ); +} + +plist_t plist_find_query_node(plist_t plist, char *key, char *request) { if (!plist) return NULL; - GNode *current = NULL; - for (current = plist->children; current; current = current->next) { + plist_t current = NULL; + plist_t next = NULL; + for (current = plist_get_first_child(plist); current; current = next) { - struct plist_data *data = (struct plist_data *) current->data; + next = plist_get_next_sibling(current); + plist_data_t data = plist_get_data(current); - if (data->type == PLIST_KEY && !strcmp(data->strval, key) && current->next) { + if (data->type == PLIST_KEY && !strcmp(data->strval, key) && next) { - data = (struct plist_data *) current->next->data; + data = plist_get_data(next); if (data->type == PLIST_STRING && !strcmp(data->strval, request)) - return current->next; + return next; } if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { - GNode *sub = find_query_node(current, key, request); + plist_t sub = plist_find_query_node(current, key, request); if (sub) return sub; } @@ -142,7 +181,7 @@ plist_t find_query_node(plist_t plist, char *key, char *request) return NULL; } -char compare_node_value(plist_type type, struct plist_data *data, void *value) +char compare_node_value(plist_type type, plist_data_t data, void *value) { char res = FALSE; switch (type) { @@ -174,21 +213,21 @@ char compare_node_value(plist_type type, struct plist_data *data, void *value) return res; } -plist_t find_node(plist_t plist, plist_type type, void *value) +plist_t plist_find_node(plist_t plist, plist_type type, void *value) { if (!plist) return NULL; - GNode *current = NULL; - for (current = plist->children; current; current = current->next) { + plist_t current = NULL; + for (current = plist_get_first_child(plist); current; current = plist_get_next_sibling(current)) { - struct plist_data *data = (struct plist_data *) current->data; + plist_data_t data = plist_get_data(current); if (data->type == type && compare_node_value(type, data, value)) { return current; } if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { - GNode *sub = find_node(current, type, value); + plist_t sub = plist_find_node(current, type, value); if (sub) return sub; } @@ -196,12 +235,12 @@ plist_t find_node(plist_t plist, plist_type type, void *value) return NULL; } -void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * length) +void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length) { if (!node) return; - struct plist_data *data = (struct plist_data *) node->data; + plist_data_t data = plist_get_data(node); *type = data->type; *length = data->length; @@ -236,16 +275,18 @@ void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * plist_type plist_get_node_type(plist_t node) { - if (node && node->data) - return ((struct plist_data *) node->data)->type; - else - return PLIST_NONE; + if (node) { + plist_data_t data = plist_get_data(node); + if (data) + return data->type; + } + return PLIST_NONE; } uint64_t plist_get_node_uint_val(plist_t node) { if (PLIST_UINT == plist_get_node_type(node)) - return ((struct plist_data *) node->data)->intval; + return plist_get_data(node)->intval; else return 0; } diff --git a/src/plist.h b/src/plist.h index 1dc464a..ca3201a 100644 --- a/src/plist.h +++ b/src/plist.h @@ -22,6 +22,8 @@ #ifndef PLIST_H #define PLIST_H +#include "plist/plist.h" + #include #include @@ -31,22 +33,10 @@ #include -typedef enum { - PLIST_BOOLEAN, - PLIST_UINT, - PLIST_REAL, - PLIST_STRING, - PLIST_UNICODE, - PLIST_ARRAY, - PLIST_DICT, - PLIST_DATE, - PLIST_DATA, - PLIST_KEY, - PLIST_NONE -} plist_type; -struct plist_data { + +struct plist_data_s { union { char boolval; uint64_t intval; @@ -59,25 +49,12 @@ struct plist_data { plist_type type; }; +typedef struct plist_data_s* plist_data_t; +plist_t plist_new_node(plist_data_t data); +plist_data_t plist_get_data(plist_t node); +plist_data_t plist_new_plist_data(); +void plist_free_plist_data(plist_data_t node); -typedef GNode *plist_t; - - -void plist_new_dict(plist_t * plist); -void plist_new_array(plist_t * plist); -void plist_new_dict_in_plist(plist_t plist, plist_t * dict); -void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length); -void plist_free(plist_t plist); - -void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length); -void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length); - -void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist); -void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist); - -plist_t find_query_node(plist_t plist, char *key, char *request); -plist_t find_node(plist_t plist, plist_type type, void *value); -void get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length); #endif diff --git a/src/xplist.c b/src/xplist.c index 2d650b4..4f81e1b 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -120,7 +120,7 @@ void node_to_xml(GNode * node, gpointer xml_struct) return; struct xml_node *xstruct = (struct xml_node *) xml_struct; - struct plist_data *node_data = (struct plist_data *) node->data; + plist_data_t node_data = plist_get_data(node); xmlNodePtr child_node = NULL; char isStruct = FALSE; @@ -220,7 +220,7 @@ void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) if (!node) break; - struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1); + plist_data_t data = plist_new_plist_data(); GNode *subnode = g_node_new(data); if (*plist_node) g_node_append(*plist_node, subnode); @@ -303,7 +303,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length); } -void xml_to_plist(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); xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); -- cgit v1.1-32-gdbae