summaryrefslogtreecommitdiffstats
path: root/src/xplist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/xplist.c')
-rw-r--r--src/xplist.c61
1 files changed, 45 insertions, 16 deletions
diff --git a/src/xplist.c b/src/xplist.c
index 94006f1..3971622 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -143,7 +143,7 @@ static int node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth)
143 143
144 if (!node) { 144 if (!node) {
145 PLIST_XML_WRITE_ERR("Encountered invalid empty node in property list\n"); 145 PLIST_XML_WRITE_ERR("Encountered invalid empty node in property list\n");
146 return -1; 146 return PLIST_ERR_INVALID_ARG;
147 } 147 }
148 148
149 node_data = plist_get_data(node); 149 node_data = plist_get_data(node);
@@ -238,9 +238,9 @@ static int node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth)
238 break; 238 break;
239 case PLIST_NULL: 239 case PLIST_NULL:
240 PLIST_XML_WRITE_ERR("PLIST_NULL type is not valid for XML format\n"); 240 PLIST_XML_WRITE_ERR("PLIST_NULL type is not valid for XML format\n");
241 return -1; 241 return PLIST_ERR_FORMAT;
242 default: 242 default:
243 break; 243 return PLIST_ERR_UNKNOWN;
244 } 244 }
245 245
246 for (i = 0; i < depth; i++) { 246 for (i = 0; i < depth; i++) {
@@ -377,7 +377,7 @@ static int node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth)
377 str_buf_append(*outbuf, ">", 1); 377 str_buf_append(*outbuf, ">", 1);
378 } 378 }
379 str_buf_append(*outbuf, "\n", 1); 379 str_buf_append(*outbuf, "\n", 1);
380 return 0; 380 return PLIST_ERR_SUCCESS;
381} 381}
382 382
383static void parse_date(const char *strval, struct TM *btime) 383static void parse_date(const char *strval, struct TM *btime)
@@ -438,11 +438,11 @@ static int num_digits_u(uint64_t i)
438 return n; 438 return n;
439} 439}
440 440
441static void node_estimate_size(node_t *node, uint64_t *size, uint32_t depth) 441static int node_estimate_size(node_t *node, uint64_t *size, uint32_t depth)
442{ 442{
443 plist_data_t data; 443 plist_data_t data;
444 if (!node) { 444 if (!node) {
445 return; 445 return PLIST_ERR_INVALID_ARG;
446 } 446 }
447 data = plist_get_data(node); 447 data = plist_get_data(node);
448 if (node->children) { 448 if (node->children) {
@@ -511,28 +511,47 @@ static void node_estimate_size(node_t *node, uint64_t *size, uint32_t depth)
511 *size += 18; /* <key>CF$UID</key> */ 511 *size += 18; /* <key>CF$UID</key> */
512 *size += (XPLIST_INT_LEN << 1) + 6; 512 *size += (XPLIST_INT_LEN << 1) + 6;
513 break; 513 break;
514 case PLIST_NULL:
515 PLIST_XML_WRITE_ERR("PLIST_NULL type is not valid for XML format\n");
516 return PLIST_ERR_FORMAT;
514 default: 517 default:
515 break; 518 PLIST_XML_WRITE_ERR("invalid node type encountered\n");
519 return PLIST_ERR_UNKNOWN;
516 } 520 }
517 *size += indent; 521 *size += indent;
518 } 522 }
523 return PLIST_ERR_SUCCESS;
519} 524}
520 525
521PLIST_API void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) 526PLIST_API plist_err_t plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
522{ 527{
523 uint64_t size = 0; 528 uint64_t size = 0;
524 node_estimate_size(plist, &size, 0); 529 int res;
530
531 if (!plist || !plist_xml || !length) {
532 return PLIST_ERR_INVALID_ARG;
533 }
534
535 res = node_estimate_size(plist, &size, 0);
536 if (res < 0) {
537 return res;
538 }
525 size += sizeof(XML_PLIST_PROLOG) + sizeof(XML_PLIST_EPILOG) - 1; 539 size += sizeof(XML_PLIST_PROLOG) + sizeof(XML_PLIST_EPILOG) - 1;
526 540
527 strbuf_t *outbuf = str_buf_new(size); 541 strbuf_t *outbuf = str_buf_new(size);
542 if (!outbuf) {
543 PLIST_XML_WRITE_ERR("Could not allocate output buffer");
544 return PLIST_ERR_NO_MEM;
545 }
528 546
529 str_buf_append(outbuf, XML_PLIST_PROLOG, sizeof(XML_PLIST_PROLOG)-1); 547 str_buf_append(outbuf, XML_PLIST_PROLOG, sizeof(XML_PLIST_PROLOG)-1);
530 548
531 if (node_to_xml(plist, &outbuf, 0) < 0) { 549 res = node_to_xml(plist, &outbuf, 0);
550 if (res < 0) {
532 str_buf_free(outbuf); 551 str_buf_free(outbuf);
533 *plist_xml = NULL; 552 *plist_xml = NULL;
534 *length = 0; 553 *length = 0;
535 return; 554 return res;
536 } 555 }
537 556
538 str_buf_append(outbuf, XML_PLIST_EPILOG, sizeof(XML_PLIST_EPILOG)); 557 str_buf_append(outbuf, XML_PLIST_EPILOG, sizeof(XML_PLIST_EPILOG));
@@ -542,6 +561,8 @@ PLIST_API void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
542 561
543 outbuf->data = NULL; 562 outbuf->data = NULL;
544 str_buf_free(outbuf); 563 str_buf_free(outbuf);
564
565 return PLIST_ERR_SUCCESS;
545} 566}
546 567
547struct _parse_ctx { 568struct _parse_ctx {
@@ -932,8 +953,9 @@ static char* text_parts_get_content(text_part_t *tp, int unesc_entities, size_t
932 return str; 953 return str;
933} 954}
934 955
935static void node_from_xml(parse_ctx ctx, plist_t *plist) 956static int node_from_xml(parse_ctx ctx, plist_t *plist)
936{ 957{
958 int res;
937 char *tag = NULL; 959 char *tag = NULL;
938 char *keyname = NULL; 960 char *keyname = NULL;
939 plist_t subnode = NULL; 961 plist_t subnode = NULL;
@@ -1427,17 +1449,24 @@ err_out:
1427 if (ctx->err) { 1449 if (ctx->err) {
1428 plist_free(*plist); 1450 plist_free(*plist);
1429 *plist = NULL; 1451 *plist = NULL;
1452 res = PLIST_ERR_PARSE;
1453 } else {
1454 res = PLIST_ERR_SUCCESS;
1430 } 1455 }
1456 return res;
1431} 1457}
1432 1458
1433PLIST_API void plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist) 1459PLIST_API plist_err_t plist_from_xml(const char *plist_xml, uint32_t length, plist_t * plist)
1434{ 1460{
1461 if (!plist) {
1462 return PLIST_ERR_INVALID_ARG;
1463 }
1464 *plist = NULL;
1435 if (!plist_xml || (length == 0)) { 1465 if (!plist_xml || (length == 0)) {
1436 *plist = NULL; 1466 return PLIST_ERR_INVALID_ARG;
1437 return;
1438 } 1467 }
1439 1468
1440 struct _parse_ctx ctx = { plist_xml, plist_xml + length, 0 }; 1469 struct _parse_ctx ctx = { plist_xml, plist_xml + length, 0 };
1441 1470
1442 node_from_xml(&ctx, plist); 1471 return node_from_xml(&ctx, plist);
1443} 1472}