diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Array.cpp | 51 | ||||
| -rw-r--r-- | src/Boolean.cpp | 2 | ||||
| -rw-r--r-- | src/Data.cpp | 13 | ||||
| -rw-r--r-- | src/Date.cpp | 24 | ||||
| -rw-r--r-- | src/Dictionary.cpp | 10 | ||||
| -rw-r--r-- | src/Integer.cpp | 5 | ||||
| -rw-r--r-- | src/Key.cpp | 4 | ||||
| -rw-r--r-- | src/Makefile.am | 1 | ||||
| -rw-r--r-- | src/Node.cpp | 2 | ||||
| -rw-r--r-- | src/Real.cpp | 2 | ||||
| -rw-r--r-- | src/String.cpp | 25 | ||||
| -rw-r--r-- | src/Structure.cpp | 27 | ||||
| -rw-r--r-- | src/Uid.cpp | 2 | ||||
| -rw-r--r-- | src/base64.c | 3 | ||||
| -rw-r--r-- | src/bplist.c | 611 | ||||
| -rw-r--r-- | src/bytearray.c | 70 | ||||
| -rw-r--r-- | src/common.c | 115 | ||||
| -rw-r--r-- | src/common.h | 34 | ||||
| -rw-r--r-- | src/hashtable.c | 23 | ||||
| -rw-r--r-- | src/hashtable.h | 5 | ||||
| -rw-r--r-- | src/jplist.c | 358 | ||||
| -rw-r--r-- | src/jsmn.c | 43 | ||||
| -rw-r--r-- | src/jsmn.h | 18 | ||||
| -rw-r--r-- | src/oplist.c | 390 | ||||
| -rw-r--r-- | src/out-default.c | 170 | ||||
| -rw-r--r-- | src/out-limd.c | 207 | ||||
| -rw-r--r-- | src/out-plutil.c | 195 | ||||
| -rw-r--r-- | src/plist.c | 1532 | ||||
| -rw-r--r-- | src/plist.h | 23 | ||||
| -rw-r--r-- | src/ptrarray.c | 37 | ||||
| -rw-r--r-- | src/ptrarray.h | 4 | ||||
| -rw-r--r-- | src/time64.c | 11 | ||||
| -rw-r--r-- | src/time64.h | 11 | ||||
| -rw-r--r-- | src/xplist.c | 695 |
34 files changed, 3359 insertions, 1364 deletions
diff --git a/src/Array.cpp b/src/Array.cpp index bc448d3..784df7c 100644 --- a/src/Array.cpp +++ b/src/Array.cpp @@ -40,7 +40,9 @@ static void array_fill(Array *_this, std::vector<Node*> &array, plist_t node) do { subnode = NULL; plist_array_next_item(node, iter, &subnode); - array.push_back( Node::FromPlist(subnode, _this) ); + if (subnode) { + array.push_back( Node::FromPlist(subnode, _this) ); + } } while (subnode); free(iter); } @@ -51,7 +53,7 @@ Array::Array(plist_t node, Node* parent) : Structure(parent) array_fill(this, _array, _node); } -Array::Array(const PList::Array& a) +Array::Array(const PList::Array& a) : Structure(a.GetParent()) { _array.clear(); _node = plist_copy(a.GetPlist()); @@ -60,6 +62,8 @@ Array::Array(const PList::Array& a) Array& Array::operator=(const PList::Array& a) { + if (this == &a) return *this; + plist_free(_node); for (size_t it = 0; it < _array.size(); it++) { delete _array.at(it); @@ -88,6 +92,26 @@ Node* Array::operator[](unsigned int array_index) return _array.at(array_index); } +Node* Array::Back() +{ + return _array.back(); +} + +Node* Array::back() +{ + return _array.back(); +} + +Node* Array::Front() +{ + return _array.front(); +} + +Node* Array::front() +{ + return _array.front(); +} + Array::iterator Array::Begin() { return _array.begin(); @@ -132,7 +156,7 @@ size_t Array::size() const { return _array.size(); } -void Array::Append(Node* node) +void Array::Append(const Node* node) { if (node) { @@ -143,7 +167,12 @@ void Array::Append(Node* node) } } -void Array::Insert(Node* node, unsigned int pos) +void Array::Append(const Node& node) +{ + Append(&node); +} + +void Array::Insert(const Node* node, unsigned int pos) { if (node) { @@ -156,6 +185,11 @@ void Array::Insert(Node* node, unsigned int pos) } } +void Array::Insert(const Node &node, unsigned int pos) +{ + Insert(&node, pos); +} + void Array::Remove(Node* node) { if (node) @@ -168,7 +202,7 @@ void Array::Remove(Node* node) std::vector<Node*>::iterator it = _array.begin(); it += pos; _array.erase(it); - delete node; + free(node); } } @@ -181,10 +215,15 @@ void Array::Remove(unsigned int pos) _array.erase(it); } -unsigned int Array::GetNodeIndex(Node* node) const +unsigned int Array::GetNodeIndex(const Node* node) const { std::vector<Node*>::const_iterator it = std::find(_array.begin(), _array.end(), node); return std::distance (_array.begin(), it); } +unsigned int Array::GetNodeIndex(const Node& node) const +{ + return GetNodeIndex(&node); +} + } // namespace PList diff --git a/src/Boolean.cpp b/src/Boolean.cpp index 9ec1a63..2a5e303 100644 --- a/src/Boolean.cpp +++ b/src/Boolean.cpp @@ -40,6 +40,8 @@ Boolean::Boolean(const PList::Boolean& b) : Node(PLIST_BOOLEAN) Boolean& Boolean::operator=(const PList::Boolean& b) { + if (this == &b) return *this; + plist_free(_node); _node = plist_copy(b.GetPlist()); return *this; diff --git a/src/Data.cpp b/src/Data.cpp index a96fc50..94767c9 100644 --- a/src/Data.cpp +++ b/src/Data.cpp @@ -40,6 +40,8 @@ Data::Data(const PList::Data& d) : Node(PLIST_DATA) Data& Data::operator=(const PList::Data& b) { + if (this == &b) return *this; + plist_free(_node); _node = plist_copy(b.GetPlist()); return *this; @@ -50,6 +52,11 @@ Data::Data(const std::vector<char>& buff) : Node(PLIST_DATA) plist_set_data_val(_node, &buff[0], buff.size()); } +Data::Data(const char* buff, uint64_t size) : Node(PLIST_DATA) +{ + plist_set_data_val(_node, buff, size); +} + Data::~Data() { } @@ -66,14 +73,10 @@ void Data::SetValue(const std::vector<char>& buff) std::vector<char> Data::GetValue() const { - char* buff = NULL; uint64_t length = 0; - plist_get_data_val(_node, &buff, &length); + const char* buff = plist_get_data_ptr(_node, &length); std::vector<char> ret(buff, buff + length); - delete buff; return ret; } - - } // namespace PList diff --git a/src/Date.cpp b/src/Date.cpp index 8b8e650..214220b 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -34,20 +34,22 @@ Date::Date(plist_t node, Node* parent) : Node(node, parent) Date::Date(const PList::Date& d) : Node(PLIST_DATE) { - timeval t = d.GetValue(); - plist_set_date_val(_node, t.tv_sec, t.tv_usec); + int64_t t = d.GetValue(); + plist_set_unix_date_val(_node, t); } Date& Date::operator=(const PList::Date& d) { + if (this == &d) return *this; + plist_free(_node); _node = plist_copy(d.GetPlist()); return *this; } -Date::Date(timeval t) : Node(PLIST_DATE) +Date::Date(int64_t t) : Node(PLIST_DATE) { - plist_set_date_val(_node, t.tv_sec, t.tv_usec); + plist_set_unix_date_val(_node, t); } Date::~Date() @@ -59,18 +61,16 @@ Node* Date::Clone() const return new Date(*this); } -void Date::SetValue(timeval t) +void Date::SetValue(int64_t t) { - plist_set_date_val(_node, t.tv_sec, t.tv_usec); + plist_set_unix_date_val(_node, t); } -timeval Date::GetValue() const +int64_t Date::GetValue() const { - int32_t tv_sec = 0; - int32_t tv_usec = 0; - plist_get_date_val(_node, &tv_sec, &tv_usec); - timeval t = {tv_sec, tv_usec}; - return t; + int64_t sec = 0; + plist_get_unix_date_val(_node, &sec); + return sec; } } // namespace PList diff --git a/src/Dictionary.cpp b/src/Dictionary.cpp index 30c20b6..b354945 100644 --- a/src/Dictionary.cpp +++ b/src/Dictionary.cpp @@ -40,7 +40,7 @@ static void dictionary_fill(Dictionary *_this, std::map<std::string,Node*> &map, plist_dict_next_item(node, it, &key, &subnode); if (key && subnode) map[std::string(key)] = Node::FromPlist(subnode, _this); - delete key; + free(key); } while (subnode); free(it); } @@ -51,7 +51,7 @@ Dictionary::Dictionary(plist_t node, Node* parent) : Structure(parent) dictionary_fill(this, _map, _node); } -Dictionary::Dictionary(const PList::Dictionary& d) +Dictionary::Dictionary(const PList::Dictionary& d) : Structure(d.GetParent()) { for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) { @@ -65,6 +65,8 @@ Dictionary::Dictionary(const PList::Dictionary& d) Dictionary& Dictionary::operator=(const PList::Dictionary& d) { + if (this == &d) return *this; + for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) { plist_free(it->second->GetPlist()); @@ -176,9 +178,9 @@ void Dictionary::Remove(Node* node) plist_dict_get_item_key(node->GetPlist(), &key); plist_dict_remove_item(_node, key); std::string skey = key; - delete key; + free(key); _map.erase(skey); - delete node; + free(node); } } diff --git a/src/Integer.cpp b/src/Integer.cpp index 30a5405..26e7ccf 100644 --- a/src/Integer.cpp +++ b/src/Integer.cpp @@ -35,11 +35,14 @@ Integer::Integer(plist_t node, Node* parent) : Node(node, parent) Integer::Integer(const PList::Integer& i) : Node(PLIST_INT) { - plist_set_uint_val(_node, i.GetValue()); + plist_free(_node); + _node = plist_copy(i.GetPlist()); } Integer& Integer::operator=(const PList::Integer& i) { + if (this == &i) return *this; + plist_free(_node); _node = plist_copy(i.GetPlist()); return *this; diff --git a/src/Key.cpp b/src/Key.cpp index 79265d5..459227a 100644 --- a/src/Key.cpp +++ b/src/Key.cpp @@ -40,6 +40,8 @@ Key::Key(const PList::Key& k) : Node(PLIST_INT) Key& Key::operator=(const PList::Key& k) { + if (this == &k) return *this; + plist_free(_node); _node = plist_copy(k.GetPlist()); return *this; @@ -69,7 +71,7 @@ std::string Key::GetValue() const char* s = NULL; plist_get_key_val(_node, &s); std::string ret = s ? s : ""; - delete s; + free(s); return ret; } diff --git a/src/Makefile.am b/src/Makefile.am index 1a416ad..ce4f931 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,6 +21,7 @@ libplist_2_0_la_SOURCES = \ ptrarray.c ptrarray.h \ time64.c time64.h \ time64_limits.h \ + common.c common.h \ xplist.c \ bplist.c \ jsmn.c jsmn.h \ diff --git a/src/Node.cpp b/src/Node.cpp index 0bd428a..1043b31 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -73,7 +73,7 @@ Node::Node(plist_type type, Node* parent) : _parent(parent) _node = plist_new_data(NULL,0); break; case PLIST_DATE: - _node = plist_new_date(0,0); + _node = plist_new_unix_date(0); break; case PLIST_ARRAY: _node = plist_new_array(); diff --git a/src/Real.cpp b/src/Real.cpp index 02d1d9b..b743ab5 100644 --- a/src/Real.cpp +++ b/src/Real.cpp @@ -39,6 +39,8 @@ Real::Real(const PList::Real& d) : Node(PLIST_INT) Real& Real::operator=(const PList::Real& d) { + if (this == &d) return *this; + plist_free(_node); _node = plist_copy(d.GetPlist()); return *this; diff --git a/src/String.cpp b/src/String.cpp index 2ddc28b..bcd5e2e 100644 --- a/src/String.cpp +++ b/src/String.cpp @@ -40,16 +40,37 @@ String::String(const PList::String& s) : Node(PLIST_INT) String& String::operator=(const PList::String& s) { + if (this == &s) return *this; + plist_free(_node); _node = plist_copy(s.GetPlist()); return *this; } +String& String::operator=(const std::string& s) +{ + plist_free(_node); + _node = plist_new_string(s.c_str()); + return *this; +} + +String& String::operator=(const char* s) +{ + plist_free(_node); + _node = plist_new_string(s); + return *this; +} + String::String(const std::string& s) : Node(PLIST_STRING) { plist_set_string_val(_node, s.c_str()); } +String::String(const char *s) : Node(PLIST_STRING) +{ + plist_set_string_val(_node, s); +} + String::~String() { } @@ -66,10 +87,8 @@ void String::SetValue(const std::string& s) std::string String::GetValue() const { - char* s = NULL; - plist_get_string_val(_node, &s); + const char* s = plist_get_string_ptr(_node, NULL); std::string ret = s ? s : ""; - delete s; return ret; } diff --git a/src/Structure.cpp b/src/Structure.cpp index 670cce6..65e5ca8 100644 --- a/src/Structure.cpp +++ b/src/Structure.cpp @@ -57,7 +57,7 @@ std::string Structure::ToXml() const uint32_t length = 0; plist_to_xml(_node, &xml, &length); std::string ret(xml, xml+length); - delete xml; + free(xml); return ret; } @@ -67,7 +67,7 @@ std::vector<char> Structure::ToBin() const uint32_t length = 0; plist_to_bin(_node, &bin, &length); std::vector<char> ret(bin, bin+length); - delete bin; + free(bin); return ret; } @@ -77,7 +77,7 @@ void Structure::UpdateNodeParent(Node* node) if ( NULL != node->_parent ) { plist_type type = plist_get_node_type(node->_parent); - if (PLIST_ARRAY ==type || PLIST_DICT == type ) + if (PLIST_ARRAY == type || PLIST_DICT == type) { Structure* s = static_cast<Structure*>(node->_parent); s->Remove(node); @@ -117,8 +117,27 @@ Structure* Structure::FromBin(const std::vector<char>& bin) plist_from_bin(&bin[0], bin.size(), &root); return ImportStruct(root); +} + +Structure* Structure::FromBin(const char* bin, uint64_t size) +{ + plist_t root = NULL; + plist_from_bin(bin, size, &root); + return ImportStruct(root); } -} // namespace PList +Structure* Structure::FromMemory(const std::vector<char>& buf, plist_format_t *format) +{ + return Structure::FromMemory(&buf[0], buf.size(), format); +} +Structure* Structure::FromMemory(const char* buf, uint64_t size, plist_format_t *format) +{ + plist_t root = NULL; + plist_from_memory(buf, size, &root, format); + return ImportStruct(root); +} + + +} // namespace PList diff --git a/src/Uid.cpp b/src/Uid.cpp index 8c73c80..d73f777 100644 --- a/src/Uid.cpp +++ b/src/Uid.cpp @@ -40,6 +40,8 @@ Uid::Uid(const PList::Uid& i) : Node(PLIST_UID) Uid& Uid::operator=(const PList::Uid& i) { + if (this == &i) return *this; + plist_free(_node); _node = plist_copy(i.GetPlist()); return *this; diff --git a/src/base64.c b/src/base64.c index ee02356..603ab6d 100644 --- a/src/base64.c +++ b/src/base64.c @@ -77,8 +77,9 @@ unsigned char *base64decode(const char *buf, size_t *size) size_t len = (*size > 0) ? *size : strlen(buf); if (len <= 0) return NULL; unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3); + if (!outbuf) return NULL; const char *ptr = buf; - int p = 0; + size_t p = 0; int wv, w1, w2, w3, w4; int tmpval[4]; int tmpcnt = 0; diff --git a/src/bplist.c b/src/bplist.c index 93f0bc6..663a118 100644 --- a/src/bplist.c +++ b/src/bplist.c @@ -27,7 +27,6 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> -#include <assert.h> #include <ctype.h> #include <inttypes.h> @@ -87,6 +86,28 @@ union plist_uint_ptr uint64_t *u64ptr; }; +#ifdef _MSC_VER +uint64_t get_unaligned_64(uint64_t *ptr) +{ + uint64_t temp; + memcpy(&temp, ptr, sizeof(temp)); + return temp; +} + +uint32_t get_unaligned_32(uint32_t *ptr) +{ + uint32_t temp; + memcpy(&temp, ptr, sizeof(temp)); + return temp; +} + +uint16_t get_unaligned_16(uint16_t *ptr) +{ + uint16_t temp; + memcpy(&temp, ptr, sizeof(temp)); + return temp; +} +#else #define get_unaligned(ptr) \ ({ \ struct __attribute__((packed)) { \ @@ -94,6 +115,7 @@ union plist_uint_ptr } *__p = (void *) (ptr); \ __p->__v; \ }) +#endif #ifndef bswap16 @@ -148,17 +170,31 @@ union plist_uint_ptr #define beNtoh(x,n) be64toh((x) << ((8-(n)) << 3)) #endif +#ifdef _MSC_VER +static uint64_t UINT_TO_HOST(const void* x, uint8_t n) +{ + union plist_uint_ptr __up; + __up.src = (n > 8) ? (const char*)x + (n - 8) : (const char*)x; + return (n >= 8 ? be64toh( get_unaligned_64(__up.u64ptr) ) : + (n == 4 ? be32toh( get_unaligned_32(__up.u32ptr) ) : + (n == 2 ? be16toh( get_unaligned_16(__up.u16ptr) ) : + (n == 1 ? *__up.u8ptr : + beNtoh( get_unaligned_64(__up.u64ptr), n) + )))); +} +#else #define UINT_TO_HOST(x, n) \ ({ \ union plist_uint_ptr __up; \ - __up.src = ((n) > 8) ? (x) + ((n) - 8) : (x); \ + __up.src = ((n) > 8) ? (const char*)(x) + ((n) - 8) : (const char*)(x); \ ((n) >= 8 ? be64toh( get_unaligned(__up.u64ptr) ) : \ ((n) == 4 ? be32toh( get_unaligned(__up.u32ptr) ) : \ ((n) == 2 ? be16toh( get_unaligned(__up.u16ptr) ) : \ - ((n) == 1 ? *__up.u8ptr : \ + ((n) == 1 ? *__up.u8ptr : \ beNtoh( get_unaligned(__up.u64ptr), n) \ )))); \ }) +#endif #define get_needed_bytes(x) \ ( ((uint64_t)(x)) < (1ULL << 8) ? 1 : \ @@ -168,15 +204,13 @@ union plist_uint_ptr #define get_real_bytes(x) ((x) == (float) (x) ? sizeof(float) : sizeof(double)) -#if (defined(__LITTLE_ENDIAN__) \ - && !defined(__FLOAT_WORD_ORDER__)) \ - || (defined(__FLOAT_WORD_ORDER__) \ - && __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__) -#define float_bswap64(x) bswap64(x) -#define float_bswap32(x) bswap32(x) -#else +#if (defined(__BIG_ENDIAN__) && !defined(__FLOAT_WORD_ORDER__)) \ + || (defined(__FLOAT_WORD_ORDER__) && __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__) #define float_bswap64(x) (x) #define float_bswap32(x) (x) +#else +#define float_bswap64(x) bswap64(x) +#define float_bswap32(x) bswap32(x) #endif #ifndef __has_builtin @@ -189,7 +223,7 @@ union plist_uint_ptr static int uint64_mul_overflow(uint64_t a, uint64_t b, uint64_t *res) { *res = a * b; - return (a > UINT64_MAX / b); + return (b != 0 && a > UINT64_MAX / b); } #endif @@ -203,14 +237,50 @@ struct bplist_data { uint8_t offset_size; const char* offset_table; uint32_t level; + uint64_t nodes_parsed; + uint64_t max_nodes; ptrarray_t* used_indexes; + plist_err_t err; }; +/* + * Check that len bytes starting at ptr lie within the object area of the + * binary plist, i.e. between the start of the data and the offset table. + * The check compares lengths instead of computing ptr + len, so it never + * forms a pointer outside of the input buffer and cannot wrap around. + */ +static int bplist_object_range_valid(const struct bplist_data *bplist, const char *ptr, uint64_t len) +{ + if (ptr < bplist->data || ptr > bplist->offset_table) { + return 0; + } + return len <= (uint64_t)(bplist->offset_table - ptr); +} + +/* + * Read the object reference at position ref_index of the reference list + * that starts at refs. The offset is computed with overflow checking and + * validated before the reference is read. + */ +static int bplist_read_object_ref(const struct bplist_data *bplist, const char *refs, uint64_t ref_index, uint64_t *ref) +{ + uint64_t ref_offset = 0; + if (uint64_mul_overflow(ref_index, bplist->ref_size, &ref_offset) || + ref_offset > UINT64_MAX - bplist->ref_size || + !bplist_object_range_valid(bplist, refs, ref_offset + bplist->ref_size)) { + return 0; + } + *ref = UINT_TO_HOST(refs + ref_offset, bplist->ref_size); + return 1; +} + #ifdef DEBUG static int plist_bin_debug = 0; #define PLIST_BIN_ERR(...) if (plist_bin_debug) { fprintf(stderr, "libplist[binparser] ERROR: " __VA_ARGS__); } +#define PLIST_BIN_WRITE_ERR(...) if (plist_bin_debug) { fprintf(stderr, "libplist[binwriter] ERROR: " __VA_ARGS__); } #else #define PLIST_BIN_ERR(...) +#define PLIST_BIN_WRITE_ERR(...) #endif void plist_bin_init(void) @@ -238,9 +308,23 @@ void plist_bin_set_debug(int debug) static plist_t parse_bin_node_at_index(struct bplist_data *bplist, uint32_t node_index); +static plist_t bplist_node_new(plist_data_t data) +{ + plist_t node = node_create(NULL, data); + if (!node) { + PLIST_BIN_ERR("%s: failed to create node\n", __func__); + plist_free_data(data); + } + return node; +} + static plist_t parse_int_node(const char **bnode, uint8_t size) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } size = 1 << size; // make length less misleading switch (size) @@ -265,25 +349,40 @@ static plist_t parse_int_node(const char **bnode, uint8_t size) (*bnode) += size; data->type = PLIST_INT; - return node_create(NULL, data); + return bplist_node_new(data); } static plist_t parse_real_node(const char **bnode, uint8_t size) { plist_data_t data = plist_new_plist_data(); - uint8_t buf[8]; + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } size = 1 << size; // make length less misleading switch (size) { case sizeof(uint32_t): - *(uint32_t*)buf = float_bswap32(get_unaligned((uint32_t*)*bnode)); - data->realval = *(float *) buf; - break; + { + uint32_t ival; + memcpy(&ival, *bnode, sizeof(uint32_t)); + ival = float_bswap32(ival); + float fval; + memcpy(&fval, &ival, sizeof(float)); + data->realval = fval; + } + break; + case sizeof(uint64_t): - *(uint64_t*)buf = float_bswap64(get_unaligned((uint64_t*)*bnode)); - data->realval = *(double *) buf; + { + uint64_t ival; + memcpy(&ival, *bnode, sizeof(uint64_t)); + ival = float_bswap64(ival); + memcpy(&data->realval, &ival, sizeof(double)); break; + } + default: free(data); PLIST_BIN_ERR("%s: Invalid byte size for real node\n", __func__); @@ -292,12 +391,15 @@ static plist_t parse_real_node(const char **bnode, uint8_t size) data->type = PLIST_REAL; data->length = sizeof(double); - return node_create(NULL, data); + return bplist_node_new(data); } static plist_t parse_date_node(const char **bnode, uint8_t size) { plist_t node = parse_real_node(bnode, size); + if (!node) { + return NULL; + } plist_data_t data = plist_get_data(node); data->type = PLIST_DATE; @@ -308,6 +410,10 @@ static plist_t parse_date_node(const char **bnode, uint8_t size) static plist_t parse_string_node(const char **bnode, uint64_t size) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_STRING; data->strval = (char *) malloc(sizeof(char) * (size + 1)); @@ -320,22 +426,26 @@ static plist_t parse_string_node(const char **bnode, uint64_t size) data->strval[size] = '\0'; data->length = strlen(data->strval); - return node_create(NULL, data); + return bplist_node_new(data); } -static char *plist_utf16be_to_utf8(uint16_t *unistr, long len, long *items_read, long *items_written) +static char *plist_utf16be_to_utf8(uint16_t *unistr, size_t len, size_t *items_read, size_t *items_written) { if (!unistr || (len <= 0)) return NULL; char* outbuf; char* outbuf_new; - int p = 0; - long i = 0; + size_t p = 0; + size_t i = 0; uint16_t wc; uint32_t w; int read_lead_surrogate = 0; - /* allocate with enough space */ + /* allocate with enough space, guarding against integer overflow */ + if (len > (SIZE_MAX / 4) - 1) { + PLIST_BIN_ERR("%s: Input length %" PRIu64 " is too large\n", __func__, (uint64_t)len); + return NULL; + } outbuf = (char*)malloc(4*(len+1)); if (!outbuf) { PLIST_BIN_ERR("%s: Could not allocate %" PRIu64 " bytes\n", __func__, (uint64_t)(4*(len+1))); @@ -343,7 +453,7 @@ static char *plist_utf16be_to_utf8(uint16_t *unistr, long len, long *items_read, } while (i < len) { - wc = be16toh(get_unaligned(unistr + i)); + wc = UINT_TO_HOST(unistr + i, sizeof(wc)); i++; if (wc >= 0xD800 && wc <= 0xDBFF) { if (!read_lead_surrogate) { @@ -397,8 +507,12 @@ static char *plist_utf16be_to_utf8(uint16_t *unistr, long len, long *items_read, static plist_t parse_unicode_node(const char **bnode, uint64_t size) { plist_data_t data = plist_new_plist_data(); - long items_read = 0; - long items_written = 0; + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } + size_t items_read = 0; + size_t items_written = 0; data->type = PLIST_STRING; data->strval = plist_utf16be_to_utf8((uint16_t*)(*bnode), size, &items_read, &items_written); @@ -408,56 +522,58 @@ static plist_t parse_unicode_node(const char **bnode, uint64_t size) } data->length = items_written; - return node_create(NULL, data); + return bplist_node_new(data); } static plist_t parse_data_node(const char **bnode, uint64_t size) { plist_data_t data = plist_new_plist_data(); - + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_DATA; data->length = size; data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size); - if (!data->strval) { + if (!data->buff) { plist_free_data(data); PLIST_BIN_ERR("%s: Could not allocate %" PRIu64 " bytes\n", __func__, sizeof(uint8_t) * size); return NULL; } memcpy(data->buff, *bnode, sizeof(uint8_t) * size); - return node_create(NULL, data); + return bplist_node_new(data); } static plist_t parse_dict_node(struct bplist_data *bplist, const char** bnode, uint64_t size) { uint64_t j; - uint64_t str_i = 0, str_j = 0; uint64_t index1, index2; plist_data_t data = plist_new_plist_data(); - const char *index1_ptr = NULL; - const char *index2_ptr = NULL; + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_DICT; data->length = size; plist_t node = node_create(NULL, data); + if (!node) { + plist_free_data(data); + PLIST_BIN_ERR("%s: failed to create node\n", __func__); + return NULL; + } for (j = 0; j < data->length; j++) { - str_i = j * bplist->ref_size; - str_j = (j + size) * bplist->ref_size; - index1_ptr = (*bnode) + str_i; - index2_ptr = (*bnode) + str_j; - - if ((index1_ptr < bplist->data || index1_ptr + bplist->ref_size > bplist->offset_table) || - (index2_ptr < bplist->data || index2_ptr + bplist->ref_size > bplist->offset_table)) { + /* key references come first, followed by all value references */ + if (!bplist_read_object_ref(bplist, *bnode, j, &index1) || + !bplist_read_object_ref(bplist, *bnode, j + size, &index2)) { plist_free(node); PLIST_BIN_ERR("%s: dict entry %" PRIu64 " is outside of valid range\n", __func__, j); return NULL; } - index1 = UINT_TO_HOST(index1_ptr, bplist->ref_size); - index2 = UINT_TO_HOST(index2_ptr, bplist->ref_size); - if (index1 >= bplist->num_objects) { plist_free(node); PLIST_BIN_ERR("%s: dict entry %" PRIu64 ": key index (%" PRIu64 ") must be smaller than the number of objects (%" PRIu64 ")\n", __func__, j, index1, bplist->num_objects); @@ -500,8 +616,17 @@ static plist_t parse_dict_node(struct bplist_data *bplist, const char** bnode, u return NULL; } - node_attach((node_t)node, (node_t)key); - node_attach((node_t)node, (node_t)val); + if (node_attach((node_t)node, (node_t)key) != NODE_ERR_SUCCESS) { + plist_free(key); + plist_free(val); + plist_free(node); + return NULL; + } + if (node_attach((node_t)node, (node_t)val) != NODE_ERR_SUCCESS) { + plist_free(val); + plist_free(node); + return NULL; + } } return node; @@ -510,28 +635,30 @@ static plist_t parse_dict_node(struct bplist_data *bplist, const char** bnode, u static plist_t parse_array_node(struct bplist_data *bplist, const char** bnode, uint64_t size) { uint64_t j; - uint64_t str_j = 0; uint64_t index1; plist_data_t data = plist_new_plist_data(); - const char *index1_ptr = NULL; + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_ARRAY; data->length = size; plist_t node = node_create(NULL, data); + if (!node) { + plist_free_data(data); + PLIST_BIN_ERR("%s: failed to create node\n", __func__); + return NULL; + } for (j = 0; j < data->length; j++) { - str_j = j * bplist->ref_size; - index1_ptr = (*bnode) + str_j; - - if (index1_ptr < bplist->data || index1_ptr + bplist->ref_size > bplist->offset_table) { + if (!bplist_read_object_ref(bplist, *bnode, j, &index1)) { plist_free(node); PLIST_BIN_ERR("%s: array item %" PRIu64 " is outside of valid range\n", __func__, j); return NULL; } - index1 = UINT_TO_HOST(index1_ptr, bplist->ref_size); - if (index1 >= bplist->num_objects) { plist_free(node); PLIST_BIN_ERR("%s: array item %" PRIu64 " object index (%" PRIu64 ") must be smaller than the number of objects (%" PRIu64 ")\n", __func__, j, index1, bplist->num_objects); @@ -545,7 +672,11 @@ static plist_t parse_array_node(struct bplist_data *bplist, const char** bnode, return NULL; } - node_attach((node_t)node, (node_t)val); + if (node_attach((node_t)node, (node_t)val) != NODE_ERR_SUCCESS) { + plist_free(val); + plist_free(node); + return NULL; + } } return node; @@ -554,6 +685,10 @@ static plist_t parse_array_node(struct bplist_data *bplist, const char** bnode, static plist_t parse_uid_node(const char **bnode, uint8_t size) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } size = size + 1; data->intval = UINT_TO_HOST(*bnode, size); if (data->intval > UINT32_MAX) { @@ -566,19 +701,23 @@ static plist_t parse_uid_node(const char **bnode, uint8_t size) data->type = PLIST_UID; data->length = sizeof(uint64_t); - return node_create(NULL, data); + return bplist_node_new(data); } static plist_t parse_bin_node(struct bplist_data *bplist, const char** object) { uint16_t type = 0; uint64_t size = 0; - uint64_t pobject = 0; - uint64_t poffset_table = (uint64_t)(uintptr_t)bplist->offset_table; + uint64_t data_size = 0; if (!object) return NULL; + if (!bplist_object_range_valid(bplist, *object, 1)) { + PLIST_BIN_ERR("%s: node marker points outside of valid range\n", __func__); + return NULL; + } + type = (**object) & BPLIST_MASK; size = (**object) & BPLIST_FILL; (*object)++; @@ -592,6 +731,10 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object) case BPLIST_SET: case BPLIST_DICT: { + if (!bplist_object_range_valid(bplist, *object, 1)) { + PLIST_BIN_ERR("%s: size node for node type 0x%02x points outside of valid range\n", __func__, type); + return NULL; + } uint16_t next_size = **object & BPLIST_FILL; if ((**object & BPLIST_MASK) != BPLIST_INT) { PLIST_BIN_ERR("%s: invalid size node type for node type 0x%02x: found 0x%02x, expected 0x%02x\n", __func__, type, **object & BPLIST_MASK, BPLIST_INT); @@ -599,7 +742,7 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object) } (*object)++; next_size = 1 << next_size; - if (*object + next_size > bplist->offset_table) { + if (!bplist_object_range_valid(bplist, *object, next_size)) { PLIST_BIN_ERR("%s: size node data bytes for node type 0x%02x point outside of valid range\n", __func__, type); return NULL; } @@ -612,8 +755,6 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object) } } - pobject = (uint64_t)(uintptr_t)*object; - switch (type) { @@ -624,27 +765,39 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object) case BPLIST_TRUE: { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_BOOLEAN; data->boolval = TRUE; data->length = 1; - return node_create(NULL, data); + return bplist_node_new(data); } case BPLIST_FALSE: { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_BOOLEAN; data->boolval = FALSE; data->length = 1; - return node_create(NULL, data); + return bplist_node_new(data); } case BPLIST_NULL: { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_NULL; data->length = 0; - return node_create(NULL, data); + return bplist_node_new(data); } default: @@ -652,14 +805,14 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object) } case BPLIST_INT: - if (pobject + (uint64_t)(1 << size) > poffset_table) { + if (!bplist_object_range_valid(bplist, *object, (uint64_t)1 << size)) { PLIST_BIN_ERR("%s: BPLIST_INT data bytes point outside of valid range\n", __func__); return NULL; } return parse_int_node(object, size); case BPLIST_REAL: - if (pobject + (uint64_t)(1 << size) > poffset_table) { + if (!bplist_object_range_valid(bplist, *object, (uint64_t)1 << size)) { PLIST_BIN_ERR("%s: BPLIST_REAL data bytes point outside of valid range\n", __func__); return NULL; } @@ -670,32 +823,32 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object) PLIST_BIN_ERR("%s: invalid data size for BPLIST_DATE node\n", __func__); return NULL; } - if (pobject + (uint64_t)(1 << size) > poffset_table) { + if (!bplist_object_range_valid(bplist, *object, (uint64_t)1 << size)) { PLIST_BIN_ERR("%s: BPLIST_DATE data bytes point outside of valid range\n", __func__); return NULL; } return parse_date_node(object, size); case BPLIST_DATA: - if (pobject + size < pobject || pobject + size > poffset_table) { + if (!bplist_object_range_valid(bplist, *object, size)) { PLIST_BIN_ERR("%s: BPLIST_DATA data bytes point outside of valid range\n", __func__); return NULL; } return parse_data_node(object, size); case BPLIST_STRING: - if (pobject + size < pobject || pobject + size > poffset_table) { + if (!bplist_object_range_valid(bplist, *object, size)) { PLIST_BIN_ERR("%s: BPLIST_STRING data bytes point outside of valid range\n", __func__); return NULL; } return parse_string_node(object, size); case BPLIST_UNICODE: - if (size*2 < size) { + if (uint64_mul_overflow(size, sizeof(uint16_t), &data_size)) { PLIST_BIN_ERR("%s: Integer overflow when calculating BPLIST_UNICODE data size.\n", __func__); return NULL; } - if (pobject + size*2 < pobject || pobject + size*2 > poffset_table) { + if (!bplist_object_range_valid(bplist, *object, data_size)) { PLIST_BIN_ERR("%s: BPLIST_UNICODE data bytes point outside of valid range\n", __func__); return NULL; } @@ -703,21 +856,25 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object) case BPLIST_SET: case BPLIST_ARRAY: - if (pobject + size < pobject || pobject + size > poffset_table) { + /* one object reference per item */ + if (uint64_mul_overflow(size, bplist->ref_size, &data_size) || + !bplist_object_range_valid(bplist, *object, data_size)) { PLIST_BIN_ERR("%s: BPLIST_ARRAY data bytes point outside of valid range\n", __func__); return NULL; } return parse_array_node(bplist, object, size); case BPLIST_UID: - if (pobject + size+1 > poffset_table) { + if (!bplist_object_range_valid(bplist, *object, size + 1)) { PLIST_BIN_ERR("%s: BPLIST_UID data bytes point outside of valid range\n", __func__); return NULL; } return parse_uid_node(object, size); case BPLIST_DICT: - if (pobject + size < pobject || pobject + size > poffset_table) { + /* one key and one value reference per entry */ + if (uint64_mul_overflow(size, 2 * (uint64_t)bplist->ref_size, &data_size) || + !bplist_object_range_valid(bplist, *object, data_size)) { PLIST_BIN_ERR("%s: BPLIST_DICT data bytes point outside of valid range\n", __func__); return NULL; } @@ -739,27 +896,56 @@ static plist_t parse_bin_node_at_index(struct bplist_data *bplist, uint32_t node if (node_index >= bplist->num_objects) { PLIST_BIN_ERR("node index (%u) must be smaller than the number of objects (%" PRIu64 ")\n", node_index, bplist->num_objects); + bplist->err = PLIST_ERR_PARSE; return NULL; } - idx_ptr = bplist->offset_table + node_index * bplist->offset_size; - if (idx_ptr < bplist->offset_table || - idx_ptr >= bplist->offset_table + bplist->num_objects * bplist->offset_size) { + /* num_objects * offset_size was checked against the input size in plist_from_bin() */ + uint64_t idx_offset = (uint64_t)node_index * bplist->offset_size; + if (idx_offset >= bplist->num_objects * bplist->offset_size) { PLIST_BIN_ERR("node index %u points outside of valid range\n", node_index); + bplist->err = PLIST_ERR_PARSE; return NULL; } + idx_ptr = bplist->offset_table + idx_offset; - ptr = bplist->data + UINT_TO_HOST(idx_ptr, bplist->offset_size); + uint64_t node_offset = UINT_TO_HOST(idx_ptr, bplist->offset_size); + if (node_offset > (uint64_t)bplist->size) { + PLIST_BIN_ERR("node offset overflow (%" PRIu64 ")\n", node_offset); + bplist->err = PLIST_ERR_PARSE; + return NULL; + } /* make sure the node offset is in a sane range */ - if ((ptr < bplist->data+BPLIST_MAGIC_SIZE+BPLIST_VERSION_SIZE) || (ptr >= bplist->offset_table)) { + if (node_offset < BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE || + node_offset >= (uint64_t)(bplist->offset_table - bplist->data)) { PLIST_BIN_ERR("offset for node index %u points outside of valid range\n", node_index); + bplist->err = PLIST_ERR_PARSE; + return NULL; + } + ptr = bplist->data + node_offset; + + /* Objects can be referenced multiple times, so limit the total number of + nodes created to avoid excessive memory use from reference expansion. */ + if (++bplist->nodes_parsed > bplist->max_nodes) { + PLIST_BIN_ERR("too many nodes created while parsing (reference expansion?)\n"); + bplist->err = PLIST_ERR_PARSE; + return NULL; + } + + /* check nesting depth */ + if (bplist->level > PLIST_MAX_NESTING_DEPTH) { + PLIST_BIN_ERR("maximum nesting depth (%u) exceeded\n",(unsigned)PLIST_MAX_NESTING_DEPTH); + bplist->err = PLIST_ERR_MAX_NESTING; return NULL; } /* store node_index for current recursion level */ if ((uint32_t)ptr_array_size(bplist->used_indexes) < bplist->level+1) { while ((uint32_t)ptr_array_size(bplist->used_indexes) < bplist->level+1) { - ptr_array_add(bplist->used_indexes, (void*)(uintptr_t)node_index); + if (ptr_array_add(bplist->used_indexes, (void*)(uintptr_t)node_index) < 0) { + bplist->err = PLIST_ERR_NO_MEM; + return NULL; + } } } else { ptr_array_set(bplist->used_indexes, (void*)(uintptr_t)node_index, bplist->level); @@ -772,6 +958,7 @@ static plist_t parse_bin_node_at_index(struct bplist_data *bplist, uint32_t node void *node_level = ptr_array_index(bplist->used_indexes, bplist->level); if (node_i == node_level) { PLIST_BIN_ERR("recursion detected in binary plist\n"); + bplist->err = PLIST_ERR_CIRCULAR_REF; return NULL; } } @@ -793,7 +980,6 @@ plist_err_t plist_from_bin(const char *plist_bin, uint32_t length, plist_t * pli uint64_t root_object = 0; const char *offset_table = NULL; uint64_t offset_table_size = 0; - const char *start_data = NULL; const char *end_data = NULL; if (!plist) { @@ -820,7 +1006,6 @@ plist_err_t plist_from_bin(const char *plist_bin, uint32_t length, plist_t * pli return PLIST_ERR_PARSE; } - start_data = plist_bin + BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE; end_data = plist_bin + length - sizeof(bplist_trailer_t); //now parse trailer @@ -830,7 +1015,14 @@ plist_err_t plist_from_bin(const char *plist_bin, uint32_t length, plist_t * pli ref_size = trailer->ref_size; num_objects = be64toh(trailer->num_objects); root_object = be64toh(trailer->root_object_index); - offset_table = (char *)(plist_bin + be64toh(trailer->offset_table_offset)); + + uint64_t offset_table_offset = be64toh(trailer->offset_table_offset); + uint64_t max_valid_offset = (uint64_t)length - sizeof(bplist_trailer_t); + if (offset_table_offset > max_valid_offset) { + PLIST_BIN_ERR("offset table offset outside of valid range\n"); + return PLIST_ERR_PARSE; + } + offset_table = (char *)(plist_bin + offset_table_offset); if (num_objects == 0) { PLIST_BIN_ERR("number of objects must be larger than 0\n"); @@ -852,7 +1044,7 @@ plist_err_t plist_from_bin(const char *plist_bin, uint32_t length, plist_t * pli return PLIST_ERR_PARSE; } - if (offset_table < start_data || offset_table >= end_data) { + if (offset_table_offset < BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE || offset_table_offset >= max_valid_offset) { PLIST_BIN_ERR("offset table offset points outside of valid range\n"); return PLIST_ERR_PARSE; } @@ -875,7 +1067,10 @@ plist_err_t plist_from_bin(const char *plist_bin, uint32_t length, plist_t * pli bplist.offset_size = offset_size; bplist.offset_table = offset_table; bplist.level = 0; + bplist.nodes_parsed = 0; + bplist.max_nodes = num_objects * 16 + 65536; bplist.used_indexes = ptr_array_new(16); + bplist.err = PLIST_ERR_SUCCESS; if (!bplist.used_indexes) { PLIST_BIN_ERR("failed to create array to hold used node indexes. Out of memory?\n"); @@ -887,7 +1082,7 @@ plist_err_t plist_from_bin(const char *plist_bin, uint32_t length, plist_t * pli ptr_array_free(bplist.used_indexes); if (!*plist) { - return PLIST_ERR_PARSE; + return (bplist.err != PLIST_ERR_SUCCESS) ? bplist.err : PLIST_ERR_PARSE; } return PLIST_ERR_SUCCESS; @@ -944,35 +1139,65 @@ struct serialize_s { ptrarray_t* objects; hashtable_t* ref_table; + hashtable_t* in_stack; }; -static void serialize_plist(node_t node, void* data) +static plist_err_t serialize_plist(node_t node, void* data, uint32_t depth) { uint64_t *index_val = NULL; struct serialize_s *ser = (struct serialize_s *) data; - uint64_t current_index = ser->objects->len; - //first check that node is not yet in objects + if (depth > PLIST_MAX_NESTING_DEPTH) { + PLIST_BIN_WRITE_ERR("maximum nesting depth (%u) exceeded\n", (unsigned)PLIST_MAX_NESTING_DEPTH); + return PLIST_ERR_MAX_NESTING; + } + + // circular reference check: is node on current recursion stack? + if (hash_table_lookup(ser->in_stack, node)) { + PLIST_BIN_WRITE_ERR("circular reference detected\n"); + return PLIST_ERR_CIRCULAR_REF; + } + + // first check that node is not yet in objects void* val = hash_table_lookup(ser->ref_table, node); - if (val) - { - //data is already in table - return; + if (val) { + // data is already in table + return PLIST_ERR_SUCCESS; + } + + // mark as active + if (hash_table_insert(ser->in_stack, node, (void*)1) < 0) { + return PLIST_ERR_NO_MEM; } - //insert new ref + + // insert new ref index_val = (uint64_t *) malloc(sizeof(uint64_t)); - assert(index_val != NULL); - *index_val = current_index; - hash_table_insert(ser->ref_table, node, index_val); + if (!index_val) return PLIST_ERR_NO_MEM; + *index_val = ser->objects->len; + if (hash_table_insert(ser->ref_table, node, index_val) < 0) { + free(index_val); + return PLIST_ERR_NO_MEM; + } - //now append current node to object array - ptr_array_add(ser->objects, node); + // now append current node to object array + if (ptr_array_add(ser->objects, node) < 0) { + return PLIST_ERR_NO_MEM; + } - //now recurse on children + // now recurse on children node_t ch; + plist_err_t err = PLIST_ERR_SUCCESS; for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - serialize_plist(ch, data); + err = serialize_plist(ch, data, depth+1); + if (err != PLIST_ERR_SUCCESS) { + break; + } } + + // leave recursion stack + hash_table_remove(ser->in_stack, node); + + return err; } #define Log2(x) ((x) == 8 ? 3 : ((x) == 4 ? 2 : ((x) == 2 ? 1 : 0))) @@ -1051,72 +1276,91 @@ static void write_string(bytearray_t * bplist, char *val, uint64_t size) write_raw_data(bplist, BPLIST_STRING, (uint8_t *) val, size); } -static uint16_t *plist_utf8_to_utf16be(char *unistr, long size, long *items_read, long *items_written) +static uint16_t *plist_utf8_to_utf16be(const unsigned char *unistr, size_t size, size_t *items_read, size_t *items_written) { - uint16_t *outbuf; - int p = 0; - long i = 0; + uint16_t *outbuf; + size_t p = 0; + size_t i = 0; - unsigned char c0; - unsigned char c1; - unsigned char c2; - unsigned char c3; - - uint32_t w; + unsigned char c0; + unsigned char c1; + unsigned char c2; + unsigned char c3; - outbuf = (uint16_t*)malloc(((size*2)+1)*sizeof(uint16_t)); - if (!outbuf) { - PLIST_BIN_ERR("%s: Could not allocate %" PRIu64 " bytes\n", __func__, (uint64_t)((size*2)+1)*sizeof(uint16_t)); - return NULL; - } + if (size > (SIZE_MAX / sizeof(uint16_t) - 1) / 2) { + PLIST_BIN_ERR("%s: Input size %" PRIu64 " is too large\n", __func__, (uint64_t)size); + return NULL; + } + outbuf = (uint16_t*)malloc(((size*2)+1)*sizeof(uint16_t)); + if (!outbuf) { + PLIST_BIN_ERR("%s: Could not allocate %" PRIu64 " bytes\n", __func__, (uint64_t)((size*2)+1)*sizeof(uint16_t)); + return NULL; + } - while (i < size) { - c0 = unistr[i]; - c1 = (i < size-1) ? unistr[i+1] : 0; - c2 = (i < size-2) ? unistr[i+2] : 0; - c3 = (i < size-3) ? unistr[i+3] : 0; - if ((c0 >= 0xF0) && (i < size-3) && (c1 >= 0x80) && (c2 >= 0x80) && (c3 >= 0x80)) { - // 4 byte sequence. Need to generate UTF-16 surrogate pair - w = ((((c0 & 7) << 18) + ((c1 & 0x3F) << 12) + ((c2 & 0x3F) << 6) + (c3 & 0x3F)) & 0x1FFFFF) - 0x010000; - outbuf[p++] = be16toh(0xD800 + (w >> 10)); - outbuf[p++] = be16toh(0xDC00 + (w & 0x3FF)); - i+=4; - } else if ((c0 >= 0xE0) && (i < size-2) && (c1 >= 0x80) && (c2 >= 0x80)) { - // 3 byte sequence - outbuf[p++] = be16toh(((c2 & 0x3F) + ((c1 & 3) << 6)) + (((c1 >> 2) & 15) << 8) + ((c0 & 15) << 12)); - i+=3; - } else if ((c0 >= 0xC0) && (i < size-1) && (c1 >= 0x80)) { - // 2 byte sequence - outbuf[p++] = be16toh(((c1 & 0x3F) + ((c0 & 3) << 6)) + (((c0 >> 2) & 7) << 8)); - i+=2; - } else if (c0 < 0x80) { - // 1 byte sequence - outbuf[p++] = be16toh(c0); - i+=1; - } else { - // invalid character - PLIST_BIN_ERR("%s: invalid utf8 sequence in string at index %lu\n", __func__, i); - break; - } - } - if (items_read) { - *items_read = i; - } - if (items_written) { - *items_written = p; - } - outbuf[p] = 0; + while (i < size) { + c0 = unistr[i]; + c1 = (i+1 < size) ? unistr[i+1] : 0; + c2 = (i+2 < size) ? unistr[i+2] : 0; + c3 = (i+3 < size) ? unistr[i+3] : 0; + if ((c0 >= 0xF0 && c0 <= 0xF4) && (i+3 < size) && ((c1 & 0xC0) == 0x80) && ((c2 & 0xC0) == 0x80) && ((c3 & 0xC0) == 0x80)) { + // 4 byte sequence. Need to generate UTF-16 surrogate pair + /* lead-specific second-byte constraints */ + if ((c0 == 0xF0 && c1 < 0x90) || /* overlong (< U+10000) */ + (c0 == 0xF4 && c1 > 0x8F)) /* > U+10FFFF */ + { + break; + } + uint32_t w = ((uint32_t)(c3 & 0x3F)) | ((uint32_t)(c2 & 0x3F) << 6) | ((uint32_t)(c1 & 0x3F) << 12) | ((uint32_t)(c0 & 0x07) << 18); + if (w < 0x10000 || w > 0x10FFFF) break; + w -= 0x10000; + outbuf[p++] = be16toh((uint16_t)(0xD800 + (w >> 10))); + outbuf[p++] = be16toh((uint16_t)(0xDC00 + (w & 0x3FF))); + i+=4; + } else if (((c0 & 0xF0) == 0xE0) && (i+2 < size) && ((c1 & 0xC0) == 0x80) && ((c2 & 0xC0) == 0x80)) { + // 3 byte sequence + if ((c0 == 0xE0 && c1 < 0xA0) || /* overlong (< U+0800) */ + (c0 == 0xED && c1 > 0x9F)) /* UTF-16 surrogate range */ + { + break; + } + uint32_t w = ((uint32_t)(c2 & 0x3F)) | ((uint32_t)(c1 & 0x3F) << 6) | ((uint32_t)(c0 & 0x0F) << 12); + if (w < 0x800) break; + if (w >= 0xD800 && w <= 0xDFFF) break; // invalid Unicode scalar values + outbuf[p++] = be16toh((uint16_t)w); + i+=3; + } else if ((c0 >= 0xC2 && c0 <= 0xDF) && (i+1 < size) && ((c1 & 0xC0) == 0x80)) { + // 2 byte sequence + uint32_t w = ((uint32_t)(c1 & 0x3F)) | ((uint32_t)(c0 & 0x1F) << 6); + outbuf[p++] = be16toh((uint16_t)w); + i+=2; + } else if (c0 < 0x80) { + // 1 byte sequence + outbuf[p++] = be16toh((uint16_t)c0); + i+=1; + } else { + // invalid character + PLIST_BIN_ERR("%s: invalid utf8 sequence in string at index %zu\n", __func__, i); + break; + } + } + if (items_read) { + *items_read = i; + } + if (items_written) { + *items_written = p; + } + outbuf[p] = 0; - return outbuf; + return outbuf; } -static void write_unicode(bytearray_t * bplist, char *val, uint64_t size) +static void write_unicode(bytearray_t * bplist, char *val, size_t size) { - long items_read = 0; - long items_written = 0; + size_t items_read = 0; + size_t items_written = 0; uint16_t *unicodestr = NULL; - unicodestr = plist_utf8_to_utf16be(val, size, &items_read, &items_written); + unicodestr = plist_utf8_to_utf16be((const unsigned char *)val, size, &items_read, &items_written); write_raw_data(bplist, BPLIST_UNICODE, (uint8_t*)unicodestr, items_written); free(unicodestr); } @@ -1180,24 +1424,24 @@ static void write_uid(bytearray_t * bplist, uint64_t val) byte_array_append(bplist, (uint8_t*)&val + (8-size), size); } -static int is_ascii_string(char* s, int len) +static int is_ascii_string(const char* s, size_t len) { - int ret = 1, i = 0; - for(i = 0; i < len; i++) - { - if ( !isascii( s[i] ) ) - { - ret = 0; - break; - } - } - return ret; + int ret = 1; + size_t i = 0; + for (i = 0; i < len; i++) { + if ( !isascii( s[i] ) ) { + ret = 0; + break; + } + } + return ret; } plist_err_t plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) { ptrarray_t* objects = NULL; hashtable_t* ref_table = NULL; + hashtable_t* in_stack = NULL; struct serialize_s ser_s; uint8_t offset_size = 0; uint8_t ref_size = 0; @@ -1227,11 +1471,28 @@ plist_err_t plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) ptr_array_free(objects); return PLIST_ERR_NO_MEM; } + //hashtable for circular reference detection + in_stack = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); + if (!in_stack) { + ptr_array_free(objects); + hash_table_destroy(ref_table); + return PLIST_ERR_NO_MEM; + } //serialize plist ser_s.objects = objects; ser_s.ref_table = ref_table; - serialize_plist((node_t)plist, &ser_s); + ser_s.in_stack = in_stack; + plist_err_t err = serialize_plist((node_t)plist, &ser_s, 0); + if (err != PLIST_ERR_SUCCESS) { + ptr_array_free(objects); + hash_table_destroy(ref_table); + hash_table_destroy(in_stack); + return err; + } + //no longer needed + hash_table_destroy(in_stack); + ser_s.in_stack = NULL; //now stream to output buffer offset_size = 0; //unknown yet @@ -1338,7 +1599,12 @@ plist_err_t plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) //write objects and table offsets = (uint64_t *) malloc(num_objects * sizeof(uint64_t)); - assert(offsets != NULL); + if (!offsets) { + byte_array_free(bplist_buff); + ptr_array_free(objects); + hash_table_destroy(ref_table); + return PLIST_ERR_NO_MEM; + } for (i = 0; i < num_objects; i++) { @@ -1424,6 +1690,13 @@ plist_err_t plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length) byte_array_append(bplist_buff, &trailer, sizeof(bplist_trailer_t)); + if (!bplist_buff->data) { + byte_array_free(bplist_buff); + *plist_bin = NULL; + *length = 0; + return PLIST_ERR_NO_MEM; + } + //set output buffer and size *plist_bin = (char*)bplist_buff->data; *length = bplist_buff->len; diff --git a/src/bytearray.c b/src/bytearray.c index 39fad5f..19303e0 100644 --- a/src/bytearray.c +++ b/src/bytearray.c @@ -19,6 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include <string.h> +#include <stdint.h> #include "bytearray.h" #define PAGE_SIZE 4096 @@ -26,8 +27,24 @@ bytearray_t *byte_array_new(size_t initial) { bytearray_t *a = (bytearray_t*)malloc(sizeof(bytearray_t)); - a->capacity = (initial > PAGE_SIZE) ? (initial+(PAGE_SIZE-1)) & (~(PAGE_SIZE-1)) : PAGE_SIZE; + if (!a) { + return NULL; + } + if (initial > PAGE_SIZE) { + if (initial > SIZE_MAX - (PAGE_SIZE-1)) { + /* rounding up to the page size would overflow */ + free(a); + return NULL; + } + a->capacity = (initial+(PAGE_SIZE-1)) & (~(PAGE_SIZE-1)); + } else { + a->capacity = PAGE_SIZE; + } a->data = malloc(a->capacity); + if (!a->data) { + free(a); + return NULL; + } a->len = 0; a->stream = NULL; return a; @@ -36,6 +53,9 @@ bytearray_t *byte_array_new(size_t initial) bytearray_t *byte_array_new_for_stream(FILE *stream) { bytearray_t *a = (bytearray_t*)malloc(sizeof(bytearray_t)); + if (!a) { + return NULL; + } a->capacity = (size_t)-1; a->data = NULL; a->len = 0; @@ -54,11 +74,46 @@ void byte_array_free(bytearray_t *ba) void byte_array_grow(bytearray_t *ba, size_t amount) { - if (ba->stream) { + if (ba->stream || !ba->data) { return; } - size_t increase = (amount > PAGE_SIZE) ? (amount+(PAGE_SIZE-1)) & (~(PAGE_SIZE-1)) : PAGE_SIZE; - ba->data = realloc(ba->data, ba->capacity + increase); + size_t increase; + if (amount > PAGE_SIZE) { + /* round up to the page size, guarding against integer overflow */ + increase = (amount <= SIZE_MAX - (PAGE_SIZE-1)) ? ((amount+(PAGE_SIZE-1)) & (~(PAGE_SIZE-1))) : 0; + } else { + increase = PAGE_SIZE; + } + if (increase == 0 || increase > SIZE_MAX - ba->capacity) { + /* integer overflow */ + free(ba->data); + ba->data = NULL; + ba->len = 0; + ba->capacity = 0; + return; + } + /* grow geometrically (doubling) to keep repeated appends linear; if that + * fails, fall back to the minimum required size before giving up */ + void *newdata = NULL; + if (ba->capacity <= ((size_t)-1) / 2 && ba->capacity > increase) { + newdata = realloc(ba->data, ba->capacity * 2); + if (newdata) { + increase = ba->capacity; + } + } + if (!newdata) { + newdata = realloc(ba->data, ba->capacity + increase); + } + if (!newdata) { + /* out of memory: put the array into a failed state (data == NULL), + * further appends are ignored and callers must check ba->data */ + free(ba->data); + ba->data = NULL; + ba->len = 0; + ba->capacity = 0; + return; + } + ba->data = newdata; ba->capacity += increase; } @@ -72,10 +127,17 @@ void byte_array_append(bytearray_t *ba, void *buf, size_t len) #endif } } else { + if (len > SIZE_MAX - ba->len) { + /* integer overflow when computing the new total length */ + return; + } size_t remaining = ba->capacity-ba->len; if (len > remaining) { size_t needed = len - remaining; byte_array_grow(ba, needed); + if (!ba->data) { + return; + } } memcpy(((char*)ba->data) + ba->len, buf, len); } diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..810c2e0 --- /dev/null +++ b/src/common.c @@ -0,0 +1,115 @@ +/* + * common.c + * contains some common functions + * + * Copyright (c) 2026 Nikias Bassen, 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 <math.h> +#include <stdio.h> +#include <stdint.h> +#include "common.h" + +size_t dtostr(char *buf, size_t bufsize, double realval) +{ + int slen = 0; + if (isnan(realval)) { + slen = snprintf(buf, bufsize, "nan"); + } else if (isinf(realval)) { + slen = snprintf(buf, bufsize, "%cinfinity", (realval > 0.0) ? '+' : '-'); + } else if (realval == 0.0f) { + slen = snprintf(buf, bufsize, "0.0"); + } else { + slen = snprintf(buf, bufsize, "%.*g", 17, realval); + if (slen < 0) { + return 0; + } + if (!buf || bufsize == 0) { + return (size_t)slen; + } + size_t len = (size_t)slen; + if (len >= bufsize) { + len = bufsize - 1; + } + size_t i = 0; + for (i = 0; i < len; i++) { + if (buf[i] == ',') { + buf[i] = '.'; + break; + } else if (buf[i] == '.') { + break; + } + } + return len; + } + if (slen < 0) { + return 0; + } + return (size_t)slen; +} + +/* based on https://stackoverflow.com/a/4143288 */ +#define PO10i_LIMIT (INT64_MAX/10) +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; +} +#undef PO10i_LIMIT + +/* based on https://stackoverflow.com/a/4143288 */ +#define PO10u_LIMIT (UINT64_MAX/10) +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; +} +#undef PO10u_LIMIT + +int plist_real_to_time64(double realval, Time64_T *timev) +{ + if (!timev || !isfinite(realval)) { + return -1; + } + + if (realval < (double)TIME64_MIN - (double)MAC_EPOCH || + realval > (double)TIME64_MAX - (double)MAC_EPOCH) { + return -1; + } + + *timev = (Time64_T)realval + MAC_EPOCH; + return 0; +} diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..ffaa78c --- /dev/null +++ b/src/common.h @@ -0,0 +1,34 @@ +/* + * common.h + * contains some common functions + * + * Copyright (c) 2026 Nikias Bassen, 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 + */ +#ifndef COMMON_H +#define COMMON_H + +#include <stddef.h> +#include "time64.h" + +#define MAC_EPOCH 978307200 + +size_t dtostr(char *buf, size_t bufsize, double realval); +int num_digits_i(int64_t i); +int num_digits_u(uint64_t i); +int plist_real_to_time64(double realval, Time64_T *timev); + +#endif diff --git a/src/hashtable.c b/src/hashtable.c index 86dae82..e3f9b98 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -24,6 +24,9 @@ hashtable_t* hash_table_new(hash_func_t hash_func, compare_func_t compare_func, { hashtable_t* ht = (hashtable_t*)malloc(sizeof(hashtable_t)); int i; + if (!ht) { + return NULL; + } for (i = 0; i < 4096; i++) { ht->entries[i] = NULL; } @@ -47,7 +50,7 @@ void hash_table_destroy(hashtable_t *ht) ht->free_func(e->value); } hashentry_t* old = e; - e = (hashentry_t*)e->next; + e = e->next; free(old); } } @@ -55,9 +58,9 @@ void hash_table_destroy(hashtable_t *ht) free(ht); } -void hash_table_insert(hashtable_t* ht, void *key, void *value) +int hash_table_insert(hashtable_t* ht, void *key, void *value) { - if (!ht || !key) return; + if (!ht || !key) return -1; unsigned int hash = ht->hash_func(key); @@ -69,15 +72,18 @@ void hash_table_insert(hashtable_t* ht, void *key, void *value) if (ht->compare_func(e->key, key)) { // element already present. replace value. e->value = value; - return; + return 0; } - e = (hashentry_t*)e->next; + e = e->next; } // if we get here, the element is not yet in the list. // make a new entry. hashentry_t* entry = (hashentry_t*)malloc(sizeof(hashentry_t)); + if (!entry) { + return -1; + } entry->key = key; entry->value = value; if (!ht->entries[idx0]) { @@ -89,6 +95,7 @@ void hash_table_insert(hashtable_t* ht, void *key, void *value) } ht->entries[idx0] = entry; ht->count++; + return 0; } void* hash_table_lookup(hashtable_t* ht, void *key) @@ -103,7 +110,7 @@ void* hash_table_lookup(hashtable_t* ht, void *key) if (ht->compare_func(e->key, key)) { return e->value; } - e = (hashentry_t*)e->next; + e = e->next; } return NULL; } @@ -124,7 +131,7 @@ void hash_table_remove(hashtable_t* ht, void *key) // found element, remove it from the list hashentry_t* old = e; if (e == ht->entries[idx0]) { - ht->entries[idx0] = (hashentry_t*)e->next; + ht->entries[idx0] = e->next; } else { last->next = e->next; } @@ -135,6 +142,6 @@ void hash_table_remove(hashtable_t* ht, void *key) return; } last = e; - e = (hashentry_t*)e->next; + e = e->next; } } diff --git a/src/hashtable.h b/src/hashtable.h index 42d7b93..1b6976d 100644 --- a/src/hashtable.h +++ b/src/hashtable.h @@ -25,7 +25,7 @@ typedef struct hashentry_t { void *key; void *value; - void *next; + struct hashentry_t *next; } hashentry_t; typedef unsigned int(*hash_func_t)(const void* key); @@ -43,7 +43,8 @@ typedef struct hashtable_t { hashtable_t* hash_table_new(hash_func_t hash_func, compare_func_t compare_func, free_func_t free_func); void hash_table_destroy(hashtable_t *ht); -void hash_table_insert(hashtable_t* ht, void *key, void *value); +/* returns 0 on success, -1 on error (invalid argument or out of memory) */ +int hash_table_insert(hashtable_t* ht, void *key, void *value); void* hash_table_lookup(hashtable_t* ht, void *key); void hash_table_remove(hashtable_t* ht, void *key); diff --git a/src/jplist.c b/src/jplist.c index 782d2b3..193d231 100644 --- a/src/jplist.c +++ b/src/jplist.c @@ -38,6 +38,10 @@ #include "plist.h" #include "strbuf.h" #include "jsmn.h" +#include "hashtable.h" +#include "base64.h" +#include "time64.h" +#include "common.h" #ifdef DEBUG static int plist_json_debug = 0; @@ -72,8 +76,10 @@ void plist_json_set_debug(int debug) } #ifndef HAVE_STRNDUP +#ifndef _MSC_VER #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" +#endif static char* strndup(const char* str, size_t len) { char *newstr = (char *)malloc(len+1); @@ -83,34 +89,12 @@ static char* strndup(const char* str, size_t len) } return newstr; } +#ifndef _MSC_VER #pragma GCC diagnostic pop #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) +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; @@ -142,6 +126,9 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept case PLIST_INT: val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } if (node_data->length == 16) { val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); } else { @@ -153,6 +140,9 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept case PLIST_REAL: val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } val_len = dtostr(val, 64, node_data->realval); str_buf_append(*outbuf, val, val_len); free(val); @@ -206,7 +196,7 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept str_buf_append(*outbuf, " ", 2); } } - plist_err_t res = node_to_json(ch, outbuf, depth+1, prettify); + plist_err_t res = node_to_json(ch, outbuf, depth+1, prettify, coerce); if (res < 0) { return res; } @@ -234,7 +224,7 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept str_buf_append(*outbuf, " ", 2); } } - plist_err_t res = node_to_json(ch, outbuf, depth+1, prettify); + plist_err_t res = node_to_json(ch, outbuf, depth+1, prettify, coerce); if (res < 0) { return res; } @@ -255,17 +245,67 @@ static plist_err_t node_to_json(node_t node, bytearray_t **outbuf, uint32_t dept str_buf_append(*outbuf, "}", 1); } break; case PLIST_DATA: - // NOT VALID FOR JSON - PLIST_JSON_WRITE_ERR("PLIST_DATA type is not valid for JSON format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + size_t b64_len = ((node_data->length + 2) / 3) * 4; + char *b64_buf = (char*)malloc(b64_len + 1); + if (!b64_buf) { + return PLIST_ERR_NO_MEM; + } + size_t actual_len = base64encode(b64_buf, node_data->buff, node_data->length); + str_buf_append(*outbuf, "\"", 1); + str_buf_append(*outbuf, b64_buf, actual_len); + str_buf_append(*outbuf, "\"", 1); + free(b64_buf); + } else { + PLIST_JSON_WRITE_ERR("PLIST_DATA type is not valid for JSON format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_DATE: - // NOT VALID FOR JSON - PLIST_JSON_WRITE_ERR("PLIST_DATE type is not valid for JSON format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + Time64_T timev; + if (plist_real_to_time64(node_data->realval, &timev) < 0) { + PLIST_JSON_WRITE_ERR("Encountered invalid date value %f\n", node_data->realval); + return PLIST_ERR_INVALID_ARG; + } + struct TM _btime; + struct TM *btime = gmtime64_r(&timev, &_btime); + char datebuf[32]; + size_t datelen = 0; + if (btime) { + struct tm _tmcopy; + copy_TM64_to_tm(btime, &_tmcopy); + datelen = strftime(datebuf, sizeof(datebuf), "%Y-%m-%dT%H:%M:%SZ", &_tmcopy); + } + if (datelen <= 0) { + datelen = snprintf(datebuf, sizeof(datebuf), "1970-01-01T00:00:00Z"); + } + str_buf_append(*outbuf, "\"", 1); + str_buf_append(*outbuf, datebuf, datelen); + str_buf_append(*outbuf, "\"", 1); + } else { + PLIST_JSON_WRITE_ERR("PLIST_DATE type is not valid for JSON format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_UID: - // NOT VALID FOR JSON - PLIST_JSON_WRITE_ERR("PLIST_UID type is not valid for JSON format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } + if (node_data->length == 16) { + val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + } else { + val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + } + str_buf_append(*outbuf, val, val_len); + free(val); + } else { + PLIST_JSON_WRITE_ERR("PLIST_UID type is not valid for JSON format\n"); + return PLIST_ERR_FORMAT; + } + break; default: return PLIST_ERR_UNKNOWN; } @@ -273,56 +313,34 @@ 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) +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; if (!node) { return PLIST_ERR_INVALID_ARG; } + + if (depth > PLIST_MAX_NESTING_DEPTH) { + PLIST_JSON_WRITE_ERR("maximum nesting depth (%u) exceeded\n", (unsigned)PLIST_MAX_NESTING_DEPTH); + return PLIST_ERR_MAX_NESTING; + } + + if (hash_table_lookup(visited, node)) { + PLIST_JSON_WRITE_ERR("circular reference detected\n"); + return PLIST_ERR_CIRCULAR_REF; + } + + // mark as visited + if (hash_table_insert(visited, node, (void*)1) < 0) { + return PLIST_ERR_NO_MEM; + } + data = plist_get_data(node); if (node->children) { node_t ch; unsigned int n_children = node_n_children(node); for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - plist_err_t res = node_estimate_size(ch, size, depth + 1, prettify); + plist_err_t res = _node_estimate_size(ch, size, depth + 1, prettify, coerce, visited); if (res < 0) { return res; } @@ -379,17 +397,36 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept *size += 2; break; case PLIST_DATA: - // NOT VALID FOR JSON - PLIST_JSON_WRITE_ERR("PLIST_DATA type is not valid for JSON format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + // base64 encoded string: 2 quotes + ((len+2)/3)*4 base64 chars + *size += 2 + ((data->length + 2) / 3) * 4; + } else { + PLIST_JSON_WRITE_ERR("PLIST_DATA type is not valid for JSON format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_DATE: - // NOT VALID FOR JSON - PLIST_JSON_WRITE_ERR("PLIST_DATE type is not valid for JSON format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + // ISO 8601 string: "YYYY-MM-DDTHH:MM:SSZ" = 22 chars max + *size += 24; + } else { + PLIST_JSON_WRITE_ERR("PLIST_DATE type is not valid for JSON format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_UID: - // NOT VALID FOR JSON - PLIST_JSON_WRITE_ERR("PLIST_UID type is not valid for JSON format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + // integer representation + if (data->length == 16) { + *size += num_digits_u(data->intval); + } else { + *size += num_digits_i((int64_t)data->intval); + } + } else { + PLIST_JSON_WRITE_ERR("PLIST_UID type is not valid for JSON format\n"); + return PLIST_ERR_FORMAT; + } + break; default: PLIST_JSON_WRITE_ERR("invalid node type encountered\n"); return PLIST_ERR_UNKNOWN; @@ -398,8 +435,23 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept return PLIST_ERR_SUCCESS; } +static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify, int coerce) +{ + hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); + if (!visited) return PLIST_ERR_NO_MEM; + plist_err_t err = _node_estimate_size(node, size, depth, prettify, coerce, visited); + hash_table_destroy(visited); + return err; +} + plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, int prettify) { + plist_write_options_t opts = prettify ? PLIST_OPT_NONE : PLIST_OPT_COMPACT; + return plist_to_json_with_options(plist, plist_json, length, opts); +} + +plist_err_t plist_to_json_with_options(plist_t plist, char **plist_json, uint32_t* length, plist_write_options_t options) +{ uint64_t size = 0; plist_err_t res; @@ -412,7 +464,10 @@ plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, in return PLIST_ERR_FORMAT; } - res = node_estimate_size((node_t)plist, &size, 0, prettify); + int prettify = !(options & PLIST_OPT_COMPACT); + int coerce = options & PLIST_OPT_COERCE; + + res = node_estimate_size((node_t)plist, &size, 0, prettify, coerce); if (res < 0) { return res; } @@ -423,7 +478,7 @@ plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, in return PLIST_ERR_NO_MEM; } - res = node_to_json((node_t)plist, &outbuf, 0, prettify); + res = node_to_json((node_t)plist, &outbuf, 0, prettify, coerce); if (res < 0) { str_buf_free(outbuf); *plist_json = NULL; @@ -436,6 +491,13 @@ plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, in str_buf_append(outbuf, "\0", 1); + if (!outbuf->data) { + str_buf_free(outbuf); + *plist_json = NULL; + *length = 0; + return PLIST_ERR_NO_MEM; + } + *plist_json = (char*)outbuf->data; *length = outbuf->len - 1; @@ -448,10 +510,12 @@ plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, in typedef struct { jsmntok_t* tokens; int count; + plist_err_t err; } jsmntok_info_t; 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; @@ -460,12 +524,14 @@ static int64_t parse_decimal(const char* str, const char* str_end, char** endp) if (str[0] == '-') { is_neg = 1; (*endp)++; + } else if (str[0] == '+') { + (*endp)++; } if (is_neg) { MAX++; } while (*endp < str_end && isdigit(**endp)) { - if (x > PO10i_LIMIT) { + if (x > po10i_limit) { x = MAX; break; } @@ -509,8 +575,15 @@ static plist_t parse_primitive(const char* js, jsmntok_info_t* ti, int* index) val = plist_new_bool(1); } else if (!strncmp("null", str_val, str_len)) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_JSON_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_NULL; val = plist_new_node(data); + if (!val) { + plist_free_data(data); + } } else if (isdigit(str_val[0]) || (str_val[0] == '-' && str_val+1 < str_end && isdigit(str_val[1]))) { char* endp = (char*)str_val; int is_neg = (str_val[0] == '-'); @@ -522,7 +595,7 @@ static plist_t parse_primitive(const char* js, jsmntok_info_t* ti, int* index) } else { val = plist_new_uint((uint64_t)intpart); } - } else if ((*endp == '.' && endp+1 < str_end && isdigit(*(endp+1))) || ((*endp == 'e' || *endp == 'E') && endp+1 < str_end && (isdigit(*(endp+1)) || ((*(endp+1) == '-') && endp+2 < str_end && isdigit(*(endp+2)))))) { + } else if ((*endp == '.' && endp+1 < str_end && isdigit(*(endp+1))) || ((*endp == 'e' || *endp == 'E') && endp+1 < str_end && (isdigit(*(endp+1)) || (((*(endp+1) == '-') || (*(endp+1) == '+')) && endp+2 < str_end && isdigit(*(endp+2)))))) { /* floating point */ double dval = (double)intpart; char* fendp = endp; @@ -546,7 +619,7 @@ static plist_t parse_primitive(const char* js, jsmntok_info_t* ti, int* index) if (fendp >= str_end) { break; } - if (fendp+1 < str_end && (*fendp == 'e' || *fendp == 'E') && (isdigit(*(fendp+1)) || ((*(fendp+1) == '-') && fendp+2 < str_end && isdigit(*(fendp+2))))) { + if (fendp+1 < str_end && (*fendp == 'e' || *fendp == 'E') && (isdigit(*(fendp+1)) || (((*(fendp+1) == '-') || (*(fendp+1) == '+')) && fendp+2 < str_end && isdigit(*(fendp+2))))) { int64_t exp = parse_decimal(fendp+1, str_end, &fendp); dval = dval * pow(10, (double)exp); } else { @@ -567,6 +640,10 @@ static plist_t parse_primitive(const char* js, jsmntok_info_t* ti, int* index) } else { PLIST_JSON_ERR("%s: invalid primitive value '%.*s' encountered\n", __func__, (int)str_len, str_val); } + if (!val) { + PLIST_JSON_ERR("%s: failed to create node\n", __func__); + return NULL; + } (*index)++; return val; } @@ -574,6 +651,7 @@ static plist_t parse_primitive(const char* js, jsmntok_info_t* ti, int* index) static char* unescape_string(const char* str_val, size_t str_len, size_t *new_len) { char* strval = strndup(str_val, str_len); + if (!strval) return NULL; size_t i = 0; while (i < str_len) { if (strval[i] == '\\' && i < str_len-1) { @@ -664,40 +742,62 @@ static plist_t parse_string(const char* js, jsmntok_info_t* ti, int* index) plist_t node; plist_data_t data = plist_new_plist_data(); + if (!data) { + free(strval); + PLIST_JSON_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_STRING; data->strval = strval; data->length = str_len; node = plist_new_node(data); + if (!node) { + plist_free_data(data); + PLIST_JSON_ERR("%s: failed to create node\n", __func__); + return NULL; + } (*index)++; return node; } -static plist_t parse_object(const char* js, jsmntok_info_t* ti, int* index); +static plist_t parse_object(const char* js, jsmntok_info_t* ti, int* index, uint32_t depth); -static plist_t parse_array(const char* js, jsmntok_info_t* ti, int* index) +static plist_t parse_array(const char* js, jsmntok_info_t* ti, int* index, uint32_t depth) { if (ti->tokens[*index].type != JSMN_ARRAY) { PLIST_JSON_ERR("%s: token type != JSMN_ARRAY\n", __func__); + ti->err = PLIST_ERR_PARSE; + return NULL; + } + if (depth > PLIST_MAX_NESTING_DEPTH) { + PLIST_JSON_ERR("%s: maximum nesting depth (%u) exceeded\n", __func__, (unsigned)PLIST_MAX_NESTING_DEPTH); + ti->err = PLIST_ERR_MAX_NESTING; return NULL; } plist_t arr = plist_new_array(); - int num_tokens = ti->tokens[*index].size; - int num; + if (!arr) { + PLIST_JSON_ERR("%s: failed to create array node\n", __func__); + ti->err = PLIST_ERR_NO_MEM; + return NULL; + } + size_t num_tokens = ti->tokens[*index].size; + size_t num; int j = (*index)+1; for (num = 0; num < num_tokens; num++) { if (j >= ti->count) { PLIST_JSON_ERR("%s: token index out of valid range\n", __func__); plist_free(arr); + ti->err = PLIST_ERR_PARSE; return NULL; } plist_t val = NULL; switch (ti->tokens[j].type) { case JSMN_OBJECT: - val = parse_object(js, ti, &j); + val = parse_object(js, ti, &j, depth+1); break; case JSMN_ARRAY: - val = parse_array(js, ti, &j); + val = parse_array(js, ti, &j, depth+1); break; case JSMN_STRING: val = parse_string(js, ti, &j); @@ -710,8 +810,16 @@ static plist_t parse_array(const char* js, jsmntok_info_t* ti, int* index) } if (val) { plist_array_append_item(arr, val); + // if append failed, val still has no parent, free it and abort + if (((node_t)val)->parent == NULL) { + plist_free(val); + plist_free(arr); + ti->err = PLIST_ERR_NO_MEM; + return NULL; + } } else { plist_free(arr); + ti->err = PLIST_ERR_PARSE; return NULL; } } @@ -719,30 +827,44 @@ static plist_t parse_array(const char* js, jsmntok_info_t* ti, int* index) return arr; } -static plist_t parse_object(const char* js, jsmntok_info_t* ti, int* index) +static plist_t parse_object(const char* js, jsmntok_info_t* ti, int* index, uint32_t depth) { if (ti->tokens[*index].type != JSMN_OBJECT) { PLIST_JSON_ERR("%s: token type != JSMN_OBJECT\n", __func__); + ti->err = PLIST_ERR_PARSE; + return NULL; + } + if (depth > PLIST_MAX_NESTING_DEPTH) { + PLIST_JSON_ERR("%s: maximum nesting depth (%u) exceeded\n", __func__, (unsigned)PLIST_MAX_NESTING_DEPTH); + ti->err = PLIST_ERR_MAX_NESTING; return NULL; } - int num_tokens = ti->tokens[*index].size; - int num; + size_t num_tokens = ti->tokens[*index].size; + size_t num; int j = (*index)+1; if (num_tokens % 2 != 0) { PLIST_JSON_ERR("%s: number of children must be even\n", __func__); + ti->err = PLIST_ERR_PARSE; return NULL; } plist_t obj = plist_new_dict(); + if (!obj) { + PLIST_JSON_ERR("%s: failed to create dict node\n", __func__); + ti->err = PLIST_ERR_NO_MEM; + return NULL; + } for (num = 0; num < num_tokens; num++) { if (j+1 >= ti->count) { PLIST_JSON_ERR("%s: token index out of valid range\n", __func__); plist_free(obj); + ti->err = PLIST_ERR_PARSE; return NULL; } if (ti->tokens[j].type == JSMN_STRING) { char* key = unescape_string(js + ti->tokens[j].start, ti->tokens[j].end - ti->tokens[j].start, NULL); if (!key) { plist_free(obj); + ti->err = PLIST_ERR_PARSE; return NULL; } plist_t val = NULL; @@ -750,10 +872,10 @@ static plist_t parse_object(const char* js, jsmntok_info_t* ti, int* index) num++; switch (ti->tokens[j].type) { case JSMN_OBJECT: - val = parse_object(js, ti, &j); + val = parse_object(js, ti, &j, depth+1); break; case JSMN_ARRAY: - val = parse_array(js, ti, &j); + val = parse_array(js, ti, &j, depth+1); break; case JSMN_STRING: val = parse_string(js, ti, &j); @@ -766,15 +888,25 @@ static plist_t parse_object(const char* js, jsmntok_info_t* ti, int* index) } if (val) { plist_dict_set_item(obj, key, val); + // if set failed, val still has no parent, free it and abort + if (((node_t)val)->parent == NULL) { + plist_free(val); + free(key); + plist_free(obj); + ti->err = PLIST_ERR_NO_MEM; + return NULL; + } } else { free(key); plist_free(obj); + ti->err = PLIST_ERR_PARSE; return NULL; } free(key); } else { PLIST_JSON_ERR("%s: keys must be of type STRING\n", __func__); plist_free(obj); + ti->err = PLIST_ERR_PARSE; return NULL; } } @@ -794,14 +926,15 @@ plist_err_t plist_from_json(const char *json, uint32_t length, plist_t * plist) jsmn_parser parser; jsmn_init(&parser); - int maxtoks = 256; - int curtoks = 0; + unsigned int maxtoks = 256; + unsigned int curtoks = 0; int r = 0; jsmntok_t *tokens = NULL; do { jsmntok_t* newtokens = (jsmntok_t*)realloc(tokens, sizeof(jsmntok_t)*maxtoks); if (!newtokens) { + free(tokens); PLIST_JSON_ERR("%s: Out of memory\n", __func__); return PLIST_ERR_NO_MEM; } @@ -811,8 +944,14 @@ plist_err_t plist_from_json(const char *json, uint32_t length, plist_t * plist) r = jsmn_parse(&parser, json, length, tokens, maxtoks); if (r == JSMN_ERROR_NOMEM) { + if (maxtoks > (unsigned int)INT_MAX - 16) { + free(tokens); + return PLIST_ERR_NO_MEM; + } maxtoks+=16; continue; + } else if (r < 0) { + break; } } while (r == JSMN_ERROR_NOMEM); @@ -829,12 +968,16 @@ plist_err_t plist_from_json(const char *json, uint32_t length, plist_t * plist) PLIST_JSON_ERR("%s: Incomplete JSON, more bytes expected\n", __func__); free(tokens); return PLIST_ERR_PARSE; + case JSMN_ERROR_LIMIT: + PLIST_JSON_ERR("%s: Input data too large\n", __func__); + free(tokens); + return PLIST_ERR_PARSE; default: break; } int startindex = 0; - jsmntok_info_t ti = { tokens, parser.toknext }; + jsmntok_info_t ti = { tokens, parser.toknext, PLIST_ERR_SUCCESS }; switch (tokens[startindex].type) { case JSMN_PRIMITIVE: *plist = parse_primitive(json, &ti, &startindex); @@ -843,14 +986,17 @@ plist_err_t plist_from_json(const char *json, uint32_t length, plist_t * plist) *plist = parse_string(json, &ti, &startindex); break; case JSMN_ARRAY: - *plist = parse_array(json, &ti, &startindex); + *plist = parse_array(json, &ti, &startindex, 0); break; case JSMN_OBJECT: - *plist = parse_object(json, &ti, &startindex); + *plist = parse_object(json, &ti, &startindex, 0); break; default: break; } free(tokens); + if (!*plist) { + return (ti.err != PLIST_ERR_SUCCESS) ? ti.err : PLIST_ERR_PARSE; + } return PLIST_ERR_SUCCESS; } @@ -3,6 +3,8 @@ * Simple JSON parser * * Copyright (c) 2010 Serge A. Zaitsev + * Updated to use size_t for token offsets and harden against overflows. + * (Nikias Bassen, January 2026) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,20 +26,25 @@ */ #include <stdlib.h> +#include <stdint.h> +#include <limits.h> +#include <assert.h> #include "jsmn.h" +#define JSMN_POS_INVALID ((size_t)SIZE_MAX) + /** * Allocates a fresh unused token from the token pull. */ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, - jsmntok_t *tokens, int num_tokens) { + jsmntok_t *tokens, unsigned int num_tokens) { jsmntok_t *tok; - if (parser->toknext >= num_tokens) { + if ((unsigned int)parser->toknext >= num_tokens) { return NULL; } tok = &tokens[parser->toknext++]; - tok->start = tok->end = -1; + tok->start = tok->end = JSMN_POS_INVALID; tok->size = 0; #ifdef JSMN_PARENT_LINKS tok->parent = -1; @@ -49,7 +56,7 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, * Fills token type and boundaries. */ static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, - int start, int end) { + size_t start, size_t end) { token->type = type; token->start = start; token->end = end; @@ -60,9 +67,9 @@ static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, * Fills next available token with JSON primitive. */ static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js, - jsmntok_t *tokens, int num_tokens) { + jsmntok_t *tokens, unsigned int num_tokens) { jsmntok_t *token; - int start; + size_t start; start = parser->pos; @@ -107,10 +114,10 @@ found: * Fills next token with JSON string. */ static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js, - jsmntok_t *tokens, int num_tokens) { + jsmntok_t *tokens, unsigned int num_tokens) { jsmntok_t *token; - int start = parser->pos; + size_t start = parser->pos; parser->pos++; @@ -162,7 +169,7 @@ static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js, /** * Parse JSON string and fill tokens. */ -jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned int length, jsmntok_t *tokens, +jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t length, jsmntok_t *tokens, unsigned int num_tokens) { jsmnerr_t r; int i; @@ -170,6 +177,13 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned int length, j parser->end = length; + if (num_tokens >= INT_MAX) { + return JSMN_ERROR_LIMIT; + } + if (length > SIZE_MAX / 2) { + return JSMN_ERROR_LIMIT; + } + for (; (parser->end > 0 && parser->pos < parser->end) && js[parser->pos] != '\0'; parser->pos++) { char c; jsmntype_t type; @@ -198,10 +212,13 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned int length, j } token = &tokens[parser->toknext - 1]; for (;;) { - if (token->start != -1 && token->end == -1) { + if (token->start != JSMN_POS_INVALID && token->end == JSMN_POS_INVALID) { if (token->type != type) { return JSMN_ERROR_INVAL; } + if (parser->pos == SIZE_MAX) { + return JSMN_ERROR_INVAL; + } token->end = parser->pos + 1; parser->toksuper = token->parent; break; @@ -214,7 +231,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned int length, j #else for (i = parser->toknext - 1; i >= 0; i--) { token = &tokens[i]; - if (token->start != -1 && token->end == -1) { + if (token->start != JSMN_POS_INVALID && token->end == JSMN_POS_INVALID) { if (token->type != type) { return JSMN_ERROR_INVAL; } @@ -227,7 +244,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned int length, j if (i == -1) return JSMN_ERROR_INVAL; for (; i >= 0; i--) { token = &tokens[i]; - if (token->start != -1 && token->end == -1) { + if (token->start != JSMN_POS_INVALID && token->end == JSMN_POS_INVALID) { parser->toksuper = i; break; } @@ -268,7 +285,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned int length, j for (i = parser->toknext - 1; i >= 0; i--) { /* Unmatched opened object or array */ - if (tokens[i].start != -1 && tokens[i].end == -1) { + if (tokens[i].start != JSMN_POS_INVALID && tokens[i].end == JSMN_POS_INVALID) { return JSMN_ERROR_PART; } } @@ -3,6 +3,8 @@ * Simple JSON parser (header file) * * Copyright (c) 2010 Serge A. Zaitsev + * Updated to use size_t for token offsets and harden against overflows. + * (Nikias Bassen, January 2026) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,6 +27,8 @@ #ifndef __JSMN_H_ #define __JSMN_H_ +#include <stddef.h> + /** * JSON type identifier. Basic types are: * o Object @@ -46,6 +50,8 @@ typedef enum { JSMN_ERROR_INVAL = -2, /* The string is not a full JSON packet, more bytes expected */ JSMN_ERROR_PART = -3, + /* Input exceeds implementation-defined limits */ + JSMN_ERROR_LIMIT = -4, /* Everything was fine */ JSMN_SUCCESS = 0 } jsmnerr_t; @@ -58,9 +64,9 @@ typedef enum { */ typedef struct { jsmntype_t type; - int start; - int end; - int size; + size_t start; + size_t end; + size_t size; #ifdef JSMN_PARENT_LINKS int parent; #endif @@ -71,8 +77,8 @@ typedef struct { * the string being parsed now and current position in that string */ typedef struct { - unsigned int pos; /* offset in the JSON string */ - unsigned int end; /* offset after last character of JSON string */ + size_t pos; /* offset in the JSON string */ + size_t end; /* offset after last character of JSON string */ int toknext; /* next token to allocate */ int toksuper; /* superior token node, e.g parent object or array */ } jsmn_parser; @@ -86,7 +92,7 @@ void jsmn_init(jsmn_parser *parser); * Run JSON parser. It parses a JSON data string into and array of tokens, each describing * a single JSON object. */ -jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, unsigned int length, +jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t length, jsmntok_t *tokens, unsigned int num_tokens); #endif /* __JSMN_H_ */ diff --git a/src/oplist.c b/src/oplist.c index 6ab6603..977427d 100644 --- a/src/oplist.c +++ b/src/oplist.c @@ -37,6 +37,9 @@ #include "plist.h" #include "strbuf.h" +#include "time64.h" +#include "hashtable.h" +#include "common.h" #ifdef DEBUG static int plist_ostep_debug = 0; @@ -71,8 +74,10 @@ void plist_ostep_set_debug(int debug) } #ifndef HAVE_STRNDUP +#ifndef _MSC_VER #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" +#endif static char* strndup(const char* str, size_t len) { char *newstr = (char *)malloc(len+1); @@ -82,32 +87,10 @@ static char* strndup(const char* str, size_t len) } return newstr; } +#ifndef _MSC_VER #pragma GCC diagnostic pop #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; -} +#endif static const char allowed_unquoted_chars[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -139,7 +122,7 @@ static int str_needs_quotes(const char* str, size_t len) return 0; } -static plist_err_t node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t depth, int prettify) +static plist_err_t node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t depth, int prettify, int coerce) { plist_data_t node_data = NULL; @@ -157,6 +140,9 @@ static plist_err_t node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t { case PLIST_INT: val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } if (node_data->length == 16) { val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); } else { @@ -168,6 +154,9 @@ static plist_err_t node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t case PLIST_REAL: val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } val_len = dtostr(val, 64, node_data->realval); str_buf_append(*outbuf, val, val_len); free(val); @@ -230,7 +219,7 @@ static plist_err_t node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t str_buf_append(*outbuf, " ", 2); } } - plist_err_t res = node_to_openstep(ch, outbuf, depth+1, prettify); + plist_err_t res = node_to_openstep(ch, outbuf, depth+1, prettify, coerce); if (res < 0) { return res; } @@ -258,7 +247,7 @@ static plist_err_t node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t str_buf_append(*outbuf, " ", 2); } } - plist_err_t res = node_to_openstep(ch, outbuf, depth+1, prettify); + plist_err_t res = node_to_openstep(ch, outbuf, depth+1, prettify, coerce); if (res < 0) { return res; } @@ -297,19 +286,72 @@ static plist_err_t node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t str_buf_append(*outbuf, ">", 1); } break; case PLIST_BOOLEAN: - PLIST_OSTEP_WRITE_ERR("PLIST_BOOLEAN type is not valid for OpenStep format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + if (node_data->boolval) { + str_buf_append(*outbuf, "1", 1); + } else { + str_buf_append(*outbuf, "0", 1); + } + } else { + PLIST_OSTEP_WRITE_ERR("PLIST_BOOLEAN type is not valid for OpenStep format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_NULL: - PLIST_OSTEP_WRITE_ERR("PLIST_NULL type is not valid for OpenStep format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + str_buf_append(*outbuf, "NULL", 4); + } else { + PLIST_OSTEP_WRITE_ERR("PLIST_NULL type is not valid for OpenStep format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_DATE: - // NOT VALID FOR OPENSTEP - PLIST_OSTEP_WRITE_ERR("PLIST_DATE type is not valid for OpenStep format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + Time64_T timev; + if (plist_real_to_time64(node_data->realval, &timev) < 0) { + PLIST_OSTEP_WRITE_ERR("Encountered invalid date value %f\n", node_data->realval); + return PLIST_ERR_INVALID_ARG; + } + struct TM _btime; + struct TM *btime = gmtime64_r(&timev, &_btime); + char datebuf[32]; + size_t datelen = 0; + if (btime) { + struct tm _tmcopy; + copy_TM64_to_tm(btime, &_tmcopy); + datelen = strftime(datebuf, sizeof(datebuf), "%Y-%m-%dT%H:%M:%SZ", &_tmcopy); + } + if (datelen <= 0) { + datelen = snprintf(datebuf, sizeof(datebuf), "1970-01-01T00:00:00Z"); + } + str_buf_append(*outbuf, "\"", 1); + str_buf_append(*outbuf, datebuf, datelen); + str_buf_append(*outbuf, "\"", 1); + } else { + // NOT VALID FOR OPENSTEP + PLIST_OSTEP_WRITE_ERR("PLIST_DATE type is not valid for OpenStep format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_UID: - // NOT VALID FOR OPENSTEP - PLIST_OSTEP_WRITE_ERR("PLIST_UID type is not valid for OpenStep format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } + if (node_data->length == 16) { + val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + } else { + val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + } + str_buf_append(*outbuf, val, val_len); + free(val); + } else { + // NOT VALID FOR OPENSTEP + PLIST_OSTEP_WRITE_ERR("PLIST_UID type is not valid for OpenStep format\n"); + return PLIST_ERR_FORMAT; + } + break; default: return PLIST_ERR_UNKNOWN; } @@ -317,56 +359,34 @@ static plist_err_t node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t 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) +static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify, int coerce, hashtable_t *visited) { - 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; + plist_data_t data; + if (!node) { + return PLIST_ERR_INVALID_ARG; } - return n; -} -#define PO10u_LIMIT (UINT64_MAX/10) + if (depth > PLIST_MAX_NESTING_DEPTH) { + PLIST_OSTEP_WRITE_ERR("node tree is nested too deeply\n"); + return PLIST_ERR_MAX_NESTING; + } -/* 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; + if (hash_table_lookup(visited, node)) { + PLIST_OSTEP_WRITE_ERR("circular reference detected\n"); + return PLIST_ERR_CIRCULAR_REF; } - return n; -} -static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify) -{ - plist_data_t data; - if (!node) { - return PLIST_ERR_INVALID_ARG; + // mark as visited + if (hash_table_insert(visited, node, (void*)1) < 0) { + return PLIST_ERR_NO_MEM; } + data = plist_get_data(node); if (node->children) { node_t ch; unsigned int n_children = node_n_children(node); for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - plist_err_t res = node_estimate_size(ch, size, depth + 1, prettify); + plist_err_t res = _node_estimate_size(ch, size, depth + 1, prettify, coerce, visited); if (res < 0) { return res; } @@ -423,17 +443,46 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept *size += data->length/4; break; case PLIST_BOOLEAN: - // NOT VALID FOR OPENSTEP - PLIST_OSTEP_WRITE_ERR("PLIST_BOOLEAN type is not valid for OpenStep format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + *size += 1; + } else { + // NOT VALID FOR OPENSTEP + PLIST_OSTEP_WRITE_ERR("PLIST_BOOLEAN type is not valid for OpenStep format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_DATE: - // NOT VALID FOR OPENSTEP - PLIST_OSTEP_WRITE_ERR("PLIST_DATE type is not valid for OpenStep format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + // ISO 8601 string: "YYYY-MM-DDTHH:MM:SSZ" = 22 chars max + *size += 24; + } else { + // NOT VALID FOR OPENSTEP + PLIST_OSTEP_WRITE_ERR("PLIST_DATE type is not valid for OpenStep format\n"); + return PLIST_ERR_FORMAT; + } + break; case PLIST_UID: - // NOT VALID FOR OPENSTEP - PLIST_OSTEP_WRITE_ERR("PLIST_UID type is not valid for OpenStep format\n"); - return PLIST_ERR_FORMAT; + if (coerce) { + if (data->length == 16) { + *size += num_digits_u(data->intval); + } else { + *size += num_digits_i((int64_t)data->intval); + } + } else { + // NOT VALID FOR OPENSTEP + PLIST_OSTEP_WRITE_ERR("PLIST_UID type is not valid for OpenStep format\n"); + return PLIST_ERR_FORMAT; + } + break; + case PLIST_NULL: + if (coerce) { + *size += 4; + } else { + // NOT VALID FOR OPENSTEP + PLIST_OSTEP_WRITE_ERR("PLIST_NULL type is not valid for OpenStep format\n"); + return PLIST_ERR_FORMAT; + } + break; default: PLIST_OSTEP_WRITE_ERR("invalid node type encountered\n"); return PLIST_ERR_UNKNOWN; @@ -442,8 +491,23 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept return PLIST_ERR_SUCCESS; } +static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify, int coerce) +{ + hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); + if (!visited) return PLIST_ERR_NO_MEM; + plist_err_t err = _node_estimate_size(node, size, depth, prettify, coerce, visited); + hash_table_destroy(visited); + return err; +} + plist_err_t plist_to_openstep(plist_t plist, char **openstep, uint32_t* length, int prettify) { + plist_write_options_t opts = prettify ? PLIST_OPT_NONE : PLIST_OPT_COMPACT; + return plist_to_openstep_with_options(plist, openstep, length, opts); +} + +plist_err_t plist_to_openstep_with_options(plist_t plist, char **openstep, uint32_t* length, plist_write_options_t options) +{ uint64_t size = 0; plist_err_t res; @@ -451,7 +515,10 @@ plist_err_t plist_to_openstep(plist_t plist, char **openstep, uint32_t* length, return PLIST_ERR_INVALID_ARG; } - res = node_estimate_size((node_t)plist, &size, 0, prettify); + int prettify = !(options & PLIST_OPT_COMPACT); + int coerce = options & PLIST_OPT_COERCE; + + res = node_estimate_size((node_t)plist, &size, 0, prettify, coerce); if (res < 0) { return res; } @@ -462,7 +529,7 @@ plist_err_t plist_to_openstep(plist_t plist, char **openstep, uint32_t* length, return PLIST_ERR_NO_MEM; } - res = node_to_openstep((node_t)plist, &outbuf, 0, prettify); + res = node_to_openstep((node_t)plist, &outbuf, 0, prettify, coerce); if (res < 0) { str_buf_free(outbuf); *openstep = NULL; @@ -475,6 +542,13 @@ plist_err_t plist_to_openstep(plist_t plist, char **openstep, uint32_t* length, str_buf_append(outbuf, "\0", 1); + if (!outbuf->data) { + str_buf_free(outbuf); + *openstep = NULL; + *length = 0; + return PLIST_ERR_NO_MEM; + } + *openstep = (char*)outbuf->data; *length = outbuf->len - 1; @@ -488,7 +562,7 @@ struct _parse_ctx { const char *start; const char *pos; const char *end; - int err; + plist_err_t err; uint32_t depth; }; typedef struct _parse_ctx* parse_ctx; @@ -545,54 +619,57 @@ static void parse_dict_data(parse_ctx ctx, plist_t dict) } key = NULL; ctx->err = node_from_openstep(ctx, &key); - if (ctx->err != 0) { + if (ctx->err != PLIST_ERR_SUCCESS) { break; } if (!PLIST_IS_STRING(key)) { PLIST_OSTEP_ERR("Invalid type for dictionary key at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } parse_skip_ws(ctx); if (ctx->pos >= ctx->end) { PLIST_OSTEP_ERR("EOF while parsing dictionary '=' delimiter at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } if (*ctx->pos != '=') { PLIST_OSTEP_ERR("Missing '=' while parsing dictionary item at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } ctx->pos++; if (ctx->pos >= ctx->end) { PLIST_OSTEP_ERR("EOF while parsing dictionary item at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } val = NULL; ctx->err = node_from_openstep(ctx, &val); - if (ctx->err != 0) { + if (ctx->err != PLIST_ERR_SUCCESS) { break; } if (!val) { PLIST_OSTEP_ERR("Missing value for dictionary item at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } parse_skip_ws(ctx); if (ctx->pos >= ctx->end) { PLIST_OSTEP_ERR("EOF while parsing dictionary item terminator ';' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } if (*ctx->pos != ';') { PLIST_OSTEP_ERR("Missing terminating ';' while parsing dictionary item at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } - plist_dict_set_item(dict, plist_get_string_ptr(key, NULL), val); + if (plist_dict_set_item(dict, plist_get_string_ptr(key, NULL), val) != PLIST_ERR_SUCCESS) { + ctx->err = PLIST_ERR_NO_MEM; + break; + } plist_free(key); key = NULL; val = NULL; @@ -608,10 +685,10 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) plist_t subnode = NULL; const char *p = NULL; ctx->depth++; - if (ctx->depth > 1000) { + if (ctx->depth > PLIST_MAX_NESTING_DEPTH) { PLIST_OSTEP_ERR("Too many levels of recursion (%u) at offset %ld\n", ctx->depth, (long int)(ctx->pos - ctx->start)); - ctx->err++; - return PLIST_ERR_PARSE; + ctx->err = PLIST_ERR_MAX_NESTING; + return ctx->err; } while (ctx->pos < ctx->end && !ctx->err) { parse_skip_ws(ctx); @@ -619,9 +696,20 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) break; } plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } if (*ctx->pos == '{') { data->type = PLIST_DICT; subnode = plist_new_node(data); + if (!subnode) { + plist_free_data(data); + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } ctx->pos++; parse_dict_data(ctx, subnode); if (ctx->err) { @@ -629,12 +717,12 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) } if (ctx->pos >= ctx->end) { PLIST_OSTEP_ERR("EOF while parsing dictionary terminator '}' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } if (*ctx->pos != '}') { PLIST_OSTEP_ERR("Missing terminating '}' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } ctx->pos++; @@ -644,6 +732,12 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) } else if (*ctx->pos == '(') { data->type = PLIST_ARRAY; subnode = plist_new_node(data); + if (!subnode) { + plist_free_data(data); + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } ctx->pos++; plist_t tmp = NULL; while (ctx->pos < ctx->end && !ctx->err) { @@ -652,19 +746,22 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) break; } ctx->err = node_from_openstep(ctx, &tmp); - if (ctx->err != 0) { + if (ctx->err != PLIST_ERR_SUCCESS) { break; } if (!tmp) { - ctx->err++; + ctx->err = PLIST_ERR_PARSE; + break; + } + if (plist_array_append_item(subnode, tmp) != PLIST_ERR_SUCCESS) { + ctx->err = PLIST_ERR_NO_MEM; break; } - plist_array_append_item(subnode, tmp); tmp = NULL; parse_skip_ws(ctx); if (ctx->pos >= ctx->end) { PLIST_OSTEP_ERR("EOF while parsing array item delimiter ',' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } if (*ctx->pos != ',') { @@ -674,17 +771,17 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) } plist_free(tmp); tmp = NULL; - if (ctx->err) { + if (ctx->err != PLIST_ERR_SUCCESS) { goto err_out; } if (ctx->pos >= ctx->end) { PLIST_OSTEP_ERR("EOF while parsing array terminator ')' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } if (*ctx->pos != ')') { PLIST_OSTEP_ERR("Missing terminating ')' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } ctx->pos++; @@ -695,11 +792,17 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) data->type = PLIST_DATA; ctx->pos++; bytearray_t *bytes = byte_array_new(256); + if (!bytes) { + plist_free_data(data); + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } while (ctx->pos < ctx->end && !ctx->err) { parse_skip_ws(ctx); if (ctx->pos >= ctx->end) { PLIST_OSTEP_ERR("EOF while parsing data terminator '>' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } if (*ctx->pos == '>') { @@ -707,23 +810,28 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) } if (!isxdigit(*ctx->pos)) { PLIST_OSTEP_ERR("Invalid byte group in data at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } uint8_t b = HEX_DIGIT(*ctx->pos); ctx->pos++; if (ctx->pos >= ctx->end) { PLIST_OSTEP_ERR("Unexpected end of data at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } if (!isxdigit(*ctx->pos)) { PLIST_OSTEP_ERR("Invalid byte group in data at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } b = (b << 4) + HEX_DIGIT(*ctx->pos); byte_array_append(bytes, &b, 1); + if (!bytes->data) { + PLIST_OSTEP_ERR("Out of memory while parsing data at offset %ld\n", (long int)(ctx->pos - ctx->start)); + ctx->err = PLIST_ERR_NO_MEM; + break; + } ctx->pos++; } if (ctx->err) { @@ -735,14 +843,14 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) byte_array_free(bytes); plist_free_data(data); PLIST_OSTEP_ERR("EOF while parsing data terminator '>' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (*ctx->pos != '>') { byte_array_free(bytes); plist_free_data(data); PLIST_OSTEP_ERR("Missing terminating '>' at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } ctx->pos++; @@ -751,13 +859,19 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) bytes->data = NULL; byte_array_free(bytes); *plist = plist_new_node(data); + if (!*plist) { + plist_free_data(data); + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } parse_skip_ws(ctx); break; } else if (*ctx->pos == '"' || *ctx->pos == '\'') { char c = *ctx->pos; ctx->pos++; p = ctx->pos; - int num_escapes = 0; + size_t num_escapes = 0; while (ctx->pos < ctx->end) { if (*ctx->pos == '\\') { num_escapes++; @@ -770,18 +884,24 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) if (ctx->pos >= ctx->end) { plist_free_data(data); PLIST_OSTEP_ERR("EOF while parsing quoted string at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (*ctx->pos != c) { plist_free_data(data); PLIST_OSTEP_ERR("Missing closing quote (%c) at offset %ld\n", c, (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } size_t slen = ctx->pos - p; ctx->pos++; // skip the closing quote char* strbuf = (char*)malloc(slen+1); + if (!strbuf) { + plist_free_data(data); + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } if (num_escapes > 0) { size_t i = 0; size_t o = 0; @@ -853,6 +973,12 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) data->strval = strbuf; data->length = slen; *plist = plist_new_node(data); + if (!*plist) { + plist_free_data(data); + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } parse_skip_ws(ctx); break; } else { @@ -870,14 +996,26 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) if (slen > 0) { data->type = PLIST_STRING; data->strval = strndup(p, slen); + if (!data->strval) { + plist_free_data(data); + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } data->length = slen; *plist = plist_new_node(data); + if (!*plist) { + plist_free_data(data); + PLIST_OSTEP_ERR("Out of memory\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } parse_skip_ws(ctx); break; } else { plist_free_data(data); PLIST_OSTEP_ERR("Unexpected character when parsing unquoted string at offset %ld\n", (long int)(ctx->pos - ctx->start)); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; break; } } @@ -886,11 +1024,11 @@ static plist_err_t node_from_openstep(parse_ctx ctx, plist_t *plist) ctx->depth--; err_out: - if (ctx->err) { + if (ctx->err != PLIST_ERR_SUCCESS) { plist_free(subnode); plist_free(*plist); *plist = NULL; - return PLIST_ERR_PARSE; + return ctx->err; } return PLIST_ERR_SUCCESS; } @@ -912,17 +1050,23 @@ plist_err_t plist_from_openstep(const char *plist_ostep, uint32_t length, plist_ if (!*plist) { /* whitespace only file is considered an empty dictionary */ *plist = plist_new_dict(); + if (!*plist) { + err = PLIST_ERR_NO_MEM; + } } else if (ctx.pos < ctx.end && *ctx.pos == '=') { /* attempt to parse this as 'strings' data */ plist_free(*plist); *plist = NULL; plist_t pl = plist_new_dict(); + if (!pl) { + return PLIST_ERR_NO_MEM; + } ctx.pos = plist_ostep; parse_dict_data(&ctx, pl); - if (ctx.err > 0) { + if (ctx.err != PLIST_ERR_SUCCESS) { plist_free(pl); PLIST_OSTEP_ERR("Failed to parse strings data\n"); - err = PLIST_ERR_PARSE; + err = ctx.err; } else { *plist = pl; } diff --git a/src/out-default.c b/src/out-default.c index 266070b..9fe8334 100644 --- a/src/out-default.c +++ b/src/out-default.c @@ -38,46 +38,27 @@ #include "plist.h" #include "strbuf.h" #include "time64.h" - -#define MAC_EPOCH 978307200 - -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; -} +#include "hashtable.h" +#include "common.h" static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t depth, uint32_t indent, int partial_data) { plist_data_t node_data = NULL; char *val = NULL; + int slen = 0; size_t val_len = 0; uint32_t i = 0; - if (!node) + if (!node || !outbuf || !*outbuf) { return PLIST_ERR_INVALID_ARG; + } node_data = plist_get_data(node); + if (!node_data) { + return PLIST_ERR_INVALID_ARG; + } switch (node_data->type) { @@ -97,17 +78,24 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de case PLIST_INT: val = (char*)malloc(64); + if (!val) return PLIST_ERR_NO_MEM; if (node_data->length == 16) { - val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + slen = snprintf(val, 64, "%" PRIu64, node_data->intval); } else { - val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + slen = snprintf(val, 64, "%" PRIi64, node_data->intval); + } + if (slen < 0) { + free(val); + return PLIST_ERR_UNKNOWN; } + val_len = (size_t)slen; str_buf_append(*outbuf, val, val_len); free(val); break; case PLIST_REAL: val = (char*)malloc(64); + if (!val) return PLIST_ERR_NO_MEM; val_len = dtostr(val, 64, node_data->realval); str_buf_append(*outbuf, val, val_len); free(val); @@ -115,6 +103,9 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de case PLIST_STRING: case PLIST_KEY: { + if (!node_data->strval && node_data->length > 0) { + return PLIST_ERR_INVALID_ARG; + } const char *charmap[32] = { "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007", "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r", "\\u000e", "\\u000f", @@ -123,8 +114,8 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de }; size_t j = 0; size_t len = 0; - off_t start = 0; - off_t cur = 0; + size_t start = 0; + size_t cur = 0; str_buf_append(*outbuf, "\"", 1); @@ -206,28 +197,32 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de } break; case PLIST_DATA: { + if (!node_data->buff && node_data->length > 0) { + return PLIST_ERR_INVALID_ARG; + } str_buf_append(*outbuf, "<", 1); size_t len = node_data->length; char charb[4]; + size_t j; if (!partial_data || len <= 24) { - for (i = 0; i < len; i++) { - if (i > 0 && (i % 4 == 0)) + for (j = 0; j < len; j++) { + if (j > 0 && (j % 4 == 0)) str_buf_append(*outbuf, " ", 1); - sprintf(charb, "%02x", (unsigned char)node_data->buff[i]); + snprintf(charb, sizeof(charb), "%02x", (unsigned char)node_data->buff[j]); str_buf_append(*outbuf, charb, 2); } } else { - for (i = 0; i < 16; i++) { - if (i > 0 && (i % 4 == 0)) + for (j = 0; j < 16; j++) { + if (j > 0 && (j % 4 == 0)) str_buf_append(*outbuf, " ", 1); - sprintf(charb, "%02x", (unsigned char)node_data->buff[i]); + snprintf(charb, sizeof(charb), "%02x", (unsigned char)node_data->buff[j]); str_buf_append(*outbuf, charb, 2); } str_buf_append(*outbuf, " ... ", 5); - for (i = len - 8; i < len; i++) { - sprintf(charb, "%02x", (unsigned char)node_data->buff[i]); + for (j = len - 8; j < len; j++) { + snprintf(charb, sizeof(charb), "%02x", (unsigned char)node_data->buff[j]); str_buf_append(*outbuf, charb, 2); - if (i > 0 && i < len-1 && (i % 4 == 0)) + if (j > 0 && j < len-1 && (j % 4 == 0)) str_buf_append(*outbuf, " ", 1); } } @@ -236,11 +231,18 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de break; case PLIST_DATE: { - Time64_T timev = (Time64_T)node_data->realval + MAC_EPOCH; + Time64_T timev; + if (plist_real_to_time64(node_data->realval, &timev) < 0) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: Encountered invalid date value %f\n", node_data->realval); +#endif + return PLIST_ERR_INVALID_ARG; + } struct TM _btime; struct TM *btime = gmtime64_r(&timev, &_btime); if (btime) { val = (char*)calloc(1, 26); + if (!val) return PLIST_ERR_NO_MEM; struct tm _tmcopy; copy_TM64_to_tm(btime, &_tmcopy); val_len = strftime(val, 26, "%Y-%m-%d %H:%M:%S +0000", &_tmcopy); @@ -256,11 +258,17 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de { str_buf_append(*outbuf, "CF$UID:", 7); val = (char*)malloc(64); + if (!val) return PLIST_ERR_NO_MEM; if (node_data->length == 16) { - val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + slen = snprintf(val, 64, "%" PRIu64, node_data->intval); } else { - val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + slen = snprintf(val, 64, "%" PRIi64, node_data->intval); } + if (slen < 0) { + free(val); + return PLIST_ERR_UNKNOWN; + } + val_len = (size_t)slen; str_buf_append(*outbuf, val, val_len); free(val); } @@ -272,57 +280,39 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de 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) +static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, uint32_t indent, int partial_data, hashtable_t *visited) { - 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; + plist_data_t data; + if (!node) { + return PLIST_ERR_INVALID_ARG; } - return n; -} -#define PO10u_LIMIT (UINT64_MAX/10) + if (depth > PLIST_MAX_NESTING_DEPTH) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: maximum nesting depth (%u) exceeded\n", (unsigned)PLIST_MAX_NESTING_DEPTH); +#endif + return PLIST_ERR_MAX_NESTING; + } -/* 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; + if (hash_table_lookup(visited, node)) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: circular reference detected\n"); +#endif + return PLIST_ERR_CIRCULAR_REF; } - return n; -} -static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, uint32_t indent, int partial_data) -{ - plist_data_t data; - if (!node) { - return PLIST_ERR_INVALID_ARG; + // mark as visited + if (hash_table_insert(visited, node, (void*)1) < 0) { + return PLIST_ERR_NO_MEM; } + data = plist_get_data(node); if (node->children) { node_t ch; unsigned int n_children = node_n_children(node); for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - plist_err_t res = node_estimate_size(ch, size, depth + 1, indent, partial_data); - if (res < 0) { + plist_err_t res = _node_estimate_size(ch, size, depth + 1, indent, partial_data, visited); + if (res != PLIST_ERR_SUCCESS) { return res; } } @@ -401,6 +391,15 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept return PLIST_ERR_SUCCESS; } +static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, uint32_t indent, int partial_data) +{ + hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); + if (!visited) return PLIST_ERR_NO_MEM; + plist_err_t err = _node_estimate_size(node, size, depth, indent, partial_data, visited); + hash_table_destroy(visited); + return err; +} + static plist_err_t _plist_write_to_strbuf(plist_t plist, strbuf_t *outbuf, plist_write_options_t options) { uint8_t indent = 0; @@ -457,6 +456,13 @@ plist_err_t plist_write_to_string_default(plist_t plist, char **output, uint32_t } str_buf_append(outbuf, "\0", 1); + if (!outbuf->data) { + str_buf_free(outbuf); + *output = NULL; + *length = 0; + return PLIST_ERR_NO_MEM; + } + *output = (char*)outbuf->data; *length = outbuf->len - 1; diff --git a/src/out-limd.c b/src/out-limd.c index 7d861f8..dd4c252 100644 --- a/src/out-limd.c +++ b/src/out-limd.c @@ -40,47 +40,28 @@ #include "strbuf.h" #include "time64.h" #include "base64.h" - -#define MAC_EPOCH 978307200 - -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; -} +#include "hashtable.h" +#include "common.h" static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t depth, uint32_t indent) { plist_data_t node_data = NULL; char *val = NULL; + int slen = 0; size_t val_len = 0; char buf[16]; uint32_t i = 0; - if (!node) + if (!node || !outbuf || !*outbuf) { return PLIST_ERR_INVALID_ARG; + } node_data = plist_get_data(node); + if (!node_data) { + return PLIST_ERR_INVALID_ARG; + } switch (node_data->type) { @@ -91,26 +72,37 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de } else { str_buf_append(*outbuf, "false", 5); } + break; } - break; case PLIST_NULL: str_buf_append(*outbuf, "null", 4); - break; + break; case PLIST_INT: val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } if (node_data->length == 16) { - val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + slen = snprintf(val, 64, "%" PRIu64, node_data->intval); } else { - val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + slen = snprintf(val, 64, "%" PRIi64, node_data->intval); + } + if (slen < 0) { + free(val); + return PLIST_ERR_UNKNOWN; } + val_len = (size_t)slen; str_buf_append(*outbuf, val, val_len); free(val); break; case PLIST_REAL: val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } val_len = dtostr(val, 64, node_data->realval); str_buf_append(*outbuf, val, val_len); free(val); @@ -118,18 +110,21 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de case PLIST_STRING: case PLIST_KEY: { + if (!node_data->strval && node_data->length > 0) { + return PLIST_ERR_INVALID_ARG; + } const char *charmap[32] = { "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007", "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r", "\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", "\\u0013", "\\u0014", "\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019", "\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f", }; + size_t j = 0; - size_t len = 0; - off_t start = 0; - off_t cur = 0; + size_t len = node_data->length; + size_t start = 0; + size_t cur = 0; - len = node_data->length; for (j = 0; j < len; j++) { unsigned char ch = (unsigned char)node_data->strval[j]; if (ch < 0x20) { @@ -137,11 +132,12 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de str_buf_append(*outbuf, charmap[ch], (charmap[ch][1] == 'u') ? 6 : 2); start = cur+1; } + cur++; } str_buf_append(*outbuf, node_data->strval + start, cur - start); - } break; - + break; + } case PLIST_ARRAY: { node_t ch; uint32_t cnt = 0; @@ -152,15 +148,19 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de str_buf_append(*outbuf, " ", 1); } } - size_t sl = sprintf(buf, "%u: ", cnt); - str_buf_append(*outbuf, buf, sl); + slen = snprintf(buf, sizeof(buf), "%u: ", cnt); + if (slen < 0) { + return PLIST_ERR_UNKNOWN; + } + str_buf_append(*outbuf, buf, (size_t)slen); plist_err_t res = node_to_string(ch, outbuf, depth+1, indent); if (res < 0) { return res; } cnt++; } - } break; + break; + } case PLIST_DICT: { node_t ch; uint32_t cnt = 0; @@ -178,37 +178,60 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de if (cnt % 2 == 0) { plist_t valnode = (plist_t)node_next_sibling(ch); if (PLIST_IS_ARRAY(valnode)) { - size_t sl = sprintf(buf, "[%u]:", plist_array_get_size(valnode)); - str_buf_append(*outbuf, buf, sl); + slen = snprintf(buf, sizeof(buf), "[%u]:", plist_array_get_size(valnode)); + if (slen < 0) { + return PLIST_ERR_UNKNOWN; + } + str_buf_append(*outbuf, buf, (size_t)slen); } else { str_buf_append(*outbuf, ": ", 2); } } cnt++; } - } break; + break; + } case PLIST_DATA: { - val = (char*)malloc(4096); + if (!node_data->buff && node_data->length > 0) { + return PLIST_ERR_INVALID_ARG; + } + if (node_data->length == 0) { + break; + } +#define BASE64_CHUNK_SIZE 3072 +#define BASE64_BUF_SIZE (4 * ((BASE64_CHUNK_SIZE + 2) / 3) + 4) + val = (char*)malloc(BASE64_BUF_SIZE); + if (!val) return PLIST_ERR_NO_MEM; size_t done = 0; while (done < node_data->length) { size_t amount = node_data->length - done; - if (amount > 3072) { - amount = 3072; + if (amount > BASE64_CHUNK_SIZE) { + amount = BASE64_CHUNK_SIZE; } size_t bsize = base64encode(val, node_data->buff + done, amount); str_buf_append(*outbuf, val, bsize); done += amount; } + free(val); +#undef BASE64_CHUNK_SIZE +#undef BASE64_BUF_SIZE } break; case PLIST_DATE: { - Time64_T timev = (Time64_T)node_data->realval + MAC_EPOCH; + Time64_T timev; + if (plist_real_to_time64(node_data->realval, &timev) < 0) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: Encountered invalid date value %f\n", node_data->realval); +#endif + return PLIST_ERR_INVALID_ARG; + } struct TM _btime; struct TM *btime = gmtime64_r(&timev, &_btime); if (btime) { val = (char*)calloc(1, 24); + if (!val) return PLIST_ERR_NO_MEM; struct tm _tmcopy; copy_TM64_to_tm(btime, &_tmcopy); val_len = strftime(val, 24, "%Y-%m-%dT%H:%M:%SZ", &_tmcopy); @@ -216,19 +239,27 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de str_buf_append(*outbuf, val, val_len); } free(val); - val = NULL; } } break; + case PLIST_UID: { str_buf_append(*outbuf, "CF$UID:", 7); val = (char*)malloc(64); + if (!val) { + return PLIST_ERR_NO_MEM; + } if (node_data->length == 16) { - val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + slen = snprintf(val, 64, "%" PRIu64, node_data->intval); } else { - val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + slen = snprintf(val, 64, "%" PRIi64, node_data->intval); + } + if (slen < 0) { + free(val); + return PLIST_ERR_UNKNOWN; } + val_len = (size_t)slen; str_buf_append(*outbuf, val, val_len); free(val); } @@ -240,57 +271,39 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de 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) +static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, uint32_t indent, hashtable_t *visited) { - 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; + plist_data_t data; + if (!node) { + return PLIST_ERR_INVALID_ARG; } - return n; -} -#define PO10u_LIMIT (UINT64_MAX/10) + if (depth > PLIST_MAX_NESTING_DEPTH) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: maximum nesting depth (%u) exceeded\n", (unsigned)PLIST_MAX_NESTING_DEPTH); +#endif + return PLIST_ERR_MAX_NESTING; + } -/* 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; + if (hash_table_lookup(visited, node)) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: circular reference detected\n"); +#endif + return PLIST_ERR_CIRCULAR_REF; } - return n; -} -static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, uint32_t indent) -{ - plist_data_t data; - if (!node) { - return PLIST_ERR_INVALID_ARG; + // mark as visited + if (hash_table_insert(visited, node, (void*)1) < 0) { + return PLIST_ERR_NO_MEM; } + data = plist_get_data(node); if (node->children) { node_t ch; unsigned int n_children = node_n_children(node); for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - plist_err_t res = node_estimate_size(ch, size, depth + 1, indent); - if (res < 0) { + plist_err_t res = _node_estimate_size(ch, size, depth + 1, indent, visited); + if (res != PLIST_ERR_SUCCESS) { return res; } } @@ -359,6 +372,15 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept return PLIST_ERR_SUCCESS; } +static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, uint32_t indent) +{ + hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); + if (!visited) return PLIST_ERR_NO_MEM; + plist_err_t err = _node_estimate_size(node, size, depth, indent, visited); + hash_table_destroy(visited); + return err; +} + static plist_err_t _plist_write_to_strbuf(plist_t plist, strbuf_t *outbuf, plist_write_options_t options) { uint8_t indent = 0; @@ -415,6 +437,13 @@ plist_err_t plist_write_to_string_limd(plist_t plist, char **output, uint32_t* l } str_buf_append(outbuf, "\0", 1); + if (!outbuf->data) { + str_buf_free(outbuf); + *output = NULL; + *length = 0; + return PLIST_ERR_NO_MEM; + } + *output = (char*)outbuf->data; *length = outbuf->len - 1; diff --git a/src/out-plutil.c b/src/out-plutil.c index d85f22c..52edaaf 100644 --- a/src/out-plutil.c +++ b/src/out-plutil.c @@ -38,46 +38,25 @@ #include "plist.h" #include "strbuf.h" #include "time64.h" - -#define MAC_EPOCH 978307200 - -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; -} +#include "hashtable.h" +#include "common.h" static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t depth) { plist_data_t node_data = NULL; char *val = NULL; + int slen = 0; size_t val_len = 0; - uint32_t i = 0; - - if (!node) + if (!node || !outbuf || !*outbuf) { return PLIST_ERR_INVALID_ARG; + } node_data = plist_get_data(node); + if (!node_data) { + return PLIST_ERR_INVALID_ARG; + } switch (node_data->type) { @@ -97,17 +76,24 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de case PLIST_INT: val = (char*)malloc(64); + if (!val) return PLIST_ERR_NO_MEM; if (node_data->length == 16) { - val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + slen = snprintf(val, 64, "%" PRIu64, node_data->intval); } else { - val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + slen = snprintf(val, 64, "%" PRIi64, node_data->intval); + } + if (slen < 0) { + free(val); + return PLIST_ERR_UNKNOWN; } + val_len = (size_t)slen; str_buf_append(*outbuf, val, val_len); free(val); break; case PLIST_REAL: val = (char*)malloc(64); + if (!val) return PLIST_ERR_NO_MEM; val_len = dtostr(val, 64, node_data->realval); str_buf_append(*outbuf, val, val_len); free(val); @@ -115,6 +101,9 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de case PLIST_STRING: case PLIST_KEY: { + if (!node_data->strval && node_data->length > 0) { + return PLIST_ERR_INVALID_ARG; + } const char *charmap[32] = { "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007", "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r", "\\u000e", "\\u000f", @@ -123,8 +112,8 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de }; size_t j = 0; size_t len = 0; - off_t start = 0; - off_t cur = 0; + size_t start = 0; + size_t cur = 0; str_buf_append(*outbuf, "\"", 1); @@ -151,14 +140,18 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de str_buf_append(*outbuf, "[", 1); node_t ch; uint32_t cnt = 0; + uint32_t i; for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { str_buf_append(*outbuf, "\n", 1); for (i = 0; i <= depth; i++) { str_buf_append(*outbuf, " ", 2); } char indexbuf[16]; - int l = sprintf(indexbuf, "%u => ", cnt); - str_buf_append(*outbuf, indexbuf, l); + slen = snprintf(indexbuf, sizeof(indexbuf), "%u => ", cnt); + if (slen < 0) { + return PLIST_ERR_UNKNOWN; + } + str_buf_append(*outbuf, indexbuf, (size_t)slen); plist_err_t res = node_to_string(ch, outbuf, depth+1); if (res < 0) { return res; @@ -177,6 +170,7 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de str_buf_append(*outbuf, "{", 1); node_t ch; uint32_t cnt = 0; + uint32_t i; for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { if (cnt % 2 == 0) { str_buf_append(*outbuf, "\n", 1); @@ -203,27 +197,36 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de } break; case PLIST_DATA: { + if (!node_data->buff && node_data->length > 0) { + return PLIST_ERR_INVALID_ARG; + } val = (char*)calloc(1, 48); + if (!val) return PLIST_ERR_NO_MEM; size_t len = node_data->length; - size_t slen = snprintf(val, 48, "{length = %" PRIu64 ", bytes = 0x", (uint64_t)len); - str_buf_append(*outbuf, val, slen); + slen = snprintf(val, 48, "{length = %" PRIu64 ", bytes = 0x", (uint64_t)len); + if (slen < 0) { + free(val); + return PLIST_ERR_UNKNOWN; + } + str_buf_append(*outbuf, val, (size_t)slen); + size_t j; if (len <= 24) { - for (i = 0; i < len; i++) { - sprintf(val, "%02x", (unsigned char)node_data->buff[i]); + for (j = 0; j < len; j++) { + snprintf(val, 4, "%02x", (unsigned char)node_data->buff[j]); str_buf_append(*outbuf, val, 2); } } else { - for (i = 0; i < 16; i++) { - if (i > 0 && (i % 4 == 0)) + for (j = 0; j < 16; j++) { + if (j > 0 && (j % 4 == 0)) str_buf_append(*outbuf, " ", 1); - sprintf(val, "%02x", (unsigned char)node_data->buff[i]); + snprintf(val, 4, "%02x", (unsigned char)node_data->buff[j]); str_buf_append(*outbuf, val, 2); } str_buf_append(*outbuf, " ... ", 5); - for (i = len - 8; i < len; i++) { - sprintf(val, "%02x", (unsigned char)node_data->buff[i]); + for (j = len - 8; j < len; j++) { + snprintf(val, 4, "%02x", (unsigned char)node_data->buff[j]); str_buf_append(*outbuf, val, 2); - if (i > 0 && (i % 4 == 0)) + if (j > 0 && (j % 4 == 0)) str_buf_append(*outbuf, " ", 1); } } @@ -234,11 +237,18 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de break; case PLIST_DATE: { - Time64_T timev = (Time64_T)node_data->realval + MAC_EPOCH; + Time64_T timev; + if (plist_real_to_time64(node_data->realval, &timev) < 0) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: Encountered invalid date value %f\n", node_data->realval); +#endif + return PLIST_ERR_INVALID_ARG; + } struct TM _btime; struct TM *btime = gmtime64_r(&timev, &_btime); if (btime) { val = (char*)calloc(1, 26); + if (!val) return PLIST_ERR_NO_MEM; struct tm _tmcopy; copy_TM64_to_tm(btime, &_tmcopy); val_len = strftime(val, 26, "%Y-%m-%d %H:%M:%S +0000", &_tmcopy); @@ -252,11 +262,24 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de break; case PLIST_UID: { - val = (char*)malloc(88); - val_len = sprintf(val, "<CFKeyedArchiverUID %p [%p]>{value = %" PRIu64 "}", node, node_data, node_data->intval); +#define UID_FMT "<CFKeyedArchiverUID %p [%p]>{value = %" PRIu64 "}" + slen = snprintf(NULL, 0, UID_FMT, node, node_data, node_data->intval); + if (slen < 0) { + return PLIST_ERR_UNKNOWN; + } + val_len = (size_t)slen; + val = (char*)malloc(val_len + 1); + if (!val) return PLIST_ERR_NO_MEM; + slen = snprintf(val, val_len+1, UID_FMT, node, node_data, node_data->intval); + if (slen < 0 || (size_t)slen > val_len) { + free(val); + return PLIST_ERR_UNKNOWN; + } + val_len = (size_t)slen; str_buf_append(*outbuf, val, val_len); free(val); val = NULL; +#undef UID_FMT } break; default: @@ -266,57 +289,39 @@ static plist_err_t node_to_string(node_t node, bytearray_t **outbuf, uint32_t de 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) +static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, hashtable_t *visited) { - 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; + plist_data_t data; + if (!node) { + return PLIST_ERR_INVALID_ARG; } - return n; -} -#define PO10u_LIMIT (UINT64_MAX/10) + if (depth > PLIST_MAX_NESTING_DEPTH) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: maximum nesting depth (%u) exceeded\n", (unsigned)PLIST_MAX_NESTING_DEPTH); +#endif + return PLIST_ERR_MAX_NESTING; + } -/* 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; + if (hash_table_lookup(visited, node)) { +#if DEBUG + fprintf(stderr, "libplist: ERROR: circular reference detected\n"); +#endif + return PLIST_ERR_CIRCULAR_REF; } - return n; -} -static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth) -{ - plist_data_t data; - if (!node) { - return PLIST_ERR_INVALID_ARG; + // mark as visited + if (hash_table_insert(visited, node, (void*)1) < 0) { + return PLIST_ERR_NO_MEM; } + data = plist_get_data(node); if (node->children) { node_t ch; unsigned int n_children = node_n_children(node); for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - plist_err_t res = node_estimate_size(ch, size, depth + 1); - if (res < 0) { + plist_err_t res = _node_estimate_size(ch, size, depth + 1, visited); + if (res != PLIST_ERR_SUCCESS) { return res; } } @@ -388,6 +393,15 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept return PLIST_ERR_SUCCESS; } +static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth) +{ + hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); + if (!visited) return PLIST_ERR_NO_MEM; + plist_err_t err = _node_estimate_size(node, size, depth, visited); + hash_table_destroy(visited); + return err; +} + static plist_err_t _plist_write_to_strbuf(plist_t plist, strbuf_t *outbuf, plist_write_options_t options) { plist_err_t res = node_to_string((node_t)plist, &outbuf, 0); @@ -431,6 +445,13 @@ plist_err_t plist_write_to_string_plutil(plist_t plist, char **output, uint32_t* } str_buf_append(outbuf, "\0", 1); + if (!outbuf->data) { + str_buf_free(outbuf); + *output = NULL; + *length = 0; + return PLIST_ERR_NO_MEM; + } + *output = (char*)outbuf->data; *length = outbuf->len - 1; diff --git a/src/plist.c b/src/plist.c index 2078520..55e3b41 100644 --- a/src/plist.c +++ b/src/plist.c @@ -35,11 +35,10 @@ #include <limits.h> #include <float.h> #include <ctype.h> +#include <inttypes.h> #ifdef WIN32 #include <windows.h> -#else -#include <pthread.h> #endif #include <node.h> @@ -47,10 +46,92 @@ #include <hashtable.h> #include <ptrarray.h> +#include "common.h" + #ifdef _MSC_VER typedef SSIZE_T ssize_t; #endif +#ifdef DEBUG +static int plist_debug = 0; +#define PLIST_ERR(...) if (plist_debug > 0) { fprintf(stderr, "libplist ERROR: " __VA_ARGS__); } +#else +#define PLIST_ERR(...) +#endif + +#ifndef bswap16 +#define bswap16(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) +#endif + +#ifndef bswap32 +#define bswap32(x) ((((x) & 0xFF000000) >> 24) \ + | (((x) & 0x00FF0000) >> 8) \ + | (((x) & 0x0000FF00) << 8) \ + | (((x) & 0x000000FF) << 24)) +#endif + +#ifndef bswap64 +#define bswap64(x) ((((x) & 0xFF00000000000000ull) >> 56) \ + | (((x) & 0x00FF000000000000ull) >> 40) \ + | (((x) & 0x0000FF0000000000ull) >> 24) \ + | (((x) & 0x000000FF00000000ull) >> 8) \ + | (((x) & 0x00000000FF000000ull) << 8) \ + | (((x) & 0x0000000000FF0000ull) << 24) \ + | (((x) & 0x000000000000FF00ull) << 40) \ + | (((x) & 0x00000000000000FFull) << 56)) +#endif + +#ifndef le16toh +#ifdef __BIG_ENDIAN__ +#define le16toh(x) bswap16(x) +#else +#define le16toh(x) (x) +#endif +#endif + +#ifndef le32toh +#ifdef __BIG_ENDIAN__ +#define le32toh(x) bswap32(x) +#else +#define le32toh(x) (x) +#endif +#endif + +#ifndef le64toh +#ifdef __BIG_ENDIAN__ +#define le64toh(x) bswap64(x) +#else +#define le64toh(x) (x) +#endif +#endif + +// Reference: https://stackoverflow.com/a/2390626/1806760 +// Initializer/finalizer sample for MSVC and GCC/Clang. +// 2010-2016 Joe Lowe. Released into the public domain. + +#ifdef __cplusplus + #define INITIALIZER(f) \ + static void f(void); \ + struct f##_t_ { f##_t_(void) { f(); } }; static f##_t_ f##_; \ + static void f(void) +#elif defined(_MSC_VER) + #pragma section(".CRT$XCU",read) + #define INITIALIZER2_(f,p) \ + static void f(void); \ + __declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \ + __pragma(comment(linker,"/include:" p #f "_")) \ + static void f(void) + #ifdef _WIN64 + #define INITIALIZER(f) INITIALIZER2_(f,"") + #else + #define INITIALIZER(f) INITIALIZER2_(f,"_") + #endif +#else + #define INITIALIZER(f) \ + static void f(void) __attribute__((__constructor__)); \ + static void f(void) +#endif + extern void plist_xml_init(void); extern void plist_xml_deinit(void); extern void plist_bin_init(void); @@ -60,14 +141,6 @@ extern void plist_json_deinit(void); extern void plist_ostep_init(void); extern void plist_ostep_deinit(void); -static void internal_plist_init(void) -{ - plist_bin_init(); - plist_xml_init(); - plist_json_init(); - plist_ostep_init(); -} - static void internal_plist_deinit(void) { plist_bin_deinit(); @@ -76,66 +149,14 @@ static void internal_plist_deinit(void) plist_ostep_deinit(); } -#ifdef WIN32 -typedef volatile struct { - LONG lock; - int state; -} thread_once_t; - -static thread_once_t init_once = {0, 0}; -static thread_once_t deinit_once = {0, 0}; - -static void thread_once(thread_once_t *once_control, void (*init_routine)(void)) -{ - while (InterlockedExchange(&(once_control->lock), 1) != 0) { - Sleep(1); - } - if (!once_control->state) { - once_control->state = 1; - init_routine(); - } - InterlockedExchange(&(once_control->lock), 0); -} -#else -static pthread_once_t init_once = PTHREAD_ONCE_INIT; -static pthread_once_t deinit_once = PTHREAD_ONCE_INIT; -#define thread_once pthread_once -#endif - -#ifndef HAVE_ATTRIBUTE_CONSTRUCTOR - #if defined(__llvm__) || defined(__GNUC__) - #define HAVE_ATTRIBUTE_CONSTRUCTOR - #endif -#endif - -#ifdef HAVE_ATTRIBUTE_CONSTRUCTOR -static void __attribute__((constructor)) libplist_initialize(void) -{ - thread_once(&init_once, internal_plist_init); -} - -static void __attribute__((destructor)) libplist_deinitialize(void) -{ - thread_once(&deinit_once, internal_plist_deinit); -} -#elif defined(WIN32) -BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) +INITIALIZER(internal_plist_init) { - switch (dwReason) { - case DLL_PROCESS_ATTACH: - thread_once(&init_once, internal_plist_init); - break; - case DLL_PROCESS_DETACH: - thread_once(&deinit_once, internal_plist_deinit); - break; - default: - break; - } - return 1; + plist_bin_init(); + plist_xml_init(); + plist_json_init(); + plist_ostep_init(); + atexit(internal_plist_deinit); } -#else -#warning No compiler support for constructor/destructor attributes, some features might not be available. -#endif #ifndef HAVE_MEMMEM // see https://sourceware.org/legacy-ml/libc-alpha/2007-12/msg00000.html @@ -189,7 +210,7 @@ void* memmem(const void* haystack, size_t haystack_len, const void* needle, size int plist_is_binary(const char *plist_data, uint32_t length) { - if (length < 8) { + if (plist_data == NULL || length < 8) { return 0; } @@ -328,6 +349,16 @@ plist_t plist_new_node(plist_data_t data) return (plist_t) node_create(NULL, data); } +/* like plist_new_node(), but takes care of data if the node can't be created */ +static plist_t plist_new_node_or_free(plist_data_t data) +{ + plist_t node = plist_new_node(data); + if (!node) { + plist_free_data(data); + } + return node; +} + plist_data_t plist_get_data(plist_t node) { if (!node) @@ -337,8 +368,7 @@ plist_data_t plist_get_data(plist_t node) plist_data_t plist_new_plist_data(void) { - plist_data_t data = (plist_data_t) calloc(sizeof(struct plist_data_s), 1); - return data; + return (plist_data_t) calloc(1, sizeof(struct plist_data_s)); } static unsigned int dict_key_hash(const void *data) @@ -366,156 +396,286 @@ static int dict_key_compare(const void* a, const void* b) return (strcmp(data_a->strval, data_b->strval) == 0) ? TRUE : FALSE; } -void plist_free_data(plist_data_t data) +static void _plist_free_data(plist_data_t data) { - if (data) - { - switch (data->type) - { + if (!data) return; + switch (data->type) { case PLIST_KEY: case PLIST_STRING: free(data->strval); + data->strval = NULL; break; case PLIST_DATA: free(data->buff); + data->buff = NULL; break; case PLIST_ARRAY: ptr_array_free((ptrarray_t*)data->hashtable); + data->hashtable = NULL; break; - case PLIST_DICT: - hash_table_destroy((hashtable_t*)data->hashtable); + case PLIST_DICT: { + hashtable_t *ht = (hashtable_t*)data->hashtable; + // PLIST_DICT hashtables must not own/free values; values are freed via node tree. + assert(!ht || ht->free_func == NULL); + if (ht) ht->free_func = NULL; + hash_table_destroy(ht); + data->hashtable = NULL; break; + } default: break; + } +} + +void plist_free_data(plist_data_t data) +{ + if (!data) return; + _plist_free_data(data); + free(data); +} + +static int plist_free_children(node_t root) +{ + if (!root) return NODE_ERR_INVALID_ARG; + + /* Walk down to the deepest first child, free it and step back up. + * This needs no extra memory, so freeing can't fail with out-of-memory + * (which would leak the whole tree). */ + node_t cur = root; + for (;;) { + node_t ch = node_first_child(cur); + if (ch) { + cur = ch; + continue; + } + if (cur == root) break; + + node_t parent = (node_t)cur->parent; + int di = node_detach(parent, cur); + if (di < 0) { + return di; } - free(data); + plist_data_t data = plist_get_data(cur); + plist_free_data(data); + cur->data = NULL; + node_destroy(cur); + cur = parent; } + + return NODE_ERR_SUCCESS; } -static int plist_free_node(node_t node) +static int plist_free_node(node_t root) { - plist_data_t data = NULL; - int node_index = node_detach(node->parent, node); - data = plist_get_data(node); - plist_free_data(data); - node->data = NULL; + if (!root) return NODE_ERR_INVALID_ARG; + + int root_index = -1; - node_t ch; - for (ch = node_first_child(node); ch; ) { - node_t next = node_next_sibling(ch); - plist_free_node(ch); - ch = next; + if (root->parent) { + root_index = node_detach(root->parent, root); + if (root_index < 0) { + return root_index; + } } - node_destroy(node); + int r = plist_free_children(root); + if (r < 0) { + // root is already detached; caller should treat as error. + return r; + } + + plist_data_t data = plist_get_data(root); + plist_free_data(data); + root->data = NULL; - return node_index; + node_destroy(root); + + return root_index; } plist_t plist_new_dict(void) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_DICT; - return plist_new_node(data); + return plist_new_node_or_free(data); } plist_t plist_new_array(void) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_ARRAY; - return plist_new_node(data); + return plist_new_node_or_free(data); } //These nodes should not be handled by users static plist_t plist_new_key(const char *val) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_KEY; data->strval = strdup(val); - data->length = strlen(val); - return plist_new_node(data); + if (!data->strval) { + plist_free_data(data); + PLIST_ERR("%s: strdup failed\n", __func__); + return NULL; + } else { + data->length = strlen(val); + } + return plist_new_node_or_free(data); } plist_t plist_new_string(const char *val) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_STRING; data->strval = strdup(val); - data->length = strlen(val); - return plist_new_node(data); + if (!data->strval) { + plist_free_data(data); + PLIST_ERR("%s: strdup failed\n", __func__); + return NULL; + } else { + data->length = strlen(val); + } + return plist_new_node_or_free(data); } plist_t plist_new_bool(uint8_t val) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_BOOLEAN; data->boolval = val; data->length = sizeof(uint8_t); - return plist_new_node(data); + return plist_new_node_or_free(data); } plist_t plist_new_uint(uint64_t val) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_INT; data->intval = val; data->length = (val > INT_MAX) ? sizeof(uint64_t)*2 : sizeof(uint64_t); - return plist_new_node(data); + return plist_new_node_or_free(data); } plist_t plist_new_int(int64_t val) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_INT; data->intval = val; data->length = sizeof(uint64_t); - return plist_new_node(data); + return plist_new_node_or_free(data); } plist_t plist_new_uid(uint64_t val) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_UID; data->intval = val; data->length = sizeof(uint64_t); - return plist_new_node(data); + return plist_new_node_or_free(data); } plist_t plist_new_real(double val) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_REAL; data->realval = val; data->length = sizeof(double); - return plist_new_node(data); + return plist_new_node_or_free(data); } plist_t plist_new_data(const char *val, uint64_t length) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_DATA; - data->buff = (uint8_t *) malloc(length); - memcpy(data->buff, val, length); + if (val && length) { + data->buff = (uint8_t *) malloc(length); + if (!data->buff) { + PLIST_ERR("%s: failed to allocate %" PRIu64 " bytes\n", __func__, length); + return NULL; + } + memcpy(data->buff, val, length); + } data->length = length; - return plist_new_node(data); + return plist_new_node_or_free(data); } plist_t plist_new_date(int32_t sec, int32_t usec) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_DATE; data->realval = (double)sec + (double)usec / 1000000; data->length = sizeof(double); - return plist_new_node(data); + return plist_new_node_or_free(data); +} + +plist_t plist_new_unix_date(int64_t sec) +{ + plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } + data->type = PLIST_DATE; + data->realval = (double)sec - MAC_EPOCH; + data->length = sizeof(double); + return plist_new_node_or_free(data); } plist_t plist_new_null(void) { plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_ERR("%s: failed to allocate plist data\n", __func__); + return NULL; + } data->type = PLIST_NULL; data->intval = 0; data->length = 0; - return plist_new_node(data); + return plist_new_node_or_free(data); } void plist_free(plist_t plist) @@ -534,72 +694,235 @@ void plist_mem_free(void* ptr) } } -static plist_t plist_copy_node(node_t node) +static int plist_copy_node_shallow(node_t node, plist_t *out_newnode, plist_data_t *out_newdata, plist_type *out_type) { - plist_type node_type = PLIST_NONE; - plist_t newnode = NULL; + if (!node || !out_newnode || !out_newdata || !out_type) return NODE_ERR_INVALID_ARG; + plist_data_t data = plist_get_data(node); - plist_data_t newdata = plist_new_plist_data(); + if (!data) return NODE_ERR_INVALID_ARG; - assert(data); // plist should always have data - assert(newdata); + plist_data_t newdata = plist_new_plist_data(); + if (!newdata) return NODE_ERR_NO_MEM; memcpy(newdata, data, sizeof(struct plist_data_s)); - node_type = plist_get_node_type(node); + plist_type node_type = plist_get_node_type(node); switch (node_type) { case PLIST_DATA: - newdata->buff = (uint8_t *) malloc(data->length); - memcpy(newdata->buff, data->buff, data->length); + if (data->buff) { + newdata->buff = (uint8_t*)malloc(data->length); + if (!newdata->buff) { + plist_free_data(newdata); + return NODE_ERR_NO_MEM; + } + memcpy(newdata->buff, data->buff, data->length); + } else { + newdata->buff = NULL; + newdata->length = 0; + } break; + case PLIST_KEY: case PLIST_STRING: - newdata->strval = strdup(data->strval); + if (data->strval) { + size_t n = strlen(data->strval); + newdata->strval = (char*)malloc(n+1); + if (!newdata->strval) { + plist_free_data(newdata); + return NODE_ERR_NO_MEM; + } + memcpy(newdata->strval, data->strval, n+1); + newdata->length = (uint64_t)n; + } else { + newdata->strval = NULL; + newdata->length = 0; + } break; + case PLIST_ARRAY: + newdata->hashtable = NULL; // don't share (or free) the original's table on error if (data->hashtable) { ptrarray_t* pa = ptr_array_new(((ptrarray_t*)data->hashtable)->capacity); - assert(pa); + if (!pa) { + plist_free_data(newdata); + return NODE_ERR_NO_MEM; + } newdata->hashtable = pa; } break; + case PLIST_DICT: + newdata->hashtable = NULL; // don't share (or free) the original's table on error if (data->hashtable) { hashtable_t* ht = hash_table_new(dict_key_hash, dict_key_compare, NULL); - assert(ht); + if (!ht) { + plist_free_data(newdata); + return NODE_ERR_NO_MEM; + } newdata->hashtable = ht; } break; + default: break; } - newnode = plist_new_node(newdata); - - node_t ch; - unsigned int node_index = 0; - for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - /* copy child node */ - plist_t newch = plist_copy_node(ch); - /* attach to new parent node */ - node_attach((node_t)newnode, (node_t)newch); - /* if needed, add child node to lookup table of parent node */ - switch (node_type) { + + plist_t newnode = plist_new_node(newdata); + if (!newnode) { + plist_free_data(newdata); + return NODE_ERR_NO_MEM; + } + + *out_newnode = newnode; + *out_newdata = newdata; + *out_type = node_type; + return NODE_ERR_SUCCESS; +} + +static plist_t plist_copy_node(node_t root) +{ + typedef struct copy_frame { + node_t orig; // original node + plist_t copy; // copied node + plist_data_t copydata; // copied node's data (for cache updates) + plist_type type; // copied node type + node_t next_child; // next child of orig to process + unsigned int node_index; // child index (for dict key/value odd/even) + int depth; // optional depth tracking + } copy_frame_t; + + if (!root) return NULL; + + // shallow-copy root first + plist_t newroot = NULL; + plist_data_t newroot_data = NULL; + plist_type newroot_type = PLIST_NONE; + + int r = plist_copy_node_shallow(root, &newroot, &newroot_data, &newroot_type); + if (r != NODE_ERR_SUCCESS) { + PLIST_ERR("%s: shallow node copy failed (%d)\n", __func__, r); + return NULL; + } + + // stack of frames + size_t cap = 64, sp = 0; + copy_frame_t *st = (copy_frame_t*)malloc(cap * sizeof(*st)); + if (!st) { + plist_free_node((node_t)newroot); + return NULL; + } + + copy_frame_t cf; + cf.orig = root; + cf.copy = newroot; + cf.copydata = newroot_data; + cf.type = newroot_type; + cf.next_child = node_first_child(root); + cf.node_index = 0; + cf.depth = 0; + st[sp++] = cf; + + while (sp) { + copy_frame_t *f = &st[sp - 1]; + + if (f->depth > NODE_MAX_DEPTH) { + plist_free_node((node_t)newroot); + free(st); + PLIST_ERR("%s: maximum nesting depth exceeded\n", __func__); + return NULL; + } + + // done with this node? + if (!f->next_child) { + sp--; + continue; + } + + // take next child and advance iterator + node_t ch = f->next_child; + f->next_child = node_next_sibling(ch); + + // shallow copy child + plist_t newch = NULL; + plist_data_t newch_data = NULL; + plist_type newch_type = PLIST_NONE; + + r = plist_copy_node_shallow(ch, &newch, &newch_data, &newch_type); + if (r != NODE_ERR_SUCCESS) { + plist_free_node((node_t)newroot); + free(st); + PLIST_ERR("%s: shallow node copy failed (%d)\n", __func__, r); + return NULL; + } + + // attach child to copied parent + r = node_attach((node_t)f->copy, (node_t)newch); + if (r != NODE_ERR_SUCCESS) { + plist_free_node((node_t)newch); + plist_free_node((node_t)newroot); + free(st); + PLIST_ERR("%s: failed to attach child to copied parent (%d)\n", __func__, r); + return NULL; + } + + // update lookup cache on the *parent* copy + switch (f->type) { case PLIST_ARRAY: - if (newdata->hashtable) { - ptr_array_add((ptrarray_t*)newdata->hashtable, newch); + if (f->copydata->hashtable) { + if (ptr_array_add((ptrarray_t*)f->copydata->hashtable, newch) < 0) { + ptr_array_free((ptrarray_t*)f->copydata->hashtable); + f->copydata->hashtable = NULL; + } } break; + case PLIST_DICT: - if (newdata->hashtable && (node_index % 2 != 0)) { - hash_table_insert((hashtable_t*)newdata->hashtable, (node_prev_sibling((node_t)newch))->data, newch); + if (f->copydata->hashtable && (f->node_index % 2 != 0)) { + node_t new_key = node_prev_sibling((node_t)newch); + if (new_key) { + if (hash_table_insert((hashtable_t*)f->copydata->hashtable, new_key->data, newch) < 0) { + /* drop the lookup cache; lookups fall back to a linear search */ + hash_table_destroy((hashtable_t*)f->copydata->hashtable); + f->copydata->hashtable = NULL; + } + } } break; + default: break; } - node_index++; + + f->node_index++; + + // push child frame to process its children + // (read depth before realloc, which may invalidate f) + int child_depth = f->depth + 1; + if (sp == cap) { + cap += 64; + copy_frame_t *tmp = (copy_frame_t*)realloc(st, cap * sizeof(*st)); + if (!tmp) { + plist_free_node((node_t)newroot); + free(st); + PLIST_ERR("%s: out of memory when reallocating\n", __func__); + return NULL; + } + st = tmp; + } + + copy_frame_t nf; + nf.orig = ch; + nf.copy = newch; + nf.copydata = newch_data; + nf.type = newch_type; + nf.next_child = node_first_child(ch); + nf.node_index = 0; + nf.depth = child_depth; + st[sp++] = nf; } - return newnode; + + free(st); + return newroot; } plist_t plist_copy(plist_t node) @@ -610,8 +933,7 @@ plist_t plist_copy(plist_t node) uint32_t plist_array_get_size(plist_t node) { uint32_t ret = 0; - if (node && PLIST_ARRAY == plist_get_node_type(node)) - { + if (PLIST_IS_ARRAY(node)) { ret = node_n_children((node_t)node); } return ret; @@ -620,8 +942,7 @@ uint32_t plist_array_get_size(plist_t node) plist_t plist_array_get_item(plist_t node, uint32_t n) { plist_t ret = NULL; - if (node && PLIST_ARRAY == plist_get_node_type(node) && n < INT_MAX) - { + if (PLIST_IS_ARRAY(node) && n < INT_MAX) { ptrarray_t *pa = (ptrarray_t*)((plist_data_t)((node_t)node)->data)->hashtable; if (pa) { ret = (plist_t)ptr_array_index(pa, n); @@ -634,10 +955,9 @@ plist_t plist_array_get_item(plist_t node, uint32_t n) uint32_t plist_array_get_item_index(plist_t node) { - plist_t father = plist_get_parent(node); - if (PLIST_ARRAY == plist_get_node_type(father)) - { - return node_child_position((node_t)father, (node_t)node); + plist_t parent = plist_get_parent(node); + if (PLIST_IS_ARRAY(parent)) { + return node_child_position((node_t)parent, (node_t)node); } return UINT_MAX; } @@ -647,182 +967,289 @@ static void _plist_array_post_insert(plist_t node, plist_t item, long n) ptrarray_t *pa = (ptrarray_t*)((plist_data_t)((node_t)node)->data)->hashtable; if (pa) { /* store pointer to item in array */ - ptr_array_insert(pa, item, n); - } else { - if (((node_t)node)->count > 100) { - /* make new lookup array */ - pa = ptr_array_new(128); - plist_t current = NULL; - for (current = (plist_t)node_first_child((node_t)node); - pa && current; - current = (plist_t)node_next_sibling((node_t)current)) - { - ptr_array_add(pa, current); - } - ((plist_data_t)((node_t)node)->data)->hashtable = pa; + if (ptr_array_insert(pa, item, n) < 0) { + /* lookup array would be out of sync, drop it */ + ptr_array_free(pa); + ((plist_data_t)((node_t)node)->data)->hashtable = NULL; } + return; + } + + if (((node_t)node)->count > 100) { + /* make new lookup array */ + pa = ptr_array_new(128); + plist_t current = NULL; + for (current = (plist_t)node_first_child((node_t)node); + pa && current; + current = (plist_t)node_next_sibling((node_t)current)) + { + if (ptr_array_add(pa, current) < 0) { + ptr_array_free(pa); + pa = NULL; + } + } + ((plist_data_t)((node_t)node)->data)->hashtable = pa; } } -void plist_array_set_item(plist_t node, plist_t item, uint32_t n) +static void _plist_array_post_set(plist_t node, plist_t item, long n) { - if (!item) { + ptrarray_t *pa = (ptrarray_t*)((plist_data_t)((node_t)node)->data)->hashtable; + + if (pa) { + if (n < 0 || n >= pa->len) { + PLIST_ERR("%s: cache index out of range (n=%ld len=%ld)\n", __func__, n, pa->len); + return; + } + ptr_array_set(pa, item, n); return; } - if (node && PLIST_ARRAY == plist_get_node_type(node) && n < INT_MAX) - { - plist_t old_item = plist_array_get_item(node, n); - if (old_item) + + if (((node_t)node)->count > 100) { + pa = ptr_array_new(128); + plist_t current = NULL; + for (current = (plist_t)node_first_child((node_t)node); + pa && current; + current = (plist_t)node_next_sibling((node_t)current)) { - int idx = plist_free_node((node_t)old_item); - assert(idx >= 0); - if (idx < 0) { - return; - } - node_insert((node_t)node, idx, (node_t)item); - ptrarray_t* pa = (ptrarray_t*)((plist_data_t)((node_t)node)->data)->hashtable; - if (pa) { - ptr_array_set(pa, item, idx); - } + ptr_array_add(pa, current); + } + ((plist_data_t)((node_t)node)->data)->hashtable = pa; + + // Now that it exists (and is filled), apply the set (will no-op if out of range) + if (pa) { + ptr_array_set(pa, item, n); } } } -void plist_array_append_item(plist_t node, plist_t item) +plist_err_t plist_array_set_item(plist_t node, plist_t item, uint32_t n) { - if (!item) { - return; + if (!PLIST_IS_ARRAY(node) || !item || n >= INT_MAX) { + PLIST_ERR("invalid argument passed to %s (node=%p, item=%p, n=%u)\n", __func__, node, item, n); + return PLIST_ERR_INVALID_ARG; } - if (node && PLIST_ARRAY == plist_get_node_type(node)) - { - node_attach((node_t)node, (node_t)item); - _plist_array_post_insert(node, item, -1); + node_t it = (node_t)item; + if (it->parent != NULL) { + assert(it->parent == NULL && "item already has a parent; use plist_copy() or detach first"); + PLIST_ERR("%s: item already has a parent; use plist_copy() or detach first\n", __func__); + return PLIST_ERR_INVALID_ARG; } + plist_t old_item = plist_array_get_item(node, n); + if (!old_item) return PLIST_ERR_INVALID_ARG; + + int idx = node_detach((node_t)node, (node_t)old_item); + if (idx < 0) { + PLIST_ERR("%s: Failed to detach old item (err=%d)\n", __func__, idx); + return PLIST_ERR_UNKNOWN; + } + + int r = node_insert((node_t)node, (unsigned)idx, (node_t)item); + if (r != NODE_ERR_SUCCESS) { + int rb = node_insert((node_t)node, (unsigned)idx, (node_t)old_item); + if (rb == NODE_ERR_SUCCESS) { + _plist_array_post_set(node, old_item, idx); // restore cache correctly + PLIST_ERR("%s: failed to insert replacement (idx=%d err=%d); rollback succeeded\n", __func__, idx, r); + return (r == NODE_ERR_NO_MEM) ? PLIST_ERR_NO_MEM : PLIST_ERR_UNKNOWN; + } else { + PLIST_ERR("%s: insert failed (err=%d) and rollback failed (err=%d); array now missing element at idx=%d\n", __func__, r, rb, idx); + return PLIST_ERR_UNKNOWN; + } + } + + _plist_array_post_set(node, item, idx); // update cache + plist_free_node((node_t)old_item); + + return PLIST_ERR_SUCCESS; } -void plist_array_insert_item(plist_t node, plist_t item, uint32_t n) +plist_err_t plist_array_append_item(plist_t node, plist_t item) { - if (!item) { - return; + if (!PLIST_IS_ARRAY(node) || !item) { + PLIST_ERR("invalid argument passed to %s (node=%p, item=%p)\n", __func__, node, item); + return PLIST_ERR_INVALID_ARG; } - if (node && PLIST_ARRAY == plist_get_node_type(node) && n < INT_MAX) - { - node_insert((node_t)node, n, (node_t)item); - _plist_array_post_insert(node, item, (long)n); + node_t it = (node_t)item; + if (it->parent != NULL) { + assert(it->parent == NULL && "item already has a parent; use plist_copy() or detach first"); + PLIST_ERR("%s: item already has a parent; use plist_copy() or detach first\n", __func__); + return PLIST_ERR_INVALID_ARG; } + + int r = node_attach((node_t)node, (node_t)item); + if (r != NODE_ERR_SUCCESS) { + PLIST_ERR("%s: failed to append item (err=%d)\n", __func__, r); + return PLIST_ERR_UNKNOWN; + } + _plist_array_post_insert(node, item, -1); + + return PLIST_ERR_SUCCESS; } -void plist_array_remove_item(plist_t node, uint32_t n) +plist_err_t plist_array_insert_item(plist_t node, plist_t item, uint32_t n) { - if (node && PLIST_ARRAY == plist_get_node_type(node) && n < INT_MAX) - { - plist_t old_item = plist_array_get_item(node, n); - if (old_item) - { - ptrarray_t* pa = (ptrarray_t*)((plist_data_t)((node_t)node)->data)->hashtable; - if (pa) { - ptr_array_remove(pa, n); - } - plist_free(old_item); - } + if (!PLIST_IS_ARRAY(node) || !item || n >= INT_MAX) { + PLIST_ERR("invalid argument passed to %s (node=%p, item=%p, n=%u)\n", __func__, node, item, n); + return PLIST_ERR_INVALID_ARG; + } + node_t it = (node_t)item; + if (it->parent != NULL) { + assert(it->parent == NULL && "item already has a parent; use plist_copy() or detach first"); + PLIST_ERR("%s: item already has a parent; use plist_copy() or detach first\n", __func__); + return PLIST_ERR_INVALID_ARG; + } + + int r = node_insert((node_t)node, n, (node_t)item); + if (r != NODE_ERR_SUCCESS) { + PLIST_ERR("%s: Failed to insert item at index %u (err=%d)\n", __func__, n, r); + return PLIST_ERR_UNKNOWN; } + _plist_array_post_insert(node, item, (long)n); + + return PLIST_ERR_SUCCESS; } -void plist_array_item_remove(plist_t node) +plist_err_t plist_array_remove_item(plist_t node, uint32_t n) { - plist_t father = plist_get_parent(node); - if (PLIST_ARRAY == plist_get_node_type(father)) - { - int n = node_child_position((node_t)father, (node_t)node); - if (n < 0) return; - ptrarray_t* pa = (ptrarray_t*)((plist_data_t)((node_t)father)->data)->hashtable; + if (!PLIST_IS_ARRAY(node) || n >= INT_MAX || n > plist_array_get_size(node)) { + PLIST_ERR("invalid argument passed to %s (node=%p, n=%u)\n", __func__, node, n); + return PLIST_ERR_INVALID_ARG; + } + + plist_t old_item = plist_array_get_item(node, n); + if (!old_item) { + PLIST_ERR("item not found at index %u\n", n); + return PLIST_ERR_INVALID_ARG; + } + ptrarray_t* pa = (ptrarray_t*)((plist_data_t)((node_t)node)->data)->hashtable; + if (pa) { + ptr_array_remove(pa, n); + } + plist_free(old_item); + + return PLIST_ERR_SUCCESS; +} + +plist_err_t plist_array_item_remove(plist_t item) +{ + plist_t parent = plist_get_parent(item); + if (PLIST_IS_ARRAY(parent)) { + int n = node_child_position((node_t)parent, (node_t)item); + if (n < 0) return PLIST_ERR_INVALID_ARG; + ptrarray_t* pa = (ptrarray_t*)((plist_data_t)((node_t)parent)->data)->hashtable; if (pa) { ptr_array_remove(pa, n); } - plist_free(node); + plist_free(item); } + return PLIST_ERR_SUCCESS; } +typedef struct { + node_t cur; +} plist_array_iter_private; + void plist_array_new_iter(plist_t node, plist_array_iter *iter) { - if (iter) - { - *iter = malloc(sizeof(node_t)); - *((node_t*)(*iter)) = node_first_child((node_t)node); - } + if (!iter) return; + *iter = NULL; + if (!PLIST_IS_ARRAY(node)) return; + + plist_array_iter_private* it = (plist_array_iter_private*)malloc(sizeof(*it)); + if (!it) return; + it->cur = node_first_child((node_t)node); + *iter = (plist_array_iter)it; } void plist_array_next_item(plist_t node, plist_array_iter iter, plist_t *item) { - node_t* iter_node = (node_t*)iter; + if (item) *item = NULL; + if (!iter) return; + if (!PLIST_IS_ARRAY(node)) return; - if (item) - { - *item = NULL; - } + plist_array_iter_private* it = (plist_array_iter_private*)iter; + node_t cur = it->cur; + if (!cur) return; - if (node && PLIST_ARRAY == plist_get_node_type(node) && *iter_node) - { - if (item) - { - *item = (plist_t)(*iter_node); - } - *iter_node = node_next_sibling(*iter_node); + if (item) { + *item = (plist_t)cur; } + it->cur = node_next_sibling(cur); +} + +void plist_array_free_iter(plist_array_iter iter) +{ + free(iter); } uint32_t plist_dict_get_size(plist_t node) { uint32_t ret = 0; - if (node && PLIST_DICT == plist_get_node_type(node)) - { + if (PLIST_IS_DICT(node)) { ret = node_n_children((node_t)node) / 2; } return ret; } +typedef struct { + node_t cur; +} plist_dict_iter_private; + void plist_dict_new_iter(plist_t node, plist_dict_iter *iter) { - if (iter) - { - *iter = malloc(sizeof(node_t)); - *((node_t*)(*iter)) = node_first_child((node_t)node); - } + if (!iter) return; + *iter = NULL; + if (!PLIST_IS_DICT(node)) return; + + plist_dict_iter_private* it = (plist_dict_iter_private*)malloc(sizeof(*it)); + if (!it) return; + it->cur = node_first_child((node_t)node); + *iter = (plist_dict_iter)it; } void plist_dict_next_item(plist_t node, plist_dict_iter iter, char **key, plist_t *val) { - node_t* iter_node = (node_t*)iter; + if (key) *key = NULL; + if (val) *val = NULL; + if (!iter) return; + if (!PLIST_IS_DICT(node)) return; - if (key) - { - *key = NULL; + plist_dict_iter_private* it = (plist_dict_iter_private*)iter; + + node_t k = it->cur; + if (!k) return; + + if (!PLIST_IS_KEY((plist_t)k)) { + // malformed dict, terminate iteration + it->cur = NULL; + return; } - if (val) - { - *val = NULL; + + node_t v = node_next_sibling(k); + if (!v) { + // key without value, terminate iteration + it->cur = NULL; + return; } - if (node && PLIST_DICT == plist_get_node_type(node) && *iter_node) - { - if (key) - { - plist_get_key_val((plist_t)(*iter_node), key); - } - *iter_node = node_next_sibling(*iter_node); - if (val) - { - *val = (plist_t)(*iter_node); - } - *iter_node = node_next_sibling(*iter_node); + if (key) { + plist_get_key_val((plist_t)k, key); + } + if (val) { + *val = (plist_t)v; } + it->cur = node_next_sibling(v); +} + +void plist_dict_free_iter(plist_dict_iter iter) +{ + free(iter); } void plist_dict_get_item_key(plist_t node, char **key) { - plist_t father = plist_get_parent(node); - if (PLIST_DICT == plist_get_node_type(father)) - { + plist_t parent = plist_get_parent(node); + if (PLIST_IS_DICT(parent)) { plist_get_key_val( (plist_t) node_prev_sibling((node_t)node), key); } } @@ -830,9 +1257,8 @@ void plist_dict_get_item_key(plist_t node, char **key) plist_t plist_dict_item_get_key(plist_t node) { plist_t ret = NULL; - plist_t father = plist_get_parent(node); - if (PLIST_DICT == plist_get_node_type(father)) - { + plist_t parent = plist_get_parent(node); + if (PLIST_IS_DICT(parent)) { ret = (plist_t)node_prev_sibling((node_t)node); } return ret; @@ -841,109 +1267,198 @@ plist_t plist_dict_item_get_key(plist_t node) plist_t plist_dict_get_item(plist_t node, const char* key) { plist_t ret = NULL; - - if (node && PLIST_DICT == plist_get_node_type(node)) - { - plist_data_t data = plist_get_data(node); - hashtable_t *ht = (hashtable_t*)data->hashtable; - if (ht) { - struct plist_data_s sdata; - sdata.strval = (char*)key; - sdata.length = strlen(key); - ret = (plist_t)hash_table_lookup(ht, &sdata); - } else { - plist_t current = NULL; - for (current = (plist_t)node_first_child((node_t)node); - current; - current = (plist_t)node_next_sibling(node_next_sibling((node_t)current))) - { - data = plist_get_data(current); - assert( PLIST_KEY == plist_get_node_type(current) ); - - if (data && !strcmp(key, data->strval)) - { - ret = (plist_t)node_next_sibling((node_t)current); - break; - } + if (!PLIST_IS_DICT(node) || !key) { + PLIST_ERR("invalid argument passed to %s (node=%p, key=%p)\n", __func__, node, key); + return NULL; + } + plist_data_t data = plist_get_data(node); + if (!data) { + PLIST_ERR("%s: invalid node\n", __func__); + return NULL; + } + size_t keylen = strlen(key); + hashtable_t *ht = (hashtable_t*)data->hashtable; + if (ht) { + struct plist_data_s sdata = { 0 }; + sdata.strval = (char*)key; + sdata.length = keylen; + return (plist_t)hash_table_lookup(ht, &sdata); + } else { + plist_t k = NULL; + for (k = (plist_t)node_first_child((node_t)node); k; ) { + plist_t v = (plist_t)node_next_sibling(k); + if (!v) break; + data = plist_get_data(k); + assert(PLIST_IS_KEY(k)); + if (!PLIST_IS_KEY(k) || !data || !data->strval) { + PLIST_ERR("invalid key node at %p\n", k); + break; } + if (data->length == keylen && !memcmp(key, data->strval, keylen+1)) { + ret = v; + break; + } + k = node_next_sibling(v); } } return ret; } -void plist_dict_set_item(plist_t node, const char* key, plist_t item) +/* Drop the key lookup cache of a dict; lookups then fall back to a linear search. */ +static void plist_dict_drop_cache(plist_t node) { - if (!item) { - return; + plist_data_t data = plist_get_data(node); + if (data && data->hashtable) { + hash_table_destroy((hashtable_t*)data->hashtable); + data->hashtable = NULL; + } +} + +plist_err_t plist_dict_set_item(plist_t node, const char* key, plist_t item) +{ + if (!PLIST_IS_DICT(node) || !key || !item) { + PLIST_ERR("invalid argument passed to %s (node=%p, key=%p, item=%p)\n", __func__, node, key, item); + return PLIST_ERR_INVALID_ARG; } - if (node && PLIST_DICT == plist_get_node_type(node)) { - plist_t old_item = plist_dict_get_item(node, key); - plist_t key_node = NULL; - if (old_item) { - int idx = plist_free_node((node_t)old_item); - assert(idx >= 0); - if (idx < 0) { - return; + node_t it = (node_t)item; + if (it->parent != NULL) { + assert(it->parent == NULL && "item already has a parent"); + PLIST_ERR("%s: item already has a parent\n", __func__); + return PLIST_ERR_INVALID_ARG; + } + + hashtable_t *ht = (hashtable_t*)((plist_data_t)((node_t)node)->data)->hashtable; + + plist_t old_item = plist_dict_get_item(node, key); + plist_t key_node = NULL; + + if (old_item) { + // --- REPLACE EXISTING VALUE --- + node_t old_val = (node_t)old_item; + node_t old_key = node_prev_sibling(old_val); + if (!old_key) { + PLIST_ERR("%s: corrupt dict (value without key)\n", __func__); + return PLIST_ERR_UNKNOWN; + } + if (!PLIST_IS_KEY((plist_t)old_key)) { + PLIST_ERR("%s: corrupt dict ('key' node is not PLIST_KEY\n", __func__); + return PLIST_ERR_UNKNOWN; + } + + // detach old value (do NOT free yet) + int idx = node_detach((node_t)node, old_val); + if (idx < 0) { + PLIST_ERR("%s: failed to detach old value (err=%d)\n", __func__, idx); + return PLIST_ERR_UNKNOWN; + } + + // insert new value at same position + int r = node_insert((node_t)node, (unsigned)idx, (node_t)item); + if (r != NODE_ERR_SUCCESS) { + // rollback: reinsert old value + int rb = node_insert((node_t)node, (unsigned)idx, old_val); + if (rb == NODE_ERR_SUCCESS && ht) { + if (hash_table_insert(ht, ((node_t)old_key)->data, old_item) < 0) { + plist_dict_drop_cache(node); + ht = NULL; + } } - node_insert((node_t)node, idx, (node_t)item); - key_node = node_prev_sibling((node_t)item); - } else { - key_node = plist_new_key(key); - node_attach((node_t)node, (node_t)key_node); - node_attach((node_t)node, (node_t)item); + PLIST_ERR("%s: failed to replace dict value (err=%d)\n", __func__, r); + return PLIST_ERR_UNKNOWN; } + key_node = old_key; - hashtable_t *ht = (hashtable_t*)((plist_data_t)((node_t)node)->data)->hashtable; + // update hash table if (ht) { - /* store pointer to item in hash table */ - hash_table_insert(ht, (plist_data_t)((node_t)key_node)->data, item); - } else { - if (((node_t)node)->count > 500) { - /* make new hash table */ - ht = hash_table_new(dict_key_hash, dict_key_compare, NULL); - /* calculate the hashes for all entries we have so far */ - plist_t current = NULL; - for (current = (plist_t)node_first_child((node_t)node); - ht && current; - current = (plist_t)node_next_sibling(node_next_sibling((node_t)current))) - { - hash_table_insert(ht, ((node_t)current)->data, node_next_sibling((node_t)current)); + if (hash_table_insert(ht, (plist_data_t)((node_t)key_node)->data, item) < 0) { + plist_dict_drop_cache(node); + ht = NULL; + } + } + + // now it’s safe to free old value + plist_free_node(old_val); + } else { + // --- INSERT NEW KEY/VALUE PAIR --- + key_node = plist_new_key(key); + if (!key_node) return PLIST_ERR_NO_MEM; + + int r = node_attach((node_t)node, (node_t)key_node); + if (r != NODE_ERR_SUCCESS) { + plist_free_node((node_t)key_node); + PLIST_ERR("%s: failed to attach dict key (err=%d)\n", __func__, r); + return PLIST_ERR_UNKNOWN; + } + r = node_attach((node_t)node, (node_t)item); + if (r != NODE_ERR_SUCCESS) { + // rollback key insertion + node_detach((node_t)node, (node_t)key_node); + plist_free_node((node_t)key_node); + PLIST_ERR("%s: failed to attach dict value (err=%d)\n", __func__, r); + return PLIST_ERR_UNKNOWN; + } + + if (ht) { + // store pointer to item in hash table + if (hash_table_insert(ht, (plist_data_t)((node_t)key_node)->data, item) < 0) { + plist_dict_drop_cache(node); + ht = NULL; + } + } else if (((node_t)node)->count > 500) { + // make new hash table + ht = hash_table_new(dict_key_hash, dict_key_compare, NULL); + // calculate the hashes for all entries we have so far + plist_t current = NULL; + for (current = (plist_t)node_first_child((node_t)node); + ht && current; + current = (plist_t)node_next_sibling(node_next_sibling((node_t)current))) + { + if (hash_table_insert(ht, ((node_t)current)->data, node_next_sibling((node_t)current)) < 0) { + hash_table_destroy(ht); + ht = NULL; } - ((plist_data_t)((node_t)node)->data)->hashtable = ht; } + ((plist_data_t)((node_t)node)->data)->hashtable = ht; } } + return PLIST_ERR_SUCCESS; } -void plist_dict_remove_item(plist_t node, const char* key) +plist_err_t plist_dict_remove_item(plist_t node, const char* key) { - if (node && PLIST_DICT == plist_get_node_type(node)) - { - plist_t old_item = plist_dict_get_item(node, key); - if (old_item) - { - plist_t key_node = node_prev_sibling((node_t)old_item); - hashtable_t* ht = (hashtable_t*)((plist_data_t)((node_t)node)->data)->hashtable; - if (ht) { - hash_table_remove(ht, ((node_t)key_node)->data); - } - plist_free(key_node); - plist_free(old_item); - } + if (!PLIST_IS_DICT(node) || !key) { + PLIST_ERR("invalid argument passed to %s (node=%p, key=%p)\n", __func__, node, key); + return PLIST_ERR_INVALID_ARG; + } + + plist_t old_item = plist_dict_get_item(node, key); + if (!old_item) { + PLIST_ERR("item not found for key '%s'\n", key); + return PLIST_ERR_INVALID_ARG; + } + + plist_t key_node = node_prev_sibling((node_t)old_item); + hashtable_t* ht = (hashtable_t*)((plist_data_t)((node_t)node)->data)->hashtable; + if (ht) { + hash_table_remove(ht, ((node_t)key_node)->data); } + plist_free(key_node); + plist_free(old_item); + + return PLIST_ERR_SUCCESS; } -void plist_dict_merge(plist_t *target, plist_t source) +plist_err_t plist_dict_merge(plist_t *target, plist_t source) { - if (!target || !*target || (plist_get_node_type(*target) != PLIST_DICT) || !source || (plist_get_node_type(source) != PLIST_DICT)) - return; + if (!target || !PLIST_IS_DICT(*target) || !PLIST_IS_DICT(source)) + return PLIST_ERR_INVALID_ARG; char* key = NULL; plist_dict_iter it = NULL; plist_t subnode = NULL; plist_dict_new_iter(source, &it); if (!it) - return; + return PLIST_ERR_NO_MEM; do { plist_dict_next_item(source, it, &key, &subnode); @@ -955,6 +1470,196 @@ void plist_dict_merge(plist_t *target, plist_t source) key = NULL; } while (1); free(it); + return PLIST_ERR_SUCCESS; +} + +uint8_t plist_dict_get_bool(plist_t dict, const char *key) +{ + uint8_t bval = 0; + uint64_t uintval = 0; + const char *strval = NULL; + uint64_t strsz = 0; + plist_t node = plist_dict_get_item(dict, key); + if (!node) { + return 0; + } + switch (plist_get_node_type(node)) { + case PLIST_BOOLEAN: + plist_get_bool_val(node, &bval); + break; + case PLIST_INT: + plist_get_uint_val(node, &uintval); + bval = (uintval) ? 1 : 0; + break; + case PLIST_STRING: + strval = plist_get_string_ptr(node, NULL); + if (strval) { + if (strcmp(strval, "true") == 0) { + bval = 1; + } else if (strcmp(strval, "false") == 0) { + bval = 0; + } else { + PLIST_ERR("%s: invalid string '%s' for string to boolean conversion\n", __func__, strval); + } + } + break; + case PLIST_DATA: + strval = (const char*)plist_get_data_ptr(node, &strsz); + if (strval) { + if (strsz == 1) { + bval = (strval[0]) ? 1 : 0; + } else { + PLIST_ERR("%s: invalid size %" PRIu64 " for data to boolean conversion\n", __func__, strsz); + } + } + break; + default: + break; + } + return bval; +} + +int64_t plist_dict_get_int(plist_t dict, const char *key) +{ + int64_t intval = 0; + const char *strval = NULL; + uint64_t strsz = 0; + plist_t node = plist_dict_get_item(dict, key); + if (!node) { + return intval; + } + switch (plist_get_node_type(node)) { + case PLIST_INT: + plist_get_int_val(node, &intval); + break; + case PLIST_STRING: + strval = plist_get_string_ptr(node, NULL); + if (strval) { + intval = strtoll(strval, NULL, 0); + } + break; + case PLIST_DATA: + strval = (const char*)plist_get_data_ptr(node, &strsz); + if (strval) { + if (strsz == 8) { + intval = le64toh(*(int64_t*)strval); + } else if (strsz == 4) { + intval = le32toh(*(int32_t*)strval); + } else if (strsz == 2) { + intval = le16toh(*(int16_t*)strval); + } else if (strsz == 1) { + intval = strval[0]; + } else { + PLIST_ERR("%s: invalid size %" PRIu64 " for data to integer conversion\n", __func__, strsz); + } + } + break; + default: + break; + } + return intval; +} + + +uint64_t plist_dict_get_uint(plist_t dict, const char *key) +{ + uint64_t uintval = 0; + const char *strval = NULL; + uint64_t strsz = 0; + plist_t node = plist_dict_get_item(dict, key); + if (!node) { + return uintval; + } + switch (plist_get_node_type(node)) { + case PLIST_INT: + plist_get_uint_val(node, &uintval); + break; + case PLIST_STRING: + strval = plist_get_string_ptr(node, NULL); + if (strval) { + uintval = strtoull(strval, NULL, 0); + } + break; + case PLIST_DATA: + strval = (const char*)plist_get_data_ptr(node, &strsz); + if (strval) { + if (strsz == 8) { + uintval = le64toh(*(uint64_t*)strval); + } else if (strsz == 4) { + uintval = le32toh(*(uint32_t*)strval); + } else if (strsz == 2) { + uintval = le16toh(*(uint16_t*)strval); + } else if (strsz == 1) { + uintval = strval[0]; + } else { + PLIST_ERR("%s: invalid size %" PRIu64 " for data to integer conversion\n", __func__, strsz); + } + } + break; + default: + break; + } + return uintval; +} + +plist_err_t plist_dict_copy_item(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key) +{ + plist_t node = plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key); + if (!node) { + return PLIST_ERR_INVALID_ARG; + } + plist_dict_set_item(target_dict, key, plist_copy(node)); + return PLIST_ERR_SUCCESS; +} + +plist_err_t plist_dict_copy_bool(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key) +{ + if (plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key) == NULL) { + return PLIST_ERR_INVALID_ARG; + } + uint8_t bval = plist_dict_get_bool(source_dict, (alt_source_key) ? alt_source_key : key); + plist_dict_set_item(target_dict, key, plist_new_bool(bval)); + return PLIST_ERR_SUCCESS; +} + +plist_err_t plist_dict_copy_int(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key) +{ + if (plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key) == NULL) { + return PLIST_ERR_INVALID_ARG; + } + int64_t i64val = plist_dict_get_int(source_dict, (alt_source_key) ? alt_source_key : key); + plist_dict_set_item(target_dict, key, plist_new_int(i64val)); + return PLIST_ERR_SUCCESS; +} + +plist_err_t plist_dict_copy_uint(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key) +{ + if (plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key) == NULL) { + return PLIST_ERR_INVALID_ARG; + } + uint64_t u64val = plist_dict_get_uint(source_dict, (alt_source_key) ? alt_source_key : key); + plist_dict_set_item(target_dict, key, plist_new_uint(u64val)); + return PLIST_ERR_SUCCESS; +} + +plist_err_t plist_dict_copy_data(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key) +{ + plist_t node = plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key); + if (!PLIST_IS_DATA(node)) { + return PLIST_ERR_INVALID_ARG; + } + plist_dict_set_item(target_dict, key, plist_copy(node)); + return PLIST_ERR_SUCCESS; +} + +plist_err_t plist_dict_copy_string(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key) +{ + plist_t node = plist_dict_get_item(source_dict, (alt_source_key) ? alt_source_key : key); + if (!PLIST_IS_STRING(node)) { + return PLIST_ERR_INVALID_ARG; + } + plist_dict_set_item(target_dict, key, plist_copy(node)); + return PLIST_ERR_SUCCESS; } plist_t plist_access_pathv(plist_t plist, uint32_t length, va_list v) @@ -996,10 +1701,11 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu { plist_data_t data = NULL; - if (!node) + if (!node || !type || !value || !length) return; data = plist_get_data(node); + if (!data) return; *type = data->type; *length = data->length; @@ -1020,9 +1726,17 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu case PLIST_KEY: case PLIST_STRING: *((char **) value) = strdup(data->strval); + if (!*((char **) value)) { + PLIST_ERR("%s: strdup failed\n", __func__); + return; + } break; case PLIST_DATA: *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t)); + if (!*((uint8_t **) value)) { + PLIST_ERR("%s: malloc failed\n", __func__); + return; + } memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t)); break; case PLIST_ARRAY: @@ -1175,13 +1889,39 @@ void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) return; plist_get_type_and_value(node, &type, (void *) &val, &length); assert(length == sizeof(double)); + /* Inverse of plist_set_date_val (val = sec + usec / 1000000): + * whole seconds rounded down, the fraction rounded to microseconds. */ + if (isnan(val)) + val = 0; + double whole = floor(val); + double frac_usec = round((val - whole) * 1000000); + if (frac_usec >= 1000000) { + whole += 1; + frac_usec -= 1000000; + } + /* keep the int32_t conversion defined */ + if (whole > INT32_MAX) + whole = INT32_MAX; + else if (whole < INT32_MIN) + whole = INT32_MIN; if (sec) - *sec = (int32_t)val; + *sec = (int32_t)whole; if (usec) - { - val = fabs((val - (int64_t)val) * 1000000); - *usec = (int32_t)val; - } + *usec = (int32_t)frac_usec; +} + +void plist_get_unix_date_val(plist_t node, int64_t *sec) +{ + if (!node || !sec) + return; + plist_type type = plist_get_node_type(node); + uint64_t length = 0; + double val = 0; + if (PLIST_DATE != type) + return; + plist_get_type_and_value(node, &type, (void *) &val, &length); + assert(length == sizeof(double)); + *sec = (int64_t)val + MAC_EPOCH; } int plist_data_compare(const void *a, const void *b) @@ -1189,15 +1929,21 @@ int plist_data_compare(const void *a, const void *b) plist_data_t val_a = NULL; plist_data_t val_b = NULL; - if (!a || !b) - return FALSE; + if (a == b) + return TRUE; - if (!((node_t) a)->data || !((node_t) b)->data) + if (!a || !b) return FALSE; val_a = plist_get_data((plist_t) a); val_b = plist_get_data((plist_t) b); + if (val_a == NULL && val_b == NULL) + return TRUE; + + if (val_a == NULL || val_b == NULL) + return FALSE; + if (val_a->type != val_b->type) return FALSE; @@ -1209,28 +1955,30 @@ int plist_data_compare(const void *a, const void *b) case PLIST_REAL: case PLIST_DATE: case PLIST_UID: - if (val_a->length != val_b->length) - return FALSE; - return val_a->intval == val_b->intval; //it is an union so this is sufficient + return val_a->length == val_b->length + && val_a->intval == val_b->intval; // it is a union so this is sufficient case PLIST_KEY: case PLIST_STRING: + if (!val_a->strval || !val_b->strval) + return val_a->strval == val_b->strval; return strcmp(val_a->strval, val_b->strval) == 0; - case PLIST_DATA: + case PLIST_DATA: { if (val_a->length != val_b->length) return FALSE; + if (val_a->length == 0) + return TRUE; return memcmp(val_a->buff, val_b->buff, val_a->length) == 0; - + } case PLIST_ARRAY: case PLIST_DICT: //compare pointer return a == b; default: - break; + return FALSE; } - return FALSE; } char plist_compare_node_value(plist_t node_l, plist_t node_r) @@ -1238,26 +1986,20 @@ char plist_compare_node_value(plist_t node_l, plist_t node_r) return plist_data_compare(node_l, node_r); } -static void plist_set_element_val(plist_t node, plist_type type, const void *value, uint64_t length) +static plist_err_t plist_set_element_val(plist_t node, plist_type type, const void *value, uint64_t length) { - //free previous allocated buffer + //free previous allocated data plist_data_t data = plist_get_data(node); - assert(data); // a node should always have data attached + if (!data) { // a node should always have data attached + PLIST_ERR("%s: Failed to allocate plist data\n", __func__); + return PLIST_ERR_NO_MEM; + } - switch (data->type) - { - case PLIST_KEY: - case PLIST_STRING: - free(data->strval); - data->strval = NULL; - break; - case PLIST_DATA: - free(data->buff); - data->buff = NULL; - break; - default: - break; + if (node_first_child((node_t)node)) { + int r = plist_free_children((node_t)node); + if (r < 0) return PLIST_ERR_UNKNOWN; } + _plist_free_data(data); //now handle value @@ -1280,9 +2022,17 @@ static void plist_set_element_val(plist_t node, plist_type type, const void *val case PLIST_KEY: case PLIST_STRING: data->strval = strdup((char *) value); + if (!data->strval) { + PLIST_ERR("%s: strdup failed\n", __func__); + return PLIST_ERR_NO_MEM; + } break; case PLIST_DATA: data->buff = (uint8_t *) malloc(length); + if (!data->buff) { + PLIST_ERR("%s: malloc failed\n", __func__); + return PLIST_ERR_NO_MEM; + } memcpy(data->buff, value, length); break; case PLIST_ARRAY: @@ -1290,12 +2040,13 @@ static void plist_set_element_val(plist_t node, plist_type type, const void *val default: break; } + return PLIST_ERR_SUCCESS; } void plist_set_key_val(plist_t node, const char *val) { - plist_t father = plist_get_parent(node); - plist_t item = plist_dict_get_item(father, val); + plist_t parent = plist_get_parent(node); + plist_t item = plist_dict_get_item(parent, val); if (item) { return; } @@ -1340,7 +2091,13 @@ void plist_set_data_val(plist_t node, const char *val, uint64_t length) void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) { double val = (double)sec + (double)usec / 1000000; - plist_set_element_val(node, PLIST_DATE, &val, sizeof(struct timeval)); + plist_set_element_val(node, PLIST_DATE, &val, sizeof(double)); +} + +void plist_set_unix_date_val(plist_t node, int64_t sec) +{ + double val = (double)(sec - MAC_EPOCH); + plist_set_element_val(node, PLIST_DATE, &val, sizeof(double)); } int plist_bool_val_is_true(plist_t boolnode) @@ -1462,9 +2219,12 @@ int plist_date_val_compare(plist_t datenode, int32_t cmpsec, int32_t cmpusec) if (!PLIST_IS_DATE(datenode)) { return -1; } - int32_t sec = 0; - int32_t usec = 0; - plist_get_date_val(datenode, &sec, &usec); + plist_data_t data = plist_get_data(datenode); + assert(data->length == sizeof(double)); + double val = data->realval; + int32_t sec = (int32_t)val; + val = fabs((val - (int64_t)val) * 1000000); + int32_t usec = (int32_t)val; uint64_t dateval = ((int64_t)sec << 32) | usec; uint64_t cmpval = ((int64_t)cmpsec << 32) | cmpusec; if (dateval == cmpval) { @@ -1478,6 +2238,24 @@ int plist_date_val_compare(plist_t datenode, int32_t cmpsec, int32_t cmpusec) return 1; } +int plist_unix_date_val_compare(plist_t datenode, int64_t cmpval) +{ + if (!PLIST_IS_DATE(datenode)) { + return -1; + } + int64_t dateval = 0; + plist_get_unix_date_val(datenode, &dateval); + if (dateval == cmpval) { + return 0; + } + + if (dateval < cmpval) { + return -1; + } + + return 1; +} + int plist_string_val_compare(plist_t strnode, const char* cmpval) { if (!PLIST_IS_STRING(strnode)) { @@ -1577,6 +2355,9 @@ extern void plist_ostep_set_debug(int debug); void plist_set_debug(int debug) { +#if DEBUG + plist_debug = debug; +#endif plist_xml_set_debug(debug); plist_bin_set_debug(debug); plist_json_set_debug(debug); @@ -1597,6 +2378,9 @@ void plist_sort(plist_t plist) } else if (PLIST_IS_DICT(plist)) { node_t node = (node_t)plist; node_t ch; + if (!node_first_child(node)) { + return; + } for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { ch = node_next_sibling(ch); plist_sort((plist_t)ch); @@ -1655,10 +2439,10 @@ plist_err_t plist_write_to_string(plist_t plist, char **output, uint32_t* length err = plist_to_xml(plist, output, length); break; case PLIST_FORMAT_JSON: - err = plist_to_json(plist, output, length, ((options & PLIST_OPT_COMPACT) == 0)); + err = plist_to_json_with_options(plist, output, length, options); break; case PLIST_FORMAT_OSTEP: - err = plist_to_openstep(plist, output, length, ((options & PLIST_OPT_COMPACT) == 0)); + err = plist_to_openstep_with_options(plist, output, length, options); break; case PLIST_FORMAT_PRINT: err = plist_write_to_string_default(plist, output, length, options); @@ -1693,10 +2477,10 @@ plist_err_t plist_write_to_stream(plist_t plist, FILE *stream, plist_format_t fo err = plist_to_xml(plist, &output, &length); break; case PLIST_FORMAT_JSON: - err = plist_to_json(plist, &output, &length, ((options & PLIST_OPT_COMPACT) == 0)); + err = plist_to_json_with_options(plist, &output, &length, options); break; case PLIST_FORMAT_OSTEP: - err = plist_to_openstep(plist, &output, &length, ((options & PLIST_OPT_COMPACT) == 0)); + err = plist_to_openstep_with_options(plist, &output, &length, options); break; case PLIST_FORMAT_PRINT: err = plist_write_to_stream_default(plist, stream, options); @@ -1740,7 +2524,7 @@ void plist_print(plist_t plist) plist_write_to_stream(plist, stdout, PLIST_FORMAT_PRINT, PLIST_OPT_PARTIAL_DATA); } -const char* libplist_version() +const char* libplist_version(void) { #ifndef PACKAGE_VERSION #error PACKAGE_VERSION is not defined! diff --git a/src/plist.h b/src/plist.h index a993e3a..7228696 100644 --- a/src/plist.h +++ b/src/plist.h @@ -49,6 +49,16 @@ #endif #endif +#include "node.h" + +#ifndef PLIST_MAX_NESTING_DEPTH +#ifdef NODE_MAX_DEPTH +#define PLIST_MAX_NESTING_DEPTH NODE_MAX_DEPTH +#else +#define PLIST_MAX_NESTING_DEPTH 512 +#endif +#endif + #include "plist/plist.h" struct plist_data_s @@ -81,4 +91,17 @@ extern plist_err_t plist_write_to_stream_default(plist_t plist, FILE *stream, pl extern plist_err_t plist_write_to_stream_limd(plist_t plist, FILE *stream, plist_write_options_t options); extern plist_err_t plist_write_to_stream_plutil(plist_t plist, FILE *stream, plist_write_options_t options); +static inline unsigned int plist_node_ptr_hash(const void *ptr) +{ + uintptr_t h = (uintptr_t)ptr; + h ^= (h >> 16); + h *= 0x85ebca6b; + return (unsigned int)h; +} + +static inline int plist_node_ptr_compare(const void *a, const void *b) +{ + return a == b; +} + #endif diff --git a/src/ptrarray.c b/src/ptrarray.c index 3a11031..eb9ff0a 100644 --- a/src/ptrarray.c +++ b/src/ptrarray.c @@ -20,11 +20,24 @@ */ #include "ptrarray.h" #include <string.h> +#include <stdint.h> +#include <limits.h> ptrarray_t *ptr_array_new(int capacity) { - ptrarray_t *pa = (ptrarray_t*)malloc(sizeof(ptrarray_t)); - pa->pdata = (void**)malloc(sizeof(void*) * capacity); + ptrarray_t *pa; + if (capacity < 0 || (size_t)capacity > SIZE_MAX / sizeof(void*)) { + return NULL; + } + pa = (ptrarray_t*)malloc(sizeof(ptrarray_t)); + if (!pa) { + return NULL; + } + pa->pdata = (void**)malloc(sizeof(void*) * (size_t)capacity); + if (!pa->pdata) { + free(pa); + return NULL; + } pa->capacity = capacity; pa->capacity_step = (capacity > 4096) ? 4096 : capacity; pa->len = 0; @@ -40,12 +53,21 @@ void ptr_array_free(ptrarray_t *pa) free(pa); } -void ptr_array_insert(ptrarray_t *pa, void *data, long array_index) +int ptr_array_insert(ptrarray_t *pa, void *data, long array_index) { - if (!pa || !pa->pdata) return; + if (!pa || !pa->pdata) return -1; long remaining = pa->capacity-pa->len; if (remaining == 0) { - pa->pdata = (void**)realloc(pa->pdata, sizeof(void*) * (pa->capacity + pa->capacity_step)); + void **newdata; + if (pa->capacity > INT_MAX - pa->capacity_step || + (size_t)(pa->capacity + pa->capacity_step) > SIZE_MAX / sizeof(void*)) { + return -1; + } + newdata = (void**)realloc(pa->pdata, sizeof(void*) * (size_t)(pa->capacity + pa->capacity_step)); + if (!newdata) { + return -1; + } + pa->pdata = newdata; pa->capacity += pa->capacity_step; } if (array_index < 0 || array_index >= pa->len) { @@ -55,11 +77,12 @@ void ptr_array_insert(ptrarray_t *pa, void *data, long array_index) pa->pdata[array_index] = data; } pa->len++; + return 0; } -void ptr_array_add(ptrarray_t *pa, void *data) +int ptr_array_add(ptrarray_t *pa, void *data) { - ptr_array_insert(pa, data, -1); + return ptr_array_insert(pa, data, -1); } void ptr_array_remove(ptrarray_t *pa, long array_index) diff --git a/src/ptrarray.h b/src/ptrarray.h index ed67351..28a521f 100644 --- a/src/ptrarray.h +++ b/src/ptrarray.h @@ -31,8 +31,8 @@ typedef struct ptrarray_t { ptrarray_t *ptr_array_new(int capacity); void ptr_array_free(ptrarray_t *pa); -void ptr_array_add(ptrarray_t *pa, void *data); -void ptr_array_insert(ptrarray_t *pa, void *data, long index); +int ptr_array_add(ptrarray_t *pa, void *data); +int ptr_array_insert(ptrarray_t *pa, void *data, long index); void ptr_array_remove(ptrarray_t *pa, long index); void ptr_array_set(ptrarray_t *pa, void *data, long index); void* ptr_array_index(ptrarray_t *pa, long index); diff --git a/src/time64.c b/src/time64.c index 07639df..6a9218c 100644 --- a/src/time64.c +++ b/src/time64.c @@ -45,6 +45,7 @@ gmtime64_r() is a 64-bit equivalent of gmtime_r(). #include <string.h> #include <time.h> #include <errno.h> +#include <limits.h> #include "time64.h" #include "time64_limits.h" @@ -125,7 +126,7 @@ static const char dow_year_start[SOLAR_CYCLE_LENGTH] = { #define CHEAT_DAYS (1199145600 / 24 / 60 / 60) #define CHEAT_YEARS 108 -#define IS_LEAP(n) ((!(((n) + 1900) % 400) || (!(((n) + 1900) % 4) && (((n) + 1900) % 100))) != 0) +#define IS_LEAP(n) ((!(((Year)(n) + 1900) % 400) || (!(((Year)(n) + 1900) % 4) && (((Year)(n) + 1900) % 100))) != 0) #define WRAP(a,b,m) ((a) = ((a) < 0 ) ? ((b)--, (a) + (m)) : (a)) #ifdef USE_SYSTEM_LOCALTIME @@ -224,6 +225,7 @@ Time64_T timegm64(const struct TM *date) { Time64_T days = 0; Time64_T seconds = 0; Year year; + assert(date != NULL); Year orig_year = (Year)date->tm_year; int cycles = 0; @@ -442,7 +444,7 @@ void copy_TM64_to_tm(const struct TM *src, struct tm *dest) { } -#ifndef HAVE_LOCALTIME_R +#if !defined(HAVE_LOCALTIME_R) && !defined(_WIN32) /* Simulate localtime_r() to the best of our ability */ static struct tm * fake_localtime_r(const time_t *time, struct tm *result) { const struct tm *static_result = localtime(time); @@ -461,7 +463,7 @@ static struct tm * fake_localtime_r(const time_t *time, struct tm *result) { #endif -#ifndef HAVE_GMTIME_R +#if !defined(HAVE_GMTIME_R) && !defined(_WIN32) /* Simulate gmtime_r() to the best of our ability */ static struct tm * fake_gmtime_r(const time_t *time, struct tm *result) { const struct tm *static_result = gmtime(time); @@ -648,7 +650,8 @@ struct TM *gmtime64_r (const Time64_T *in_time, struct TM *p) } p->tm_year = year; - if( p->tm_year != year ) { + /* The calendar year (tm_year + 1900) must also fit in an int, e.g. for strftime() */ + if( p->tm_year != year || year > (Year)INT_MAX - 1900 || year < (Year)INT_MIN ) { #ifdef EOVERFLOW errno = EOVERFLOW; #endif diff --git a/src/time64.h b/src/time64.h index efdc716..2c20ffe 100644 --- a/src/time64.h +++ b/src/time64.h @@ -11,6 +11,13 @@ typedef long long Int64; typedef Int64 Time64_T; typedef Int64 Year; +#ifndef TIME64_MIN +#define TIME64_MIN ((Time64_T)INT64_MIN) +#endif + +#ifndef TIME64_MAX +#define TIME64_MAX ((Time64_T)INT64_MAX) +#endif /* A copy of the tm struct but with a 64 bit year */ struct TM64 { @@ -58,11 +65,15 @@ Time64_T timelocal64 (struct TM *); /* Not everyone has gm/localtime_r(), provide a replacement */ #ifdef HAVE_LOCALTIME_R # define LOCALTIME_R(clock, result) localtime_r(clock, result) +#elif defined(_WIN32) +# define LOCALTIME_R(clock, result) (localtime_s(result, clock) ? NULL : result) #else # define LOCALTIME_R(clock, result) fake_localtime_r(clock, result) #endif #ifdef HAVE_GMTIME_R # define GMTIME_R(clock, result) gmtime_r(clock, result) +#elif defined (_WIN32) +# define GMTIME_R(clock, result) (gmtime_s(result, clock) ? NULL : result) #else # define GMTIME_R(clock, result) fake_gmtime_r(clock, result) #endif diff --git a/src/xplist.c b/src/xplist.c index 66e1dba..92ca15d 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -39,6 +39,7 @@ #include <float.h> #include <math.h> #include <limits.h> +#include <errno.h> #include <node.h> @@ -46,6 +47,8 @@ #include "base64.h" #include "strbuf.h" #include "time64.h" +#include "hashtable.h" +#include "common.h" #define XPLIST_KEY "key" #define XPLIST_KEY_LEN 3 @@ -68,8 +71,6 @@ #define XPLIST_DICT "dict" #define XPLIST_DICT_LEN 4 -#define MAC_EPOCH 978307200 - #define MAX_DATA_BYTES_PER_LINE(__i) (((76 - ((__i) << 3)) >> 2) * 3) static const char XML_PLIST_PROLOG[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\ @@ -109,30 +110,6 @@ void plist_xml_set_debug(int debug) #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_xml(node_t node, bytearray_t **outbuf, uint32_t depth) { plist_data_t node_data = NULL; @@ -142,7 +119,7 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth const char *tag = NULL; size_t tag_len = 0; - char *val = NULL; + char val[64] = { 0 }; size_t val_len = 0; uint32_t i = 0; @@ -171,19 +148,17 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth case PLIST_INT: tag = XPLIST_INT; tag_len = XPLIST_INT_LEN; - val = (char*)malloc(64); if (node_data->length == 16) { - val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + val_len = snprintf(val, sizeof(val), "%" PRIu64, node_data->intval); } else { - val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + val_len = snprintf(val, sizeof(val), "%" PRIi64, node_data->intval); } break; case PLIST_REAL: tag = XPLIST_REAL; tag_len = XPLIST_REAL_LEN; - val = (char*)malloc(64); - val_len = dtostr(val, 64, node_data->realval); + val_len = dtostr(val, sizeof(val), node_data->realval); break; case PLIST_STRING: @@ -217,17 +192,19 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth tag = XPLIST_DATE; tag_len = XPLIST_DATE_LEN; { - Time64_T timev = (Time64_T)node_data->realval + MAC_EPOCH; + Time64_T timev; + if (plist_real_to_time64(node_data->realval, &timev) < 0) { + PLIST_XML_WRITE_ERR("Encountered invalid date value %f\n", node_data->realval); + return PLIST_ERR_INVALID_ARG; + } struct TM _btime; struct TM *btime = gmtime64_r(&timev, &_btime); if (btime) { - val = (char*)calloc(1, 24); struct tm _tmcopy; copy_TM64_to_tm(btime, &_tmcopy); - val_len = strftime(val, 24, "%Y-%m-%dT%H:%M:%SZ", &_tmcopy); + val_len = strftime(val, sizeof(val), "%Y-%m-%dT%H:%M:%SZ", &_tmcopy); if (val_len <= 0) { - free (val); - val = NULL; + snprintf(val, sizeof(val), "1970-01-01T00:00:00Z"); } } } @@ -235,11 +212,10 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth case PLIST_UID: tag = XPLIST_DICT; tag_len = XPLIST_DICT_LEN; - val = (char*)malloc(64); if (node_data->length == 16) { - val_len = snprintf(val, 64, "%" PRIu64, node_data->intval); + val_len = snprintf(val, sizeof(val), "%" PRIu64, node_data->intval); } else { - val_len = snprintf(val, 64, "%" PRIi64, node_data->intval); + val_len = snprintf(val, sizeof(val), "%" PRIi64, node_data->intval); } break; case PLIST_NULL: @@ -256,7 +232,7 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth /* append tag */ str_buf_append(*outbuf, "<", 1); str_buf_append(*outbuf, tag, tag_len); - if (node_data->type == PLIST_STRING || node_data->type == PLIST_KEY) { + if ((node_data->type == PLIST_STRING || node_data->type == PLIST_KEY) && node_data->length > 0) { size_t j; size_t len; off_t start = 0; @@ -300,16 +276,27 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth uint32_t indent = (depth > 8) ? 8 : depth; uint32_t maxread = MAX_DATA_BYTES_PER_LINE(indent); size_t count = 0; + size_t encoded = 0; size_t amount = (node_data->length / 3 * 4) + 4 + (((node_data->length / maxread) + 1) * (indent+1)); if ((*outbuf)->len + amount > (*outbuf)->capacity) { str_buf_grow(*outbuf, amount); + if (!(*outbuf)->data) { + return PLIST_ERR_NO_MEM; + } } while (j < node_data->length) { for (i = 0; i < indent; i++) { str_buf_append(*outbuf, "\t", 1); } count = (node_data->length-j < maxread) ? node_data->length-j : maxread; - assert((*outbuf)->len + count < (*outbuf)->capacity); + /* base64encode writes 4*ceil(count/3) bytes plus a terminating NUL */ + encoded = ((count + 2) / 3) * 4 + 1; + if ((*outbuf)->len + encoded > (*outbuf)->capacity) { + str_buf_grow(*outbuf, (*outbuf)->len + encoded - (*outbuf)->capacity); + if (!(*outbuf)->data) { + return PLIST_ERR_NO_MEM; + } + } (*outbuf)->len += base64encode((char*)(*outbuf)->data + (*outbuf)->len, node_data->buff + j, count); str_buf_append(*outbuf, "\n", 1); j+=count; @@ -343,7 +330,7 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth for (i = 0; i < depth; i++) { str_buf_append(*outbuf, "\t", 1); } - } else if (val) { + } else if (val_len > 0) { str_buf_append(*outbuf, ">", 1); tagOpen = TRUE; str_buf_append(*outbuf, val, val_len); @@ -354,7 +341,6 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth tagOpen = FALSE; str_buf_append(*outbuf, "/>", 2); } - free(val); if (isStruct) { /* add newline for structured types */ @@ -386,75 +372,76 @@ static plist_err_t node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth return PLIST_ERR_SUCCESS; } -static void parse_date(const char *strval, struct TM *btime) +static int parse_date(const char *strval, struct TM *btime) { - if (!btime) return; - memset(btime, 0, sizeof(struct tm)); - if (!strval) return; + if (!btime) return -1; + memset(btime, 0, sizeof(*btime)); + if (!strval) return -1; #ifdef HAVE_STRPTIME - strptime((char*)strval, "%Y-%m-%dT%H:%M:%SZ", btime); +#ifdef USE_TM64 + struct tm t = { 0 }; + char* r = strptime((char*)strval, "%Y-%m-%dT%H:%M:%SZ", &t); + if (!r || *r != '\0') { + return -1; + } + copy_tm_to_TM64(&t, btime); +#else + char* r = strptime((char*)strval, "%Y-%m-%dT%H:%M:%SZ", btime); + if (!r || *r != '\0') { + return -1; + } +#endif #else #ifdef USE_TM64 #define PLIST_SSCANF_FORMAT "%lld-%d-%dT%d:%d:%dZ" #else #define PLIST_SSCANF_FORMAT "%d-%d-%dT%d:%d:%dZ" #endif - sscanf(strval, PLIST_SSCANF_FORMAT, &btime->tm_year, &btime->tm_mon, &btime->tm_mday, &btime->tm_hour, &btime->tm_min, &btime->tm_sec); + int n = 0; + if (sscanf(strval, PLIST_SSCANF_FORMAT "%n", &btime->tm_year, &btime->tm_mon, &btime->tm_mday, &btime->tm_hour, &btime->tm_min, &btime->tm_sec, &n) != 6) return -1; + if (strval[n] != '\0') return -1; + if (btime->tm_mon < 1 || btime->tm_mon > 12) return -1; + if (btime->tm_mday < 1 || btime->tm_mday > 31) return -1; + if (btime->tm_hour < 0 || btime->tm_hour > 23) return -1; + if (btime->tm_min < 0 || btime->tm_min > 59) return -1; + if (btime->tm_sec < 0 || btime->tm_sec > 59) return -1; btime->tm_year-=1900; btime->tm_mon--; #endif btime->tm_isdst=0; + return 0; } -#define PO10i_LIMIT (INT64_MAX/10) - -/* based on https://stackoverflow.com/a/4143288 */ -static int num_digits_i(int64_t i) +static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, hashtable_t *visited) { - 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; + plist_data_t data; + if (!node) { + return PLIST_ERR_INVALID_ARG; } - return n; -} -#define PO10u_LIMIT (UINT64_MAX/10) + if (depth > PLIST_MAX_NESTING_DEPTH) { + PLIST_XML_WRITE_ERR("maximum nesting depth (%u) exceeded\n", (unsigned)PLIST_MAX_NESTING_DEPTH); + return PLIST_ERR_MAX_NESTING; + } -/* 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; + if (hash_table_lookup(visited, node)) { + PLIST_XML_WRITE_ERR("circular reference detected\n"); + return PLIST_ERR_CIRCULAR_REF; } - return n; -} -static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth) -{ - plist_data_t data; - if (!node) { - return PLIST_ERR_INVALID_ARG; + // mark as visited + if (hash_table_insert(visited, node, (void*)1) < 0) { + return PLIST_ERR_NO_MEM; } + data = plist_get_data(node); if (node->children) { node_t ch; for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { - node_estimate_size(ch, size, depth + 1); + plist_err_t err = _node_estimate_size(ch, size, depth + 1, visited); + if (err != PLIST_ERR_SUCCESS) { + return err; + } } switch (data->type) { case PLIST_DICT: @@ -529,6 +516,15 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept return PLIST_ERR_SUCCESS; } +static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth) +{ + hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL); + if (!visited) return PLIST_ERR_NO_MEM; + plist_err_t err = _node_estimate_size(node, size, depth, visited); + hash_table_destroy(visited); + return err; +} + plist_err_t plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) { uint64_t size = 0; @@ -562,6 +558,13 @@ plist_err_t plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) str_buf_append(outbuf, XML_PLIST_EPILOG, sizeof(XML_PLIST_EPILOG)); + if (!outbuf->data) { + str_buf_free(outbuf); + *plist_xml = NULL; + *length = 0; + return PLIST_ERR_NO_MEM; + } + *plist_xml = (char*)outbuf->data; *length = outbuf->len - 1; @@ -574,13 +577,18 @@ plist_err_t plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length) struct _parse_ctx { const char *pos; const char *end; - int err; + plist_err_t err; }; typedef struct _parse_ctx* parse_ctx; +static inline int is_xml_ws(unsigned char c) +{ + return (c == ' ' || c == '\t' || c == '\r' || c == '\n'); +} + static void parse_skip_ws(parse_ctx ctx) { - while (ctx->pos < ctx->end && ((*(ctx->pos) == ' ') || (*(ctx->pos) == '\t') || (*(ctx->pos) == '\r') || (*(ctx->pos) == '\n'))) { + while (ctx->pos < ctx->end && is_xml_ws(*(ctx->pos))) { ctx->pos++; } } @@ -679,7 +687,9 @@ static void text_parts_free(text_part_t *tp) static text_part_t* text_part_append(text_part_t* parts, const char *begin, size_t length, int is_cdata) { text_part_t* newpart = (text_part_t*)malloc(sizeof(text_part_t)); - assert(newpart); + if (!newpart) { + return NULL; + } parts->next = text_part_init(newpart, begin, length, is_cdata); return newpart; } @@ -698,26 +708,30 @@ static text_part_t* get_text_parts(parse_ctx ctx, const char* tag, size_t tag_le find_char(ctx, '<', 0); if (ctx->pos >= ctx->end || *ctx->pos != '<') { PLIST_XML_ERR("EOF while looking for closing tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } q = ctx->pos; ctx->pos++; if (ctx->pos >= ctx->end) { PLIST_XML_ERR("EOF while parsing '%s'\n", p); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } if (*ctx->pos == '!') { ctx->pos++; if (ctx->pos >= ctx->end-1) { PLIST_XML_ERR("EOF while parsing <! special tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } if (*ctx->pos == '-' && *(ctx->pos+1) == '-') { if (last) { last = text_part_append(last, p, q-p, 0); + if (!last) { + ctx->err = PLIST_ERR_NO_MEM; + return NULL; + } } else if (parts) { last = text_part_init(parts, p, q-p, 0); } @@ -725,7 +739,7 @@ static text_part_t* get_text_parts(parse_ctx ctx, const char* tag, size_t tag_le find_str(ctx, "-->", 3, 0); if (ctx->pos > ctx->end-3 || strncmp(ctx->pos, "-->", 3) != 0) { PLIST_XML_ERR("EOF while looking for end of comment\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } ctx->pos += 3; @@ -733,13 +747,17 @@ static text_part_t* get_text_parts(parse_ctx ctx, const char* tag, size_t tag_le ctx->pos++; if (ctx->pos >= ctx->end - 8) { PLIST_XML_ERR("EOF while parsing <[ tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } if (strncmp(ctx->pos, "CDATA[", 6) == 0) { if (q-p > 0) { if (last) { last = text_part_append(last, p, q-p, 0); + if (!last) { + ctx->err = PLIST_ERR_NO_MEM; + return NULL; + } } else if (parts) { last = text_part_init(parts, p, q-p, 0); } @@ -749,12 +767,16 @@ static text_part_t* get_text_parts(parse_ctx ctx, const char* tag, size_t tag_le find_str(ctx, "]]>", 3, 0); if (ctx->pos > ctx->end-3 || strncmp(ctx->pos, "]]>", 3) != 0) { PLIST_XML_ERR("EOF while looking for end of CDATA block\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } q = ctx->pos; if (last) { last = text_part_append(last, p, q-p, 1); + if (!last) { + ctx->err = PLIST_ERR_NO_MEM; + return NULL; + } } else if (parts) { last = text_part_init(parts, p, q-p, 1); } @@ -763,14 +785,14 @@ static text_part_t* get_text_parts(parse_ctx ctx, const char* tag, size_t tag_le p = ctx->pos; find_next(ctx, " \r\n\t>", 5, 1); PLIST_XML_ERR("Invalid special tag <[%.*s> encountered inside <%s> tag\n", (int)(ctx->pos - p), p, tag); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } } else { p = ctx->pos; find_next(ctx, " \r\n\t>", 5, 1); PLIST_XML_ERR("Invalid special tag <!%.*s> encountered inside <%s> tag\n", (int)(ctx->pos - p), p, tag); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } } else if (*ctx->pos == '/') { @@ -779,32 +801,35 @@ static text_part_t* get_text_parts(parse_ctx ctx, const char* tag, size_t tag_le p = ctx->pos; find_next(ctx, " \r\n\t>", 5, 1); PLIST_XML_ERR("Invalid tag <%.*s> encountered inside <%s> tag\n", (int)(ctx->pos - p), p, tag); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } } while (1); ctx->pos++; if (ctx->pos >= ctx->end-tag_len || strncmp(ctx->pos, tag, tag_len) != 0) { PLIST_XML_ERR("EOF or end tag mismatch\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } ctx->pos+=tag_len; parse_skip_ws(ctx); if (ctx->pos >= ctx->end) { PLIST_XML_ERR("EOF while parsing closing tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } else if (*ctx->pos != '>') { PLIST_XML_ERR("Invalid closing tag; expected '>', found '%c'\n", *ctx->pos); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; return NULL; } ctx->pos++; - - if (q-p > 0) { + if (q > p) { if (last) { last = text_part_append(last, p, q-p, 0); + if (!last) { + ctx->err = PLIST_ERR_NO_MEM; + return NULL; + } } else if (parts) { last = text_part_init(parts, p, q-p, 0); } @@ -826,42 +851,50 @@ static int unescape_entities(char *str, size_t *length) PLIST_XML_ERR("Invalid entity sequence encountered (missing terminating ';')\n"); return -1; } - if (str+i >= entp+1) { - int entlen = str+i - entp; - int bytelen = 1; - if (!strncmp(entp, "amp", 3)) { + size_t entlen = (size_t)(str+i - entp); + if (entlen > 0) { + if (entlen > 256) { + PLIST_XML_ERR("Rejecting absurdly large entity at &%.*s...\n", 8, entp); + return -1; + } + size_t bytelen = 1; + if (entlen == 3 && memcmp(entp, "amp", 3) == 0) { /* the '&' is already there */ - } else if (!strncmp(entp, "apos", 4)) { + } else if (entlen == 4 && memcmp(entp, "apos", 4) == 0) { *(entp-1) = '\''; - } else if (!strncmp(entp, "quot", 4)) { + } else if (entlen == 4 && memcmp(entp, "quot", 4) == 0) { *(entp-1) = '"'; - } else if (!strncmp(entp, "lt", 2)) { + } else if (entlen == 2 && memcmp(entp, "lt", 2) == 0) { *(entp-1) = '<'; - } else if (!strncmp(entp, "gt", 2)) { + } else if (entlen == 2 && memcmp(entp, "gt", 2) == 0) { *(entp-1) = '>'; } else if (*entp == '#') { /* numerical character reference */ uint64_t val = 0; char* ep = NULL; if (entlen > 8) { - PLIST_XML_ERR("Invalid numerical character reference encountered, sequence too long: &%.*s;\n", entlen, entp); + PLIST_XML_ERR("Invalid numerical character reference encountered, sequence too long: &%.*s;\n", (int)entlen, entp); return -1; } - if (*(entp+1) == 'x' || *(entp+1) == 'X') { + if (entlen >= 2 && (entp[1] == 'x' || entp[1] == 'X')) { if (entlen < 3) { - PLIST_XML_ERR("Invalid numerical character reference encountered, sequence too short: &%.*s;\n", entlen, entp); + PLIST_XML_ERR("Invalid numerical character reference encountered, sequence too short: &%.*s;\n", (int)entlen, entp); return -1; } val = strtoull(entp+2, &ep, 16); } else { if (entlen < 2) { - PLIST_XML_ERR("Invalid numerical character reference encountered, sequence too short: &%.*s;\n", entlen, entp); + PLIST_XML_ERR("Invalid numerical character reference encountered, sequence too short: &%.*s;\n", (int)entlen, entp); return -1; } val = strtoull(entp+1, &ep, 10); } - if (val == 0 || val > 0x10FFFF || ep-entp != entlen) { - PLIST_XML_ERR("Invalid numerical character reference found: &%.*s;\n", entlen, entp); + if (val == 0 || val > 0x10FFFF || (size_t)(ep-entp) != entlen) { + PLIST_XML_ERR("Invalid numerical character reference found: &%.*s;\n", (int)entlen, entp); + return -1; + } + if (val >= 0xD800 && val <= 0xDFFF) { + PLIST_XML_ERR("Invalid numerical character reference found (surrogate): &%.*s;\n", (int)entlen, entp); return -1; } /* convert to UTF8 */ @@ -891,12 +924,18 @@ static int unescape_entities(char *str, size_t *length) *(entp-1) = (char)(val & 0x7F); } } else { - PLIST_XML_ERR("Invalid entity encountered: &%.*s;\n", entlen, entp); + PLIST_XML_ERR("Invalid entity encountered: &%.*s;\n", (int)entlen, entp); return -1; } - memmove(entp, str+i+1, len - i); - i -= entlen+1 - bytelen; - len -= entlen+2 - bytelen; + memmove(entp, str+i+1, len - i); /* include '\0' */ + size_t dec = entlen + 1 - bytelen; + size_t shrink = entlen + 2 - bytelen; + if (i < dec || len < shrink) { + PLIST_XML_ERR("Internal error: length underflow?!\n"); + return -1; + } + i -= dec; + len -= shrink; continue; } else { PLIST_XML_ERR("Invalid empty entity sequence &;\n"); @@ -909,36 +948,42 @@ static int unescape_entities(char *str, size_t *length) return 0; } -static char* text_parts_get_content(text_part_t *tp, int unesc_entities, size_t *length, int *requires_free) +static char* text_parts_get_content(text_part_t *tp, int unesc_entities, int trim_ws, size_t *length, int *requires_free, plist_err_t *err) { char *str = NULL; size_t total_length = 0; + if (err) *err = PLIST_ERR_PARSE; if (!tp) { return NULL; } char *p; - if (requires_free && !tp->next) { - if (tp->is_cdata || !unesc_entities) { - *requires_free = 0; - if (length) { - *length = tp->length; - } - return (char*)tp->begin; + if (requires_free && !tp->next && !unesc_entities && !trim_ws) { + *requires_free = 0; + if (length) { + *length = tp->length; } + return (char*)tp->begin; } text_part_t *tmp = tp; while (tp && tp->begin) { + if (tp->length > SIZE_MAX - 1 - total_length) { + if (err) *err = PLIST_ERR_NO_MEM; + return NULL; + } total_length += tp->length; tp = (text_part_t*)tp->next; } str = (char*)malloc(total_length + 1); - assert(str); + if (!str) { + if (err) *err = PLIST_ERR_NO_MEM; + return NULL; + } p = str; tp = tmp; while (tp && tp->begin) { size_t len = tp->length; - strncpy(p, tp->begin, len); + memcpy(p, tp->begin, len); p[len] = '\0'; if (!tp->is_cdata && unesc_entities) { if (unescape_entities(p, &len) < 0) { @@ -950,6 +995,21 @@ static char* text_parts_get_content(text_part_t *tp, int unesc_entities, size_t tp = (text_part_t*)tp->next; } *p = '\0'; + if (trim_ws) { + char* start = str; + char* end = p; + while (start < end && is_xml_ws((unsigned char)start[0])) start++; + while (end > start && is_xml_ws((unsigned char)end[-1])) end--; + if (start != str) { + size_t newlen = (size_t)(end - start); + memmove(str, start, newlen); + str[newlen] = '\0'; + p = str + newlen; + } else { + *end = '\0'; + p = end; + } + } if (length) { *length = p - str; } @@ -961,18 +1021,19 @@ static char* text_parts_get_content(text_part_t *tp, int unesc_entities, size_t static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) { - char *tag = NULL; + plist_err_t content_err = PLIST_ERR_PARSE; + char tag[16] = { 0 }; char *keyname = NULL; plist_t subnode = NULL; const char *p = NULL; plist_t parent = NULL; - int has_content = 0; struct node_path_item { const char *type; - void *prev; + struct node_path_item *prev; }; struct node_path_item* node_path = NULL; + int depth = 0; while (ctx->pos < ctx->end && !ctx->err) { parse_skip_ws(ctx); @@ -983,13 +1044,13 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) p = ctx->pos; find_next(ctx, " \t\r\n", 4, 0); PLIST_XML_ERR("Expected: opening tag, found: %.*s\n", (int)(ctx->pos - p), p); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } ctx->pos++; if (ctx->pos >= ctx->end) { PLIST_XML_ERR("EOF while parsing tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } @@ -997,12 +1058,12 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) find_str(ctx, "?>", 2, 1); if (ctx->pos > ctx->end-2) { PLIST_XML_ERR("EOF while looking for <? tag closing marker\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (strncmp(ctx->pos, "?>", 2) != 0) { PLIST_XML_ERR("Couldn't find <? tag closing marker\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } ctx->pos += 2; @@ -1011,10 +1072,10 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) /* comment or DTD */ if (((ctx->end - ctx->pos) > 3) && !strncmp(ctx->pos, "!--", 3)) { ctx->pos += 3; - find_str(ctx,"-->", 3, 0); + find_str(ctx, "-->", 3, 0); if (ctx->pos > ctx->end-3 || strncmp(ctx->pos, "-->", 3) != 0) { PLIST_XML_ERR("Couldn't find end of comment\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } ctx->pos+=3; @@ -1025,7 +1086,7 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) find_next(ctx, " \t\r\n[>", 6, 1); if (ctx->pos >= ctx->end) { PLIST_XML_ERR("EOF while parsing !DOCTYPE\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (*ctx->pos == '[') { @@ -1043,7 +1104,7 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) find_str(ctx, "]>", 2, 1); if (ctx->pos > ctx->end-2 || strncmp(ctx->pos, "]>", 2) != 0) { PLIST_XML_ERR("Couldn't find end of DOCTYPE\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } ctx->pos += 2; @@ -1052,7 +1113,7 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) p = ctx->pos; find_next(ctx, " \r\n\t>", 5, 1); PLIST_XML_ERR("Invalid or incomplete special tag <%.*s> encountered\n", (int)(ctx->pos - p), p); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } continue; @@ -1060,55 +1121,57 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) int is_empty = 0; int closing_tag = 0; p = ctx->pos; - find_next(ctx," \r\n\t<>", 6, 0); + find_next(ctx, " \r\n\t<>", 6, 0); if (ctx->pos >= ctx->end) { PLIST_XML_ERR("Unexpected EOF while parsing XML\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; + goto err_out; + } + size_t taglen = ctx->pos - p; + if (taglen >= sizeof(tag)) { + PLIST_XML_ERR("Unexpected tag <%.*s> encountered\n", (int)taglen, p); + ctx->pos = ctx->end; + ctx->err = PLIST_ERR_PARSE; goto err_out; } - int taglen = ctx->pos - p; - tag = (char*)malloc(taglen + 1); - strncpy(tag, p, taglen); + memcpy(tag, p, taglen); tag[taglen] = '\0'; if (*ctx->pos != '>') { find_next(ctx, "<>", 2, 1); } if (ctx->pos >= ctx->end) { PLIST_XML_ERR("Unexpected EOF while parsing XML\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (*ctx->pos != '>') { PLIST_XML_ERR("Missing '>' for tag <%s\n", tag); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (*(ctx->pos-1) == '/') { - int idx = ctx->pos - p - 1; + size_t idx = ctx->pos - p - 1; if (idx < taglen) tag[idx] = '\0'; is_empty = 1; } ctx->pos++; if (!strcmp(tag, "plist")) { - free(tag); - tag = NULL; - has_content = 0; - if (!node_path && *plist) { - /* we don't allow another top-level <plist> */ - break; + PLIST_XML_ERR("Multiple top-level <plist> elements encountered\n"); + ctx->err = PLIST_ERR_PARSE; + goto err_out; } if (is_empty) { PLIST_XML_ERR("Empty plist tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } struct node_path_item *path_item = (struct node_path_item*)malloc(sizeof(struct node_path_item)); if (!path_item) { PLIST_XML_ERR("out of memory when allocating node path item\n"); - ctx->err++; + ctx->err = PLIST_ERR_NO_MEM; goto err_out; } path_item->type = "plist"; @@ -1117,34 +1180,43 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) continue; } else if (!strcmp(tag, "/plist")) { - if (!has_content) { + if (!*plist) { PLIST_XML_ERR("encountered empty plist tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (!node_path) { PLIST_XML_ERR("node path is empty while trying to match closing tag with opening tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (strcmp(node_path->type, tag+1) != 0) { PLIST_XML_ERR("mismatching closing tag <%s> found for opening tag <%s>\n", tag, node_path->type); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } struct node_path_item *path_item = node_path; node_path = (struct node_path_item*)node_path->prev; free(path_item); - - free(tag); - tag = NULL; - continue; } - + if (tag[0] == '/') { + closing_tag = 1; + goto handle_closing; + } plist_data_t data = plist_new_plist_data(); + if (!data) { + PLIST_XML_ERR("failed to allocate plist data\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } subnode = plist_new_node(data); - has_content = 1; + if (!subnode) { + plist_free_data(data); + PLIST_XML_ERR("failed to create node\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } if (!strcmp(tag, XPLIST_DICT)) { data->type = PLIST_DICT; @@ -1157,16 +1229,19 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) if (!tp) { PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = PLIST_ERR_PARSE; + } goto err_out; } if (tp->begin) { - int requires_free = 0; - char *str_content = text_parts_get_content(tp, 0, NULL, &requires_free); + char *str_content = text_parts_get_content(tp, 0, 1, NULL, NULL, &content_err); if (!str_content) { PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = content_err; + } goto err_out; } char *str = str_content; @@ -1177,7 +1252,30 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) } str++; } - data->intval = strtoull(str, NULL, 0); + errno = 0; + char* endp = NULL; + data->intval = strtoull(str, &endp, 0); + if (errno == ERANGE) { + PLIST_XML_ERR("Integer overflow detected while parsing '%.20s'\n", str_content); + text_parts_free((text_part_t*)first_part.next); + ctx->err = PLIST_ERR_PARSE; + free(str_content); + goto err_out; + } + if (endp == str || *endp != '\0') { + PLIST_XML_ERR("Invalid characters while parsing integer value '%.20s'\n", str_content); + text_parts_free((text_part_t*)first_part.next); + ctx->err = PLIST_ERR_PARSE; + free(str_content); + goto err_out; + } + if (is_negative && data->intval > ((uint64_t)INT64_MAX + 1)) { + PLIST_XML_ERR("Signed integer value out of range while parsing '%.20s'\n", str_content); + text_parts_free((text_part_t*)first_part.next); + ctx->err = PLIST_ERR_PARSE; + free(str_content); + goto err_out; + } if (is_negative || (data->intval <= INT64_MAX)) { uint64_t v = data->intval; if (is_negative) { @@ -1188,17 +1286,16 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) } else { data->length = 16; } - if (requires_free) { - free(str_content); - } + free(str_content); } else { is_empty = 1; } - text_parts_free((text_part_t*)tp->next); + text_parts_free((text_part_t*)first_part.next); } if (is_empty) { - data->intval = 0; - data->length = 8; + PLIST_XML_ERR("Encountered empty " XPLIST_INT " tag\n"); + ctx->err = PLIST_ERR_PARSE; + goto err_out; } data->type = PLIST_INT; } else if (!strcmp(tag, XPLIST_REAL)) { @@ -1208,24 +1305,56 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) if (!tp) { PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = PLIST_ERR_PARSE; + } goto err_out; } if (tp->begin) { - int requires_free = 0; - char *str_content = text_parts_get_content(tp, 0, NULL, &requires_free); + char *str_content = text_parts_get_content(tp, 0, 1, NULL, NULL, &content_err); if (!str_content) { PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = content_err; + } goto err_out; } - data->realval = atof(str_content); - if (requires_free) { + errno = 0; + char *endp = NULL; + data->realval = strtod(str_content, &endp); + if (errno == ERANGE) { + PLIST_XML_ERR("Invalid range while parsing value for '%s' node\n", tag); + text_parts_free((text_part_t*)first_part.next); + ctx->err = PLIST_ERR_PARSE; + free(str_content); + goto err_out; + } + if (endp == str_content || *endp != '\0') { + PLIST_XML_ERR("Could not parse value for '%s' node\n", tag); + text_parts_free((text_part_t*)first_part.next); + ctx->err = PLIST_ERR_PARSE; + free(str_content); + goto err_out; + + } + if (!isfinite(data->realval)) { + PLIST_XML_ERR("Invalid real value while parsing '%.20s'\n", str_content); + text_parts_free((text_part_t*)first_part.next); + ctx->err = PLIST_ERR_PARSE; free(str_content); + goto err_out; } + free(str_content); + } else { + is_empty = 1; } - text_parts_free((text_part_t*)tp->next); + text_parts_free((text_part_t*)first_part.next); + } + if (is_empty) { + PLIST_XML_ERR("Encountered empty " XPLIST_REAL " tag\n"); + ctx->err = PLIST_ERR_PARSE; + goto err_out; } data->type = PLIST_REAL; data->length = 8; @@ -1252,20 +1381,22 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) if (!tp) { PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = PLIST_ERR_PARSE; + } goto err_out; } - str = text_parts_get_content(tp, 1, &length, NULL); + str = text_parts_get_content(tp, 1, 0, &length, NULL, &content_err); text_parts_free((text_part_t*)first_part.next); if (!str) { PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = content_err; + } goto err_out; } if (!strcmp(tag, "key") && !keyname && parent && (plist_get_node_type(parent) == PLIST_DICT)) { keyname = str; - free(tag); - tag = NULL; plist_free(subnode); subnode = NULL; continue; @@ -1274,6 +1405,17 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) data->length = length; } } else { + if (!strcmp(tag, XPLIST_KEY) && !keyname && parent && (plist_get_node_type(parent) == PLIST_DICT)) { + keyname = strdup(""); + if (!keyname) { + PLIST_XML_ERR("failed to allocate key name\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } + plist_free(subnode); + subnode = NULL; + continue; + } data->strval = strdup(""); data->length = 0; } @@ -1285,21 +1427,31 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) if (!tp) { PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = PLIST_ERR_PARSE; + } goto err_out; } if (tp->begin) { int requires_free = 0; - char *str_content = text_parts_get_content(tp, 0, NULL, &requires_free); + char *str_content = text_parts_get_content(tp, 0, 0, NULL, &requires_free, &content_err); if (!str_content) { PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = content_err; + } goto err_out; } size_t size = tp->length; if (size > 0) { data->buff = base64decode(str_content, &size); + if (!data->buff) { + text_parts_free((text_part_t*)first_part.next); + PLIST_XML_ERR("failed to decode base64 stream\n"); + ctx->err = PLIST_ERR_NO_MEM; + goto err_out; + } data->length = size; } @@ -1307,7 +1459,7 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) free(str_content); } } - text_parts_free((text_part_t*)tp->next); + text_parts_free((text_part_t*)first_part.next); } data->type = PLIST_DATA; } else if (!strcmp(tag, XPLIST_DATE)) { @@ -1317,116 +1469,160 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) if (!tp) { PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = PLIST_ERR_PARSE; + } goto err_out; } Time64_T timev = 0; if (tp->begin) { - int requires_free = 0; - size_t length = 0; - char *str_content = text_parts_get_content(tp, 0, &length, &requires_free); + char *str_content = text_parts_get_content(tp, 0, 1, NULL, NULL, &content_err); if (!str_content) { PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); text_parts_free((text_part_t*)first_part.next); - ctx->err++; + if (PLIST_ERR_SUCCESS == ctx->err) { + ctx->err = content_err; + } goto err_out; } - - if ((length >= 11) && (length < 32)) { - /* we need to copy here and 0-terminate because sscanf will read the entire string (whole rest of XML data) which can be huge */ - char strval[32]; - struct TM btime; - strncpy(strval, str_content, length); - strval[tp->length] = '\0'; - parse_date(strval, &btime); - timev = timegm64(&btime); - } else { - PLIST_XML_ERR("Invalid text content in date node\n"); - } - if (requires_free) { + struct TM btime; + if (parse_date(str_content, &btime) < 0) { + PLIST_XML_ERR("Failed to parse date node\n"); + text_parts_free((text_part_t*)first_part.next); + ctx->err = PLIST_ERR_PARSE; free(str_content); + goto err_out; } + timev = timegm64(&btime); + free(str_content); + } else { + is_empty = 1; } - text_parts_free((text_part_t*)tp->next); + text_parts_free((text_part_t*)first_part.next); data->realval = (double)(timev - MAC_EPOCH); } + if (is_empty) { + PLIST_XML_ERR("Encountered empty " XPLIST_DATE " tag\n"); + ctx->err = PLIST_ERR_PARSE; + goto err_out; + } data->length = sizeof(double); data->type = PLIST_DATE; - } else if (tag[0] == '/') { - closing_tag = 1; } else { PLIST_XML_ERR("Unexpected tag <%s%s> encountered\n", tag, (is_empty) ? "/" : ""); ctx->pos = ctx->end; - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (subnode && !closing_tag) { + plist_t attached = subnode; if (!*plist) { - /* first node, make this node the parent node */ + /* first value node inside <plist> */ *plist = subnode; - if (data->type != PLIST_DICT && data->type != PLIST_ARRAY) { - /* if the first node is not a structered node, we're done */ - subnode = NULL; - goto err_out; + /* the tree owns the node now; err_out must not free it again */ + subnode = NULL; + + if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) { + parent = attached; + } else { + /* scalar root: keep parsing until </plist> */ + parent = NULL; } - parent = subnode; } else if (parent) { switch (plist_get_node_type(parent)) { case PLIST_DICT: if (!keyname) { PLIST_XML_ERR("missing key name while adding dict item\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } - plist_dict_set_item(parent, keyname, subnode); + if (plist_dict_set_item(parent, keyname, subnode) != PLIST_ERR_SUCCESS) { + PLIST_XML_ERR("failed to add item to dict\n"); + ctx->err = PLIST_ERR_PARSE; + goto err_out; + } + subnode = NULL; break; case PLIST_ARRAY: - plist_array_append_item(parent, subnode); + if (plist_array_append_item(parent, subnode) != PLIST_ERR_SUCCESS) { + PLIST_XML_ERR("failed to add item to array\n"); + ctx->err = PLIST_ERR_PARSE; + goto err_out; + } + subnode = NULL; break; default: /* should not happen */ PLIST_XML_ERR("parent is not a structured node\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } + } else { + /* We already produced root, and we're not inside a container */ + PLIST_XML_ERR("Unexpected tag <%s> found while </plist> is expected\n", tag); + ctx->err = PLIST_ERR_PARSE; + goto err_out; } if (!is_empty && (data->type == PLIST_DICT || data->type == PLIST_ARRAY)) { + if (depth >= PLIST_MAX_NESTING_DEPTH) { + PLIST_XML_ERR("maximum nesting depth (%u) exceeded\n", (unsigned)PLIST_MAX_NESTING_DEPTH); + ctx->err = PLIST_ERR_MAX_NESTING; + goto err_out; + } struct node_path_item *path_item = (struct node_path_item*)malloc(sizeof(struct node_path_item)); if (!path_item) { PLIST_XML_ERR("out of memory when allocating node path item\n"); - ctx->err++; + ctx->err = PLIST_ERR_NO_MEM; goto err_out; } path_item->type = (data->type == PLIST_DICT) ? XPLIST_DICT : XPLIST_ARRAY; path_item->prev = node_path; node_path = path_item; - parent = subnode; + depth++; + parent = attached; + } else { + /* If we inserted a child scalar into a container, nothing to push. */ } - subnode = NULL; - } else if (closing_tag) { + } +handle_closing: + if (closing_tag) { if (!node_path) { PLIST_XML_ERR("node path is empty while trying to match closing tag with opening tag\n"); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } if (strcmp(node_path->type, tag+1) != 0) { PLIST_XML_ERR("unexpected %s found (for opening %s)\n", tag, node_path->type); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; goto err_out; } + + /* When closing a dictionary, convert a single-entry + { "CF$UID" : <integer> } dictionary into a PLIST_UID node. + Perform the conversion before moving to the parent node. */ + if (!strcmp(node_path->type, XPLIST_DICT) && parent && plist_get_node_type(parent) == PLIST_DICT) { + if (plist_dict_get_size(parent) == 1) { + plist_t uid = plist_dict_get_item(parent, "CF$UID"); + if (uid) { + uint64_t val = 0; + if (plist_get_node_type(uid) != PLIST_INT) { + ctx->err = PLIST_ERR_PARSE; + PLIST_XML_ERR("Invalid node type for CF$UID dict entry (must be PLIST_INT)\n"); + goto err_out; + } + plist_get_uint_val(uid, &val); + plist_set_uid_val(parent, val); + } + } + } + + if (depth > 0) depth--; struct node_path_item *path_item = node_path; node_path = (struct node_path_item*)node_path->prev; free(path_item); - - parent = ((node_t)parent)->parent; - if (!parent) { - goto err_out; - } + parent = (parent) ? ((node_t)parent)->parent : NULL; } - - free(tag); - tag = NULL; free(keyname); keyname = NULL; plist_free(subnode); @@ -1436,11 +1632,10 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist) if (node_path) { PLIST_XML_ERR("EOF encountered while </%s> was expected\n", node_path->type); - ctx->err++; + ctx->err = PLIST_ERR_PARSE; } err_out: - free(tag); free(keyname); plist_free(subnode); @@ -1451,16 +1646,16 @@ err_out: free(path_item); } - if (ctx->err) { + if (ctx->err != PLIST_ERR_SUCCESS) { plist_free(*plist); *plist = NULL; - return PLIST_ERR_PARSE; + return ctx->err; } /* check if we have a UID "dict" so we can replace it with a proper UID node */ if (PLIST_IS_DICT(*plist) && plist_dict_get_size(*plist) == 1) { plist_t value = plist_dict_get_item(*plist, "CF$UID"); - if (PLIST_IS_UINT(value)) { + if (PLIST_IS_INT(value)) { uint64_t u64val = 0; plist_get_uint_val(value, &u64val); plist_free(*plist); @@ -1481,7 +1676,7 @@ plist_err_t plist_from_xml(const char *plist_xml, uint32_t length, plist_t * pli return PLIST_ERR_INVALID_ARG; } - struct _parse_ctx ctx = { plist_xml, plist_xml + length, 0 }; + struct _parse_ctx ctx = { plist_xml, plist_xml + length, PLIST_ERR_SUCCESS }; return node_from_xml(&ctx, plist); } |
