Add example/cbor-to-json

This commit is contained in:
Nigel Tao committed 2020-08-09 23:03:42 +10:00
1 parent 9dd8c32683
commit 0b7eaf291b
9 files changed
+1511 -1

No files matched your search

+343
View File
@@ -0,0 +1,343 @@
// After editing this file, run "go generate" in the ../data directory.
// Copyright 2020 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ---------------- Auxiliary - CBOR
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__AUX__CBOR)
#include <utility>
namespace wuffs_aux {
DecodeCborResult::DecodeCborResult(std::string&& error_message0,
uint64_t cursor_position0)
: error_message(std::move(error_message0)),
cursor_position(cursor_position0) {}
void DecodeCborCallbacks::Done(DecodeCborResult& result,
sync_io::Input& input,
IOBuffer& buffer) {}
DecodeCborResult //
DecodeCbor(DecodeCborCallbacks&& callbacks,
sync_io::Input&& input,
wuffs_base__slice_u32 quirks) {
// Prepare the wuffs_base__io_buffer and the resultant error_message.
wuffs_base__io_buffer* io_buf = input.BringsItsOwnIOBuffer();
wuffs_base__io_buffer fallback_io_buf = wuffs_base__empty_io_buffer();
std::unique_ptr<uint8_t[]> fallback_io_array(nullptr);
if (!io_buf) {
fallback_io_array = std::unique_ptr<uint8_t[]>(new uint8_t[4096]);
fallback_io_buf = wuffs_base__ptr_u8__writer(fallback_io_array.get(), 4096);
io_buf = &fallback_io_buf;
}
size_t cursor_index = 0;
std::string ret_error_message;
std::string io_error_message;
do {
// Prepare the low-level CBOR decoder.
wuffs_cbor__decoder::unique_ptr dec = wuffs_cbor__decoder::alloc();
if (!dec) {
ret_error_message = "wuffs_aux::CborDecoder: out of memory";
goto done;
}
for (size_t i = 0; i < quirks.len; i++) {
dec->set_quirk_enabled(quirks.ptr[i], true);
}
// Prepare the wuffs_base__tok_buffer.
wuffs_base__token tok_array[256];
wuffs_base__token_buffer tok_buf =
wuffs_base__slice_token__writer(wuffs_base__make_slice_token(
&tok_array[0], (sizeof(tok_array) / sizeof(tok_array[0]))));
wuffs_base__status tok_status = wuffs_base__make_status(nullptr);
// Prepare other state.
uint32_t depth = 0;
std::string str;
int64_t extension_category = 0;
uint64_t extension_detail = 0;
// Valid token's VBCs range in 0 ..= 15. Values over that are for tokens
// from outside of the base package, such as the CBOR package.
constexpr int64_t EXT_CAT__CBOR_TAG = 16;
// Loop, doing these two things:
// 1. Get the next token.
// 2. Process that token.
while (true) {
// 1. Get the next token.
while (tok_buf.meta.ri >= tok_buf.meta.wi) {
if (tok_status.repr == nullptr) {
// No-op.
} else if (tok_status.repr == wuffs_base__suspension__short_write) {
tok_buf.compact();
} else if (tok_status.repr == wuffs_base__suspension__short_read) {
// Read from input to io_buf.
if (!io_error_message.empty()) {
ret_error_message = std::move(io_error_message);
goto done;
} else if (cursor_index != io_buf->meta.ri) {
ret_error_message =
"wuffs_aux::CborDecoder: internal error: bad cursor_index";
goto done;
} else if (io_buf->meta.closed) {
ret_error_message =
"wuffs_aux::CborDecoder: internal error: io_buf is closed";
goto done;
}
io_buf->compact();
if (io_buf->meta.wi >= io_buf->data.len) {
ret_error_message =
"wuffs_aux::CborDecoder: internal error: io_buf is full";
goto done;
}
cursor_index = io_buf->meta.ri;
io_error_message = input.CopyIn(io_buf);
} else {
ret_error_message = tok_status.message();
goto done;
}
if (WUFFS_CBOR__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) {
ret_error_message =
"wuffs_aux::CborDecoder: internal error: bad WORKBUF_LEN";
goto done;
}
wuffs_base__slice_u8 work_buf = wuffs_base__empty_slice_u8();
tok_status = dec->decode_tokens(&tok_buf, io_buf, work_buf);
}
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++];
uint64_t token_len = token.length();
if ((io_buf->meta.ri < cursor_index) ||
((io_buf->meta.ri - cursor_index) < token_len)) {
ret_error_message =
"wuffs_aux::CborDecoder: internal error: bad token indexes";
goto done;
}
uint8_t* token_ptr = io_buf->data.ptr + cursor_index;
cursor_index += token_len;
// 2. Process that token.
uint64_t vbd = token.value_base_detail();
if (extension_category != 0) {
int64_t ext = token.value_extension();
if ((ext >= 0) && !token.continued()) {
extension_detail = (extension_detail
<< WUFFS_BASE__TOKEN__VALUE_EXTENSION__NUM_BITS) |
static_cast<uint64_t>(ext);
switch (extension_category) {
case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED:
extension_category = 0;
ret_error_message =
callbacks.AppendI64(static_cast<int64_t>(extension_detail));
goto parsed_a_value;
case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED:
extension_category = 0;
ret_error_message = callbacks.AppendU64(extension_detail);
goto parsed_a_value;
case EXT_CAT__CBOR_TAG:
extension_category = 0;
ret_error_message = callbacks.AppendCborTag(extension_detail);
if (!ret_error_message.empty()) {
goto done;
}
continue;
}
}
ret_error_message =
"wuffs_aux::CborDecoder: internal error: bad extended token";
goto done;
}
switch (token.value_base_category()) {
case WUFFS_BASE__TOKEN__VBC__FILLER:
continue;
case WUFFS_BASE__TOKEN__VBC__STRUCTURE: {
if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH) {
ret_error_message = callbacks.Push(static_cast<uint32_t>(vbd));
if (!ret_error_message.empty()) {
goto done;
}
depth++;
continue;
}
ret_error_message = callbacks.Pop(static_cast<uint32_t>(vbd));
depth--;
goto parsed_a_value;
}
case WUFFS_BASE__TOKEN__VBC__STRING: {
if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) {
// No-op.
} else if (vbd &
WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) {
const char* ptr = // Convert from (uint8_t*).
static_cast<const char*>(static_cast<void*>(token_ptr));
str.append(ptr, token_len);
} else {
goto fail;
}
if (token.continued()) {
continue;
}
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;
}
case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: {
uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL];
size_t n = wuffs_base__utf_8__encode(
wuffs_base__make_slice_u8(
&u[0], WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL),
static_cast<uint32_t>(vbd));
const char* ptr = // Convert from (uint8_t*).
static_cast<const char*>(static_cast<void*>(&u[0]));
str.append(ptr, n);
if (token.continued()) {
continue;
}
goto fail;
}
case WUFFS_BASE__TOKEN__VBC__LITERAL: {
if (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__NULL) {
ret_error_message = callbacks.AppendNull();
} else if (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__UNDEFINED) {
ret_error_message = callbacks.AppendUndefined();
} else {
ret_error_message = callbacks.AppendBool(
vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE);
}
goto parsed_a_value;
}
case WUFFS_BASE__TOKEN__VBC__NUMBER: {
const uint64_t cfp_fbbe_fifb =
WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT |
WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN |
WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE;
if ((vbd & cfp_fbbe_fifb) == cfp_fbbe_fifb) {
double f;
switch (token_len) {
case 3:
f = wuffs_base__ieee_754_bit_representation__from_u16_to_f64(
wuffs_base__load_u16be__no_bounds_check(token_ptr + 1));
break;
case 5:
f = wuffs_base__ieee_754_bit_representation__from_u32_to_f64(
wuffs_base__load_u32be__no_bounds_check(token_ptr + 1));
break;
case 9:
f = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(
wuffs_base__load_u64be__no_bounds_check(token_ptr + 1));
break;
default:
goto fail;
}
ret_error_message = callbacks.AppendF64(f);
goto parsed_a_value;
}
goto fail;
}
case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED: {
if (token.continued()) {
extension_category = WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED;
extension_detail =
static_cast<uint64_t>(token.value_base_detail__sign_extended());
continue;
}
ret_error_message =
callbacks.AppendI64(token.value_base_detail__sign_extended());
goto parsed_a_value;
}
case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED: {
if (token.continued()) {
extension_category =
WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED;
extension_detail = vbd;
continue;
}
ret_error_message = callbacks.AppendU64(vbd);
goto parsed_a_value;
}
}
if (token.value_major() == WUFFS_CBOR__TOKEN_VALUE_MAJOR) {
uint64_t value_minor = token.value_minor();
if (value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__MINUS_1_MINUS_X) {
if (token_len == 9) {
ret_error_message = callbacks.AppendMinus1MinusX(
wuffs_base__load_u64be__no_bounds_check(token_ptr + 1));
goto parsed_a_value;
}
} else if (value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__SIMPLE_VALUE) {
ret_error_message =
callbacks.AppendCborSimpleValue(static_cast<uint8_t>(
value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__DETAIL_MASK));
goto parsed_a_value;
} else if (value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__TAG) {
if (token.continued()) {
extension_category = EXT_CAT__CBOR_TAG;
extension_detail =
value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__DETAIL_MASK;
continue;
}
ret_error_message = callbacks.AppendCborTag(
value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__DETAIL_MASK);
if (!ret_error_message.empty()) {
goto done;
}
continue;
}
}
fail:
ret_error_message =
"wuffs_aux::CborDecoder: internal error: unexpected token";
goto done;
parsed_a_value:
if (!ret_error_message.empty() || (depth == 0)) {
goto done;
}
}
} while (false);
done:
DecodeCborResult result(
std::move(ret_error_message),
wuffs_base__u64__sat_add(io_buf->meta.pos, cursor_index));
callbacks.Done(result, input, *io_buf);
return result;
}
} // namespace wuffs_aux
#endif // !defined(WUFFS_CONFIG__MODULES) ||
// defined(WUFFS_CONFIG__MODULE__AUX__CBOR)
+80
View File
@@ -0,0 +1,80 @@
// After editing this file, run "go generate" in the ../data directory.
// Copyright 2020 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ---------------- Auxiliary - CBOR
namespace wuffs_aux {
struct DecodeCborResult {
DecodeCborResult(std::string&& error_message0, uint64_t cursor_position0);
std::string error_message;
uint64_t cursor_position;
};
class DecodeCborCallbacks {
public:
// AppendXxx are called for leaf nodes: literals, numbers, strings, etc.
virtual std::string AppendNull() = 0;
virtual std::string AppendUndefined() = 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 AppendU64(uint64_t val) = 0;
virtual std::string AppendByteString(std::string&& val) = 0;
virtual std::string AppendTextString(std::string&& val) = 0;
virtual std::string AppendMinus1MinusX(uint64_t val) = 0;
virtual std::string AppendCborSimpleValue(uint8_t val) = 0;
virtual std::string AppendCborTag(uint64_t val) = 0;
// Push and Pop are called for container nodes: CBOR arrays (lists) and CBOR
// maps (dictionaries).
//
// The flags bits combine exactly one of:
// - WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_NONE
// - WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST
// - WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT
// and exactly one of:
// - WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE
// - WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST
// - WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT
virtual std::string Push(uint32_t flags) = 0;
virtual std::string Pop(uint32_t flags) = 0;
// Done is always the last Callback method called by DecodeCbor, whether or
// not parsing the input as CBOR encountered an error. Even when successful,
// trailing data may remain in input and buffer.
//
// Do not keep a reference to buffer or buffer.data.ptr after Done returns,
// as DecodeCbor may then de-allocate the backing array.
virtual void Done(DecodeCborResult& result,
sync_io::Input& input,
IOBuffer& buffer);
};
// DecodeCbor calls callbacks based on the CBOR-formatted data in input.
//
// On success, the returned error_message is empty and cursor_position counts
// the number of bytes consumed. On failure, error_message is non-empty and
// cursor_position is the location of the error. That error may be a content
// error (invalid CBOR) or an input error (e.g. network failure).
DecodeCborResult DecodeCbor(DecodeCborCallbacks&& callbacks,
sync_io::Input&& input,
wuffs_base__slice_u32 quirks);
} // namespace wuffs_aux
+23
View File
@@ -625,6 +625,27 @@ const AuxBaseHh = "" +
"// --------\n\n} // namespace sync_io\n\n} // namespace wuffs_aux\n" +
""
const AuxCborCc = "" +
"// ---------------- Auxiliary - CBOR\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__AUX__CBOR)\n\n#include <utility>\n\nnamespace wuffs_aux {\n\nDecodeCborResult::DecodeCborResult(std::string&& error_message0,\n uint64_t cursor_position0)\n : error_message(std::move(error_message0)),\n cursor_position(cursor_position0) {}\n\nvoid DecodeCborCallbacks::Done(DecodeCborResult& result,\n sync_io::Input& input,\n IOBuffer& buffer) {}\n\nDecodeCborResult //\nDecodeCbor(DecodeCborCallbacks&& callbacks,\n sync_io::Input&& input,\n wuffs_base__slice_u32 quirks) {\n // Prepare the wuffs_base__io_buffer and the resultant error_message.\n wuffs_base__io_buffer* io_buf = input.BringsItsOwnIOBuffer();\n wuffs_base__io_buffer fallback_io_buf = wuffs_base__empty_io_buffer();\n std::unique_ptr<uint8_t[]> fallback_io_array(nullptr);\n if (!io_buf) {\n fallback_io_array = std::unique_ptr<uint8_t[]>(ne" +
"w uint8_t[4096]);\n fallback_io_buf = wuffs_base__ptr_u8__writer(fallback_io_array.get(), 4096);\n io_buf = &fallback_io_buf;\n }\n size_t cursor_index = 0;\n std::string ret_error_message;\n std::string io_error_message;\n\n do {\n // Prepare the low-level CBOR decoder.\n wuffs_cbor__decoder::unique_ptr dec = wuffs_cbor__decoder::alloc();\n if (!dec) {\n ret_error_message = \"wuffs_aux::CborDecoder: out of memory\";\n goto done;\n }\n for (size_t i = 0; i < quirks.len; i++) {\n dec->set_quirk_enabled(quirks.ptr[i], true);\n }\n\n // Prepare the wuffs_base__tok_buffer.\n wuffs_base__token tok_array[256];\n wuffs_base__token_buffer tok_buf =\n wuffs_base__slice_token__writer(wuffs_base__make_slice_token(\n &tok_array[0], (sizeof(tok_array) / sizeof(tok_array[0]))));\n wuffs_base__status tok_status = wuffs_base__make_status(nullptr);\n\n // Prepare other state.\n uint32_t depth = 0;\n std::string str;\n int64_t extension_category = 0;\n uint64_t extension" +
"_detail = 0;\n\n // Valid token's VBCs range in 0 ..= 15. Values over that are for tokens\n // from outside of the base package, such as the CBOR package.\n constexpr int64_t EXT_CAT__CBOR_TAG = 16;\n\n // Loop, doing these two things:\n // 1. Get the next token.\n // 2. Process that token.\n while (true) {\n // 1. Get the next token.\n\n while (tok_buf.meta.ri >= tok_buf.meta.wi) {\n if (tok_status.repr == nullptr) {\n // No-op.\n } else if (tok_status.repr == wuffs_base__suspension__short_write) {\n tok_buf.compact();\n } else if (tok_status.repr == wuffs_base__suspension__short_read) {\n // Read from input to io_buf.\n if (!io_error_message.empty()) {\n ret_error_message = std::move(io_error_message);\n goto done;\n } else if (cursor_index != io_buf->meta.ri) {\n ret_error_message =\n \"wuffs_aux::CborDecoder: internal error: bad cursor_index\";\n goto done;\n } else if" +
" (io_buf->meta.closed) {\n ret_error_message =\n \"wuffs_aux::CborDecoder: internal error: io_buf is closed\";\n goto done;\n }\n io_buf->compact();\n if (io_buf->meta.wi >= io_buf->data.len) {\n ret_error_message =\n \"wuffs_aux::CborDecoder: internal error: io_buf is full\";\n goto done;\n }\n cursor_index = io_buf->meta.ri;\n io_error_message = input.CopyIn(io_buf);\n } else {\n ret_error_message = tok_status.message();\n goto done;\n }\n\n if (WUFFS_CBOR__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) {\n ret_error_message =\n \"wuffs_aux::CborDecoder: internal error: bad WORKBUF_LEN\";\n goto done;\n }\n wuffs_base__slice_u8 work_buf = wuffs_base__empty_slice_u8();\n tok_status = dec->decode_tokens(&tok_buf, io_buf, work_buf);\n }\n\n wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++];\n uint64_t t" +
"oken_len = token.length();\n if ((io_buf->meta.ri < cursor_index) ||\n ((io_buf->meta.ri - cursor_index) < token_len)) {\n ret_error_message =\n \"wuffs_aux::CborDecoder: internal error: bad token indexes\";\n goto done;\n }\n uint8_t* token_ptr = io_buf->data.ptr + cursor_index;\n cursor_index += token_len;\n\n // 2. Process that token.\n\n uint64_t vbd = token.value_base_detail();\n\n if (extension_category != 0) {\n int64_t ext = token.value_extension();\n if ((ext >= 0) && !token.continued()) {\n extension_detail = (extension_detail\n << WUFFS_BASE__TOKEN__VALUE_EXTENSION__NUM_BITS) |\n static_cast<uint64_t>(ext);\n switch (extension_category) {\n case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED:\n extension_category = 0;\n ret_error_message =\n callbacks.AppendI64(static_cast<int64_t>(extension_detail));\n g" +
"oto parsed_a_value;\n case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED:\n extension_category = 0;\n ret_error_message = callbacks.AppendU64(extension_detail);\n goto parsed_a_value;\n case EXT_CAT__CBOR_TAG:\n extension_category = 0;\n ret_error_message = callbacks.AppendCborTag(extension_detail);\n if (!ret_error_message.empty()) {\n goto done;\n }\n continue;\n }\n }\n ret_error_message =\n \"wuffs_aux::CborDecoder: internal error: bad extended token\";\n goto done;\n }\n\n switch (token.value_base_category()) {\n case WUFFS_BASE__TOKEN__VBC__FILLER:\n continue;\n\n case WUFFS_BASE__TOKEN__VBC__STRUCTURE: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH) {\n ret_error_message = callbacks.Push(static_cast<uint32_t>(vbd));\n if (!ret_error_message.empty()) {\n goto done;\n " +
" }\n depth++;\n continue;\n }\n ret_error_message = callbacks.Pop(static_cast<uint32_t>(vbd));\n depth--;\n goto parsed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__STRING: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) {\n // No-op.\n } else if (vbd &\n WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) {\n const char* ptr = // Convert from (uint8_t*).\n static_cast<const char*>(static_cast<void*>(token_ptr));\n str.append(ptr, token_len);\n } else {\n goto fail;\n }\n if (token.continued()) {\n continue;\n }\n ret_error_message =\n (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8)\n ? callbacks.AppendTextString(std::move(str))\n : callbacks.AppendByteString(std::move(str));\n str.clear();\n goto par" +
"sed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: {\n uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL];\n size_t n = wuffs_base__utf_8__encode(\n wuffs_base__make_slice_u8(\n &u[0], WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL),\n static_cast<uint32_t>(vbd));\n const char* ptr = // Convert from (uint8_t*).\n static_cast<const char*>(static_cast<void*>(&u[0]));\n str.append(ptr, n);\n if (token.continued()) {\n continue;\n }\n goto fail;\n }\n\n case WUFFS_BASE__TOKEN__VBC__LITERAL: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__NULL) {\n ret_error_message = callbacks.AppendNull();\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__UNDEFINED) {\n ret_error_message = callbacks.AppendUndefined();\n } else {\n ret_error_message = callbacks.AppendBool(\n vbd & WUFFS_BASE__TOKEN__VBD__LITER" +
"AL__TRUE);\n }\n goto parsed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__NUMBER: {\n const uint64_t cfp_fbbe_fifb =\n WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT |\n WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN |\n WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE;\n if ((vbd & cfp_fbbe_fifb) == cfp_fbbe_fifb) {\n double f;\n switch (token_len) {\n case 3:\n f = wuffs_base__ieee_754_bit_representation__from_u16_to_f64(\n wuffs_base__load_u16be__no_bounds_check(token_ptr + 1));\n break;\n case 5:\n f = wuffs_base__ieee_754_bit_representation__from_u32_to_f64(\n wuffs_base__load_u32be__no_bounds_check(token_ptr + 1));\n break;\n case 9:\n f = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n wuffs_base__load_u64be__" +
"no_bounds_check(token_ptr + 1));\n break;\n default:\n goto fail;\n }\n ret_error_message = callbacks.AppendF64(f);\n goto parsed_a_value;\n }\n goto fail;\n }\n\n case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED: {\n if (token.continued()) {\n extension_category = WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED;\n extension_detail =\n static_cast<uint64_t>(token.value_base_detail__sign_extended());\n continue;\n }\n ret_error_message =\n callbacks.AppendI64(token.value_base_detail__sign_extended());\n goto parsed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED: {\n if (token.continued()) {\n extension_category =\n WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED;\n extension_detail = vbd;\n continue;\n }\n ret_error_message = ca" +
"llbacks.AppendU64(vbd);\n goto parsed_a_value;\n }\n }\n\n if (token.value_major() == WUFFS_CBOR__TOKEN_VALUE_MAJOR) {\n uint64_t value_minor = token.value_minor();\n if (value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__MINUS_1_MINUS_X) {\n if (token_len == 9) {\n ret_error_message = callbacks.AppendMinus1MinusX(\n wuffs_base__load_u64be__no_bounds_check(token_ptr + 1));\n goto parsed_a_value;\n }\n } else if (value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__SIMPLE_VALUE) {\n ret_error_message =\n callbacks.AppendCborSimpleValue(static_cast<uint8_t>(\n value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__DETAIL_MASK));\n goto parsed_a_value;\n } else if (value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__TAG) {\n if (token.continued()) {\n extension_category = EXT_CAT__CBOR_TAG;\n extension_detail =\n value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__DETAIL_" +
"MASK;\n continue;\n }\n ret_error_message = callbacks.AppendCborTag(\n value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__DETAIL_MASK);\n if (!ret_error_message.empty()) {\n goto done;\n }\n continue;\n }\n }\n\n fail:\n ret_error_message =\n \"wuffs_aux::CborDecoder: internal error: unexpected token\";\n goto done;\n\n parsed_a_value:\n if (!ret_error_message.empty() || (depth == 0)) {\n goto done;\n }\n }\n } while (false);\n\ndone:\n DecodeCborResult result(\n std::move(ret_error_message),\n wuffs_base__u64__sat_add(io_buf->meta.pos, cursor_index));\n callbacks.Done(result, input, *io_buf);\n return result;\n}\n\n} // namespace wuffs_aux\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__AUX__CBOR)\n" +
""
const AuxCborHh = "" +
"// ---------------- Auxiliary - CBOR\n\nnamespace wuffs_aux {\n\nstruct DecodeCborResult {\n DecodeCborResult(std::string&& error_message0, uint64_t cursor_position0);\n\n std::string error_message;\n uint64_t cursor_position;\n};\n\nclass DecodeCborCallbacks {\n public:\n // AppendXxx are called for leaf nodes: literals, numbers, strings, etc.\n\n virtual std::string AppendNull() = 0;\n virtual std::string AppendUndefined() = 0;\n virtual std::string AppendBool(bool val) = 0;\n virtual std::string AppendF64(double val) = 0;\n virtual std::string AppendI64(int64_t val) = 0;\n virtual std::string AppendU64(uint64_t val) = 0;\n virtual std::string AppendByteString(std::string&& val) = 0;\n virtual std::string AppendTextString(std::string&& val) = 0;\n virtual std::string AppendMinus1MinusX(uint64_t val) = 0;\n virtual std::string AppendCborSimpleValue(uint8_t val) = 0;\n virtual std::string AppendCborTag(uint64_t val) = 0;\n\n // Push and Pop are called for container nodes: CBOR arrays (lists) and CBOR\n // maps (dictiona" +
"ries).\n //\n // The flags bits combine exactly one of:\n // - WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_NONE\n // - WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST\n // - WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT\n // and exactly one of:\n // - WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE\n // - WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST\n // - WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT\n\n virtual std::string Push(uint32_t flags) = 0;\n virtual std::string Pop(uint32_t flags) = 0;\n\n // Done is always the last Callback method called by DecodeCbor, whether or\n // not parsing the input as CBOR encountered an error. Even when successful,\n // trailing data may remain in input and buffer.\n //\n // Do not keep a reference to buffer or buffer.data.ptr after Done returns,\n // as DecodeCbor may then de-allocate the backing array.\n virtual void Done(DecodeCborResult& result,\n sync_io::Input& input,\n IOBuffer& buffer);\n};\n\n// DecodeCbor calls callbacks based on the CBOR-form" +
"atted data in input.\n//\n// On success, the returned error_message is empty and cursor_position counts\n// the number of bytes consumed. On failure, error_message is non-empty and\n// cursor_position is the location of the error. That error may be a content\n// error (invalid CBOR) or an input error (e.g. network failure).\nDecodeCborResult DecodeCbor(DecodeCborCallbacks&& callbacks,\n sync_io::Input&& input,\n wuffs_base__slice_u32 quirks);\n\n} // namespace wuffs_aux\n" +
""
const AuxJsonCc = "" +
"// ---------------- Auxiliary - JSON\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__AUX__JSON)\n\n#include <utility>\n\nnamespace wuffs_aux {\n\nDecodeJsonResult::DecodeJsonResult(std::string&& error_message0,\n uint64_t cursor_position0)\n : error_message(std::move(error_message0)),\n cursor_position(cursor_position0) {}\n\nDecodeJsonResult //\nDecodeJson(DecodeJsonCallbacks&& callbacks,\n sync_io::Input&& input,\n wuffs_base__slice_u32 quirks) {\n // Prepare the wuffs_base__io_buffer and the resultant error_message.\n wuffs_base__io_buffer* io_buf = input.BringsItsOwnIOBuffer();\n wuffs_base__io_buffer fallback_io_buf = wuffs_base__empty_io_buffer();\n std::unique_ptr<uint8_t[]> fallback_io_array(nullptr);\n if (!io_buf) {\n fallback_io_array = std::unique_ptr<uint8_t[]>(new uint8_t[4096]);\n fallback_io_buf = wuffs_base__ptr_u8__writer(fallback_io_array.get(), 4096);\n io_buf = &fallback_io_buf;\n }\n size_t cursor_index = 0;\n s" +
"td::string ret_error_message;\n std::string io_error_message;\n\n do {\n // Prepare the low-level JSON decoder.\n wuffs_json__decoder::unique_ptr dec = wuffs_json__decoder::alloc();\n if (!dec) {\n ret_error_message = \"wuffs_aux::JsonDecoder: out of memory\";\n goto done;\n }\n for (size_t i = 0; i < quirks.len; i++) {\n dec->set_quirk_enabled(quirks.ptr[i], true);\n }\n\n // Prepare the wuffs_base__tok_buffer.\n wuffs_base__token tok_array[256];\n wuffs_base__token_buffer tok_buf =\n wuffs_base__slice_token__writer(wuffs_base__make_slice_token(\n &tok_array[0], (sizeof(tok_array) / sizeof(tok_array[0]))));\n wuffs_base__status tok_status = wuffs_base__make_status(nullptr);\n\n // Prepare other state.\n uint32_t depth = 0;\n std::string str;\n\n // Loop, doing these two things:\n // 1. Get the next token.\n // 2. Process that token.\n while (true) {\n // 1. Get the next token.\n\n while (tok_buf.meta.ri >= tok_buf.meta.wi) {\n if (tok_sta" +
@@ -644,10 +665,12 @@ const AuxJsonHh = "" +
""
var AuxNonBaseCcFiles = []string{
AuxCborCc,
AuxJsonCc,
}
var AuxNonBaseHhFiles = []string{
AuxCborHh,
AuxJsonHh,
}
+2
View File
@@ -99,6 +99,8 @@ func genData(out *bytes.Buffer) error {
{"../auxiliary/base.cc", "AuxBaseCc"},
{"../auxiliary/base.hh", "AuxBaseHh"},
{"../auxiliary/cbor.cc", "AuxCborCc"},
{"../auxiliary/cbor.hh", "AuxCborHh"},
{"../auxiliary/json.cc", "AuxJsonCc"},
{"../auxiliary/json.hh", "AuxJsonHh"},
}