26#include <absl/status/status.h>
27#include <absl/status/status_macros.h>
28#include <absl/status/statusor.h>
29#include <absl/strings/str_cat.h>
30#include <nlohmann/json.hpp>
37using Json = nlohmann::json;
40constexpr int kMaxMsgpackNesting = 256;
46 explicit MsgpackDecoder(std::string_view bytes) : bytes_(bytes) {}
52 [[nodiscard]]
size_t position()
const {
return position_; }
55 absl::Status Need(
size_t count)
const {
56 if (position_ > bytes_.size() || count > bytes_.size() - position_) {
57 return absl::InvalidArgumentError(
"truncated");
59 return absl::OkStatus();
63 template <
typename Un
signed>
64 absl::StatusOr<Unsigned> ReadBigEndian() {
65 ABSL_RETURN_IF_ERROR(Need(
sizeof(Unsigned)));
67 for (
size_t index = 0; index <
sizeof(Unsigned); ++index) {
68 value =
static_cast<Unsigned
>(
69 static_cast<Unsigned
>(
value << 8U) |
70 static_cast<unsigned char>(bytes_[position_ + index]));
72 position_ +=
sizeof(Unsigned);
77 absl::StatusOr<std::string_view> ReadPayload(std::uint64_t
length) {
78 if (
length > std::numeric_limits<size_t>::max()) {
79 return absl::ResourceExhaustedError(
"value is too large");
81 const auto count =
static_cast<size_t>(
length);
82 ABSL_RETURN_IF_ERROR(Need(count));
83 const std::string_view payload = bytes_.substr(position_, count);
89 absl::StatusOr<std::uint64_t> ReadStringLength(std::uint8_t marker);
90 absl::StatusOr<std::string> DecodeString(std::uint8_t marker);
91 absl::StatusOr<Json> DecodeBinary(std::uint8_t marker);
92 absl::StatusOr<Json> DecodeArray(std::uint64_t count,
int depth);
93 absl::StatusOr<Json> DecodeObject(std::uint64_t count,
int depth);
95 std::string_view bytes_;
99absl::StatusOr<std::uint64_t> MsgpackDecoder::ReadStringLength(
100 std::uint8_t marker) {
101 if ((marker & 0xe0U) == 0xa0U) {
102 return marker & 0x1fU;
106 return ReadBigEndian<std::uint8_t>();
108 return ReadBigEndian<std::uint16_t>();
110 return ReadBigEndian<std::uint32_t>();
112 return absl::InvalidArgumentError(
113 absl::StrCat(
"expected a string, found marker 0x",
114 absl::Hex(marker, absl::kZeroPad2)));
118absl::StatusOr<std::string> MsgpackDecoder::DecodeString(std::uint8_t marker) {
119 ABSL_ASSIGN_OR_RETURN(
const std::uint64_t
length, ReadStringLength(marker));
120 ABSL_ASSIGN_OR_RETURN(
const std::string_view payload, ReadPayload(
length));
121 return std::string(payload);
124absl::StatusOr<Json> MsgpackDecoder::DecodeBinary(std::uint8_t marker) {
126 bool has_subtype =
false;
129 ABSL_ASSIGN_OR_RETURN(
length, ReadBigEndian<std::uint8_t>());
133 ABSL_ASSIGN_OR_RETURN(
length, ReadBigEndian<std::uint16_t>());
137 ABSL_ASSIGN_OR_RETURN(
length, ReadBigEndian<std::uint32_t>());
143 ABSL_ASSIGN_OR_RETURN(
length, ReadBigEndian<std::uint8_t>());
148 ABSL_ASSIGN_OR_RETURN(
length, ReadBigEndian<std::uint16_t>());
153 ABSL_ASSIGN_OR_RETURN(
length, ReadBigEndian<std::uint32_t>());
178 return absl::InternalError(
"Unreachable MessagePack binary marker");
180 std::uint8_t subtype = 0;
182 ABSL_ASSIGN_OR_RETURN(subtype, ReadBigEndian<std::uint8_t>());
184 ABSL_ASSIGN_OR_RETURN(
const std::string_view payload, ReadPayload(
length));
185 std::vector<std::uint8_t> bytes(payload.begin(), payload.end());
186 return has_subtype ? Json::binary(std::move(bytes), subtype)
187 : Json::binary(std::move(bytes));
190absl::StatusOr<Json> MsgpackDecoder::DecodeArray(std::uint64_t count,
192 Json
value = Json::array();
193 for (std::uint64_t index = 0; index < count; ++index) {
194 ABSL_ASSIGN_OR_RETURN(Json element,
Decode(
depth + 1));
195 value.emplace_back(std::move(element));
200absl::StatusOr<Json> MsgpackDecoder::DecodeObject(std::uint64_t count,
202 Json
value = Json::object();
203 for (std::uint64_t index = 0; index < count; ++index) {
204 ABSL_RETURN_IF_ERROR(Need(1));
205 const auto marker =
static_cast<std::uint8_t
>(bytes_[position_++]);
206 ABSL_ASSIGN_OR_RETURN(
const std::string
key, DecodeString(marker));
207 ABSL_ASSIGN_OR_RETURN(Json element,
Decode(
depth + 1));
214absl::StatusOr<Json> MsgpackDecoder::Decode(
int depth) {
215 if (
depth > kMaxMsgpackNesting) {
216 return absl::ResourceExhaustedError(
"nesting is too deep");
218 ABSL_RETURN_IF_ERROR(Need(1));
219 const auto marker =
static_cast<std::uint8_t
>(bytes_[position_++]);
221 if (marker <= 0x7f) {
222 return Json(
static_cast<std::uint64_t
>(marker));
224 if (marker >= 0xe0) {
225 return Json(
static_cast<std::int64_t
>(
static_cast<std::int8_t
>(marker)));
227 if ((marker & 0xe0U) == 0xa0U) {
228 ABSL_ASSIGN_OR_RETURN(std::string
text, DecodeString(marker));
229 return Json(std::move(
text));
231 if ((marker & 0xf0U) == 0x90U) {
232 return DecodeArray(marker & 0x0fU,
depth);
234 if ((marker & 0xf0U) == 0x80U) {
235 return DecodeObject(marker & 0x0fU,
depth);
240 return Json(
nullptr);
256 return DecodeBinary(marker);
258 ABSL_ASSIGN_OR_RETURN(
const std::uint32_t
bits,
259 ReadBigEndian<std::uint32_t>());
260 return Json(
static_cast<double>(std::bit_cast<float>(
bits)));
263 ABSL_ASSIGN_OR_RETURN(
const std::uint64_t
bits,
264 ReadBigEndian<std::uint64_t>());
265 return Json(std::bit_cast<double>(
bits));
268 ABSL_ASSIGN_OR_RETURN(
const std::uint8_t number,
269 ReadBigEndian<std::uint8_t>());
270 return Json(
static_cast<std::uint64_t
>(number));
273 ABSL_ASSIGN_OR_RETURN(
const std::uint16_t number,
274 ReadBigEndian<std::uint16_t>());
275 return Json(
static_cast<std::uint64_t
>(number));
278 ABSL_ASSIGN_OR_RETURN(
const std::uint32_t number,
279 ReadBigEndian<std::uint32_t>());
280 return Json(
static_cast<std::uint64_t
>(number));
283 ABSL_ASSIGN_OR_RETURN(
const std::uint64_t number,
284 ReadBigEndian<std::uint64_t>());
288 ABSL_ASSIGN_OR_RETURN(
const std::uint8_t raw,
289 ReadBigEndian<std::uint8_t>());
290 return Json(
static_cast<std::int64_t
>(
static_cast<std::int8_t
>(raw)));
293 ABSL_ASSIGN_OR_RETURN(
const std::uint16_t raw,
294 ReadBigEndian<std::uint16_t>());
295 return Json(
static_cast<std::int64_t
>(
static_cast<std::int16_t
>(raw)));
298 ABSL_ASSIGN_OR_RETURN(
const std::uint32_t raw,
299 ReadBigEndian<std::uint32_t>());
300 return Json(
static_cast<std::int64_t
>(
static_cast<std::int32_t
>(raw)));
303 ABSL_ASSIGN_OR_RETURN(
const std::uint64_t raw,
304 ReadBigEndian<std::uint64_t>());
305 return Json(
static_cast<std::int64_t
>(raw));
310 ABSL_ASSIGN_OR_RETURN(std::string
text, DecodeString(marker));
311 return Json(std::move(
text));
314 ABSL_ASSIGN_OR_RETURN(
const std::uint16_t count,
315 ReadBigEndian<std::uint16_t>());
316 return DecodeArray(count,
depth);
319 ABSL_ASSIGN_OR_RETURN(
const std::uint32_t count,
320 ReadBigEndian<std::uint32_t>());
321 return DecodeArray(count,
depth);
324 ABSL_ASSIGN_OR_RETURN(
const std::uint16_t count,
325 ReadBigEndian<std::uint16_t>());
326 return DecodeObject(count,
depth);
329 ABSL_ASSIGN_OR_RETURN(
const std::uint32_t count,
330 ReadBigEndian<std::uint32_t>());
331 return DecodeObject(count,
depth);
334 return absl::InvalidArgumentError(absl::StrCat(
335 "unsupported marker 0x", absl::Hex(marker, absl::kZeroPad2)));
339template <
typename JsonValue>
340class StatusJsonParser final
341 :
public nlohmann::detail::json_sax_dom_parser<
342 JsonValue, nlohmann::detail::iterator_input_adapter<const char*>> {
344 explicit StatusJsonParser(JsonValue&
value)
345 : nlohmann::
detail::json_sax_dom_parser<
346 JsonValue, nlohmann::
detail::iterator_input_adapter<const char*>>(
349 template <
typename Exception>
350 bool parse_error(
size_t,
const std::string&,
const Exception& error) {
351 diagnostic = error.what();
355 std::string diagnostic;
358template <
typename JsonValue>
359absl::StatusOr<JsonValue> ParseJsonImpl(std::string_view encoded,
360 std::string_view what) {
362 StatusJsonParser<JsonValue> parser(
value);
365 if (JsonValue::sax_parse(encoded.begin(), encoded.end(), &parser)) {
368 return absl::InvalidArgumentError(
369 absl::StrCat(
"Failed to parse ", what,
": ", parser.diagnostic));
374 std::string_view what) {
375 return ParseJsonImpl<Json>(encoded, what);
379 std::string_view encoded, std::string_view what) {
380 return ParseJsonImpl<nlohmann::ordered_json>(encoded, what);
388template <
typename JsonValue>
389const JsonValue* FindUnencodableStringImpl(
const JsonValue&
value) {
390 std::vector<const JsonValue*> pending{&
value};
391 while (!pending.empty()) {
392 const JsonValue* one = pending.back();
394 if (one->is_string()) {
395 if (!
IsValidUtf8(one->template get_ref<const std::string&>())) {
400 if (one->is_object() || one->is_array()) {
401 for (
const auto& element : *one) {
402 pending.push_back(&element);
409template <
typename JsonValue>
410absl::StatusOr<std::string> DumpJsonImpl(
const JsonValue&
value,
411 std::string_view what) {
413 if (FindUnencodableStringImpl(
value) !=
nullptr) {
414 return absl::InvalidArgumentError(absl::StrCat(
415 "Failed to serialize ", what,
416 ": it holds bytes that are not valid UTF-8, which JSON has no spelling "
417 "for. MessagePack carries them exactly, and base64 carries them "
424 return value.dump(-1,
' ',
false, JsonValue::error_handler_t::strict);
425 }
catch (
const std::exception& error) {
426 return absl::InvalidArgumentError(
427 absl::StrCat(
"Failed to serialize ", what,
": ", error.what()));
429 return absl::InvalidArgumentError(absl::StrCat(
430 "Failed to serialize ", what,
" with a non-standard exception"));
437 return FindUnencodableStringImpl(
value);
440absl::StatusOr<std::string>
DumpJson(
const Json&
value, std::string_view what) {
441 return DumpJsonImpl(
value, what);
445 std::string_view what) {
446 return DumpJsonImpl(
value, what);
452 return value.dump(-1,
' ',
false, Json::error_handler_t::replace);
456template <
typename JsonValue>
457absl::StatusOr<std::string> PackMsgpackImpl(
const JsonValue&
value,
458 std::string_view what) {
464 JsonValue::to_msgpack(
value,
465 nlohmann::detail::output_adapter<char>(encoded));
467 }
catch (
const std::exception& error) {
468 return absl::InvalidArgumentError(absl::StrCat(
469 "Failed to encode ", what,
" as MessagePack: ", error.what()));
471 return absl::InvalidArgumentError(
472 absl::StrCat(
"Failed to encode ", what,
473 " as MessagePack with a non-standard exception"));
480 std::string_view what) {
481 return PackMsgpackImpl(
value, what);
485 const nlohmann::ordered_json&
value, std::string_view what) {
486 return PackMsgpackImpl(
value, what);
490 std::string_view what) {
491 MsgpackDecoder decoder(encoded);
492 absl::StatusOr<Json>
value = decoder.Decode(0);
494 return absl::Status(
value.status().code(),
495 absl::StrCat(
"Invalid ", what,
" MessagePack data: ",
496 value.status().message()));
499 if (decoder.position() != encoded.size()) {
500 return absl::InvalidArgumentError(
501 absl::StrCat(
"Invalid ", what,
" MessagePack data: ",
502 encoded.size() - decoder.position(),
" trailing bytes"));
std::string value
Definition discover.cc:100
std::string key
Definition discover.cc:764
std::optional< std::string > text
The text, where the value is a string literal or a name bound to one.
Definition discover.cc:596
Status-returning JSON and MessagePack codecs.
size_t depth
Definition module.cc:55
std::string Decode(std::string_view value, bool plus_is_space=false)
Percent-decodes value, leaving anything malformed as it stands.
Definition percent.h:63
bool IsValid(std::string_view text)
Whether text is well-formed UTF-8.
Definition utf8.h:70
absl::StatusOr< Json > ParseJson(std::string_view encoded, std::string_view what)
Parses JSON text, returning a status for invalid input.
Definition json_codec.cc:373
const Json * FindUnencodableString(const Json &value)
Definition json_codec.cc:436
absl::StatusOr< std::string > DumpOrderedJson(const nlohmann::ordered_json &value, std::string_view what)
Serialize host fields retaining their declared insertion order.
Definition json_codec.cc:444
std::string DumpJsonLossy(const Json &value)
Definition json_codec.cc:449
absl::StatusOr< Json > UnpackMsgpack(std::string_view encoded, std::string_view what)
Decodes MessagePack bytes, or explains why they are not.
Definition json_codec.cc:489
absl::StatusOr< std::string > PackOrderedMsgpack(const nlohmann::ordered_json &value, std::string_view what)
Encode host fields retaining their declared insertion order.
Definition json_codec.cc:484
absl::StatusOr< std::string > DumpJson(const Json &value, std::string_view what)
Definition json_codec.cc:440
bool IsValidUtf8(std::string_view text)
Whether text is valid strict UTF-8.
Definition json_codec.cc:383
absl::StatusOr< nlohmann::ordered_json > ParseOrderedJson(std::string_view encoded, std::string_view what)
Parse host fields retaining their declared insertion order.
Definition json_codec.cc:378
absl::StatusOr< std::string > PackMsgpack(const Json &value, std::string_view what)
Definition json_codec.cc:479
std::string_view detail
Definition navigate.cc:475
std::optional< std::uint64_t > length
Definition runtime_streams.cc:37
UTF-8 byte-level primitives, as inline functions over a string_view.
int bits
Definition values.cc:64