Commit d99c230f authored by Niels Lohmann's avatar Niels Lohmann

implemented indefinite-length CBOR types (#387)

parent 6b84c415
......@@ -7043,6 +7043,18 @@ class basic_json
idx += len + 8; // skip 8 size bytes + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0x7f) // UTF-8 string (indefinite length)
{
std::string result;
while (v[idx] != 0xff)
{
string_t s = from_cbor_internal(v, idx);
result += s;
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] >= 0x80 and v[current_idx] <= 0x97) // array
{
basic_json result = value_t::array;
......@@ -7097,6 +7109,17 @@ class basic_json
}
return result;
}
else if (v[current_idx] == 0x9f) // array (indefinite length)
{
basic_json result = value_t::array;
while (v[idx] != 0xff)
{
result.push_back(from_cbor_internal(v, idx));
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] >= 0xa0 and v[current_idx] <= 0xb7) // map
{
basic_json result = value_t::object;
......@@ -7156,6 +7179,18 @@ class basic_json
}
return result;
}
else if (v[current_idx] == 0xbf) // map (indefinite length)
{
basic_json result = value_t::object;
while (v[idx] != 0xff)
{
std::string key = from_cbor_internal(v, idx);
result[key] = from_cbor_internal(v, idx);
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] == 0xf4) // false
{
return false;
......
......@@ -7043,6 +7043,18 @@ class basic_json
idx += len + 8; // skip 8 size bytes + content bytes
return std::string(reinterpret_cast<const char*>(v.data()) + offset, len);
}
else if (v[current_idx] == 0x7f) // UTF-8 string (indefinite length)
{
std::string result;
while (v[idx] != 0xff)
{
string_t s = from_cbor_internal(v, idx);
result += s;
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] >= 0x80 and v[current_idx] <= 0x97) // array
{
basic_json result = value_t::array;
......@@ -7097,6 +7109,17 @@ class basic_json
}
return result;
}
else if (v[current_idx] == 0x9f) // array (indefinite length)
{
basic_json result = value_t::array;
while (v[idx] != 0xff)
{
result.push_back(from_cbor_internal(v, idx));
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] >= 0xa0 and v[current_idx] <= 0xb7) // map
{
basic_json result = value_t::object;
......@@ -7156,6 +7179,18 @@ class basic_json
}
return result;
}
else if (v[current_idx] == 0xbf) // map (indefinite length)
{
basic_json result = value_t::object;
while (v[idx] != 0xff)
{
std::string key = from_cbor_internal(v, idx);
result[key] = from_cbor_internal(v, idx);
}
// skip break byte (0xFF)
idx += 1;
return result;
}
else if (v[current_idx] == 0xf4) // false
{
return false;
......
......@@ -1338,6 +1338,9 @@ TEST_CASE("examples from RFC 7049 Appendix A")
CHECK(json::to_cbor(json::parse("\"\\ud800\\udd51\"")) == std::vector<uint8_t>({0x64, 0xf0, 0x90, 0x85, 0x91}));
CHECK(json::parse("\"\\ud800\\udd51\"") == json::from_cbor(std::vector<uint8_t>({0x64, 0xf0, 0x90, 0x85, 0x91})));
// indefinite length strings
CHECK(json::parse("\"streaming\"") == json::from_cbor(std::vector<uint8_t>({0x7f, 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x67, 0xff})));
}
SECTION("arrays")
......@@ -1353,6 +1356,14 @@ TEST_CASE("examples from RFC 7049 Appendix A")
CHECK(json::to_cbor(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]")) == std::vector<uint8_t>({0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19}));
CHECK(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]") == json::from_cbor(std::vector<uint8_t>({0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19})));
// indefinite length arrays
CHECK(json::parse("[]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0xff})));
CHECK(json::parse("[1, [2, 3], [4, 5]] ") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x82, 0x02, 0x03, 0x9f, 0x04, 0x05, 0xff, 0xff})));
CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05, 0xff})));
CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x82, 0x02, 0x03, 0x9f, 0x04, 0x05, 0xff})));
CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x9f, 0x02, 0x03, 0xff, 0x82, 0x04, 0x05})));
CHECK(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0xff})));
}
SECTION("objects")
......@@ -1368,5 +1379,10 @@ TEST_CASE("examples from RFC 7049 Appendix A")
CHECK(json::to_cbor(json::parse("{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\", \"e\": \"E\"}")) == std::vector<uint8_t>({0xa5, 0x61, 0x61, 0x61, 0x41, 0x61, 0x62, 0x61, 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x45}));
CHECK(json::parse("{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\", \"e\": \"E\"}") == json::from_cbor(std::vector<uint8_t>({0xa5, 0x61, 0x61, 0x61, 0x41, 0x61, 0x62, 0x61, 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x45})));
// indefinite length objects
CHECK(json::parse("{\"a\": 1, \"b\": [2, 3]}") == json::from_cbor(std::vector<uint8_t>({0xbf, 0x61, 0x61, 0x01, 0x61, 0x62, 0x9f, 0x02, 0x03, 0xff, 0xff})));
CHECK(json::parse("[\"a\", {\"b\": \"c\"}]") == json::from_cbor(std::vector<uint8_t>({0x82, 0x61, 0x61, 0xbf, 0x61, 0x62, 0x61, 0x63, 0xff})));
CHECK(json::parse("{\"Fun\": true, \"Amt\": -2}") == json::from_cbor(std::vector<uint8_t>({0xbf, 0x63, 0x46, 0x75, 0x6e, 0xf5, 0x63, 0x41, 0x6d, 0x74, 0x21, 0xff})));
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment