include/plist/Date.h | 5 +++++ libcnary/cnary.c | 13 +++++++++---- libcnary/node.c | 27 ++++++++++++++++---------- libcnary/node_list.c | 17 ++++++++++------- src/Dictionary.cpp | 4 ++++ src/base64.c | 27 +++++++++++++++----------- src/bplist.c | 52 ++++++++++++++++++++++++++++++++++++++++++++------ src/bytearray.c | 5 +++-- src/hashtable.c | 26 ++++++++++++++++--------- src/plist.c | 10 ++++++---- src/plist.h | 5 +++++ src/ptrarray.c | 3 ++- src/xplist.c | 23 ++++++++++++++-------- 13 files changed, 155 insertions(+), 62 deletions(-) diff --git a/include/plist/Date.h b/include/plist/Date.h index ea0b270..c159fef 100644 --- a/include/plist/Date.h +++ b/include/plist/Date.h @@ -24,7 +24,12 @@ #include #include +#if defined(_MSC_VER) +#include +#include +#else #include +#endif namespace PList { diff --git a/libcnary/cnary.c b/libcnary/cnary.c index 07321fc..b08c2f1 100644 --- a/libcnary/cnary.c +++ b/libcnary/cnary.c @@ -26,16 +26,21 @@ #include "node.h" int main(int argc, char* argv[]) { + node_t* root; + node_t* one; + node_t* two; + node_t* three; + puts("Creating root node"); - node_t* root = node_create(NULL, NULL); + root = node_create(NULL, NULL); puts("Creating child 1 node"); - node_t* one = node_create(root, NULL); + one = node_create(root, NULL); puts("Creating child 2 node"); - node_t* two = node_create(root, NULL); + two = node_create(root, NULL); puts("Creating child 3 node"); - node_t* three = node_create(one, NULL); + three = node_create(one, NULL); puts("Debugging root node"); node_debug(root); diff --git a/libcnary/node.c b/libcnary/node.c index 0a8f414..2865860 100644 --- a/libcnary/node.c +++ b/libcnary/node.c @@ -79,6 +79,7 @@ node_t* node_create(node_t* parent, void* data) { } int node_attach(node_t* parent, node_t* child) { + int res; if (!parent || !child) return -1; child->isLeaf = TRUE; child->isRoot = FALSE; @@ -87,7 +88,7 @@ int node_attach(node_t* parent, node_t* child) { if(parent->isLeaf == TRUE) { parent->isLeaf = FALSE; } - int res = node_list_add(parent->children, child); + res = node_list_add(parent->children, child); if (res == 0) { parent->count++; } @@ -95,8 +96,9 @@ int node_attach(node_t* parent, node_t* child) { } int node_detach(node_t* parent, node_t* child) { + int index; if (!parent || !child) return -1; - int index = node_list_remove(parent->children, child); + index = node_list_remove(parent->children, child); if (index >= 0) { parent->count--; } @@ -105,6 +107,7 @@ int node_detach(node_t* parent, node_t* child) { int node_insert(node_t* parent, unsigned int index, node_t* child) { + int res; if (!parent || !child) return -1; child->isLeaf = TRUE; child->isRoot = FALSE; @@ -113,7 +116,7 @@ int node_insert(node_t* parent, unsigned int index, node_t* child) if(parent->isLeaf == TRUE) { parent->isLeaf = FALSE; } - int res = node_list_insert(parent->children, index, child); + res = node_list_insert(parent->children, index, child); if (res == 0) { parent->count++; } @@ -154,10 +157,11 @@ unsigned int node_n_children(struct node_t* node) node_t* node_nth_child(struct node_t* node, unsigned int n) { + int index = 0; + int found = 0; + node_t *ch; + if (!node || !node->children || !node->children->begin) return NULL; - int index = 0; - int found = 0; - node_t *ch; for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { if (index++ == n) { found = 1; @@ -190,10 +194,11 @@ node_t* node_next_sibling(struct node_t* node) int node_child_position(struct node_t* parent, node_t* child) { - if (!parent || !parent->children || !parent->children->begin || !child) return -1; int index = 0; int found = 0; node_t *ch; + + if (!parent || !parent->children || !parent->children->begin || !child) return -1; for (ch = node_first_child(parent); ch; ch = node_next_sibling(ch)) { if (ch == child) { found = 1; @@ -209,13 +214,15 @@ int node_child_position(struct node_t* parent, node_t* child) node_t* node_copy_deep(node_t* node, copy_func_t copy_func) { - if (!node) return NULL; void *data = NULL; + node_t* copy; + node_t* ch; + + if (!node) return NULL; if (copy_func) { data = copy_func(node->data); } - node_t* copy = node_create(NULL, data); - node_t* ch; + copy = node_create(NULL, data); for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { node_t* cc = node_copy_deep(ch, copy_func); node_attach(copy, cc); diff --git a/libcnary/node_list.c b/libcnary/node_list.c index 5b291e7..a514233 100644 --- a/libcnary/node_list.c +++ b/libcnary/node_list.c @@ -49,10 +49,11 @@ node_list_t* node_list_create(node_t* node) { } int node_list_add(node_list_t* list, node_t* node) { + node_t* last; if (!list || !node) return -1; // Find the last element in the list - node_t* last = list->end; + last = list->end; // Setup our new node as the new last element node->next = NULL; @@ -73,16 +74,17 @@ int node_list_add(node_list_t* list, node_t* node) { } int node_list_insert(node_list_t* list, unsigned int index, node_t* node) { + node_t* cur; + unsigned int pos = 0; + node_t* prev = NULL; + if (!list || !node) return -1; if (index >= list->count) { return node_list_add(list, node); } // Get the first element in the list - node_t* cur = list->begin; - - unsigned int pos = 0; - node_t* prev = NULL; + cur = list->begin; if (index > 0) { while (pos < index) { @@ -121,11 +123,12 @@ int node_list_insert(node_list_t* list, unsigned int index, node_t* node) { } int node_list_remove(node_list_t* list, node_t* node) { + int index = 0; + node_t* n; + if (!list || !node) return -1; if (list->count == 0) return -1; - int index = 0; - node_t* n; for (n = list->begin; n; n = n->next) { if (node == n) { node_t* newnode = node->next; diff --git a/src/Dictionary.cpp b/src/Dictionary.cpp index 0030df6..07cdba5 100644 --- a/src/Dictionary.cpp +++ b/src/Dictionary.cpp @@ -151,7 +151,11 @@ Dictionary::iterator Dictionary::Insert(const std::string& key, Node* node) _map[key] = clone; return _map.find(key); } +#if defined(_MSC_VER) + return iterator(); +#else return iterator(NULL); +#endif } void Dictionary::Remove(Node* node) diff --git a/src/base64.c b/src/base64.c index e558d9e..e4bbfc6 100644 --- a/src/base64.c +++ b/src/base64.c @@ -45,13 +45,16 @@ static const signed char base64_table[256] = { char *base64encode(const unsigned char *buf, size_t *size) { + int outlen; + char *outbuf; + size_t n = 0; + size_t m = 0; + unsigned char input[3]; + unsigned int output[4]; + if (!buf || !size || !(*size > 0)) return NULL; - int outlen = (*size / 3) * 4; - char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0' - size_t n = 0; - size_t m = 0; - unsigned char input[3]; - unsigned int output[4]; + outlen = (*size / 3) * 4; + outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0' while (n < *size) { input[0] = buf[n]; input[1] = (n+1 < *size) ? buf[n+1] : 0; @@ -104,14 +107,16 @@ static int base64decode_block(unsigned char *target, const char *data, size_t da unsigned char *base64decode(const char *buf, size_t *size) { - if (!buf) return NULL; - size_t len = strlen(buf); - if (len <= 0) return NULL; - unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3); - + size_t len; + unsigned char *outbuf; unsigned char *line; int p = 0; + if (!buf) return NULL; + len = strlen(buf); + if (len <= 0) return NULL; + outbuf = (unsigned char*)malloc((len/4)*3+3); + line = (unsigned char*)strtok((char*)buf, "\r\n\t "); while (line) { p+=base64decode_block(outbuf+p, (const char*)line, strlen((char*)line)); diff --git a/src/bplist.c b/src/bplist.c index 7893ff5..63bca34 100644 --- a/src/bplist.c +++ b/src/bplist.c @@ -96,6 +96,7 @@ union plist_uint_ptr uint64_t *u64ptr; }; +#if !defined(_MSC_VER) #define get_unaligned(ptr) \ ({ \ struct __attribute__((packed)) { \ @@ -103,6 +104,19 @@ union plist_uint_ptr } *__p = (void *) (ptr); \ __p->__v; \ }) +#else + +#define get_unaligned(ptr) get_unaligned_wrapper(ptr, sizeof(*ptr)) + +__inline __int64 get_unaligned_wrapper(void *ptr, int size) { + int i = 0; + __int64 nRs = 0; + for (i=0; i= 0x800) { @@ -746,6 +785,8 @@ static void serialize_plist(node_t* node, void* data) uint64_t *index_val = NULL; struct serialize_s *ser = (struct serialize_s *) data; uint64_t current_index = ser->objects->len; + node_iterator_t *ni; + node_t *ch; //first check that node is not yet in objects void* val = hash_table_lookup(ser->ref_table, node); @@ -763,8 +804,7 @@ static void serialize_plist(node_t* node, void* data) ptr_array_add(ser->objects, node); //now recurse on children - node_iterator_t *ni = node_iterator_create(node->children); - node_t *ch; + ni = node_iterator_create(node->children); while ((ch = node_iterator_next(ni))) { serialize_plist(ch, data); } diff --git a/src/bytearray.c b/src/bytearray.c index 0abfe49..44d4fed 100644 --- a/src/bytearray.c +++ b/src/bytearray.c @@ -41,12 +41,13 @@ void byte_array_free(bytearray_t *ba) void byte_array_append(bytearray_t *ba, void *buf, size_t len) { + size_t remaining; if (!ba || !ba->data || (len <= 0)) return; - size_t remaining = ba->capacity-ba->len; + remaining = ba->capacity-ba->len; if (len > remaining) { ba->data = realloc(ba->data, ba->capacity + (len - remaining)); ba->capacity += (len - remaining); } - memcpy(ba->data+ba->len, buf, len); + memcpy(((unsigned char*)ba->data)+ba->len, buf, len); ba->len += len; } diff --git a/src/hashtable.c b/src/hashtable.c index 08ff934..35f2ac3 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -35,15 +35,15 @@ hashtable_t* hash_table_new(hash_func_t hash_func, compare_func_t compare_func) void hash_table_destroy(hashtable_t *ht) { + int i = 0; if (!ht) return; - int i = 0; for (i = 0; i < 256; i++) { if (ht->entries[i]) { hashentry_t* e = ht->entries[i]; while (e) { - free(e->value); hashentry_t* old = e; + free(e->value); e = e->next; free(old); } @@ -54,14 +54,19 @@ void hash_table_destroy(hashtable_t *ht) void hash_table_insert(hashtable_t* ht, void *key, void *value) { + unsigned int hash; + int idx0; + hashentry_t* e; + hashentry_t* entry; + if (!ht || !key) return; - unsigned int hash = ht->hash_func(key); + hash = ht->hash_func(key); - int idx0 = hash & 0xFF; + idx0 = hash & 0xFF; // get the idx0 list - hashentry_t* e = ht->entries[idx0]; + e = ht->entries[idx0]; while (e) { if (ht->compare_func(e->key, key)) { // element already present. replace value. @@ -74,7 +79,7 @@ void hash_table_insert(hashtable_t* ht, void *key, void *value) // if we get here, the element is not yet in the list. // make a new entry. - hashentry_t* entry = (hashentry_t*)malloc(sizeof(hashentry_t)); + entry = (hashentry_t*)malloc(sizeof(hashentry_t)); entry->key = key; entry->value = value; if (!ht->entries[idx0]) { @@ -90,12 +95,15 @@ void hash_table_insert(hashtable_t* ht, void *key, void *value) void* hash_table_lookup(hashtable_t* ht, void *key) { + unsigned int hash; + int idx0; + hashentry_t* e; if (!ht || !key) return NULL; - unsigned int hash = ht->hash_func(key); + hash = ht->hash_func(key); - int idx0 = hash & 0xFF; + idx0 = hash & 0xFF; - hashentry_t* e = ht->entries[idx0]; + e = ht->entries[idx0]; while (e) { if (ht->compare_func(e->key, key)) { return e->value; diff --git a/src/plist.c b/src/plist.c index dcaf601..b452a7f 100644 --- a/src/plist.c +++ b/src/plist.c @@ -69,14 +69,15 @@ static void plist_free_data(plist_data_t data) static int plist_free_node(node_t* node) { + node_iterator_t *ni; + node_t *ch; plist_data_t data = NULL; int index = node_detach(node->parent, node); data = plist_get_data(node); plist_free_data(data); node->data = NULL; - node_iterator_t *ni = node_iterator_create(node->children); - node_t *ch; + ni = node_iterator_create(node->children); while ((ch = node_iterator_next(ni))) { plist_free_node(ch); } @@ -190,6 +191,8 @@ static void plist_copy_node(node_t *node, void *parent_node_ptr) plist_t newnode = NULL; plist_data_t data = plist_get_data(node); plist_data_t newdata = plist_new_plist_data(); + node_iterator_t *ni; + node_t *ch; assert(data); // plist should always have data @@ -223,8 +226,7 @@ static void plist_copy_node(node_t *node, void *parent_node_ptr) *(plist_t*)parent_node_ptr = newnode; } - node_iterator_t *ni = node_iterator_create(node->children); - node_t *ch; + ni = node_iterator_create(node->children); while ((ch = node_iterator_next(ni))) { plist_copy_node(ch, &newnode); } diff --git a/src/plist.h b/src/plist.h index 2a9a3b5..7689c47 100644 --- a/src/plist.h +++ b/src/plist.h @@ -27,7 +27,12 @@ #include #include +#if defined(_MSC_VER) +#include +#include +#else #include +#endif #ifdef _MSC_VER #pragma warning(disable:4996) diff --git a/src/ptrarray.c b/src/ptrarray.c index 8567752..35e4fa9 100644 --- a/src/ptrarray.c +++ b/src/ptrarray.c @@ -41,8 +41,9 @@ void ptr_array_free(ptrarray_t *pa) void ptr_array_add(ptrarray_t *pa, void *data) { + size_t remaining; if (!pa || !pa->pdata || !data) return; - size_t remaining = pa->capacity-pa->len; + remaining = pa->capacity-pa->len; if (remaining == 0) { pa->pdata = realloc(pa->pdata, sizeof(void*) * (pa->capacity + pa->capacity_step)); pa->capacity += pa->capacity_step; diff --git a/src/xplist.c b/src/xplist.c index 6147265..7fd337f 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -69,13 +69,15 @@ static const char *plist_base = "\n\ */ static char *format_string(const char *buf, size_t len, int cols, int depth) { - if (!buf || !(len > 0)) return NULL; - int colw = depth + cols + 1; - int nlines = len / cols + 1; + int colw; + int nlines; char *new_buf = NULL; int i = 0; int j = 0; + if (!buf || !(len > 0)) return NULL; + colw = depth + cols + 1; + nlines = len / cols + 1; assert(cols >= 0); assert(depth >= 0); @@ -128,9 +130,10 @@ static xmlDocPtr new_xml_plist(void) static struct node_t* new_key_node(const char* name) { + int size; plist_data_t data = plist_new_plist_data(); data->type = PLIST_KEY; - int size = strlen(name); + size = strlen(name); data->strval = strdup(name); data->length = size; return node_create(NULL, data); @@ -456,9 +459,10 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) plist_t uid = plist_dict_get_item(subnode, "CF$UID"); if (uid) { uint64_t val = 0; + plist_data_t nodedata; plist_get_uint_val(uid, &val); plist_dict_remove_item(subnode, "CF$UID"); - plist_data_t nodedata = plist_get_data((node_t*)subnode); + nodedata = plist_get_data((node_t*)subnode); free(nodedata->buff); nodedata->type = PLIST_UID; nodedata->length = sizeof(uint64_t); @@ -477,6 +481,9 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) xmlNodePtr root_node = NULL; struct xml_node root = { NULL, 0 }; int size = 0; + char *current_locale; + char *saved_locale = NULL; + xmlChar* tmp = NULL; if (!plist || !plist_xml || *plist_xml) return; @@ -484,8 +491,8 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) root_node = xmlDocGetRootElement(plist_doc); root.xml = root_node; - char *current_locale = setlocale(LC_NUMERIC, NULL); - char *saved_locale = NULL; + current_locale = setlocale(LC_NUMERIC, NULL); + saved_locale = NULL; if (current_locale) { saved_locale = strdup(current_locale); } @@ -494,7 +501,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) } node_to_xml(plist, &root); - xmlChar* tmp = NULL; + tmp = NULL; xmlDocDumpMemory(plist_doc, &tmp, &size); if (size >= 0 && tmp) {