20 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
21 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
22 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
23 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
24 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
25 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
26 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
27 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
33 for (
int t = 0;
t < 16; ++
t)
35 for (
int t = 16;
t < 64; ++
t) {
41 for (
int t = 0;
t < 64; ++
t) {
57 h[0] +=
a;
h[1] +=
b;
h[2] +=
c;
h[3] += d;
h[4] +=
e;
h[5] +=
f;
h[6] += g;
h[7] +=
k;
63 enum Type { Null, False, True, Number, String, Array, Object }
type = Null;
68 std::vector<std::pair<std::string, Value>>
members;
77 out +=
static_cast<char>(
cp);
78 }
else if (
cp < 0x800) {
79 out +=
static_cast<char>(0xC0 | (
cp >> 6));
80 out +=
static_cast<char>(0x80 | (
cp & 0x3F));
81 }
else if (
cp < 0x10000) {
82 out +=
static_cast<char>(0xE0 | (
cp >> 12));
83 out +=
static_cast<char>(0x80 | ((
cp >> 6) & 0x3F));
84 out +=
static_cast<char>(0x80 | (
cp & 0x3F));
86 out +=
static_cast<char>(0xF0 | (
cp >> 18));
87 out +=
static_cast<char>(0x80 | ((
cp >> 12) & 0x3F));
88 out +=
static_cast<char>(0x80 | ((
cp >> 6) & 0x3F));
89 out +=
static_cast<char>(0x80 | (
cp & 0x3F));
96 explicit Parser(
const std::string&
s) :
s_(
s) {}
102 if (i_ !=
s_.size()) fail(
"not JSON: text after the value at byte " + std::to_string(i_));
107 [[
noreturn]]
void fail(
const std::string&
why)
const {
throw NoDigest{
why}; }
110 while (i_ <
s_.size() && (s_[i_] ==
' ' || s_[i_] ==
'\t' || s_[i_] ==
'\n' || s_[i_] ==
'\r')) ++
i_;
113 bool lit(
const char*
w) {
114 const size_t n = std::strlen(
w);
115 if (
s_.compare(i_,
n,
w) != 0)
return false;
121 if (
depth > 10000) fail(
"not JSON: nested too deeply");
122 if (i_ >=
s_.size()) fail(
"not JSON: a value expected at the end");
123 const char c =
s_[
i_];
126 v.type = Value::Object;
129 if (i_ <
s_.size() && s_[i_] ==
'}') {
135 if (i_ >=
s_.size() || s_[i_] !=
'"') fail(
"not JSON: a member name expected at byte " + std::to_string(i_));
140 if (i_ >=
s_.size() || s_[i_] !=
':') fail(
"not JSON: ':' expected at byte " + std::to_string(i_));
144 v.members.emplace_back(std::move(
name), std::move(
item));
146 if (i_ <
s_.size() && s_[i_] ==
',') {
150 if (i_ <
s_.size() && s_[i_] ==
'}') {
154 fail(
"not JSON: ',' or '}' expected at byte " + std::to_string(i_));
158 v.type = Value::Array;
161 if (i_ <
s_.size() && s_[i_] ==
']') {
169 if (i_ <
s_.size() && s_[i_] ==
',') {
173 if (i_ <
s_.size() && s_[i_] ==
']') {
177 fail(
"not JSON: ',' or ']' expected at byte " + std::to_string(i_));
181 v.type = Value::String;
186 v.type = Value::True;
190 v.type = Value::False;
193 if (
lit(
"null"))
return v;
194 if (
c ==
'-' || (
c >=
'0' &&
c <=
'9'))
return number();
195 fail(
"not JSON: unexpected character at byte " + std::to_string(i_));
206 const size_t d0 =
i_;
207 while (i_ <
s_.size() && s_[i_] >=
'0' && s_[i_] <=
'9') ++
i_;
211 if (i_ <
s_.size() && s_[i_] ==
'0') {
213 }
else if (
digits() == 0) {
214 fail(
"not JSON: a malformed number at byte " + std::to_string(
b));
218 if (i_ <
s_.size() && s_[i_] ==
'.') {
221 if (
digits() == 0) fail(
"not JSON: a malformed number at byte " + std::to_string(
b));
223 if (i_ <
s_.size() && (s_[i_] ==
'e' || s_[i_] ==
'E')) {
226 if (i_ <
s_.size() && (s_[i_] ==
'+' || s_[i_] ==
'-')) ++
i_;
227 if (
digits() == 0) fail(
"not JSON: a malformed number at byte " + std::to_string(
b));
229 const std::string
tok =
s_.substr(
b, i_ -
b);
231 v.type = Value::Number;
235 static const std::string
kLimit =
"9007199254740992";
237 fail(
"the integer " +
tok +
" is outside I-JSON's exact range (2^53)");
238 v.number = std::strtod(
int_digits.c_str(),
nullptr);
239 if (
neg &&
v.number != 0.0)
v.number = -
v.number;
242 v.number = std::strtod(
tok.c_str(),
nullptr);
243 if (!std::isfinite(
v.number)) fail(
"the number " +
tok +
" is not finite in binary64");
248 if (i_ + 4 >
s_.size()) fail(
"not JSON: a short \\u escape");
250 for (
int k = 0;
k < 4; ++
k) {
251 const char c =
s_[
i_++];
253 if (
c >=
'0' &&
c <=
'9')
255 else if (
c >=
'a' &&
c <=
'f')
257 else if (
c >=
'A' &&
c <=
'F')
260 fail(
"not JSON: a malformed \\u escape");
270 if (i_ >=
s_.size()) fail(
"not JSON: an unterminated string");
271 const unsigned char c =
static_cast<unsigned char>(
s_[
i_]);
276 if (
c < 0x20) fail(
"not JSON: a control character in a string");
279 if (i_ >=
s_.size()) fail(
"not JSON: an unterminated string");
280 const char e =
s_[
i_++];
282 case '"':
out +=
'"';
break;
283 case '\\':
out +=
'\\';
break;
284 case '/':
out +=
'/';
break;
285 case 'b':
out +=
'\b';
break;
286 case 'f':
out +=
'\f';
break;
287 case 'n':
out +=
'\n';
break;
288 case 'r':
out +=
'\r';
break;
289 case 't':
out +=
'\t';
break;
292 if (
cp >= 0xDC00 &&
cp <= 0xDFFF) fail(
"a string holds a lone surrogate");
293 if (
cp >= 0xD800 &&
cp <= 0xDBFF) {
294 if (i_ + 6 >
s_.size() || s_[i_] !=
'\\' || s_[i_ + 1] !=
'u') fail(
"a string holds a lone surrogate");
298 cp = 0x10000 + ((
cp - 0xD800) << 10) + (
lo - 0xDC00);
303 default: fail(
"not JSON: an unknown escape in a string");
313 }
else if ((
c & 0xE0) == 0xC0) {
316 }
else if ((
c & 0xF0) == 0xE0) {
319 }
else if ((
c & 0xF8) == 0xF0) {
323 fail(
"a string is not well-formed UTF-8");
325 if (i_ +
n >
s_.size()) fail(
"a string is not well-formed UTF-8");
326 for (
int k = 1;
k <
n; ++
k) {
327 const unsigned char cc =
static_cast<unsigned char>(
s_[
i_ +
k]);
328 if ((
cc & 0xC0) != 0x80) fail(
"a string is not well-formed UTF-8");
329 cp = (
cp << 6) | (
cc & 0x3F);
331 static const uint32_t kMin[5] = {0, 0, 0x80, 0x800, 0x10000};
332 if (
cp <
kMin[
n] ||
cp > 0x10FFFF || (
cp >= 0xD800 &&
cp <= 0xDFFF)) fail(
"a string is not well-formed UTF-8");
333 out.append(s_, i_,
n);
338 const std::string&
s_;
344 case Value::Number:
return !
v.integer &&
v.number == 0.0 && std::signbit(
v.number);
346 for (
const auto&
x :
v.items)
350 for (
const auto&
m :
v.members)
353 default:
return false;
358std::vector<uint16_t>
utf16(
const std::string&
s) {
359 std::vector<uint16_t>
out;
360 for (
size_t i = 0;
i <
s.size();) {
361 const unsigned char c =
static_cast<unsigned char>(
s[
i]);
367 }
else if ((
c & 0xE0) == 0xC0) {
370 }
else if ((
c & 0xF0) == 0xE0) {
393 static const char*
hex =
"0123456789abcdef";
395 for (
const char ch :
s) {
396 const unsigned char c =
static_cast<unsigned char>(
ch);
398 case '"':
out +=
"\\\"";
break;
399 case '\\':
out +=
"\\\\";
break;
400 case '\b':
out +=
"\\b";
break;
401 case '\t':
out +=
"\\t";
break;
402 case '\n':
out +=
"\\n";
break;
403 case '\f':
out +=
"\\f";
break;
404 case '\r':
out +=
"\\r";
break;
420 case Value::Null:
out +=
"null";
return;
421 case Value::False:
out +=
"false";
return;
422 case Value::True:
out +=
"true";
return;
427 for (
size_t k = 0;
k <
v.items.size(); ++
k) {
434 case Value::Object: {
435 std::vector<std::pair<std::vector<uint16_t>,
const std::pair<std::string, Value>*>>
keep;
438 std::sort(
keep.begin(),
keep.end(), [](
const auto&
a,
const auto&
b) { return a.first < b.first; });
440 for (
size_t k = 0;
k <
keep.size(); ++
k) {
455 uint32_t h[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
458 msg +=
static_cast<char>(0x80);
459 while (
msg.size() % 64 != 56)
msg +=
'\0';
460 for (
int k = 7;
k >= 0; --
k)
msg +=
static_cast<char>((
bits >> (8 *
k)) & 0xFF);
462 static const char*
hex =
"0123456789abcdef";
465 for (
int k = 7;
k >= 0; --
k)
out +=
hex[(
x >> (4 *
k)) & 15];
473 if (
v == 0.0)
return "0";
476 const auto r = std::to_chars(
buf,
buf +
sizeof(
buf),
v, std::chars_format::scientific);
477 if (
r.ec != std::errc())
throw std::runtime_error(
"es_number_string: to_chars failed");
478 const std::string
sci(
buf,
r.ptr);
479 const size_t e =
sci.find(
'e');
481 for (
size_t i = 0;
i <
e; ++
i)
483 const int exp10 = std::atoi(
sci.c_str() +
e + 1);
484 const int k =
static_cast<int>(
digits.size()),
n =
exp10 + 1;
485 if (
k <=
n &&
n <= 21)
return digits + std::string(
static_cast<size_t>(
n -
k),
'0');
487 if (-6 <
n &&
n <= 0)
return "0." + std::string(
static_cast<size_t>(-
n),
'0') +
digits;
488 const int ex =
n - 1;
491 out +=
ex >= 0 ?
"e+" :
"e-";
492 out += std::to_string(
ex >= 0 ?
ex : -
ex);
499 const Value
v = Parser(
text).document();
500 if (
holds_negative_zero(
v))
throw NoDigest{
"a negative zero (RFC 8785 writes it as 0, so it would share +0's digest)"};
502 }
catch (
const NoDigest&
e) {
const float * p[kIrDepthSourcesMax]
std::vector< Value > items
enum ember_ir::@44::Value::Type type
std::vector< std::pair< std::string, Value > > members
DigestResult ir_digest(const std::string &text, bool prose)
std::string sha256_hex_bytes(const std::string &bytes)
std::string es_number_string(double v)
void append_utf8(uint32_t cpt, std::string &out)