From 9711459dbed7d60bb00c7d2c052623e8489c88e1 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Fri, 22 May 2026 18:46:02 +0200 Subject: refactor: centralize formatting helpers and harden out-plutil --- src/jplist.c | 68 +++--------------------------------------------------------- 1 file changed, 3 insertions(+), 65 deletions(-) (limited to 'src/jplist.c') diff --git a/src/jplist.c b/src/jplist.c index 410d4b3..cc1cd1b 100644 --- a/src/jplist.c +++ b/src/jplist.c @@ -41,8 +41,7 @@ #include "hashtable.h" #include "base64.h" #include "time64.h" - -#define MAC_EPOCH 978307200 +#include "common.h" #ifdef DEBUG static int plist_json_debug = 0; @@ -95,30 +94,6 @@ static char* strndup(const char* str, size_t len) #endif #endif -static size_t dtostr(char *buf, size_t bufsize, double realval) -{ - size_t len = 0; - if (isnan(realval)) { - len = snprintf(buf, bufsize, "nan"); - } else if (isinf(realval)) { - len = snprintf(buf, bufsize, "%cinfinity", (realval > 0.0) ? '+' : '-'); - } else if (realval == 0.0f) { - len = snprintf(buf, bufsize, "0.0"); - } else { - size_t i = 0; - len = snprintf(buf, bufsize, "%.*g", 17, realval); - for (i = 0; buf && i < len; i++) { - if (buf[i] == ',') { - buf[i] = '.'; - break; - } else if (buf[i] == '.') { - break; - } - } - } - return len; -} - static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t depth, int prettify, int coerce) { plist_data_t node_data = NULL; @@ -325,44 +300,6 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept return PLIST_ERR_SUCCESS; } -#define PO10i_LIMIT (INT64_MAX/10) - -/* based on https://stackoverflow.com/a/4143288 */ -static int num_digits_i(int64_t i) -{ - int n; - int64_t po10; - n=1; - if (i < 0) { - i = (i == INT64_MIN) ? INT64_MAX : -i; - n++; - } - po10=10; - while (i>=po10) { - n++; - if (po10 > PO10i_LIMIT) break; - po10*=10; - } - return n; -} - -#define PO10u_LIMIT (UINT64_MAX/10) - -/* based on https://stackoverflow.com/a/4143288 */ -static int num_digits_u(uint64_t i) -{ - int n; - uint64_t po10; - n=1; - po10=10; - while (i>=po10) { - n++; - if (po10 > PO10u_LIMIT) break; - po10*=10; - } - return n; -} - static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify, int coerce, hashtable_t *visited) { plist_data_t data; @@ -556,6 +493,7 @@ typedef struct { static int64_t parse_decimal(const char* str, const char* str_end, char** endp) { + const uint64_t po10i_limit = INT64_MAX / 10; uint64_t MAX = INT64_MAX; uint64_t x = 0; int is_neg = 0; @@ -571,7 +509,7 @@ static int64_t parse_decimal(const char* str, const char* str_end, char** endp) MAX++; } while (*endp < str_end && isdigit(**endp)) { - if (x > PO10i_LIMIT) { + if (x > po10i_limit) { x = MAX; break; } -- cgit v1.1-32-gdbae