mirror of
https://github.com/nlohmann/json.git
synced 2026-09-08 17:27:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efece7665a | ||
|
|
539d934088 | ||
|
|
6ea776c201 |
@@ -199,11 +199,21 @@ class binary_reader
|
|||||||
*/
|
*/
|
||||||
struct container_frame
|
struct container_frame
|
||||||
{
|
{
|
||||||
container_frame(const std::size_t remaining_, const bool is_object_) noexcept
|
container_frame(const std::size_t remaining_, const bool is_object_,
|
||||||
: remaining(remaining_), is_object(is_object_) {}
|
const char_int_type type_marker_ = 0) noexcept
|
||||||
|
: remaining(remaining_), type_marker(type_marker_), is_object(is_object_) {}
|
||||||
|
|
||||||
/// number of elements that have not been read yet
|
/// number of elements that have not been read yet, or npos when the
|
||||||
|
/// container is not sized and ends at a marker instead
|
||||||
std::size_t remaining;
|
std::size_t remaining;
|
||||||
|
/// BSON: value of chars_read before this document's size prefix, which
|
||||||
|
/// check_bson_document_size() needs once the document has been read
|
||||||
|
std::size_t start_position = 0;
|
||||||
|
/// UBJSON/BJData: the type marker of an optimized container, so that
|
||||||
|
/// its elements are read without one of their own; 0 otherwise
|
||||||
|
char_int_type type_marker;
|
||||||
|
/// BSON: the size this document declares, in bytes
|
||||||
|
std::int32_t declared_size = 0;
|
||||||
/// whether to close this container with end_object() or end_array()
|
/// whether to close this container with end_object() or end_array()
|
||||||
bool is_object;
|
bool is_object;
|
||||||
};
|
};
|
||||||
@@ -220,27 +230,28 @@ class binary_reader
|
|||||||
|
|
||||||
@return whether the SAX parser accepted the start event
|
@return whether the SAX parser accepted the start event
|
||||||
*/
|
*/
|
||||||
bool enter_container(const bool is_object, const std::size_t len)
|
bool enter_container(const bool is_object, const std::size_t len,
|
||||||
|
const char_int_type type_marker = 0)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->start_object(len) : !sax->start_array(len)))
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->start_object(len) : !sax->start_array(len)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
container_stack.emplace_back(len, is_object);
|
container_stack.emplace_back(len, is_object, type_marker);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @copydoc enter_container
|
/// @copydoc enter_container
|
||||||
bool enter_array(const std::size_t len)
|
bool enter_array(const std::size_t len, const char_int_type type_marker = 0)
|
||||||
{
|
{
|
||||||
return enter_container(/*is_object*/false, len);
|
return enter_container(/*is_object*/false, len, type_marker);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @copydoc enter_container
|
/// @copydoc enter_container
|
||||||
bool enter_object(const std::size_t len)
|
bool enter_object(const std::size_t len, const char_int_type type_marker = 0)
|
||||||
{
|
{
|
||||||
return enter_container(/*is_object*/true, len);
|
return enter_container(/*is_object*/true, len, type_marker);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
@@ -277,8 +288,10 @@ class binary_reader
|
|||||||
@brief Reads in a BSON-object and passes it to the SAX-parser.
|
@brief Reads in a BSON-object and passes it to the SAX-parser.
|
||||||
@return whether a valid BSON-value was passed to the SAX parser
|
@return whether a valid BSON-value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
bool parse_bson_internal()
|
bool open_bson_document(const bool is_object)
|
||||||
{
|
{
|
||||||
|
// recorded before the size prefix is read, because
|
||||||
|
// check_bson_document_size() measures the document from here
|
||||||
const std::size_t document_start = chars_read;
|
const std::size_t document_start = chars_read;
|
||||||
std::int32_t document_size{};
|
std::int32_t document_size{};
|
||||||
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
||||||
@@ -286,22 +299,89 @@ class binary_reader
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
|
if (JSON_HEDLEY_UNLIKELY(!enter_container(is_object, detail::unknown_size())))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
|
container_frame& frame = container_stack.back();
|
||||||
|
frame.start_position = document_start;
|
||||||
|
frame.declared_size = document_size;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief read a BSON document and everything nested inside it
|
||||||
|
|
||||||
|
Reads elements until the document that was begun here is complete,
|
||||||
|
resuming the enclosing document each time an embedded one ends, so that
|
||||||
|
the nesting depth of the input costs heap rather than native stack
|
||||||
|
(see #5104).
|
||||||
|
|
||||||
|
@return whether reading the document succeeded
|
||||||
|
*/
|
||||||
|
bool parse_bson_internal()
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!open_bson_document(/*is_object*/true)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!check_bson_document_size(document_start, document_size)))
|
// the key currently being read; hoisted out of the loop so that its
|
||||||
{
|
// capacity is reused across elements and across nesting levels
|
||||||
return false;
|
string_t key;
|
||||||
}
|
|
||||||
|
|
||||||
return sax->end_object();
|
while (true)
|
||||||
|
{
|
||||||
|
const auto element_type = get();
|
||||||
|
|
||||||
|
if (element_type == 0) // end of the innermost document
|
||||||
|
{
|
||||||
|
const container_frame& top = container_stack.back();
|
||||||
|
const bool is_object = top.is_object;
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!check_bson_document_size(top.start_position, top.declared_size)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
container_stack.pop_back();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// the document begun here is complete once it is not inside one
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::size_t element_type_parse_position = chars_read;
|
||||||
|
key.clear();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// an array's elements are named "0", "1", ... in the wire format,
|
||||||
|
// and those names are not passed on
|
||||||
|
if (container_stack.back().is_object && !sax->key(key))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -413,12 +493,12 @@ class binary_reader
|
|||||||
|
|
||||||
case 0x03: // object
|
case 0x03: // object
|
||||||
{
|
{
|
||||||
return parse_bson_internal();
|
return open_bson_document(/*is_object*/true);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x04: // array
|
case 0x04: // array
|
||||||
{
|
{
|
||||||
return parse_bson_array();
|
return open_bson_document(/*is_object*/false);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x05: // binary
|
case 0x05: // binary
|
||||||
@@ -468,82 +548,7 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Read a BSON element list (as specified in the BSON-spec)
|
|
||||||
|
|
||||||
The same binary layout is used for objects and arrays, hence it must be
|
|
||||||
indicated with the argument @a is_array which one is expected
|
|
||||||
(true --> array, false --> object).
|
|
||||||
|
|
||||||
@param[in] is_array Determines if the element list being read is to be
|
|
||||||
treated as an object (@a is_array == false), or as an
|
|
||||||
array (@a is_array == true).
|
|
||||||
@return whether a valid BSON-object/array was passed to the SAX parser
|
|
||||||
*/
|
|
||||||
bool parse_bson_element_list(const bool is_array)
|
|
||||||
{
|
|
||||||
string_t key;
|
|
||||||
|
|
||||||
while (auto element_type = get())
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::size_t element_type_parse_position = chars_read;
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_array && !sax->key(key))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get_bson_cstr only appends
|
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Reads an array from the BSON input and passes it to the SAX-parser.
|
|
||||||
@return whether a valid BSON-array was passed to the SAX parser
|
|
||||||
*/
|
|
||||||
bool parse_bson_array()
|
|
||||||
{
|
|
||||||
const std::size_t document_start = chars_read;
|
|
||||||
std::int32_t document_size{};
|
|
||||||
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!check_bson_document_size(document_start, document_size)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sax->end_array();
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
// CBOR //
|
// CBOR //
|
||||||
@@ -575,9 +580,12 @@ class binary_reader
|
|||||||
|
|
||||||
@return whether a valid CBOR value was passed to the SAX parser
|
@return whether a valid CBOR value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
bool parse_cbor_internal(const bool get_char,
|
bool parse_cbor_value(const bool get_char,
|
||||||
const cbor_tag_handler_t tag_handler)
|
const cbor_tag_handler_t tag_handler,
|
||||||
|
bool& tag_pending)
|
||||||
{
|
{
|
||||||
|
tag_pending = false;
|
||||||
|
|
||||||
switch (get_char ? get() : current)
|
switch (get_char ? get() : current)
|
||||||
{
|
{
|
||||||
// EOF
|
// EOF
|
||||||
@@ -769,37 +777,36 @@ class binary_reader
|
|||||||
case 0x95:
|
case 0x95:
|
||||||
case 0x96:
|
case 0x96:
|
||||||
case 0x97:
|
case 0x97:
|
||||||
return get_cbor_array(
|
return enter_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
||||||
conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
|
|
||||||
|
|
||||||
case 0x98: // array (one-byte uint8_t for n follows)
|
case 0x98: // array (one-byte uint8_t for n follows)
|
||||||
{
|
{
|
||||||
std::uint8_t len{};
|
std::uint8_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
|
return get_number(input_format_t::cbor, len) && enter_array(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x99: // array (two-byte uint16_t for n follow)
|
case 0x99: // array (two-byte uint16_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
|
return get_number(input_format_t::cbor, len) && enter_array(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9A: // array (four-byte uint32_t for n follow)
|
case 0x9A: // array (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && enter_array(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9B: // array (eight-byte uint64_t for n follow)
|
case 0x9B: // array (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && enter_array(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9F: // array (indefinite length)
|
case 0x9F: // array (indefinite length)
|
||||||
return get_cbor_array(detail::unknown_size(), tag_handler);
|
return enter_array(detail::unknown_size());
|
||||||
|
|
||||||
// map (0x00..0x17 pairs of data items follow)
|
// map (0x00..0x17 pairs of data items follow)
|
||||||
case 0xA0:
|
case 0xA0:
|
||||||
@@ -826,36 +833,36 @@ class binary_reader
|
|||||||
case 0xB5:
|
case 0xB5:
|
||||||
case 0xB6:
|
case 0xB6:
|
||||||
case 0xB7:
|
case 0xB7:
|
||||||
return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
|
return enter_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
||||||
|
|
||||||
case 0xB8: // map (one-byte uint8_t for n follows)
|
case 0xB8: // map (one-byte uint8_t for n follows)
|
||||||
{
|
{
|
||||||
std::uint8_t len{};
|
std::uint8_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
|
return get_number(input_format_t::cbor, len) && enter_object(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xB9: // map (two-byte uint16_t for n follow)
|
case 0xB9: // map (two-byte uint16_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
|
return get_number(input_format_t::cbor, len) && enter_object(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBA: // map (four-byte uint32_t for n follow)
|
case 0xBA: // map (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && enter_object(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBB: // map (eight-byte uint64_t for n follow)
|
case 0xBB: // map (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && enter_object(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBF: // map (indefinite length)
|
case 0xBF: // map (indefinite length)
|
||||||
return get_cbor_object(detail::unknown_size(), tag_handler);
|
return enter_object(detail::unknown_size());
|
||||||
|
|
||||||
case 0xC0: // tagged item
|
case 0xC0: // tagged item
|
||||||
case 0xC1:
|
case 0xC1:
|
||||||
@@ -939,7 +946,10 @@ class binary_reader
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return parse_cbor_internal(true, tag_handler);
|
// the tagged value follows; it is read by the loop in
|
||||||
|
// parse_cbor_internal() rather than by recursing here
|
||||||
|
tag_pending = true;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case cbor_tag_handler_t::store:
|
case cbor_tag_handler_t::store:
|
||||||
@@ -989,7 +999,11 @@ class binary_reader
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return parse_cbor_internal(true, tag_handler);
|
{
|
||||||
|
// as above, the tagged value is read by the caller
|
||||||
|
tag_pending = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
get();
|
get();
|
||||||
return get_cbor_binary(b) && sax->binary(b);
|
return get_cbor_binary(b) && sax->binary(b);
|
||||||
@@ -1387,96 +1401,110 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@param[in] len the length of the array or detail::unknown_size() for an
|
@brief read a CBOR value and everything nested inside it
|
||||||
array of indefinite size
|
|
||||||
|
Reads values until the one that was begun here is complete, resuming the
|
||||||
|
enclosing container after each element, so that the nesting depth of the
|
||||||
|
input costs heap rather than native stack (see #5104).
|
||||||
|
|
||||||
|
@param[in] get_char whether a new character should be retrieved from the
|
||||||
|
input (true) or whether the last read character
|
||||||
|
@a current should be considered instead
|
||||||
@param[in] tag_handler how CBOR tags should be treated
|
@param[in] tag_handler how CBOR tags should be treated
|
||||||
@return whether array creation completed
|
|
||||||
|
@return whether reading the value succeeded
|
||||||
*/
|
*/
|
||||||
bool get_cbor_array(const std::size_t len,
|
bool parse_cbor_internal(const bool get_char,
|
||||||
const cbor_tag_handler_t tag_handler)
|
const cbor_tag_handler_t tag_handler)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
|
// whether the next value starts at a fresh byte or at the one already
|
||||||
{
|
// read into `current`
|
||||||
return false;
|
bool fetch = get_char;
|
||||||
}
|
|
||||||
|
|
||||||
if (len != detail::unknown_size())
|
// the key currently being read; hoisted out of the loop so that its
|
||||||
|
// capacity is reused across elements and across nesting levels
|
||||||
|
string_t key;
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
for (std::size_t i = 0; i < len; ++i)
|
if (!container_stack.empty())
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
// the reference is not held across parse_cbor_value() below,
|
||||||
|
// which can push onto the stack and reallocate it
|
||||||
|
container_frame& top = container_stack.back();
|
||||||
|
bool at_end = false;
|
||||||
|
|
||||||
|
if (top.remaining != npos)
|
||||||
{
|
{
|
||||||
return false;
|
// definite length: the container ends once its elements
|
||||||
|
// have been read
|
||||||
|
at_end = (top.remaining == 0);
|
||||||
|
if (!at_end)
|
||||||
|
{
|
||||||
|
// claim the element about to be read
|
||||||
|
--top.remaining;
|
||||||
|
if (top.is_object)
|
||||||
|
{
|
||||||
|
get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fetch = true;
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
while (get() != 0xFF)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
|
|
||||||
{
|
{
|
||||||
return false;
|
// indefinite length: the container ends at a break marker.
|
||||||
|
// Testing for it consumes a byte, which is the first byte
|
||||||
|
// of the next element when it is not one.
|
||||||
|
at_end = (get() == 0xFF);
|
||||||
|
fetch = top.is_object;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sax->end_array();
|
if (at_end)
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@param[in] len the length of the object or detail::unknown_size() for an
|
|
||||||
object of indefinite size
|
|
||||||
@param[in] tag_handler how CBOR tags should be treated
|
|
||||||
@return whether object creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_object(const std::size_t len,
|
|
||||||
const cbor_tag_handler_t tag_handler)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (len != 0)
|
|
||||||
{
|
|
||||||
string_t key;
|
|
||||||
if (len != detail::unknown_size())
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < len; ++i)
|
|
||||||
{
|
{
|
||||||
get();
|
const bool is_object = top.is_object;
|
||||||
|
container_stack.pop_back();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// the value begun here is complete once its container is
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (top.is_object)
|
||||||
|
{
|
||||||
|
key.clear();
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
fetch = true;
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
key.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
while (get() != 0xFF)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
// a tag is not a value of its own: read on until the tagged value
|
||||||
{
|
bool tag_pending = false;
|
||||||
return false;
|
do
|
||||||
}
|
{
|
||||||
key.clear();
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_value(fetch, tag_handler, tag_pending)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
fetch = true;
|
||||||
|
}
|
||||||
|
while (tag_pending);
|
||||||
|
|
||||||
|
// a value that opened a container left it on the stack; one that
|
||||||
|
// did not, and that was not inside a container, was the whole value
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sax->end_object();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////
|
/////////////
|
||||||
@@ -2146,7 +2174,103 @@ class binary_reader
|
|||||||
*/
|
*/
|
||||||
bool parse_ubjson_internal(const bool get_char = true)
|
bool parse_ubjson_internal(const bool get_char = true)
|
||||||
{
|
{
|
||||||
return get_ubjson_value(get_char ? get_ignore_noop() : current);
|
// the key currently being read; hoisted out of the loop so that its
|
||||||
|
// capacity is reused across elements and across nesting levels
|
||||||
|
string_t key;
|
||||||
|
|
||||||
|
// the type marker of the value to read next
|
||||||
|
char_int_type prefix = get_char ? get_ignore_noop() : current;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
const std::size_t depth = container_stack.size();
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(prefix)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the value begun here is complete once it is not inside anything
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a value was completed rather than a container opened; a
|
||||||
|
// container that ends at a marker needs the next byte to test
|
||||||
|
if (container_stack.size() == depth && container_stack.back().remaining == npos)
|
||||||
|
{
|
||||||
|
get_ignore_noop();
|
||||||
|
}
|
||||||
|
|
||||||
|
// advance to the next element, closing the containers that ended.
|
||||||
|
// The reference is not held across get_ubjson_value() above, which
|
||||||
|
// can push onto the stack and reallocate it.
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
container_frame& top = container_stack.back();
|
||||||
|
|
||||||
|
if (top.remaining != npos)
|
||||||
|
{
|
||||||
|
if (top.remaining != 0)
|
||||||
|
{
|
||||||
|
--top.remaining;
|
||||||
|
if (top.is_object)
|
||||||
|
{
|
||||||
|
key.clear();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// an optimized container gives its elements no marker
|
||||||
|
prefix = (top.type_marker != 0) ? top.type_marker : get_ignore_noop();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the end marker is compared against a literal rather than
|
||||||
|
// against a conditional expression, because char_int_type is
|
||||||
|
// unsigned for some input adapters and MSVC then reports the
|
||||||
|
// comparison as a signed/unsigned mismatch
|
||||||
|
else if (top.is_object ? (current != '}') : (current != ']'))
|
||||||
|
{
|
||||||
|
// a container that ends at a marker is never optimized, so
|
||||||
|
// every element carries its own marker; for an object the
|
||||||
|
// byte tested above is the first byte of the key
|
||||||
|
if (top.is_object)
|
||||||
|
{
|
||||||
|
key.clear();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
prefix = get_ignore_noop();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
prefix = current;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool is_object = top.is_object;
|
||||||
|
container_stack.pop_back();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// the container that just ended was an element of the one
|
||||||
|
// below it, which may need the next byte for its own test
|
||||||
|
if (container_stack.back().remaining == npos)
|
||||||
|
{
|
||||||
|
get_ignore_noop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -2918,53 +3042,22 @@ class binary_reader
|
|||||||
exception_message(input_format, "excessive array size", "size"), nullptr));
|
exception_message(input_format, "excessive array size", "size"), nullptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
|
if (JSON_HEDLEY_UNLIKELY(!enter_array(size_and_type.first, size_and_type.second)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size_and_type.second != 0)
|
if (size_and_type.second == 'N')
|
||||||
{
|
{
|
||||||
if (size_and_type.second != 'N')
|
// a no-op is not a value, so a container of them holds none;
|
||||||
{
|
// the declared size has already been passed to the SAX parser
|
||||||
for (std::size_t i = 0; i < size_and_type.first; ++i)
|
container_stack.back().remaining = 0;
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < size_and_type.first; ++i)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (current != ']')
|
return true;
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
get_ignore_noop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sax->end_array();
|
return enter_array(detail::unknown_size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -2986,68 +3079,12 @@ class binary_reader
|
|||||||
exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
|
exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t key;
|
|
||||||
if (size_and_type.first != npos)
|
if (size_and_type.first != npos)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
|
return enter_object(size_and_type.first, size_and_type.second);
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size_and_type.second != 0)
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < size_and_type.first; ++i)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < size_and_type.first; ++i)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (current != '}')
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
get_ignore_noop();
|
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sax->end_object();
|
return enter_object(detail::unknown_size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note, no reader for UBJSON binary types is implemented because they do
|
// Note, no reader for UBJSON binary types is implemented because they do
|
||||||
|
|||||||
+313
-276
@@ -10886,11 +10886,21 @@ class binary_reader
|
|||||||
*/
|
*/
|
||||||
struct container_frame
|
struct container_frame
|
||||||
{
|
{
|
||||||
container_frame(const std::size_t remaining_, const bool is_object_) noexcept
|
container_frame(const std::size_t remaining_, const bool is_object_,
|
||||||
: remaining(remaining_), is_object(is_object_) {}
|
const char_int_type type_marker_ = 0) noexcept
|
||||||
|
: remaining(remaining_), type_marker(type_marker_), is_object(is_object_) {}
|
||||||
|
|
||||||
/// number of elements that have not been read yet
|
/// number of elements that have not been read yet, or npos when the
|
||||||
|
/// container is not sized and ends at a marker instead
|
||||||
std::size_t remaining;
|
std::size_t remaining;
|
||||||
|
/// BSON: value of chars_read before this document's size prefix, which
|
||||||
|
/// check_bson_document_size() needs once the document has been read
|
||||||
|
std::size_t start_position = 0;
|
||||||
|
/// UBJSON/BJData: the type marker of an optimized container, so that
|
||||||
|
/// its elements are read without one of their own; 0 otherwise
|
||||||
|
char_int_type type_marker;
|
||||||
|
/// BSON: the size this document declares, in bytes
|
||||||
|
std::int32_t declared_size = 0;
|
||||||
/// whether to close this container with end_object() or end_array()
|
/// whether to close this container with end_object() or end_array()
|
||||||
bool is_object;
|
bool is_object;
|
||||||
};
|
};
|
||||||
@@ -10907,27 +10917,28 @@ class binary_reader
|
|||||||
|
|
||||||
@return whether the SAX parser accepted the start event
|
@return whether the SAX parser accepted the start event
|
||||||
*/
|
*/
|
||||||
bool enter_container(const bool is_object, const std::size_t len)
|
bool enter_container(const bool is_object, const std::size_t len,
|
||||||
|
const char_int_type type_marker = 0)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->start_object(len) : !sax->start_array(len)))
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->start_object(len) : !sax->start_array(len)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
container_stack.emplace_back(len, is_object);
|
container_stack.emplace_back(len, is_object, type_marker);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @copydoc enter_container
|
/// @copydoc enter_container
|
||||||
bool enter_array(const std::size_t len)
|
bool enter_array(const std::size_t len, const char_int_type type_marker = 0)
|
||||||
{
|
{
|
||||||
return enter_container(/*is_object*/false, len);
|
return enter_container(/*is_object*/false, len, type_marker);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @copydoc enter_container
|
/// @copydoc enter_container
|
||||||
bool enter_object(const std::size_t len)
|
bool enter_object(const std::size_t len, const char_int_type type_marker = 0)
|
||||||
{
|
{
|
||||||
return enter_container(/*is_object*/true, len);
|
return enter_container(/*is_object*/true, len, type_marker);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
@@ -10964,8 +10975,10 @@ class binary_reader
|
|||||||
@brief Reads in a BSON-object and passes it to the SAX-parser.
|
@brief Reads in a BSON-object and passes it to the SAX-parser.
|
||||||
@return whether a valid BSON-value was passed to the SAX parser
|
@return whether a valid BSON-value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
bool parse_bson_internal()
|
bool open_bson_document(const bool is_object)
|
||||||
{
|
{
|
||||||
|
// recorded before the size prefix is read, because
|
||||||
|
// check_bson_document_size() measures the document from here
|
||||||
const std::size_t document_start = chars_read;
|
const std::size_t document_start = chars_read;
|
||||||
std::int32_t document_size{};
|
std::int32_t document_size{};
|
||||||
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
||||||
@@ -10973,22 +10986,89 @@ class binary_reader
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
|
if (JSON_HEDLEY_UNLIKELY(!enter_container(is_object, detail::unknown_size())))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
|
container_frame& frame = container_stack.back();
|
||||||
|
frame.start_position = document_start;
|
||||||
|
frame.declared_size = document_size;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief read a BSON document and everything nested inside it
|
||||||
|
|
||||||
|
Reads elements until the document that was begun here is complete,
|
||||||
|
resuming the enclosing document each time an embedded one ends, so that
|
||||||
|
the nesting depth of the input costs heap rather than native stack
|
||||||
|
(see #5104).
|
||||||
|
|
||||||
|
@return whether reading the document succeeded
|
||||||
|
*/
|
||||||
|
bool parse_bson_internal()
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!open_bson_document(/*is_object*/true)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!check_bson_document_size(document_start, document_size)))
|
// the key currently being read; hoisted out of the loop so that its
|
||||||
{
|
// capacity is reused across elements and across nesting levels
|
||||||
return false;
|
string_t key;
|
||||||
}
|
|
||||||
|
|
||||||
return sax->end_object();
|
while (true)
|
||||||
|
{
|
||||||
|
const auto element_type = get();
|
||||||
|
|
||||||
|
if (element_type == 0) // end of the innermost document
|
||||||
|
{
|
||||||
|
const container_frame& top = container_stack.back();
|
||||||
|
const bool is_object = top.is_object;
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!check_bson_document_size(top.start_position, top.declared_size)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
container_stack.pop_back();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// the document begun here is complete once it is not inside one
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::size_t element_type_parse_position = chars_read;
|
||||||
|
key.clear();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// an array's elements are named "0", "1", ... in the wire format,
|
||||||
|
// and those names are not passed on
|
||||||
|
if (container_stack.back().is_object && !sax->key(key))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -11100,12 +11180,12 @@ class binary_reader
|
|||||||
|
|
||||||
case 0x03: // object
|
case 0x03: // object
|
||||||
{
|
{
|
||||||
return parse_bson_internal();
|
return open_bson_document(/*is_object*/true);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x04: // array
|
case 0x04: // array
|
||||||
{
|
{
|
||||||
return parse_bson_array();
|
return open_bson_document(/*is_object*/false);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x05: // binary
|
case 0x05: // binary
|
||||||
@@ -11155,82 +11235,7 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Read a BSON element list (as specified in the BSON-spec)
|
|
||||||
|
|
||||||
The same binary layout is used for objects and arrays, hence it must be
|
|
||||||
indicated with the argument @a is_array which one is expected
|
|
||||||
(true --> array, false --> object).
|
|
||||||
|
|
||||||
@param[in] is_array Determines if the element list being read is to be
|
|
||||||
treated as an object (@a is_array == false), or as an
|
|
||||||
array (@a is_array == true).
|
|
||||||
@return whether a valid BSON-object/array was passed to the SAX parser
|
|
||||||
*/
|
|
||||||
bool parse_bson_element_list(const bool is_array)
|
|
||||||
{
|
|
||||||
string_t key;
|
|
||||||
|
|
||||||
while (auto element_type = get())
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::size_t element_type_parse_position = chars_read;
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_array && !sax->key(key))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get_bson_cstr only appends
|
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Reads an array from the BSON input and passes it to the SAX-parser.
|
|
||||||
@return whether a valid BSON-array was passed to the SAX parser
|
|
||||||
*/
|
|
||||||
bool parse_bson_array()
|
|
||||||
{
|
|
||||||
const std::size_t document_start = chars_read;
|
|
||||||
std::int32_t document_size{};
|
|
||||||
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!check_bson_document_size(document_start, document_size)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sax->end_array();
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
// CBOR //
|
// CBOR //
|
||||||
@@ -11262,9 +11267,12 @@ class binary_reader
|
|||||||
|
|
||||||
@return whether a valid CBOR value was passed to the SAX parser
|
@return whether a valid CBOR value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
bool parse_cbor_internal(const bool get_char,
|
bool parse_cbor_value(const bool get_char,
|
||||||
const cbor_tag_handler_t tag_handler)
|
const cbor_tag_handler_t tag_handler,
|
||||||
|
bool& tag_pending)
|
||||||
{
|
{
|
||||||
|
tag_pending = false;
|
||||||
|
|
||||||
switch (get_char ? get() : current)
|
switch (get_char ? get() : current)
|
||||||
{
|
{
|
||||||
// EOF
|
// EOF
|
||||||
@@ -11456,37 +11464,36 @@ class binary_reader
|
|||||||
case 0x95:
|
case 0x95:
|
||||||
case 0x96:
|
case 0x96:
|
||||||
case 0x97:
|
case 0x97:
|
||||||
return get_cbor_array(
|
return enter_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
||||||
conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
|
|
||||||
|
|
||||||
case 0x98: // array (one-byte uint8_t for n follows)
|
case 0x98: // array (one-byte uint8_t for n follows)
|
||||||
{
|
{
|
||||||
std::uint8_t len{};
|
std::uint8_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
|
return get_number(input_format_t::cbor, len) && enter_array(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x99: // array (two-byte uint16_t for n follow)
|
case 0x99: // array (two-byte uint16_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
|
return get_number(input_format_t::cbor, len) && enter_array(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9A: // array (four-byte uint32_t for n follow)
|
case 0x9A: // array (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && enter_array(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9B: // array (eight-byte uint64_t for n follow)
|
case 0x9B: // array (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && enter_array(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9F: // array (indefinite length)
|
case 0x9F: // array (indefinite length)
|
||||||
return get_cbor_array(detail::unknown_size(), tag_handler);
|
return enter_array(detail::unknown_size());
|
||||||
|
|
||||||
// map (0x00..0x17 pairs of data items follow)
|
// map (0x00..0x17 pairs of data items follow)
|
||||||
case 0xA0:
|
case 0xA0:
|
||||||
@@ -11513,36 +11520,36 @@ class binary_reader
|
|||||||
case 0xB5:
|
case 0xB5:
|
||||||
case 0xB6:
|
case 0xB6:
|
||||||
case 0xB7:
|
case 0xB7:
|
||||||
return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
|
return enter_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
||||||
|
|
||||||
case 0xB8: // map (one-byte uint8_t for n follows)
|
case 0xB8: // map (one-byte uint8_t for n follows)
|
||||||
{
|
{
|
||||||
std::uint8_t len{};
|
std::uint8_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
|
return get_number(input_format_t::cbor, len) && enter_object(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xB9: // map (two-byte uint16_t for n follow)
|
case 0xB9: // map (two-byte uint16_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
|
return get_number(input_format_t::cbor, len) && enter_object(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBA: // map (four-byte uint32_t for n follow)
|
case 0xBA: // map (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && enter_object(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBB: // map (eight-byte uint64_t for n follow)
|
case 0xBB: // map (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && enter_object(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBF: // map (indefinite length)
|
case 0xBF: // map (indefinite length)
|
||||||
return get_cbor_object(detail::unknown_size(), tag_handler);
|
return enter_object(detail::unknown_size());
|
||||||
|
|
||||||
case 0xC0: // tagged item
|
case 0xC0: // tagged item
|
||||||
case 0xC1:
|
case 0xC1:
|
||||||
@@ -11626,7 +11633,10 @@ class binary_reader
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return parse_cbor_internal(true, tag_handler);
|
// the tagged value follows; it is read by the loop in
|
||||||
|
// parse_cbor_internal() rather than by recursing here
|
||||||
|
tag_pending = true;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case cbor_tag_handler_t::store:
|
case cbor_tag_handler_t::store:
|
||||||
@@ -11676,7 +11686,11 @@ class binary_reader
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return parse_cbor_internal(true, tag_handler);
|
{
|
||||||
|
// as above, the tagged value is read by the caller
|
||||||
|
tag_pending = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
get();
|
get();
|
||||||
return get_cbor_binary(b) && sax->binary(b);
|
return get_cbor_binary(b) && sax->binary(b);
|
||||||
@@ -12074,96 +12088,110 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@param[in] len the length of the array or detail::unknown_size() for an
|
@brief read a CBOR value and everything nested inside it
|
||||||
array of indefinite size
|
|
||||||
|
Reads values until the one that was begun here is complete, resuming the
|
||||||
|
enclosing container after each element, so that the nesting depth of the
|
||||||
|
input costs heap rather than native stack (see #5104).
|
||||||
|
|
||||||
|
@param[in] get_char whether a new character should be retrieved from the
|
||||||
|
input (true) or whether the last read character
|
||||||
|
@a current should be considered instead
|
||||||
@param[in] tag_handler how CBOR tags should be treated
|
@param[in] tag_handler how CBOR tags should be treated
|
||||||
@return whether array creation completed
|
|
||||||
|
@return whether reading the value succeeded
|
||||||
*/
|
*/
|
||||||
bool get_cbor_array(const std::size_t len,
|
bool parse_cbor_internal(const bool get_char,
|
||||||
const cbor_tag_handler_t tag_handler)
|
const cbor_tag_handler_t tag_handler)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
|
// whether the next value starts at a fresh byte or at the one already
|
||||||
{
|
// read into `current`
|
||||||
return false;
|
bool fetch = get_char;
|
||||||
}
|
|
||||||
|
|
||||||
if (len != detail::unknown_size())
|
// the key currently being read; hoisted out of the loop so that its
|
||||||
|
// capacity is reused across elements and across nesting levels
|
||||||
|
string_t key;
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
for (std::size_t i = 0; i < len; ++i)
|
if (!container_stack.empty())
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
// the reference is not held across parse_cbor_value() below,
|
||||||
|
// which can push onto the stack and reallocate it
|
||||||
|
container_frame& top = container_stack.back();
|
||||||
|
bool at_end = false;
|
||||||
|
|
||||||
|
if (top.remaining != npos)
|
||||||
{
|
{
|
||||||
return false;
|
// definite length: the container ends once its elements
|
||||||
|
// have been read
|
||||||
|
at_end = (top.remaining == 0);
|
||||||
|
if (!at_end)
|
||||||
|
{
|
||||||
|
// claim the element about to be read
|
||||||
|
--top.remaining;
|
||||||
|
if (top.is_object)
|
||||||
|
{
|
||||||
|
get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fetch = true;
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
while (get() != 0xFF)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
|
|
||||||
{
|
{
|
||||||
return false;
|
// indefinite length: the container ends at a break marker.
|
||||||
|
// Testing for it consumes a byte, which is the first byte
|
||||||
|
// of the next element when it is not one.
|
||||||
|
at_end = (get() == 0xFF);
|
||||||
|
fetch = top.is_object;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sax->end_array();
|
if (at_end)
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@param[in] len the length of the object or detail::unknown_size() for an
|
|
||||||
object of indefinite size
|
|
||||||
@param[in] tag_handler how CBOR tags should be treated
|
|
||||||
@return whether object creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_object(const std::size_t len,
|
|
||||||
const cbor_tag_handler_t tag_handler)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (len != 0)
|
|
||||||
{
|
|
||||||
string_t key;
|
|
||||||
if (len != detail::unknown_size())
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < len; ++i)
|
|
||||||
{
|
{
|
||||||
get();
|
const bool is_object = top.is_object;
|
||||||
|
container_stack.pop_back();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// the value begun here is complete once its container is
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (top.is_object)
|
||||||
|
{
|
||||||
|
key.clear();
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
fetch = true;
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
key.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
while (get() != 0xFF)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
// a tag is not a value of its own: read on until the tagged value
|
||||||
{
|
bool tag_pending = false;
|
||||||
return false;
|
do
|
||||||
}
|
{
|
||||||
key.clear();
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_value(fetch, tag_handler, tag_pending)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
fetch = true;
|
||||||
|
}
|
||||||
|
while (tag_pending);
|
||||||
|
|
||||||
|
// a value that opened a container left it on the stack; one that
|
||||||
|
// did not, and that was not inside a container, was the whole value
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sax->end_object();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////
|
/////////////
|
||||||
@@ -12833,7 +12861,103 @@ class binary_reader
|
|||||||
*/
|
*/
|
||||||
bool parse_ubjson_internal(const bool get_char = true)
|
bool parse_ubjson_internal(const bool get_char = true)
|
||||||
{
|
{
|
||||||
return get_ubjson_value(get_char ? get_ignore_noop() : current);
|
// the key currently being read; hoisted out of the loop so that its
|
||||||
|
// capacity is reused across elements and across nesting levels
|
||||||
|
string_t key;
|
||||||
|
|
||||||
|
// the type marker of the value to read next
|
||||||
|
char_int_type prefix = get_char ? get_ignore_noop() : current;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
const std::size_t depth = container_stack.size();
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(prefix)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the value begun here is complete once it is not inside anything
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a value was completed rather than a container opened; a
|
||||||
|
// container that ends at a marker needs the next byte to test
|
||||||
|
if (container_stack.size() == depth && container_stack.back().remaining == npos)
|
||||||
|
{
|
||||||
|
get_ignore_noop();
|
||||||
|
}
|
||||||
|
|
||||||
|
// advance to the next element, closing the containers that ended.
|
||||||
|
// The reference is not held across get_ubjson_value() above, which
|
||||||
|
// can push onto the stack and reallocate it.
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
container_frame& top = container_stack.back();
|
||||||
|
|
||||||
|
if (top.remaining != npos)
|
||||||
|
{
|
||||||
|
if (top.remaining != 0)
|
||||||
|
{
|
||||||
|
--top.remaining;
|
||||||
|
if (top.is_object)
|
||||||
|
{
|
||||||
|
key.clear();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// an optimized container gives its elements no marker
|
||||||
|
prefix = (top.type_marker != 0) ? top.type_marker : get_ignore_noop();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the end marker is compared against a literal rather than
|
||||||
|
// against a conditional expression, because char_int_type is
|
||||||
|
// unsigned for some input adapters and MSVC then reports the
|
||||||
|
// comparison as a signed/unsigned mismatch
|
||||||
|
else if (top.is_object ? (current != '}') : (current != ']'))
|
||||||
|
{
|
||||||
|
// a container that ends at a marker is never optimized, so
|
||||||
|
// every element carries its own marker; for an object the
|
||||||
|
// byte tested above is the first byte of the key
|
||||||
|
if (top.is_object)
|
||||||
|
{
|
||||||
|
key.clear();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
prefix = get_ignore_noop();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
prefix = current;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool is_object = top.is_object;
|
||||||
|
container_stack.pop_back();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// the container that just ended was an element of the one
|
||||||
|
// below it, which may need the next byte for its own test
|
||||||
|
if (container_stack.back().remaining == npos)
|
||||||
|
{
|
||||||
|
get_ignore_noop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -13605,53 +13729,22 @@ class binary_reader
|
|||||||
exception_message(input_format, "excessive array size", "size"), nullptr));
|
exception_message(input_format, "excessive array size", "size"), nullptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
|
if (JSON_HEDLEY_UNLIKELY(!enter_array(size_and_type.first, size_and_type.second)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size_and_type.second != 0)
|
if (size_and_type.second == 'N')
|
||||||
{
|
{
|
||||||
if (size_and_type.second != 'N')
|
// a no-op is not a value, so a container of them holds none;
|
||||||
{
|
// the declared size has already been passed to the SAX parser
|
||||||
for (std::size_t i = 0; i < size_and_type.first; ++i)
|
container_stack.back().remaining = 0;
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < size_and_type.first; ++i)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (current != ']')
|
return true;
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
get_ignore_noop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sax->end_array();
|
return enter_array(detail::unknown_size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -13673,68 +13766,12 @@ class binary_reader
|
|||||||
exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
|
exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t key;
|
|
||||||
if (size_and_type.first != npos)
|
if (size_and_type.first != npos)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
|
return enter_object(size_and_type.first, size_and_type.second);
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size_and_type.second != 0)
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < size_and_type.first; ++i)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < size_and_type.first; ++i)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (current != '}')
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
get_ignore_noop();
|
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sax->end_object();
|
return enter_object(detail::unknown_size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note, no reader for UBJSON binary types is implemented because they do
|
// Note, no reader for UBJSON binary types is implemented because they do
|
||||||
|
|||||||
@@ -1011,6 +1011,91 @@ TEST_CASE("BSON document size mismatch")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("BSON nesting does not consume the call stack")
|
||||||
|
{
|
||||||
|
// An embedded document or array used to be read by calling back into the
|
||||||
|
// document reader, so the native call stack grew with the nesting depth of
|
||||||
|
// the input (#5104). The open documents are kept on a heap stack now.
|
||||||
|
//
|
||||||
|
// Deeply nested values must not be compared, copied or dumped here: those
|
||||||
|
// operations are still recursive and would reintroduce the crash.
|
||||||
|
|
||||||
|
// A document nested deeply enough to have crashed. The bytes are built
|
||||||
|
// here rather than with to_bson(), because the writer still recurses once
|
||||||
|
// per level and would overflow the stack before the reader is ever
|
||||||
|
// reached. Every level is
|
||||||
|
// <int32 size> 0x03 'a' 0x00 <inner document> 0x00
|
||||||
|
// so a level is eight bytes larger than the one it holds, and the sizes
|
||||||
|
// can be filled in from the outside in.
|
||||||
|
const std::size_t depth = 30000;
|
||||||
|
std::vector<uint8_t> input;
|
||||||
|
input.reserve(5 + (8 * depth));
|
||||||
|
for (std::size_t i = 0; i < depth; ++i)
|
||||||
|
{
|
||||||
|
const auto size = static_cast<std::uint32_t>(5 + (8 * (depth - i)));
|
||||||
|
input.push_back(static_cast<uint8_t>(size & 0xFF));
|
||||||
|
input.push_back(static_cast<uint8_t>((size >> 8) & 0xFF));
|
||||||
|
input.push_back(static_cast<uint8_t>((size >> 16) & 0xFF));
|
||||||
|
input.push_back(static_cast<uint8_t>((size >> 24) & 0xFF));
|
||||||
|
input.push_back(0x03); // embedded document
|
||||||
|
input.push_back('a');
|
||||||
|
input.push_back(0x00);
|
||||||
|
}
|
||||||
|
// the innermost document is empty, then one terminator closes each level
|
||||||
|
input.insert(input.end(), {0x05, 0x00, 0x00, 0x00, 0x00});
|
||||||
|
input.insert(input.end(), depth, 0x00);
|
||||||
|
|
||||||
|
SECTION("a well-formed deep document is read through the SAX interface")
|
||||||
|
{
|
||||||
|
SaxCountdown accept_all(1000000);
|
||||||
|
CHECK(json::sax_parse(input, &accept_all, json::input_format_t::bson));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep document is read into a value")
|
||||||
|
{
|
||||||
|
json j = json::from_bson(input);
|
||||||
|
|
||||||
|
// walked rather than compared: comparing, copying or dumping a value
|
||||||
|
// this deep is still recursive
|
||||||
|
std::size_t measured = 0;
|
||||||
|
const json* q = &j;
|
||||||
|
while (q->is_object() && !q->empty())
|
||||||
|
{
|
||||||
|
q = &q->begin().value();
|
||||||
|
++measured;
|
||||||
|
}
|
||||||
|
CHECK(measured == depth);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("embedded documents and arrays are still read the same way")
|
||||||
|
{
|
||||||
|
const json values = {{"a", {{"b", {{"c", 1}}}}}};
|
||||||
|
CHECK(json::from_bson(json::to_bson(values)) == values);
|
||||||
|
|
||||||
|
const json array = {{"a", {1, 2, 3}}};
|
||||||
|
CHECK(json::from_bson(json::to_bson(array)) == array);
|
||||||
|
|
||||||
|
const json mixed = {{"a", {json{{"x", 1}}, json{{"y", 2}}}}};
|
||||||
|
CHECK(json::from_bson(json::to_bson(mixed)) == mixed);
|
||||||
|
|
||||||
|
CHECK(json::from_bson(json::to_bson(json::object())) == json::object());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a size that does not match is still reported per document")
|
||||||
|
{
|
||||||
|
// the embedded document claims one byte too many
|
||||||
|
std::vector<uint8_t> const bad =
|
||||||
|
{
|
||||||
|
0x15, 0x00, 0x00, 0x00, 0x03, 'a', 0x00,
|
||||||
|
0x0D, 0x00, 0x00, 0x00, 0x08, 'b', 0x00, 0x01, 0x00,
|
||||||
|
0x00
|
||||||
|
};
|
||||||
|
json _;
|
||||||
|
CHECK_THROWS_AS(_ = json::from_bson(bad), json::parse_error&);
|
||||||
|
CHECK(json::from_bson(bad, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("BSON numerical data")
|
TEST_CASE("BSON numerical data")
|
||||||
{
|
{
|
||||||
SECTION("number")
|
SECTION("number")
|
||||||
|
|||||||
@@ -2035,6 +2035,93 @@ TEST_CASE("CBOR definite length equal to the indefinite-length sentinel")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("CBOR nesting does not consume the call stack")
|
||||||
|
{
|
||||||
|
// Containers used to be read by calling back into the value reader once
|
||||||
|
// per element, and a tag by calling it for the tagged value, so the native
|
||||||
|
// call stack grew with the nesting depth of the input. Each of the three
|
||||||
|
// costs a single byte to encode -- 0x9F, 0x81 and 0xC2 -- so a payload of
|
||||||
|
// repeated bytes crashed the process (#5104). The containers are kept on a
|
||||||
|
// heap stack now, and a tag is read in a loop.
|
||||||
|
//
|
||||||
|
// Deeply nested values must not be compared, copied or dumped here: those
|
||||||
|
// operations are still recursive and would reintroduce the crash.
|
||||||
|
json _;
|
||||||
|
|
||||||
|
SECTION("indefinite-length containers")
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input(500000, 0x9F);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 500001: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_cbor(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("definite-length containers")
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input(500000, 0x81);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 500001: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_cbor(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("tags")
|
||||||
|
{
|
||||||
|
// a tag is not a value of its own, so a chain of them used to recurse
|
||||||
|
const std::vector<uint8_t> input(500000, 0xC2);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input, true, true, json::cbor_tag_handler_t::ignore), "[json.exception.parse_error.110] parse error at byte 500001: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_cbor(input, true, false, json::cbor_tag_handler_t::ignore).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read through the SAX interface")
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> input(200000, 0x9F);
|
||||||
|
input.insert(input.end(), 200000, 0xFF);
|
||||||
|
|
||||||
|
SaxCountdown accept_all(1000000);
|
||||||
|
CHECK(json::sax_parse(input, &accept_all, json::input_format_t::cbor));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read into a value")
|
||||||
|
{
|
||||||
|
const std::size_t depth = 10000;
|
||||||
|
std::vector<uint8_t> input(depth, 0x81);
|
||||||
|
input.push_back(0x00);
|
||||||
|
|
||||||
|
json j = json::from_cbor(input);
|
||||||
|
|
||||||
|
std::size_t measured = 0;
|
||||||
|
const json* p = &j;
|
||||||
|
while (p->is_array() && !p->empty())
|
||||||
|
{
|
||||||
|
p = &p->front();
|
||||||
|
++measured;
|
||||||
|
}
|
||||||
|
CHECK(measured == depth);
|
||||||
|
CHECK(p->is_number());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers are still read the same way")
|
||||||
|
{
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x80})) == json::array());
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xA0})) == json::object());
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0xFF})) == json::array());
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0xFF})) == json::object());
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0x01, 0x02, 0xFF})) == json({1, 2}));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 'a', 0x01, 0xFF})) == json({{"a", 1}}));
|
||||||
|
// definite and indefinite forms nested inside each other
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0x82, 0x01, 0x02, 0xA1, 0x61, 'k', 0xBF, 0xFF, 0xFF})) == json({{1, 2}, {{"k", json::object()}}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("tagged values are still read the same way")
|
||||||
|
{
|
||||||
|
const auto ignore = json::cbor_tag_handler_t::ignore;
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xC2, 0x01}), true, true, ignore) == json(1));
|
||||||
|
// a chain of tags resolves to the value that follows it
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xC2, 0xC2, 0xC2, 0x01}), true, true, ignore) == json(1));
|
||||||
|
// a tag inside a container, and one in front of a container
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x82, 0xC2, 0x01, 0x02}), true, true, ignore) == json({1, 2}));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xC2, 0x82, 0x01, 0x02}), true, true, ignore) == json({1, 2}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("CBOR indefinite-length strings do not recurse per chunk")
|
TEST_CASE("CBOR indefinite-length strings do not recurse per chunk")
|
||||||
{
|
{
|
||||||
// Reading an indefinite-length string or byte array used to call itself
|
// Reading an indefinite-length string or byte array used to call itself
|
||||||
|
|||||||
@@ -2149,6 +2149,111 @@ TEST_CASE("UBJSON")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UBJSON nesting does not consume the call stack")
|
||||||
|
{
|
||||||
|
// Containers used to be read by calling back into the value reader once
|
||||||
|
// per element, so the native call stack grew with the nesting depth of the
|
||||||
|
// input. '[' alone opens a container, so a payload of repeated '[' crashed
|
||||||
|
// the process (#5104), as did the optimized forms, which reach the same
|
||||||
|
// path through a type or size annotation. The containers are kept on a
|
||||||
|
// heap stack now.
|
||||||
|
//
|
||||||
|
// Deeply nested values must not be compared, copied or dumped here: those
|
||||||
|
// operations are still recursive and would reintroduce the crash.
|
||||||
|
json _;
|
||||||
|
|
||||||
|
SECTION("containers that end at a marker")
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input(500000, '[');
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(input), "[json.exception.parse_error.110] parse error at byte 500001: syntax error while parsing UBJSON value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers with a size")
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> input;
|
||||||
|
for (std::size_t i = 0; i < 100000; ++i)
|
||||||
|
{
|
||||||
|
input.push_back('[');
|
||||||
|
input.push_back('#');
|
||||||
|
input.push_back('i');
|
||||||
|
input.push_back(1);
|
||||||
|
}
|
||||||
|
CHECK_THROWS_AS(_ = json::from_ubjson(input), json::parse_error&);
|
||||||
|
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers with a type and a size")
|
||||||
|
{
|
||||||
|
// '[' is a permitted optimized type in UBJSON, so each element of such
|
||||||
|
// a container is itself a container, read without a marker of its own
|
||||||
|
std::vector<uint8_t> input;
|
||||||
|
for (std::size_t i = 0; i < 100000; ++i)
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> level = {'[', '$', '[', '#', 'i', 1};
|
||||||
|
input.insert(input.end(), level.begin(), level.end());
|
||||||
|
}
|
||||||
|
CHECK_THROWS_AS(_ = json::from_ubjson(input), json::parse_error&);
|
||||||
|
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read through the SAX interface")
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> input(100000, '[');
|
||||||
|
input.insert(input.end(), 100000, ']');
|
||||||
|
|
||||||
|
SaxCountdown accept_all(1000000);
|
||||||
|
CHECK(json::sax_parse(input, &accept_all, json::input_format_t::ubjson));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read into a value")
|
||||||
|
{
|
||||||
|
const std::size_t depth = 10000;
|
||||||
|
std::vector<uint8_t> input(depth, '[');
|
||||||
|
input.insert(input.end(), depth, ']');
|
||||||
|
|
||||||
|
json j = json::from_ubjson(input);
|
||||||
|
|
||||||
|
std::size_t measured = 0;
|
||||||
|
const json* p = &j;
|
||||||
|
while (p->is_array() && !p->empty())
|
||||||
|
{
|
||||||
|
p = &p->front();
|
||||||
|
++measured;
|
||||||
|
}
|
||||||
|
// the innermost array is empty, so the descent stops one level short
|
||||||
|
CHECK(measured == depth - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers are still read the same way")
|
||||||
|
{
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', ']'})) == json::array());
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'{', '}'})) == json::object());
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '#', 'i', 0})) == json::array());
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'{', '#', 'i', 0})) == json::object());
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'i', '#', 'i', 2, 1, 2})) == json({1, 2}));
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '#', 'i', 2, 'i', 1, 'i', 2})) == json({1, 2}));
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'{', '$', 'i', '#', 'i', 1, 'i', 1, 'a', 1})) == json({{"a", 1}}));
|
||||||
|
// a no-op is not a value, so a container of them holds none
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'N', '#', 'i', 2})) == json::array());
|
||||||
|
// sized and unsized forms nested inside one another
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '[', '#', 'i', 2, 'i', 1, 'i', 2, ']'})) == json({{1, 2}}));
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '#', 'i', 1, '[', 'i', 1, ']'})) == json({{1}}));
|
||||||
|
// an optimized container of containers
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', '[', '#', 'i', 2, 'i', 1, ']', 'i', 2, ']'})) == json({{1}, {2}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("BJData containers are still read the same way")
|
||||||
|
{
|
||||||
|
// the ND-array wrapper and the binary shortcut are complete values,
|
||||||
|
// not containers the reader descends into
|
||||||
|
CHECK(json::from_bjdata(std::vector<uint8_t>({'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 1, 2, 3, 4, 5, 6})) ==
|
||||||
|
json({{"_ArrayType_", "uint8"}, {"_ArraySize_", {2, 3}}, {"_ArrayData_", {1, 2, 3, 4, 5, 6}}}));
|
||||||
|
CHECK(json::from_bjdata(std::vector<uint8_t>({'[', '$', 'i', '#', 'i', 2, 1, 2})) == json({1, 2}));
|
||||||
|
CHECK(json::from_bjdata(std::vector<uint8_t>({'[', '[', 'i', 1, ']', ']'})) == json({{1}}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("UBJSON optimized arrays of a valueless type are bounded")
|
TEST_CASE("UBJSON optimized arrays of a valueless type are bounded")
|
||||||
{
|
{
|
||||||
// An element of type 'Z', 'T' or 'F' is encoded by its marker alone, so an
|
// An element of type 'Z', 'T' or 'F' is encoded by its marker alone, so an
|
||||||
|
|||||||
Reference in New Issue
Block a user