diff options
| author | 2026-02-21 10:39:24 -0300 | |
|---|---|---|
| committer | 2026-03-20 17:12:47 +0100 | |
| commit | 3edac28498d883f1f768699ee15ce85a82bb2a7b (patch) | |
| tree | b350517a84a671fc500d943168f973e7c97aa93e /src/jplist.c | |
| parent | 6e03a1df6d1aa87c8f9e2b35f1a2ca60feca1c0e (diff) | |
| download | libplist-3edac28498d883f1f768699ee15ce85a82bb2a7b.tar.gz libplist-3edac28498d883f1f768699ee15ce85a82bb2a7b.tar.bz2 | |
Add JSON coercion support for non-JSON plist types
- Add PLIST_OPT_COERCE option to coerce PLIST_DATE, PLIST_DATA, and PLIST_UID to JSON-compatible types (ISO 8601 strings, Base64 strings, and integers)
- Add plist_to_json_with_options() function to allow passing coercion options (and others)
- Update plist_write_to_string() and plist_write_to_stream() to support coercion option
- Add --coerce flag to plistutil for JSON output
- Create plist2json symlink that automatically enables coercion when invoked
Diffstat (limited to 'src/jplist.c')
| -rw-r--r-- | src/jplist.c | 129 |
1 files changed, 102 insertions, 27 deletions
diff --git a/src/jplist.c b/src/jplist.c index 0ac1e0b..410d4b3 100644 --- a/src/jplist.c +++ b/src/jplist.c | |||
| @@ -39,6 +39,10 @@ | |||
| 39 | #include "strbuf.h" | 39 | #include "strbuf.h" |
| 40 | #include "jsmn.h" | 40 | #include "jsmn.h" |
| 41 | #include "hashtable.h" | 41 | #include "hashtable.h" |
| 42 | #include "base64.h" | ||
| 43 | #include "time64.h" | ||
| 44 | |||
| 45 | #define MAC_EPOCH 978307200 | ||
| 42 | 46 | ||
| 43 | #ifdef DEBUG | 47 | #ifdef DEBUG |
| 44 | static int plist_json_debug = 0; | 48 | static int plist_json_debug = 0; |
| @@ -115,7 +119,7 @@ static size_t dtostr(char *buf, size_t bufsize, double realval) | |||
| 115 | return len; | 119 | return len; |
| 116 | } | 120 | } |
| 117 | 121 | ||
| 118 | static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t depth, int prettify) | 122 | static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t depth, int prettify, int coerce) |
| 119 | { | 123 | { |
| 120 | plist_data_t node_data = NULL; | 124 | plist_data_t node_data = NULL; |
| 121 | 125 | ||
| @@ -211,7 +215,7 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept | |||
| 211 | str_buf_append(*outbuf, " ", 2); | 215 | str_buf_append(*outbuf, " ", 2); |
| 212 | } | 216 | } |
| 213 | } | 217 | } |
| 214 | plist_err_t res = node_to_json(ch, outbuf, depth+1, prettify); | 218 | plist_err_t res = node_to_json(ch, outbuf, depth+1, prettify, coerce); |
| 215 | if (res < 0) { | 219 | if (res < 0) { |
| 216 | return res; | 220 | return res; |
| 217 | } | 221 | } |
| @@ -239,7 +243,7 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept | |||
| 239 | str_buf_append(*outbuf, " ", 2); | 243 | str_buf_append(*outbuf, " ", 2); |
| 240 | } | 244 | } |
| 241 | } | 245 | } |
| 242 | plist_err_t res = node_to_json(ch, outbuf, depth+1, prettify); | 246 | plist_err_t res = node_to_json(ch, outbuf, depth+1, prettify, coerce); |
| 243 | if (res < 0) { | 247 | if (res < 0) { |
| 244 | return res; | 248 | return res; |
| 245 | } | 249 | } |
| @@ -260,17 +264,60 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept | |||
| 260 | str_buf_append(*outbuf, "}", 1); | 264 | str_buf_append(*outbuf, "}", 1); |
| 261 | } break; | 265 | } break; |
| 262 | case PLIST_DATA: | 266 | case PLIST_DATA: |
| 263 | // NOT VALID FOR JSON | 267 | if (coerce) { |
| 264 | PLIST_JSON_WRITE_ERR("PLIST_DATA type is not valid for JSON format\n"); | 268 | size_t b64_len = ((node_data->length + 2) / 3) * 4; |
| 265 | return PLIST_ERR_FORMAT; | 269 | char *b64_buf = (char*)malloc(b64_len + 1); |
| 270 | if (!b64_buf) { | ||
| 271 | return PLIST_ERR_NO_MEM; | ||
| 272 | } | ||
| 273 | size_t actual_len = base64encode(b64_buf, node_data->buff, node_data->length); | ||
| 274 | str_buf_append(*outbuf, "\"", 1); | ||
| 275 | str_buf_append(*outbuf, b64_buf, actual_len); | ||
| 276 | str_buf_append(*outbuf, "\"", 1); | ||
| 277 | free(b64_buf); | ||
| 278 | } else { | ||
| 279 | PLIST_JSON_WRITE_ERR("PLIST_DATA type is not valid for JSON format\n"); | ||
| 280 | return PLIST_ERR_FORMAT; | ||
| 281 | } | ||
| 282 | break; | ||
| 266 | case PLIST_DATE: | 283 | case PLIST_DATE: |
| 267 | // NOT VALID FOR JSON | 284 | if (coerce) { |
| 268 | PLIST_JSON_WRITE_ERR("PLIST_DATE type is not valid for JSON format\n"); | 285 | Time64_T timev = (Time64_T)node_data->realval + MAC_EPOCH; |
| 269 | return PLIST_ERR_FORMAT; | 286 | struct TM _btime; |
| 287 | struct TM *btime = gmtime64_r(&timev, &_btime); | ||
| 288 | char datebuf[32]; | ||
| 289 | size_t datelen = 0; | ||
| 290 | if (btime) { | ||
| 291 | struct tm _tmcopy; | ||
| 292 | copy_TM64_to_tm(btime, &_tmcopy); | ||
| 293 | datelen = strftime(datebuf, sizeof(datebuf), "%Y-%m-%dT%H:%M:%SZ", &_tmcopy); | ||
| 294 | } | ||
| 295 | if (datelen <= 0) { | ||
| 296 | datelen = snprintf(datebuf, sizeof(datebuf), "1970-01-01T00:00:00Z"); | ||
| 297 | } | ||
| 298 | str_buf_append(*outbuf, "\"", 1); | ||
| 299 | str_buf_append(*outbuf, datebuf, datelen); | ||
| 300 | str_buf_append(*outbuf, "\"", 1); | ||
| 301 | } else { | ||
| 302 | PLIST_JSON_WRITE_ERR("PLIST_DATE type is not valid for JSON format\n"); | ||
| 303 | return PLIST_ERR_FORMAT; | ||
| 304 | } | ||
| 305 | break; | ||
| 270 | case PLIST_UID: | 306 | case PLIST_UID: |
| 271 | // NOT VALID FOR JSON | 307 | if (coerce) { |
| 272 | PLIST_JSON_WRITE_ERR("PLIST_UID type is not valid for JSON format\n"); | 308 | val = (char*)malloc(64); |
| 273 | return PLIST_ERR_FORMAT; | 309 | if (node_data->length == 16) { |
| 310 | val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); | ||
| 311 | } else { | ||
| 312 | val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); | ||
| 313 | } | ||
| 314 | str_buf_append(*outbuf, val, val_len); | ||
| 315 | free(val); | ||
| 316 | } else { | ||
| 317 | PLIST_JSON_WRITE_ERR("PLIST_UID type is not valid for JSON format\n"); | ||
| 318 | return PLIST_ERR_FORMAT; | ||
| 319 | } | ||
| 320 | break; | ||
| 274 | default: | 321 | default: |
| 275 | return PLIST_ERR_UNKNOWN; | 322 | return PLIST_ERR_UNKNOWN; |
| 276 | } | 323 | } |
| @@ -316,7 +363,7 @@ static int num_digits_u(uint64_t i) | |||
| 316 | return n; | 363 | return n; |
| 317 | } | 364 | } |
| 318 | 365 | ||
| 319 | static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify, hashtable_t *visited) | 366 | static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify, int coerce, hashtable_t *visited) |
| 320 | { | 367 | { |
| 321 | plist_data_t data; | 368 | plist_data_t data; |
| 322 | if (!node) { | 369 | if (!node) { |
| @@ -341,7 +388,7 @@ static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t dep | |||
| 341 | node_t ch; | 388 | node_t ch; |
| 342 | unsigned int n_children = node_n_children(node); | 389 | unsigned int n_children = node_n_children(node); |
| 343 | for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { | 390 | for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { |
| 344 | plist_err_t res = _node_estimate_size(ch, size, depth + 1, prettify, visited); | 391 | plist_err_t res = _node_estimate_size(ch, size, depth + 1, prettify, coerce, visited); |
| 345 | if (res < 0) { | 392 | if (res < 0) { |
| 346 | return res; | 393 | return res; |
| 347 | } | 394 | } |
| @@ -398,17 +445,36 @@ static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t dep | |||
| 398 | *size += 2; | 445 | *size += 2; |
| 399 | break; | 446 | break; |
| 400 | case PLIST_DATA: | 447 | case PLIST_DATA: |
| 401 | // NOT VALID FOR JSON | 448 | if (coerce) { |
| 402 | PLIST_JSON_WRITE_ERR("PLIST_DATA type is not valid for JSON format\n"); | 449 | // base64 encoded string: 2 quotes + ((len+2)/3)*4 base64 chars |
| 403 | return PLIST_ERR_FORMAT; | 450 | *size += 2 + ((data->length + 2) / 3) * 4; |
| 451 | } else { | ||
| 452 | PLIST_JSON_WRITE_ERR("PLIST_DATA type is not valid for JSON format\n"); | ||
| 453 | return PLIST_ERR_FORMAT; | ||
| 454 | } | ||
| 455 | break; | ||
| 404 | case PLIST_DATE: | 456 | case PLIST_DATE: |
| 405 | // NOT VALID FOR JSON | 457 | if (coerce) { |
| 406 | PLIST_JSON_WRITE_ERR("PLIST_DATE type is not valid for JSON format\n"); | 458 | // ISO 8601 string: "YYYY-MM-DDTHH:MM:SSZ" = 22 chars max |
| 407 | return PLIST_ERR_FORMAT; | 459 | *size += 24; |
| 460 | } else { | ||
| 461 | PLIST_JSON_WRITE_ERR("PLIST_DATE type is not valid for JSON format\n"); | ||
| 462 | return PLIST_ERR_FORMAT; | ||
| 463 | } | ||
| 464 | break; | ||
| 408 | case PLIST_UID: | 465 | case PLIST_UID: |
| 409 | // NOT VALID FOR JSON | 466 | if (coerce) { |
| 410 | PLIST_JSON_WRITE_ERR("PLIST_UID type is not valid for JSON format\n"); | 467 | // integer representation |
| 411 | return PLIST_ERR_FORMAT; | 468 | if (data->length == 16) { |
| 469 | *size += num_digits_u(data->intval); | ||
| 470 | } else { | ||
| 471 | *size += num_digits_i((int64_t)data->intval); | ||
| 472 | } | ||
| 473 | } else { | ||
| 474 | PLIST_JSON_WRITE_ERR("PLIST_UID type is not valid for JSON format\n"); | ||
| 475 | return PLIST_ERR_FORMAT; | ||
| 476 | } | ||
| 477 | break; | ||
| 412 | default: | 478 | default: |
| 413 | PLIST_JSON_WRITE_ERR("invalid node type encountered\n"); | 479 | PLIST_JSON_WRITE_ERR("invalid node type encountered\n"); |
| 414 | return PLIST_ERR_UNKNOWN; | 480 | return PLIST_ERR_UNKNOWN; |
| @@ -417,17 +483,23 @@ static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t dep | |||
| 417 | return PLIST_ERR_SUCCESS; | 483 | return PLIST_ERR_SUCCESS; |
| 418 | } | 484 | } |
| 419 | 485 | ||
| 420 | static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify) | 486 | static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify, int coerce) |
| 421 | { | 487 | { |
| 422 | hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); | 488 | hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); |
| 423 | if (!visited) return PLIST_ERR_NO_MEM; | 489 | if (!visited) return PLIST_ERR_NO_MEM; |
| 424 | plist_err_t err = _node_estimate_size(node, size, depth, prettify, visited); | 490 | plist_err_t err = _node_estimate_size(node, size, depth, prettify, coerce, visited); |
| 425 | hash_table_destroy(visited); | 491 | hash_table_destroy(visited); |
| 426 | return err; | 492 | return err; |
| 427 | } | 493 | } |
| 428 | 494 | ||
| 429 | plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, int prettify) | 495 | plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, int prettify) |
| 430 | { | 496 | { |
| 497 | plist_write_options_t opts = prettify ? PLIST_OPT_NONE : PLIST_OPT_COMPACT; | ||
| 498 | return plist_to_json_with_options(plist, plist_json, length, opts); | ||
| 499 | } | ||
| 500 | |||
| 501 | plist_err_t plist_to_json_with_options(plist_t plist, char **plist_json, uint32_t* length, plist_write_options_t options) | ||
| 502 | { | ||
| 431 | uint64_t size = 0; | 503 | uint64_t size = 0; |
| 432 | plist_err_t res; | 504 | plist_err_t res; |
| 433 | 505 | ||
| @@ -440,7 +512,10 @@ plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, in | |||
| 440 | return PLIST_ERR_FORMAT; | 512 | return PLIST_ERR_FORMAT; |
| 441 | } | 513 | } |
| 442 | 514 | ||
| 443 | res = node_estimate_size((node_t)plist, &size, 0, prettify); | 515 | int prettify = !(options & PLIST_OPT_COMPACT); |
| 516 | int coerce = options & PLIST_OPT_COERCE; | ||
| 517 | |||
| 518 | res = node_estimate_size((node_t)plist, &size, 0, prettify, coerce); | ||
| 444 | if (res < 0) { | 519 | if (res < 0) { |
| 445 | return res; | 520 | return res; |
| 446 | } | 521 | } |
| @@ -451,7 +526,7 @@ plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, in | |||
| 451 | return PLIST_ERR_NO_MEM; | 526 | return PLIST_ERR_NO_MEM; |
| 452 | } | 527 | } |
| 453 | 528 | ||
| 454 | res = node_to_json((node_t)plist, &outbuf, 0, prettify); | 529 | res = node_to_json((node_t)plist, &outbuf, 0, prettify, coerce); |
| 455 | if (res < 0) { | 530 | if (res < 0) { |
| 456 | str_buf_free(outbuf); | 531 | str_buf_free(outbuf); |
| 457 | *plist_json = NULL; | 532 | *plist_json = NULL; |
