diff options
Diffstat (limited to 'src/xplist.c')
| -rw-r--r-- | src/xplist.c | 85 |
1 files changed, 61 insertions, 24 deletions
diff --git a/src/xplist.c b/src/xplist.c index 1319f56..899cc81 100644 --- a/src/xplist.c +++ b/src/xplist.c | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | #include <assert.h> | 24 | #include <assert.h> |
| 25 | #include <stdlib.h> | 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> | 26 | #include <stdio.h> |
| 27 | #include <time.h> | ||
| 27 | 28 | ||
| 28 | #include <inttypes.h> | 29 | #include <inttypes.h> |
| 29 | #include <locale.h> | 30 | #include <locale.h> |
| @@ -31,7 +32,12 @@ | |||
| 31 | #include <libxml/parser.h> | 32 | #include <libxml/parser.h> |
| 32 | #include <libxml/tree.h> | 33 | #include <libxml/tree.h> |
| 33 | 34 | ||
| 35 | #include <node.h> | ||
| 36 | #include <node_list.h> | ||
| 37 | #include <node_iterator.h> | ||
| 38 | |||
| 34 | #include "plist.h" | 39 | #include "plist.h" |
| 40 | #include "base64.h" | ||
| 35 | 41 | ||
| 36 | #define XPLIST_TEXT BAD_CAST("text") | 42 | #define XPLIST_TEXT BAD_CAST("text") |
| 37 | #define XPLIST_KEY BAD_CAST("key") | 43 | #define XPLIST_KEY BAD_CAST("key") |
| @@ -61,18 +67,22 @@ static const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\ | |||
| 61 | * | 67 | * |
| 62 | * @return The formatted string. | 68 | * @return The formatted string. |
| 63 | */ | 69 | */ |
| 64 | static gchar *format_string(const char *buf, int cols, int depth) | 70 | static char *format_string(const char *buf, size_t len, int cols, int depth) |
| 65 | { | 71 | { |
| 72 | if (!buf || !(len > 0)) return NULL; | ||
| 66 | int colw = depth + cols + 1; | 73 | int colw = depth + cols + 1; |
| 67 | int len = strlen(buf); | ||
| 68 | int nlines = len / cols + 1; | 74 | int nlines = len / cols + 1; |
| 69 | gchar *new_buf = (gchar *) g_malloc0(nlines * colw + depth + 1); | 75 | char *new_buf = NULL; |
| 70 | int i = 0; | 76 | int i = 0; |
| 71 | int j = 0; | 77 | int j = 0; |
| 72 | 78 | ||
| 73 | assert(cols >= 0); | 79 | assert(cols >= 0); |
| 74 | assert(depth >= 0); | 80 | assert(depth >= 0); |
| 75 | 81 | ||
| 82 | new_buf = (char*) malloc(nlines * colw + depth + 1); | ||
| 83 | assert(new_buf != 0); | ||
| 84 | memset(new_buf, 0, nlines * colw + depth + 1); | ||
| 85 | |||
| 76 | // Inserts new lines and tabs at appropriate locations | 86 | // Inserts new lines and tabs at appropriate locations |
| 77 | for (i = 0; i < nlines; i++) | 87 | for (i = 0; i < nlines; i++) |
| 78 | { | 88 | { |
| @@ -116,7 +126,7 @@ static xmlDocPtr new_xml_plist(void) | |||
| 116 | return plist_xml; | 126 | return plist_xml; |
| 117 | } | 127 | } |
| 118 | 128 | ||
| 119 | static void node_to_xml(GNode * node, gpointer xml_struct) | 129 | static void node_to_xml(node_t* node, void *xml_struct) |
| 120 | { | 130 | { |
| 121 | struct xml_node *xstruct = NULL; | 131 | struct xml_node *xstruct = NULL; |
| 122 | plist_data_t node_data = NULL; | 132 | plist_data_t node_data = NULL; |
| @@ -125,10 +135,10 @@ static void node_to_xml(GNode * node, gpointer xml_struct) | |||
| 125 | char isStruct = FALSE; | 135 | char isStruct = FALSE; |
| 126 | 136 | ||
| 127 | const xmlChar *tag = NULL; | 137 | const xmlChar *tag = NULL; |
| 128 | gchar *val = NULL; | 138 | char *val = NULL; |
| 129 | 139 | ||
| 130 | //for base64 | 140 | //for base64 |
| 131 | gchar *valtmp = NULL; | 141 | char *valtmp = NULL; |
| 132 | 142 | ||
| 133 | uint32_t i = 0; | 143 | uint32_t i = 0; |
| 134 | 144 | ||
| @@ -151,31 +161,32 @@ static void node_to_xml(GNode * node, gpointer xml_struct) | |||
| 151 | 161 | ||
| 152 | case PLIST_UINT: | 162 | case PLIST_UINT: |
| 153 | tag = XPLIST_INT; | 163 | tag = XPLIST_INT; |
| 154 | val = g_strdup_printf("%"PRIu64, node_data->intval); | 164 | (void)asprintf(&val, "%"PRIu64, node_data->intval); |
| 155 | break; | 165 | break; |
| 156 | 166 | ||
| 157 | case PLIST_REAL: | 167 | case PLIST_REAL: |
| 158 | tag = XPLIST_REAL; | 168 | tag = XPLIST_REAL; |
| 159 | val = g_strdup_printf("%f", node_data->realval); | 169 | (void)asprintf(&val, "%f", node_data->realval); |
| 160 | break; | 170 | break; |
| 161 | 171 | ||
| 162 | case PLIST_STRING: | 172 | case PLIST_STRING: |
| 163 | tag = XPLIST_STRING; | 173 | tag = XPLIST_STRING; |
| 164 | val = g_strdup(node_data->strval); | 174 | val = strdup(node_data->strval); |
| 165 | break; | 175 | break; |
| 166 | 176 | ||
| 167 | case PLIST_KEY: | 177 | case PLIST_KEY: |
| 168 | tag = XPLIST_KEY; | 178 | tag = XPLIST_KEY; |
| 169 | val = g_strdup((gchar *) node_data->strval); | 179 | val = strdup((char*) node_data->strval); |
| 170 | break; | 180 | break; |
| 171 | 181 | ||
| 172 | case PLIST_DATA: | 182 | case PLIST_DATA: |
| 173 | tag = XPLIST_DATA; | 183 | tag = XPLIST_DATA; |
| 174 | if (node_data->length) | 184 | if (node_data->length) |
| 175 | { | 185 | { |
| 176 | valtmp = g_base64_encode(node_data->buff, node_data->length); | 186 | size_t len = node_data->length; |
| 177 | val = format_string(valtmp, 68, xstruct->depth); | 187 | valtmp = base64encode(node_data->buff, &len); |
| 178 | g_free(valtmp); | 188 | val = format_string(valtmp, len, 68, xstruct->depth); |
| 189 | free(valtmp); | ||
| 179 | } | 190 | } |
| 180 | break; | 191 | break; |
| 181 | case PLIST_ARRAY: | 192 | case PLIST_ARRAY: |
| @@ -188,7 +199,18 @@ static void node_to_xml(GNode * node, gpointer xml_struct) | |||
| 188 | break; | 199 | break; |
| 189 | case PLIST_DATE: | 200 | case PLIST_DATE: |
| 190 | tag = XPLIST_DATE; | 201 | tag = XPLIST_DATE; |
| 191 | val = g_time_val_to_iso8601(&node_data->timeval); | 202 | { |
| 203 | time_t time = (time_t)node_data->timeval.tv_sec; | ||
| 204 | struct tm *btime = localtime(&time); | ||
| 205 | if (btime) { | ||
| 206 | val = (char*)malloc(24); | ||
| 207 | memset(val, 0, 24); | ||
| 208 | if (strftime(val, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) { | ||
| 209 | free (val); | ||
| 210 | val = NULL; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | } | ||
| 192 | break; | 214 | break; |
| 193 | default: | 215 | default: |
| 194 | break; | 216 | break; |
| @@ -205,7 +227,9 @@ static void node_to_xml(GNode * node, gpointer xml_struct) | |||
| 205 | } else | 227 | } else |
| 206 | child_node = xmlNewChild(xstruct->xml, NULL, tag, BAD_CAST(val)); | 228 | child_node = xmlNewChild(xstruct->xml, NULL, tag, BAD_CAST(val)); |
| 207 | xmlNodeAddContent(xstruct->xml, BAD_CAST("\n")); | 229 | xmlNodeAddContent(xstruct->xml, BAD_CAST("\n")); |
| 208 | g_free(val); | 230 | if (val) { |
| 231 | free(val); | ||
| 232 | } | ||
| 209 | 233 | ||
| 210 | //add return for structured types | 234 | //add return for structured types |
| 211 | if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT) | 235 | 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) | |||
| 214 | if (isStruct) | 238 | if (isStruct) |
| 215 | { | 239 | { |
| 216 | struct xml_node child = { child_node, xstruct->depth + 1 }; | 240 | struct xml_node child = { child_node, xstruct->depth + 1 }; |
| 217 | g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child); | 241 | node_iterator_t *ni = node_iterator_create(node->children); |
| 242 | node_t *ch; | ||
| 243 | while ((ch = node_iterator_next(ni))) { | ||
| 244 | node_to_xml(ch, &child); | ||
| 245 | } | ||
| 246 | node_iterator_destroy(ni); | ||
| 218 | } | 247 | } |
| 219 | //fix indent for structured types | 248 | //fix indent for structured types |
| 220 | if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT) | 249 | 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) | |||
| 236 | plist_t subnode = NULL; | 265 | plist_t subnode = NULL; |
| 237 | 266 | ||
| 238 | //for string | 267 | //for string |
| 239 | glong len = 0; | 268 | long len = 0; |
| 240 | int type = 0; | 269 | int type = 0; |
| 241 | 270 | ||
| 242 | if (!xml_node) | 271 | if (!xml_node) |
| @@ -253,7 +282,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) | |||
| 253 | data = plist_new_plist_data(); | 282 | data = plist_new_plist_data(); |
| 254 | subnode = plist_new_node(data); | 283 | subnode = plist_new_node(data); |
| 255 | if (*plist_node) | 284 | if (*plist_node) |
| 256 | g_node_append(*plist_node, subnode); | 285 | node_attach(*plist_node, subnode); |
| 257 | else | 286 | else |
| 258 | *plist_node = subnode; | 287 | *plist_node = subnode; |
| 259 | 288 | ||
| @@ -276,7 +305,7 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) | |||
| 276 | if (!xmlStrcmp(node->name, XPLIST_INT)) | 305 | if (!xmlStrcmp(node->name, XPLIST_INT)) |
| 277 | { | 306 | { |
| 278 | xmlChar *strval = xmlNodeGetContent(node); | 307 | xmlChar *strval = xmlNodeGetContent(node); |
| 279 | data->intval = g_ascii_strtoull((char *) strval, NULL, 0); | 308 | data->intval = strtoull((char*)strval, NULL, 0); |
| 280 | data->type = PLIST_UINT; | 309 | data->type = PLIST_UINT; |
| 281 | data->length = 8; | 310 | data->length = 8; |
| 282 | xmlFree(strval); | 311 | xmlFree(strval); |
| @@ -296,9 +325,17 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) | |||
| 296 | if (!xmlStrcmp(node->name, XPLIST_DATE)) | 325 | if (!xmlStrcmp(node->name, XPLIST_DATE)) |
| 297 | { | 326 | { |
| 298 | xmlChar *strval = xmlNodeGetContent(node); | 327 | xmlChar *strval = xmlNodeGetContent(node); |
| 299 | g_time_val_from_iso8601((char *) strval, &data->timeval); | 328 | time_t time = 0; |
| 329 | if (strlen(strval) >= 11) { | ||
| 330 | struct tm btime; | ||
| 331 | memset(&btime, 0, sizeof(struct tm)); | ||
| 332 | strptime((char*)strval, "%Y-%m-%dT%H:%M:%SZ", &btime); | ||
| 333 | time = mktime(&btime); | ||
| 334 | } | ||
| 335 | data->timeval.tv_sec = (long)time; | ||
| 336 | data->timeval.tv_usec = 0; | ||
| 300 | data->type = PLIST_DATE; | 337 | data->type = PLIST_DATE; |
| 301 | data->length = sizeof(GTimeVal); | 338 | data->length = sizeof(struct timeval); |
| 302 | xmlFree(strval); | 339 | xmlFree(strval); |
| 303 | continue; | 340 | continue; |
| 304 | } | 341 | } |
| @@ -332,11 +369,11 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) | |||
| 332 | if (!xmlStrcmp(node->name, XPLIST_DATA)) | 369 | if (!xmlStrcmp(node->name, XPLIST_DATA)) |
| 333 | { | 370 | { |
| 334 | xmlChar *strval = xmlNodeGetContent(node); | 371 | xmlChar *strval = xmlNodeGetContent(node); |
| 335 | gsize size = 0; | 372 | size_t size = 0; |
| 336 | guchar *dec = g_base64_decode((char *) strval, &size); | 373 | unsigned char *dec = base64decode((char*)strval, &size); |
| 337 | data->buff = (uint8_t *) malloc(size * sizeof(uint8_t)); | 374 | data->buff = (uint8_t *) malloc(size * sizeof(uint8_t)); |
| 338 | memcpy(data->buff, dec, size * sizeof(uint8_t)); | 375 | memcpy(data->buff, dec, size * sizeof(uint8_t)); |
| 339 | g_free(dec); | 376 | free(dec); |
| 340 | data->length = size; | 377 | data->length = size; |
| 341 | data->type = PLIST_DATA; | 378 | data->type = PLIST_DATA; |
| 342 | xmlFree(strval); | 379 | xmlFree(strval); |
