Commit e220e2cf authored by Jonathan Beck's avatar Jonathan Beck

Add plutil and do some cleaning.

parent 3fdd24ae
AUTOMAKE_OPTIONS = foreign AUTOMAKE_OPTIONS = foreign
SUBDIRS = src include SUBDIRS = src include plutil
pkgconfigdir = $(libdir)/pkgconfig pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libplist-1.0.pc pkgconfig_DATA = libplist-1.0.pc
......
...@@ -44,4 +44,4 @@ if test "$no_debug_code" = true; then ...@@ -44,4 +44,4 @@ if test "$no_debug_code" = true; then
AC_DEFINE(STRIP_DEBUG_CODE,1,[Strip debug reporting code]) AC_DEFINE(STRIP_DEBUG_CODE,1,[Strip debug reporting code])
fi fi
AC_OUTPUT(Makefile src/Makefile include/Makefile libplist-1.0.pc) AC_OUTPUT(Makefile src/Makefile include/Makefile plutil/Makefile libplist-1.0.pc)
...@@ -25,7 +25,7 @@ DOXYFILE_ENCODING = UTF-8 ...@@ -25,7 +25,7 @@ DOXYFILE_ENCODING = UTF-8
# The PROJECT_NAME tag is a single word (or a sequence of words surrounded # The PROJECT_NAME tag is a single word (or a sequence of words surrounded
# by quotes) that should identify the project. # by quotes) that should identify the project.
PROJECT_NAME = libiphone PROJECT_NAME = libplist
# The PROJECT_NUMBER tag can be used to enter a project or revision number. # The PROJECT_NUMBER tag can be used to enter a project or revision number.
# This could be handy for archiving the generated documentation or # This could be handy for archiving the generated documentation or
......
...@@ -29,6 +29,46 @@ extern "C" { ...@@ -29,6 +29,46 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
typedef void* plist_t;
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;
//Plist edition
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);
//plist navigation
plist_t plist_get_first_child(plist_t node);
plist_t plist_get_next_sibling(plist_t node);
plist_t plist_get_prev_sibling(plist_t node);
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 plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist);
void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist);
plist_t plist_find_query_node(plist_t plist, char *key, char *request);
plist_t plist_find_node(plist_t plist, plist_type type, void *value);
void plist_get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length);
#ifdef __cplusplus #ifdef __cplusplus
......
INCLUDES = -I$(top_srcdir)/include
bin_PROGRAMS = plutil
plutil_SOURCES = plutil.c
plutil_LDADD = ../src/libplist.la
/*
* plutil.C
* source for plist convertion tool
*
* Copyright (c) 2008 Zach C. 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 "plist/plist.h"
#include "plutil.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
struct stat *filestats = (struct stat *) malloc(sizeof(struct stat));
Options *options = parse_arguments(argc, argv);
if (!options) {
print_usage();
return 0;
}
iphone_set_debug(options->debug);
//read input file
FILE *iplist = fopen(options->in_file, "r");
if (!iplist)
return 1;
stat(options->in_file, filestats);
char *plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1));
fread(plist_entire, sizeof(char), filestats->st_size, iplist);
fclose(iplist);
//convert one format to another
plist_t root_node = NULL;
char *plist_out = NULL;
int size = 0;
if (memcmp(plist_entire, "bplist00", 8) == 0) {
plist_from_bin(plist_entire, filestats->st_size, &root_node);
plist_to_xml(root_node, &plist_out, &size);
} else {
plist_from_xml(plist_entire, filestats->st_size, &root_node);
plist_to_bin(root_node, &plist_out, &size);
}
if (plist_out) {
if (options->out_file != NULL) {
FILE *oplist = fopen(options->out_file, "wb");
if (!oplist)
return 1;
fwrite(plist_out, size, sizeof(char), oplist);
fclose(oplist);
}
//if no output file specified, write to stdout
else
fwrite(plist_out, size, sizeof(char), stdout);
} else
printf("ERROR\n");
return 0;
}
Options *parse_arguments(int argc, char *argv[])
{
int i = 0;
Options *options = (Options *) malloc(sizeof(Options));
memset(options, 0, sizeof(Options));
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) {
if ((i + 1) == argc) {
free(options);
return NULL;
}
options->in_file = argv[i + 1];
i++;
continue;
}
if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) {
if ((i + 1) == argc) {
free(options);
return NULL;
}
options->out_file = argv[i + 1];
i++;
continue;
}
if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d") || !strcmp(argv[i], "-v")) {
options->debug = 1;
}
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
free(options);
return NULL;
}
}
if (!options->in_file /*|| !options->out_file */ ) {
free(options);
return NULL;
}
return options;
}
void print_usage()
{
printf("Usage: plistutil -i|--infile in_file.plist -o|--outfile out_file.plist [--debug]\n");
printf("\n");
printf("\t-i or --infile: The file to read in.\n");
printf("\t-o or --outfile: The file to convert to.\n");
printf("\t-d, -v or --debug: Provide extended debug information.\n\n");
}
/*
* plutil.h
* header for plist convertion tool
*
* Copyright (c) 2008 Zach C. 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
*/
typedef struct _options {
char *in_file, *out_file;
uint8_t debug, in_fmt, out_fmt;
} Options;
Options *parse_arguments(int argc, char *argv[]);
void print_usage();
...@@ -83,9 +83,9 @@ void byte_convert(char *address, size_t size) ...@@ -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_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) #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 size = 1 << size; // make length less misleading
switch (size) { switch (size) {
...@@ -114,9 +114,9 @@ GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object) ...@@ -114,9 +114,9 @@ GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object)
return g_node_new(data); 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 size = 1 << size; // make length less misleading
switch (size) { switch (size) {
...@@ -136,9 +136,9 @@ GNode *parse_real_node(char *bnode, uint8_t size) ...@@ -136,9 +136,9 @@ GNode *parse_real_node(char *bnode, uint8_t size)
return g_node_new(data); 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->type = PLIST_STRING;
data->strval = (char *) malloc(sizeof(char) * (size + 1)); data->strval = (char *) malloc(sizeof(char) * (size + 1));
...@@ -148,9 +148,9 @@ GNode *parse_string_node(char *bnode, uint8_t size) ...@@ -148,9 +148,9 @@ GNode *parse_string_node(char *bnode, uint8_t size)
return g_node_new(data); 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->type = PLIST_UNICODE;
data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1)); data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1));
...@@ -160,9 +160,9 @@ GNode *parse_unicode_node(char *bnode, uint8_t size) ...@@ -160,9 +160,9 @@ GNode *parse_unicode_node(char *bnode, uint8_t size)
return g_node_new(data); 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->type = PLIST_DATA;
data->length = size; data->length = size;
...@@ -172,9 +172,9 @@ GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size) ...@@ -172,9 +172,9 @@ GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size)
return g_node_new(data); 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->type = PLIST_DICT;
data->length = size; data->length = size;
...@@ -184,9 +184,9 @@ GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size) ...@@ -184,9 +184,9 @@ GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size)
return g_node_new(data); 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->type = PLIST_ARRAY;
data->length = size; data->length = size;
...@@ -198,7 +198,7 @@ GNode *parse_array_node(char *bnode, uint64_t size, uint32_t ref_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) if (!object)
return NULL; return NULL;
...@@ -214,7 +214,7 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) ...@@ -214,7 +214,7 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object)
case BPLIST_TRUE: 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->type = PLIST_BOOLEAN;
data->boolval = TRUE; data->boolval = TRUE;
return g_node_new(data); return g_node_new(data);
...@@ -222,7 +222,7 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) ...@@ -222,7 +222,7 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object)
case BPLIST_FALSE: 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->type = PLIST_BOOLEAN;
data->boolval = FALSE; data->boolval = FALSE;
return g_node_new(data); return g_node_new(data);
...@@ -299,8 +299,8 @@ GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object) ...@@ -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) gpointer copy_plist_data(gconstpointer src, gpointer data)
{ {
struct plist_data *srcdata = (struct plist_data *) src; plist_data_t srcdata = (plist_data_t) src;
struct plist_data *dstdata = (struct plist_data *) calloc(sizeof(struct plist_data), 1); plist_data_t dstdata = plist_new_plist_data();
dstdata->type = srcdata->type; dstdata->type = srcdata->type;
dstdata->length = srcdata->length; dstdata->length = srcdata->length;
...@@ -336,7 +336,7 @@ gpointer copy_plist_data(gconstpointer src, gpointer data) ...@@ -336,7 +336,7 @@ gpointer copy_plist_data(gconstpointer src, gpointer data)
return dstdata; 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 //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))
...@@ -393,7 +393,7 @@ void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) ...@@ -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++) { for (i = 0; i < num_objects; i++) {
log_debug_msg("parse_nodes: on node %i\n", 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) { switch (data->type) {
case PLIST_DICT: case PLIST_DICT:
...@@ -406,7 +406,7 @@ void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist) ...@@ -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); index2 = swap_n_bytes(data->buff + str_j, dict_param_size);
//first one is actually a key //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])) if (G_NODE_IS_ROOT(nodeslist[index1]))
g_node_append(nodeslist[i], 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) ...@@ -446,7 +446,7 @@ void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist)
guint plist_data_hash(gconstpointer key) 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 hash = data->type;
guint i = 0; guint i = 0;
...@@ -497,8 +497,8 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b) ...@@ -497,8 +497,8 @@ gboolean plist_data_compare(gconstpointer a, gconstpointer b)
if (!((GNode *) a)->data || !((GNode *) b)->data) if (!((GNode *) a)->data || !((GNode *) b)->data)
return FALSE; return FALSE;
struct plist_data *val_a = (struct plist_data *) ((GNode *) a)->data; plist_data_t val_a = plist_get_data(a);
struct plist_data *val_b = (struct plist_data *) ((GNode *) b)->data; plist_data_t val_b = plist_get_data(b);
if (val_a->type != val_b->type) if (val_a->type != val_b->type)
return FALSE; return FALSE;
...@@ -718,7 +718,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) ...@@ -718,7 +718,7 @@ void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
for (i = 0; i < num_objects; i++) { for (i = 0; i < num_objects; i++) {
offsets[i] = bplist_buff->len; 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) { switch (data->type) {
case PLIST_BOOLEAN: case PLIST_BOOLEAN:
......
...@@ -28,23 +28,45 @@ ...@@ -28,23 +28,45 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
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) void plist_new_dict(plist_t * plist)
{ {
if (*plist != NULL) if (*plist != NULL)
return; 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; data->type = PLIST_DICT;
*plist = g_node_new(data); *plist = plist_new_node(data);
} }
void plist_new_array(plist_t * plist) void plist_new_array(plist_t * plist)
{ {
if (*plist != NULL) if (*plist != NULL)
return; 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; 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) 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) ...@@ -52,9 +74,9 @@ void plist_new_dict_in_plist(plist_t plist, plist_t * dict)
if (!plist || *dict) if (!plist || *dict)
return; 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; data->type = PLIST_DICT;
*dict = g_node_new(data); *dict = plist_new_node(data);
g_node_append(plist, *dict); g_node_append(plist, *dict);
} }
...@@ -72,14 +94,14 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu ...@@ -72,14 +94,14 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu
if (!dict || !key || !value) if (!dict || !key || !value)
return; 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->type = PLIST_KEY;
data->strval = strdup(key); data->strval = strdup(key);
GNode *keynode = g_node_new(data); plist_t keynode = plist_new_node(data);
g_node_append(dict, keynode); g_node_append(dict, keynode);
//now handle value //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->type = type;
val->length = length; val->length = length;
...@@ -108,7 +130,7 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu ...@@ -108,7 +130,7 @@ void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *valu
default: default:
break; break;
} }
GNode *valnode = g_node_new(val); plist_t valnode = plist_new_node(val);
g_node_append(dict, valnode); g_node_append(dict, valnode);
} }
...@@ -117,24 +139,41 @@ void plist_free(plist_t plist) ...@@ -117,24 +139,41 @@ void plist_free(plist_t plist)
g_node_destroy(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) if (!plist)
return NULL; return NULL;
GNode *current = NULL; plist_t current = NULL;
for (current = plist->children; current; current = current->next) { 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)) if (data->type == PLIST_STRING && !strcmp(data->strval, request))
return current->next; return next;
} }
if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { 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) if (sub)
return sub; return sub;
} }
...@@ -142,7 +181,7 @@ plist_t find_query_node(plist_t plist, char *key, char *request) ...@@ -142,7 +181,7 @@ plist_t find_query_node(plist_t plist, char *key, char *request)
return NULL; 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; char res = FALSE;
switch (type) { switch (type) {
...@@ -174,21 +213,21 @@ char compare_node_value(plist_type type, struct plist_data *data, void *value) ...@@ -174,21 +213,21 @@ char compare_node_value(plist_type type, struct plist_data *data, void *value)
return res; 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) if (!plist)
return NULL; return NULL;
GNode *current = NULL; plist_t current = NULL;
for (current = plist->children; current; current = current->next) { 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)) { if (data->type == type && compare_node_value(type, data, value)) {
return current; return current;
} }
if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { 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) if (sub)
return sub; return sub;
} }
...@@ -196,12 +235,12 @@ plist_t find_node(plist_t plist, plist_type type, void *value) ...@@ -196,12 +235,12 @@ plist_t find_node(plist_t plist, plist_type type, void *value)
return NULL; 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) if (!node)
return; return;
struct plist_data *data = (struct plist_data *) node->data; plist_data_t data = plist_get_data(node);
*type = data->type; *type = data->type;
*length = data->length; *length = data->length;
...@@ -236,16 +275,18 @@ void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * ...@@ -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) plist_type plist_get_node_type(plist_t node)
{ {
if (node && node->data) if (node) {
return ((struct plist_data *) node->data)->type; plist_data_t data = plist_get_data(node);
else if (data)
return PLIST_NONE; return data->type;
}
return PLIST_NONE;
} }
uint64_t plist_get_node_uint_val(plist_t node) uint64_t plist_get_node_uint_val(plist_t node)
{ {
if (PLIST_UINT == plist_get_node_type(node)) if (PLIST_UINT == plist_get_node_type(node))
return ((struct plist_data *) node->data)->intval; return plist_get_data(node)->intval;
else else
return 0; return 0;
} }
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#ifndef PLIST_H #ifndef PLIST_H
#define PLIST_H #define PLIST_H
#include "plist/plist.h"
#include <stdint.h> #include <stdint.h>
#include <wchar.h> #include <wchar.h>
...@@ -31,22 +33,10 @@ ...@@ -31,22 +33,10 @@
#include <glib.h> #include <glib.h>
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 { union {
char boolval; char boolval;
uint64_t intval; uint64_t intval;
...@@ -59,25 +49,12 @@ struct plist_data { ...@@ -59,25 +49,12 @@ struct plist_data {
plist_type type; 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 #endif
...@@ -120,7 +120,7 @@ void node_to_xml(GNode * node, gpointer xml_struct) ...@@ -120,7 +120,7 @@ void node_to_xml(GNode * node, gpointer xml_struct)
return; return;
struct xml_node *xstruct = (struct xml_node *) xml_struct; 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; xmlNodePtr child_node = NULL;
char isStruct = FALSE; char isStruct = FALSE;
...@@ -220,7 +220,7 @@ void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) ...@@ -220,7 +220,7 @@ void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
if (!node) if (!node)
break; 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); GNode *subnode = g_node_new(data);
if (*plist_node) if (*plist_node)
g_node_append(*plist_node, subnode); g_node_append(*plist_node, subnode);
...@@ -303,7 +303,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) ...@@ -303,7 +303,7 @@ void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, 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); xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0);
xmlNodePtr root_node = xmlDocGetRootElement(plist_doc); xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);
......
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