Let wuffs_aux::DecodeJson use BACKSLASH_X_AS_BYTES

This commit is contained in:
Nigel Tao committed 2020-08-10 15:22:05 +10:00
1 parent 6d81764e14
commit 62c4feb268
7 files changed
+152 -41

No files matched your search

+59 -7
View File
@@ -8459,6 +8459,8 @@ class DecodeCborCallbacks {
//
// Do not keep a reference to buffer or buffer.data.ptr after Done returns,
// as DecodeCbor may then de-allocate the backing array.
//
// The default Done implementation is a no-op.
virtual void Done(DecodeCborResult& result,
sync_io::Input& input,
IOBuffer& buffer);
@@ -8493,10 +8495,21 @@ class DecodeJsonCallbacks {
// strings, the Callbacks implementation is responsible for tracking map keys
// versus other values.
// The JSON file format as specified deals only with (UTF-8) text strings,
// but an unofficial extension allows "ijk\x89m" escapes within those
// strings. DecodeJsonCallbacks' AppendByteString will not be called unless
// WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X_AS_BYTES is passed to DecodeJson. If
// it is passed, AppendTextString will not be called and all byte strings are
// potentially invalid UTF-8. It is up to the AppendByteString implementation
// whether to test the std::string for UTF-8 validity.
//
// The default AppendByteString implementation returns an error message.
virtual std::string AppendNull() = 0;
virtual std::string AppendBool(bool val) = 0;
virtual std::string AppendF64(double val) = 0;
virtual std::string AppendI64(int64_t val) = 0;
virtual std::string AppendByteString(std::string&& val);
virtual std::string AppendTextString(std::string&& val) = 0;
// Push and Pop are called for container nodes: JSON arrays (lists) and JSON
@@ -8522,6 +8535,8 @@ class DecodeJsonCallbacks {
//
// Do not keep a reference to buffer or buffer.data.ptr after Done returns,
// as DecodeJson may then de-allocate the backing array.
//
// The default Done implementation is a no-op.
virtual void Done(DecodeJsonResult& result,
sync_io::Input& input,
IOBuffer& buffer);
@@ -28939,9 +28954,10 @@ DecodeCborResult::DecodeCborResult(std::string&& error_message0,
: error_message(std::move(error_message0)),
cursor_position(cursor_position0) {}
void DecodeCborCallbacks::Done(DecodeCborResult& result,
sync_io::Input& input,
IOBuffer& buffer) {}
void //
DecodeCborCallbacks::Done(DecodeCborResult& result,
sync_io::Input& input,
IOBuffer& buffer) {}
DecodeCborResult //
DecodeCbor(DecodeCborCallbacks&& callbacks,
@@ -29267,9 +29283,15 @@ DecodeJsonResult::DecodeJsonResult(std::string&& error_message0,
: error_message(std::move(error_message0)),
cursor_position(cursor_position0) {}
void DecodeJsonCallbacks::Done(DecodeJsonResult& result,
sync_io::Input& input,
IOBuffer& buffer) {}
std::string //
DecodeJsonCallbacks::AppendByteString(std::string&& val) {
return "wuffs_aux::JsonDecoder: unexpected JSON byte string";
}
void //
DecodeJsonCallbacks::Done(DecodeJsonResult& result,
sync_io::Input& input,
IOBuffer& buffer) {}
DecodeJsonResult //
DecodeJson(DecodeJsonCallbacks&& callbacks,
@@ -29398,13 +29420,43 @@ DecodeJson(DecodeJsonCallbacks&& callbacks,
const char* ptr = // Convert from (uint8_t*).
static_cast<const char*>(static_cast<void*>(token_ptr));
str.append(ptr, token_len);
} else if (
vbd &
WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_4_SRC_BACKSLASH_X) {
wuffs_base__slice_u8 encoded =
wuffs_base__make_slice_u8(token_ptr, token_len);
while (encoded.len > 0) {
uint8_t decoded[64];
constexpr bool src_closed = true;
wuffs_base__transform__output o = wuffs_base__base_16__decode4(
wuffs_base__make_slice_u8(&decoded[0], sizeof decoded),
encoded, src_closed, WUFFS_BASE__BASE_16__DEFAULT_OPTIONS);
if (o.status.is_error()) {
ret_error_message = o.status.message();
goto done;
} else if ((o.num_dst > (sizeof decoded)) ||
(o.num_src > encoded.len)) {
ret_error_message =
"wuffs_aux::JsonDecoder: internal error: inconsistent "
"base16 decoding";
goto done;
}
str.append( // Convert from (uint8_t*).
static_cast<const char*>(static_cast<void*>(&decoded[0])),
o.num_dst);
encoded.ptr += o.num_src;
encoded.len -= o.num_src;
}
} else {
goto fail;
}
if (token.continued()) {
continue;
}
ret_error_message = callbacks.AppendTextString(std::move(str));
ret_error_message =
(vbd & WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8)
? callbacks.AppendTextString(std::move(str))
: callbacks.AppendByteString(std::move(str));
str.clear();
goto parsed_a_value;
}