Commit 024e755d authored by Nikias Bassen's avatar Nikias Bassen

Make libplist glib free

parent 94cb55d3
......@@ -16,7 +16,6 @@ IF(NOT DEFINED CMAKE_INSTALL_LIBDIR)
ENDIF(NOT DEFINED CMAKE_INSTALL_LIBDIR)
FIND_PACKAGE( LibXml2 REQUIRED )
FIND_PACKAGE( GLIB2 REQUIRED )
OPTION(ENABLE_PYTHON "Enable Python bindings (needs Swig)" ON)
......@@ -31,6 +30,7 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include
ENABLE_TESTING()
ADD_SUBDIRECTORY( libcnary )
ADD_SUBDIRECTORY( src )
ADD_SUBDIRECTORY( plutil )
ADD_SUBDIRECTORY( include )
......@@ -40,6 +40,10 @@ IF ( SWIG_FOUND AND PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND )
ADD_SUBDIRECTORY( swig )
ENDIF ( SWIG_FOUND AND PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND )
INCLUDE (TestBigEndian)
TEST_BIG_ENDIAN(endianess)
SET ( PLIST_BYTE_ORDER ${endianess} )
# add uninstall target
CONFIGURE_FILE( "${CMAKE_SOURCE_DIR}/cmake/modules/cmake_uninstall.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)
ADD_CUSTOM_TARGET(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
......
......@@ -6,7 +6,7 @@ includedir=${CMAKE_INSTALL_PREFIX}/include
Name: libplist
Description: A library to handle Apple Property Lists whereas they are binary or XML
Version: ${LIBPLIST_VERSION}
Requires.private: libxml-2.0 >= 2.6.30 glib-2.0 >= 2.14.1
Requires.private: libxml-2.0 >= 2.6.30
Libs: -L${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} -lplist
Cflags: -I${CMAKE_INSTALL_PREFIX}/include
INCLUDE_DIRECTORIES( ${LIBXML2_INCLUDE_DIR} ${GLIB2_INCLUDE_DIR} )
INCLUDE_DIRECTORIES( ${LIBXML2_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/libcnary/include )
SET(libplist_SRC
plist.c
hashtable.c
bytearray.c
ptrarray.c
bplist.c
base64.c
xplist.c )
SET(libplist++_SRC
......@@ -20,8 +24,18 @@ SET(libplist++_SRC
Dictionary.cpp
)
ADD_LIBRARY( plist SHARED ${libplist_SRC} )
TARGET_LINK_LIBRARIES( plist ${LIBXML2_LIBRARIES} ${GLIB2_LIBRARIES} )
SET(libcnary_object_files
${CMAKE_BINARY_DIR}/libcnary/CMakeFiles/libcnary.dir/iterator.c.o
${CMAKE_BINARY_DIR}/libcnary/CMakeFiles/libcnary.dir/list.c.o
${CMAKE_BINARY_DIR}/libcnary/CMakeFiles/libcnary.dir/node.c.o
${CMAKE_BINARY_DIR}/libcnary/CMakeFiles/libcnary.dir/node_iterator.c.o
${CMAKE_BINARY_DIR}/libcnary/CMakeFiles/libcnary.dir/node_list.c.o
)
SET_SOURCE_FILES_PROPERTIES(${libcnary_object_files} PROPERTIES EXTERNAL_OBJECT true GENERATED true)
ADD_LIBRARY( plist SHARED ${libplist_SRC} ${libcnary_object_files} )
TARGET_LINK_LIBRARIES( plist ${LIBXML2_LIBRARIES} )
SET_TARGET_PROPERTIES( plist PROPERTIES VERSION ${LIBPLIST_LIBVERSION} )
SET_TARGET_PROPERTIES( plist PROPERTIES SOVERSION ${LIBPLIST_SOVERSION} )
if(APPLE)
......
/*
* base64.c
* base64 encode/decode implementation
*
* Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "base64.h"
static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char base64_pad = '=';
static const int8_t base64_table[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
char *base64encode(const unsigned char *buf, size_t *size)
{
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];
while (n < *size) {
input[0] = buf[n++];
input[1] = (n < *size) ? buf[n++] : 0;
input[2] = (n < *size) ? buf[n++] : 0;
output[0] = input[0] >> 2;
output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
output[3] = input[2] & 63;
outbuf[m++] = base64_str[(int)output[0]];
outbuf[m++] = base64_str[(int)output[1]];
outbuf[m++] = base64_str[(int)output[2]];
outbuf[m++] = base64_str[(int)output[3]];
}
while ((m % 4) != 0) {
outbuf[m++] = base64_pad;
}
outbuf[m] = 0; // 0-termination!
*size = m;
return outbuf;
}
static int base64decode_block(unsigned char *target, const char *data, size_t data_size)
{
int w1,w2,w3,w4;
int n,i;
if (!data || (data_size <= 0)) {
return 0;
}
n = 0;
i = 0;
while (n < data_size-3) {
w1 = base64_table[(int)data[n]];
w2 = base64_table[(int)data[n+1]];
w3 = base64_table[(int)data[n+2]];
w4 = base64_table[(int)data[n+3]];
if (w2 >= 0) {
target[i++] = (char)((w1*4 + (w2 >> 4)) & 255);
}
if (w3 >= 0) {
target[i++] = (char)((w2*16 + (w3 >> 2)) & 255);
}
if (w4 >= 0) {
target[i++] = (char)((w3*64 + w4) & 255);
}
n+=4;
}
return i;
}
unsigned char *base64decode(const char *buf, size_t *size)
{
if (!buf) return;
size_t len = strlen(buf);
if (len <= 0) return;
unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3);
unsigned char *line;
int p = 0;
line = (unsigned char*)strtok((char*)buf, "\r\n\t ");
while (line) {
p+=base64decode_block(outbuf+p, line, strlen((char*)line));
// get next line of base64 encoded block
line = (unsigned char*)strtok(NULL, "\r\n\t ");
}
outbuf[p] = 0;
*size = p;
return outbuf;
}
/*
* base64.h
* base64 encode/decode implementation
*
* Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef BASE64_H
#define BASE64_H
#include <stdlib.h>
#include "common.h"
_PLIST_INTERNAL char *base64encode(const unsigned char *buf, size_t *size);
_PLIST_INTERNAL unsigned char *base64decode(const char *buf, size_t *size);
#endif
This diff is collapsed.
/*
* bytearray.c
* simple byte array implementation
*
* Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "bytearray.h"
bytearray_t *byte_array_new()
{
bytearray_t *a = (bytearray_t*)malloc(sizeof(bytearray_t));
a->data = malloc(256);
a->len = 0;
a->capacity = 256;
return a;
}
void byte_array_free(bytearray_t *ba)
{
if (!ba) return;
if (ba->data) {
free(ba->data);
}
free(ba);
}
void byte_array_append(bytearray_t *ba, void *buf, size_t len)
{
if (!ba || !ba->data || (len <= 0)) return;
size_t 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);
ba->len += len;
}
/*
* bytearray.h
* header file for simple byte array implementation
*
* Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef BYTEARRAY_H
#define BYTEARRAY_H
#include <stdlib.h>
#include "common.h"
typedef struct bytearray_t {
void *data;
size_t len;
size_t capacity;
} bytearray_t;
_PLIST_INTERNAL bytearray_t *byte_array_new();
_PLIST_INTERNAL void byte_array_free(bytearray_t *ba);
_PLIST_INTERNAL void byte_array_append(bytearray_t *ba, void *buf, size_t len);
#endif
#ifndef COMMON_H
#define COMMON_H
#define PLIST_LITTLE_ENDIAN 0
#define PLIST_BIG_ENDIAN 1
#if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
# define _PLIST_INTERNAL __attribute__((visibility("hidden")))
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
# define _PLIST_INTERNAL __hidden
#else /* not gcc >= 4 and not Sun Studio >= 8 */
# define _PLIST_INTERNAL
#endif /* GNUC >= 4 */
#endif
/*
* hashtable.c
* really simple hash table implementation
*
* Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hashtable.h"
hashtable_t* hash_table_new(hash_func_t hash_func, compare_func_t compare_func)
{
hashtable_t* ht = (hashtable_t*)malloc(sizeof(hashtable_t));
int i;
for (i = 0; i < 256; i++) {
ht->entries[i] = NULL;
}
ht->count = 0;
ht->hash_func = hash_func;
ht->compare_func = compare_func;
return ht;
}
void hash_table_destroy(hashtable_t *ht)
{
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;
e = e->next;
free(old);
}
}
}
free(ht);
}
void hash_table_insert(hashtable_t* ht, void *key, void *value)
{
if (!ht || !key) return;
int i;
unsigned int hash = ht->hash_func(key);
int idx0 = hash & 0xFF;
// get the idx0 list
hashentry_t* e = ht->entries[idx0];
while (e) {
if (ht->compare_func(e->key, key)) {
// element already present. replace value.
e->value = value;
return;
}
e = e->next;
}
// 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->key = key;
entry->value = value;
if (!ht->entries[idx0]) {
// first entry
entry->next = NULL;
} else {
// add to list
entry->next = ht->entries[idx0];
}
ht->entries[idx0] = entry;
ht->count++;
}
void* hash_table_lookup(hashtable_t* ht, void *key)
{
if (!ht || !key) return NULL;
unsigned int hash = ht->hash_func(key);
int idx0 = hash & 0xFF;
hashentry_t* e = ht->entries[idx0];
while (e) {
if (ht->compare_func(e->key, key)) {
return e->value;
}
e = e->next;
}
return NULL;
}
/*
* hashtable.h
* header file for really simple hash table implementation
*
* Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <stdlib.h>
#include "common.h"
typedef struct hashentry_t {
void *key;
void *value;
void *next;
} hashentry_t;
typedef unsigned int(*hash_func_t)(const void* key);
typedef int (*compare_func_t)(const void *a, const void *b);
typedef struct hashtable_t {
hashentry_t *entries[256];
size_t count;
hash_func_t hash_func;
compare_func_t compare_func;
} hashtable_t;
_PLIST_INTERNAL hashtable_t* hash_table_new(hash_func_t hash_func, compare_func_t compare_func);
_PLIST_INTERNAL void hash_table_destroy(hashtable_t *ht);
_PLIST_INTERNAL void hash_table_insert(hashtable_t* ht, void *key, void *value);
_PLIST_INTERNAL void* hash_table_lookup(hashtable_t* ht, void *key);
#endif
This diff is collapsed.
......@@ -23,11 +23,10 @@
#define PLIST_H
#include "plist/plist.h"
#include "common.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <glib.h>
#ifdef _MSC_VER
#pragma warning(disable:4996)
......@@ -44,7 +43,7 @@ struct plist_data_s
double realval;
char *strval;
uint8_t *buff;
GTimeVal timeval;
struct timeval timeval;
};
uint64_t length;
plist_type type;
......@@ -52,10 +51,10 @@ struct plist_data_s
typedef struct plist_data_s *plist_data_t;
G_GNUC_INTERNAL plist_t plist_new_node(plist_data_t data);
G_GNUC_INTERNAL plist_data_t plist_get_data(const plist_t node);
G_GNUC_INTERNAL plist_data_t plist_new_plist_data(void);
G_GNUC_INTERNAL gboolean plist_data_compare(gconstpointer a, gconstpointer b);
_PLIST_INTERNAL plist_t plist_new_node(plist_data_t data);
_PLIST_INTERNAL plist_data_t plist_get_data(const plist_t node);
_PLIST_INTERNAL plist_data_t plist_new_plist_data(void);
_PLIST_INTERNAL int plist_data_compare(const void *a, const void *b);
#endif
/*
* ptrarray.c
* simple pointer array implementation
*
* Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ptrarray.h"
ptrarray_t *ptr_array_new(int capacity)
{
ptrarray_t *pa = (ptrarray_t*)malloc(sizeof(ptrarray_t));
pa->pdata = (void**)malloc(sizeof(void*) * capacity);
pa->capacity = capacity;
pa->capacity_step = (capacity > 64) ? 64 : capacity;
pa->len = 0;
return pa;
}
void ptr_array_free(ptrarray_t *pa)
{
if (!pa) return;
if (pa->pdata) {
free(pa->pdata);
}
free(pa);
}
void ptr_array_add(ptrarray_t *pa, void *data)
{
if (!pa || !pa->pdata || !data) return;
size_t 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;
}
pa->pdata[pa->len] = data;
pa->len++;
}
void* ptr_array_index(ptrarray_t *pa, size_t index)
{
if (!pa) return NULL;
if (index >= pa->len) {
return NULL;
}
return pa->pdata[index];
}
/*
* ptrarray.h
* header file for simple pointer array implementation
*
* Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PTRARRAY_H
#define PTRARRAY_H
#include <stdlib.h>
#include "common.h"
typedef struct ptrarray_t {
void **pdata;
size_t len;
size_t capacity;
size_t capacity_step;
} ptrarray_t;
_PLIST_INTERNAL ptrarray_t *ptr_array_new(int capacity);
_PLIST_INTERNAL void ptr_array_free(ptrarray_t *pa);
_PLIST_INTERNAL void ptr_array_add(ptrarray_t *pa, void *data);
_PLIST_INTERNAL void* ptr_array_index(ptrarray_t *pa, size_t index);
#endif
......@@ -24,6 +24,7 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <inttypes.h>
#include <locale.h>
......@@ -31,7 +32,12 @@
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <node.h>
#include <node_list.h>
#include <node_iterator.h>
#include "plist.h"
#include "base64.h"
#define XPLIST_TEXT BAD_CAST("text")
#define XPLIST_KEY BAD_CAST("key")
......@@ -61,18 +67,22 @@ static const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
*
* @return The formatted string.
*/
static gchar *format_string(const char *buf, int cols, int depth)
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 len = strlen(buf);
int nlines = len / cols + 1;
gchar *new_buf = (gchar *) g_malloc0(nlines * colw + depth + 1);
char *new_buf = NULL;
int i = 0;
int j = 0;
assert(cols >= 0);
assert(depth >= 0);
new_buf = (char*) malloc(nlines * colw + depth + 1);
assert(new_buf != 0);
memset(new_buf, 0, nlines * colw + depth + 1);
// Inserts new lines and tabs at appropriate locations
for (i = 0; i < nlines; i++)
{
......@@ -116,7 +126,7 @@ static xmlDocPtr new_xml_plist(void)
return plist_xml;
}
static void node_to_xml(GNode * node, gpointer xml_struct)
static void node_to_xml(node_t* node, void *xml_struct)
{
struct xml_node *xstruct = NULL;
plist_data_t node_data = NULL;
......@@ -125,10 +135,10 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
char isStruct = FALSE;
const xmlChar *tag = NULL;
gchar *val = NULL;
char *val = NULL;
//for base64
gchar *valtmp = NULL;
char *valtmp = NULL;
uint32_t i = 0;
......@@ -151,31 +161,32 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
case PLIST_UINT:
tag = XPLIST_INT;
val = g_strdup_printf("%"PRIu64, node_data->intval);
(void)asprintf(&val, "%"PRIu64, node_data->intval);
break;
case PLIST_REAL:
tag = XPLIST_REAL;
val = g_strdup_printf("%f", node_data->realval);
(void)asprintf(&val, "%f", node_data->realval);
break;
case PLIST_STRING:
tag = XPLIST_STRING;
val = g_strdup(node_data->strval);
val = strdup(node_data->strval);
break;
case PLIST_KEY:
tag = XPLIST_KEY;
val = g_strdup((gchar *) node_data->strval);
val = strdup((char*) node_data->strval);
break;
case PLIST_DATA:
tag = XPLIST_DATA;
if (node_data->length)
{
valtmp = g_base64_encode(node_data->buff, node_data->length);
val = format_string(valtmp, 68, xstruct->depth);
g_free(valtmp);
size_t len = node_data->length;
valtmp = base64encode(node_data->buff, &len);
val = format_string(valtmp, len, 68, xstruct->depth);
free(valtmp);
}
break;
case PLIST_ARRAY:
......@@ -188,7 +199,18 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
break;
case PLIST_DATE:
tag = XPLIST_DATE;
val = g_time_val_to_iso8601(&node_data->timeval);
{
time_t time = (time_t)node_data->timeval.tv_sec;
struct tm *btime = localtime(&time);
if (btime) {
val = (char*)malloc(24);
memset(val, 0, 24);
if (strftime(val, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) {
free (val);
val = NULL;
}
}
}
break;
default:
break;
......@@ -205,7 +227,9 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
} else
child_node = xmlNewChild(xstruct->xml, NULL, tag, BAD_CAST(val));
xmlNodeAddContent(xstruct->xml, BAD_CAST("\n"));
g_free(val);
if (val) {
free(val);
}
//add return for structured types
if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT)
......@@ -214,7 +238,12 @@ static void node_to_xml(GNode * node, gpointer xml_struct)
if (isStruct)
{
struct xml_node child = { child_node, xstruct->depth + 1 };
g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child);
node_iterator_t *ni = node_iterator_create(node->children);
node_t *ch;
while ((ch = node_iterator_next(ni))) {
node_to_xml(ch, &child);
}
node_iterator_destroy(ni);
}
//fix indent for structured types
if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT)
......@@ -236,7 +265,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
plist_t subnode = NULL;
//for string
glong len = 0;
long len = 0;
int type = 0;
if (!xml_node)
......@@ -253,7 +282,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
data = plist_new_plist_data();
subnode = plist_new_node(data);
if (*plist_node)
g_node_append(*plist_node, subnode);
node_attach(*plist_node, subnode);
else
*plist_node = subnode;
......@@ -276,7 +305,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
if (!xmlStrcmp(node->name, XPLIST_INT))
{
xmlChar *strval = xmlNodeGetContent(node);
data->intval = g_ascii_strtoull((char *) strval, NULL, 0);
data->intval = strtoull((char*)strval, NULL, 0);
data->type = PLIST_UINT;
data->length = 8;
xmlFree(strval);
......@@ -296,9 +325,17 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
if (!xmlStrcmp(node->name, XPLIST_DATE))
{
xmlChar *strval = xmlNodeGetContent(node);
g_time_val_from_iso8601((char *) strval, &data->timeval);
time_t time = 0;
if (strlen(strval) >= 11) {
struct tm btime;
memset(&btime, 0, sizeof(struct tm));
strptime((char*)strval, "%Y-%m-%dT%H:%M:%SZ", &btime);
time = mktime(&btime);
}
data->timeval.tv_sec = (long)time;
data->timeval.tv_usec = 0;
data->type = PLIST_DATE;
data->length = sizeof(GTimeVal);
data->length = sizeof(struct timeval);
xmlFree(strval);
continue;
}
......@@ -332,11 +369,11 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
if (!xmlStrcmp(node->name, XPLIST_DATA))
{
xmlChar *strval = xmlNodeGetContent(node);
gsize size = 0;
guchar *dec = g_base64_decode((char *) strval, &size);
size_t size = 0;
unsigned char *dec = base64decode((char*)strval, &size);
data->buff = (uint8_t *) malloc(size * sizeof(uint8_t));
memcpy(data->buff, dec, size * sizeof(uint8_t));
g_free(dec);
free(dec);
data->length = size;
data->type = PLIST_DATA;
xmlFree(strval);
......
#build the test executable
INCLUDE_DIRECTORIES( ${GLIB2_INCLUDE_DIR} )
INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/libcnary/include )
SET(plist_test_SRC
plist_test.c)
......
......@@ -27,7 +27,7 @@
#include <string.h>
#include <sys/stat.h>
#include <glib.h>
#include <node.h>
#ifdef _MSC_VER
#pragma warning(disable:4996)
......@@ -35,12 +35,12 @@
static plist_t plist_get_first_child(plist_t node)
{
return (plist_t) g_node_first_child((GNode *) node);
return (plist_t) node_first_child((node_t*) node);
}
static plist_t plist_get_next_sibling(plist_t node)
{
return (plist_t) g_node_next_sibling((GNode *) node);
return (plist_t) node_next_sibling((node_t*) node);
}
static char compare_plist(plist_t node_l, plist_t node_r)
......
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