Debian Codegraph @

src/core/lib/json/json_reader.cc

raw
1 //
2 // Copyright 2015-2016 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <grpc/support/port_platform.h>
18
19 #include <inttypes.h>
20 #include <stdlib.h>
21
22 #include <algorithm>
23 #include <map>
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 #include "absl/base/attributes.h"
29 #include "absl/status/status.h"
30 #include "absl/status/statusor.h"
31 #include "absl/strings/str_cat.h"
32 #include "absl/strings/str_format.h"
33 #include "absl/strings/str_join.h"
34 #include "absl/strings/string_view.h"
35
36 #include <grpc/support/log.h>
37
38 #include "src/core/lib/json/json.h"
39
40 #define GRPC_JSON_MAX_DEPTH 255
41 #define GRPC_JSON_MAX_ERRORS 16
42
43 namespace grpc_core {
44
45 namespace {
46
47 class JsonReader {
48 public:
49 static absl::StatusOr<Json> Parse(absl::string_view input);
50
51 private:
52 enum class Status {
53 GRPC_JSON_DONE, /* The parser finished successfully. */
54 GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */
55 GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */
56 };
57
58 enum class State {
59 GRPC_JSON_STATE_OBJECT_KEY_BEGIN,
60 GRPC_JSON_STATE_OBJECT_KEY_STRING,
61 GRPC_JSON_STATE_OBJECT_KEY_END,
62 GRPC_JSON_STATE_VALUE_BEGIN,
63 GRPC_JSON_STATE_VALUE_STRING,
64 GRPC_JSON_STATE_STRING_ESCAPE,
65 GRPC_JSON_STATE_STRING_ESCAPE_U1,
66 GRPC_JSON_STATE_STRING_ESCAPE_U2,
67 GRPC_JSON_STATE_STRING_ESCAPE_U3,
68 GRPC_JSON_STATE_STRING_ESCAPE_U4,
69 GRPC_JSON_STATE_VALUE_NUMBER,
70 GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL,
71 GRPC_JSON_STATE_VALUE_NUMBER_ZERO,
72 GRPC_JSON_STATE_VALUE_NUMBER_DOT,
73 GRPC_JSON_STATE_VALUE_NUMBER_E,
74 GRPC_JSON_STATE_VALUE_NUMBER_EPM,
75 GRPC_JSON_STATE_VALUE_TRUE_R,
76 GRPC_JSON_STATE_VALUE_TRUE_U,
77 GRPC_JSON_STATE_VALUE_TRUE_E,
78 GRPC_JSON_STATE_VALUE_FALSE_A,
79 GRPC_JSON_STATE_VALUE_FALSE_L,
80 GRPC_JSON_STATE_VALUE_FALSE_S,
81 GRPC_JSON_STATE_VALUE_FALSE_E,
82 GRPC_JSON_STATE_VALUE_NULL_U,
83 GRPC_JSON_STATE_VALUE_NULL_L1,
84 GRPC_JSON_STATE_VALUE_NULL_L2,
85 GRPC_JSON_STATE_VALUE_END,
86 GRPC_JSON_STATE_END
87 };
88
89 /* The first non-unicode value is 0x110000. But let's pick
90 * a value high enough to start our error codes from. These
91 * values are safe to return from the read_char function.
92 */
93 static constexpr uint32_t GRPC_JSON_READ_CHAR_EOF = 0x7ffffff0;
94
95 explicit JsonReader(absl::string_view input)
96 : original_input_(reinterpret_cast<const uint8_t*>(input.data())),
97 input_(original_input_),
98 remaining_input_(input.size()) {}
99
100 Status Run();
101 uint32_t ReadChar();
102 bool IsComplete();
103
104 size_t CurrentIndex() const { return input_ - original_input_ - 1; }
105
106 GRPC_MUST_USE_RESULT bool StringAddChar(uint32_t c);
107 GRPC_MUST_USE_RESULT bool StringAddUtf32(uint32_t c);
108
109 Json* CreateAndLinkValue();
110 bool StartContainer(Json::Type type);
111 void EndContainer();
112 void SetKey();
113 void SetString();
114 bool SetNumber();
115 void SetTrue();
116 void SetFalse();
117 void SetNull();
118
119 const uint8_t* original_input_;
120 const uint8_t* input_;
121 size_t remaining_input_;
122
123 State state_ = State::GRPC_JSON_STATE_VALUE_BEGIN;
124 bool escaped_string_was_key_ = false;
125 bool container_just_begun_ = false;
126 uint16_t unicode_char_ = 0;
127 uint16_t unicode_high_surrogate_ = 0;
128 std::vector<std::string> errors_;
129 bool truncated_errors_ = false;
130 uint8_t utf8_bytes_remaining_ = 0;
131 uint8_t utf8_first_byte_ = 0;
132
133 Json root_value_;
134 std::vector<Json*> stack_;
135
136 std::string key_;
137 std::string string_;
138 };
139
140 bool JsonReader::StringAddChar(uint32_t c) {
141 if (utf8_bytes_remaining_ == 0) {
142 if ((c & 0x80) == 0) {
143 utf8_bytes_remaining_ = 0;
144 } else if ((c & 0xe0) == 0xc0 && c >= 0xc2) {
145 /// For the UTF-8 characters with length of 2 bytes, the range of the
146 /// first byte is [0xc2, 0xdf]. Reference: Table 3-7 in
147 /// https://www.unicode.org/versions/Unicode14.0.0/ch03.pdf
148 utf8_bytes_remaining_ = 1;
149 } else if ((c & 0xf0) == 0xe0) {
150 utf8_bytes_remaining_ = 2;
151 } else if ((c & 0xf8) == 0xf0 && c <= 0xf4) {
152 /// For the UTF-8 characters with length of 4 bytes, the range of the
153 /// first byte is [0xf0, 0xf4]. Reference: Table 3-7 in
154 /// https://www.unicode.org/versions/Unicode14.0.0/ch03.pdf
155 utf8_bytes_remaining_ = 3;
156 } else {
157 return false;
158 }
159 utf8_first_byte_ = c;
160 } else if (utf8_bytes_remaining_ == 1) {
161 if ((c & 0xc0) != 0x80) {
162 return false;
163 }
164 --utf8_bytes_remaining_;
165 } else if (utf8_bytes_remaining_ == 2) {
166 /// For UTF-8 characters starting with 0xe0, their length is 3 bytes, and
167 /// the range of the second byte is [0xa0, 0xbf]. For UTF-8 characters
168 /// starting with 0xed, their length is 3 bytes, and the range of the second
169 /// byte is [0x80, 0x9f]. Reference: Table 3-7 in
170 /// https://www.unicode.org/versions/Unicode14.0.0/ch03.pdf
171 if (((c & 0xc0) != 0x80) || (utf8_first_byte_ == 0xe0 && c < 0xa0) ||
172 (utf8_first_byte_ == 0xed && c > 0x9f)) {
173 return false;
174 }
175 --utf8_bytes_remaining_;
176 } else if (utf8_bytes_remaining_ == 3) {
177 /// For UTF-8 characters starting with 0xf0, their length is 4 bytes, and
178 /// the range of the second byte is [0x90, 0xbf]. For UTF-8 characters
179 /// starting with 0xf4, their length is 4 bytes, and the range of the second
180 /// byte is [0x80, 0x8f]. Reference: Table 3-7 in
181 /// https://www.unicode.org/versions/Unicode14.0.0/ch03.pdf
182 if (((c & 0xc0) != 0x80) || (utf8_first_byte_ == 0xf0 && c < 0x90) ||
183 (utf8_first_byte_ == 0xf4 && c > 0x8f)) {
184 return false;
185 }
186 --utf8_bytes_remaining_;
187 } else {
188 abort();
189 }
190
191 string_.push_back(static_cast<uint8_t>(c));
192 return true;
193 }
194
195 bool JsonReader::StringAddUtf32(uint32_t c) {
196 if (c <= 0x7f) {
197 return StringAddChar(c);
198 } else if (c <= 0x7ff) {
199 uint32_t b1 = 0xc0 | ((c >> 6) & 0x1f);
200 uint32_t b2 = 0x80 | (c & 0x3f);
201 return StringAddChar(b1) && StringAddChar(b2);
202 } else if (c <= 0xffff) {
203 uint32_t b1 = 0xe0 | ((c >> 12) & 0x0f);
204 uint32_t b2 = 0x80 | ((c >> 6) & 0x3f);
205 uint32_t b3 = 0x80 | (c & 0x3f);
206 return StringAddChar(b1) && StringAddChar(b2) && StringAddChar(b3);
207 } else if (c <= 0x1fffff) {
208 uint32_t b1 = 0xf0 | ((c >> 18) & 0x07);
209 uint32_t b2 = 0x80 | ((c >> 12) & 0x3f);
210 uint32_t b3 = 0x80 | ((c >> 6) & 0x3f);
211 uint32_t b4 = 0x80 | (c & 0x3f);
212 return StringAddChar(b1) && StringAddChar(b2) && StringAddChar(b3) &&
213 StringAddChar(b4);
214 } else {
215 return false;
216 }
217 }
218
219 uint32_t JsonReader::ReadChar() {
220 if (remaining_input_ == 0) return GRPC_JSON_READ_CHAR_EOF;
221 const uint32_t r = *input_++;
222 --remaining_input_;
223 if (r == 0) {
224 remaining_input_ = 0;
225 return GRPC_JSON_READ_CHAR_EOF;
226 }
227 return r;
228 }
229
230 Json* JsonReader::CreateAndLinkValue() {
231 Json* value;
232 if (stack_.empty()) {
233 value = &root_value_;
234 } else {
235 Json* parent = stack_.back();
236 if (parent->type() == Json::Type::OBJECT) {
237 if (parent->object_value().find(key_) != parent->object_value().end()) {
238 if (errors_.size() == GRPC_JSON_MAX_ERRORS) {
239 truncated_errors_ = true;
240 } else {
241 errors_.push_back(absl::StrFormat(
242 "duplicate key \"%s\" at index %" PRIuPTR, key_, CurrentIndex()));
243 }
244 }
245 value = &(*parent->mutable_object())[std::move(key_)];
246 } else {
247 GPR_ASSERT(parent->type() == Json::Type::ARRAY);
248 parent->mutable_array()->emplace_back();
249 value = &parent->mutable_array()->back();
250 }
251 }
252 return value;
253 }
254
255 bool JsonReader::StartContainer(Json::Type type) {
256 if (stack_.size() == GRPC_JSON_MAX_DEPTH) {
257 if (errors_.size() == GRPC_JSON_MAX_ERRORS) {
258 truncated_errors_ = true;
259 } else {
260 errors_.push_back(
261 absl::StrFormat("exceeded max stack depth (%d) at index %" PRIuPTR,
262 GRPC_JSON_MAX_DEPTH, CurrentIndex()));
263 }
264 return false;
265 }
266 Json* value = CreateAndLinkValue();
267 if (type == Json::Type::OBJECT) {
268 *value = Json::Object();
269 } else {
270 GPR_ASSERT(type == Json::Type::ARRAY);
271 *value = Json::Array();
272 }
273 stack_.push_back(value);
274 return true;
275 }
276
277 void JsonReader::EndContainer() {
278 GPR_ASSERT(!stack_.empty());
279 stack_.pop_back();
280 }
281
282 void JsonReader::SetKey() {
283 key_ = std::move(string_);
284 string_.clear();
285 }
286
287 void JsonReader::SetString() {
288 Json* value = CreateAndLinkValue();
289 *value = std::move(string_);
290 string_.clear();
291 }
292
293 bool JsonReader::SetNumber() {
294 Json* value = CreateAndLinkValue();
295 *value = Json(string_, /*is_number=*/true);
296 string_.clear();
297 return true;
298 }
299
300 void JsonReader::SetTrue() {
301 Json* value = CreateAndLinkValue();
302 *value = true;
303 string_.clear();
304 }
305
306 void JsonReader::SetFalse() {
307 Json* value = CreateAndLinkValue();
308 *value = false;
309 string_.clear();
310 }
311
312 void JsonReader::SetNull() { CreateAndLinkValue(); }
313
314 bool JsonReader::IsComplete() {
315 return (stack_.empty() && (state_ == State::GRPC_JSON_STATE_END ||
316 state_ == State::GRPC_JSON_STATE_VALUE_END));
317 }
318
319 /* Call this function to start parsing the input. It will return the following:
320 * . GRPC_JSON_DONE if the input got eof, and the parsing finished
321 * successfully.
322 * . GRPC_JSON_PARSE_ERROR if the input was somehow invalid.
323 * . GRPC_JSON_INTERNAL_ERROR if the parser somehow ended into an invalid
324 * internal state.
325 */
326 JsonReader::Status JsonReader::Run() {
327 uint32_t c;
328
329 /* This state-machine is a strict implementation of ECMA-404 */
330 while (true) {
331 c = ReadChar();
332 switch (c) {
333 /* Let's process the error case first. */
334 case GRPC_JSON_READ_CHAR_EOF:
335 switch (state_) {
336 case State::GRPC_JSON_STATE_VALUE_NUMBER:
337 case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
338 case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
339 case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
340 if (!SetNumber()) return Status::GRPC_JSON_PARSE_ERROR;
341 state_ = State::GRPC_JSON_STATE_VALUE_END;
342 break;
343
344 default:
345 break;
346 }
347 if (IsComplete()) {
348 return Status::GRPC_JSON_DONE;
349 }
350 return Status::GRPC_JSON_PARSE_ERROR;
351
352 /* Processing whitespaces. */
353 case ' ':
354 case '\t':
355 case '\n':
356 case '\r':
357 switch (state_) {
358 case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
359 case State::GRPC_JSON_STATE_OBJECT_KEY_END:
360 case State::GRPC_JSON_STATE_VALUE_BEGIN:
361 case State::GRPC_JSON_STATE_VALUE_END:
362 case State::GRPC_JSON_STATE_END:
363 break;
364
365 case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
366 case State::GRPC_JSON_STATE_VALUE_STRING:
367 if (c != ' ') return Status::GRPC_JSON_PARSE_ERROR;
368 if (unicode_high_surrogate_ != 0) {
369 return Status::GRPC_JSON_PARSE_ERROR;
370 }
371 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
372 break;
373
374 case State::GRPC_JSON_STATE_VALUE_NUMBER:
375 case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
376 case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
377 case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
378 if (!SetNumber()) return Status::GRPC_JSON_PARSE_ERROR;
379 state_ = State::GRPC_JSON_STATE_VALUE_END;
380 break;
381
382 default:
383 return Status::GRPC_JSON_PARSE_ERROR;
384 }
385 break;
386
387 /* Value, object or array terminations. */
388 case ',':
389 case '}':
390 case ']':
391 switch (state_) {
392 case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
393 case State::GRPC_JSON_STATE_VALUE_STRING:
394 if (unicode_high_surrogate_ != 0) {
395 return Status::GRPC_JSON_PARSE_ERROR;
396 }
397 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
398 break;
399
400 case State::GRPC_JSON_STATE_VALUE_NUMBER:
401 case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
402 case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
403 case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
404 if (stack_.empty()) {
405 return Status::GRPC_JSON_PARSE_ERROR;
406 } else if (c == '}' &&
407 stack_.back()->type() != Json::Type::OBJECT) {
408 return Status::GRPC_JSON_PARSE_ERROR;
409 } else if (c == ']' && stack_.back()->type() != Json::Type::ARRAY) {
410 return Status::GRPC_JSON_PARSE_ERROR;
411 }
412 if (!SetNumber()) return Status::GRPC_JSON_PARSE_ERROR;
413 state_ = State::GRPC_JSON_STATE_VALUE_END;
414 ABSL_FALLTHROUGH_INTENDED;
415
416 case State::GRPC_JSON_STATE_VALUE_END:
417 case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
418 case State::GRPC_JSON_STATE_VALUE_BEGIN:
419 if (c == ',') {
420 if (state_ != State::GRPC_JSON_STATE_VALUE_END) {
421 return Status::GRPC_JSON_PARSE_ERROR;
422 }
423 if (!stack_.empty() &&
424 stack_.back()->type() == Json::Type::OBJECT) {
425 state_ = State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
426 } else if (!stack_.empty() &&
427 stack_.back()->type() == Json::Type::ARRAY) {
428 state_ = State::GRPC_JSON_STATE_VALUE_BEGIN;
429 } else {
430 return Status::GRPC_JSON_PARSE_ERROR;
431 }
432 } else {
433 if (stack_.empty()) {
434 return Status::GRPC_JSON_PARSE_ERROR;
435 }
436 if (c == '}' && stack_.back()->type() != Json::Type::OBJECT) {
437 return Status::GRPC_JSON_PARSE_ERROR;
438 }
439 if (c == '}' &&
440 state_ == State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN &&
441 !container_just_begun_) {
442 return Status::GRPC_JSON_PARSE_ERROR;
443 }
444 if (c == ']' && stack_.back()->type() != Json::Type::ARRAY) {
445 return Status::GRPC_JSON_PARSE_ERROR;
446 }
447 if (c == ']' && state_ == State::GRPC_JSON_STATE_VALUE_BEGIN &&
448 !container_just_begun_) {
449 return Status::GRPC_JSON_PARSE_ERROR;
450 }
451 state_ = State::GRPC_JSON_STATE_VALUE_END;
452 EndContainer();
453 if (stack_.empty()) {
454 state_ = State::GRPC_JSON_STATE_END;
455 }
456 }
457 break;
458
459 default:
460 return Status::GRPC_JSON_PARSE_ERROR;
461 }
462 break;
463
464 /* In-string escaping. */
465 case '\\':
466 switch (state_) {
467 case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
468 escaped_string_was_key_ = true;
469 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE;
470 break;
471
472 case State::GRPC_JSON_STATE_VALUE_STRING:
473 escaped_string_was_key_ = false;
474 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE;
475 break;
476
477 /* This is the \\ case. */
478 case State::GRPC_JSON_STATE_STRING_ESCAPE:
479 if (unicode_high_surrogate_ != 0) {
480 return Status::GRPC_JSON_PARSE_ERROR;
481 }
482 if (!StringAddChar('\\')) return Status::GRPC_JSON_PARSE_ERROR;
483 if (escaped_string_was_key_) {
484 state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
485 } else {
486 state_ = State::GRPC_JSON_STATE_VALUE_STRING;
487 }
488 break;
489
490 default:
491 return Status::GRPC_JSON_PARSE_ERROR;
492 }
493 break;
494
495 default:
496 container_just_begun_ = false;
497 switch (state_) {
498 case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
499 if (c != '"') return Status::GRPC_JSON_PARSE_ERROR;
500 state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
501 break;
502
503 case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
504 if (unicode_high_surrogate_ != 0) {
505 return Status::GRPC_JSON_PARSE_ERROR;
506 }
507 if (c == '"') {
508 state_ = State::GRPC_JSON_STATE_OBJECT_KEY_END;
509 // Once the key is parsed, there should no un-matched utf8
510 // encoded bytes.
511 if (utf8_bytes_remaining_ != 0) {
512 return Status::GRPC_JSON_PARSE_ERROR;
513 }
514 SetKey();
515 } else {
516 if (c < 32) return Status::GRPC_JSON_PARSE_ERROR;
517 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
518 }
519 break;
520
521 case State::GRPC_JSON_STATE_VALUE_STRING:
522 if (unicode_high_surrogate_ != 0) {
523 return Status::GRPC_JSON_PARSE_ERROR;
524 }
525 if (c == '"') {
526 state_ = State::GRPC_JSON_STATE_VALUE_END;
527 // Once the value is parsed, there should no un-matched utf8
528 // encoded bytes.
529 if (utf8_bytes_remaining_ != 0) {
530 return Status::GRPC_JSON_PARSE_ERROR;
531 }
532 SetString();
533 } else {
534 if (c < 32) return Status::GRPC_JSON_PARSE_ERROR;
535 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
536 }
537 break;
538
539 case State::GRPC_JSON_STATE_OBJECT_KEY_END:
540 if (c != ':') return Status::GRPC_JSON_PARSE_ERROR;
541 state_ = State::GRPC_JSON_STATE_VALUE_BEGIN;
542 break;
543
544 case State::GRPC_JSON_STATE_VALUE_BEGIN:
545 switch (c) {
546 case 't':
547 state_ = State::GRPC_JSON_STATE_VALUE_TRUE_R;
548 break;
549
550 case 'f':
551 state_ = State::GRPC_JSON_STATE_VALUE_FALSE_A;
552 break;
553
554 case 'n':
555 state_ = State::GRPC_JSON_STATE_VALUE_NULL_U;
556 break;
557
558 case '"':
559 state_ = State::GRPC_JSON_STATE_VALUE_STRING;
560 break;
561
562 case '0':
563 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
564 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO;
565 break;
566
567 case '1':
568 case '2':
569 case '3':
570 case '4':
571 case '5':
572 case '6':
573 case '7':
574 case '8':
575 case '9':
576 case '-':
577 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
578 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER;
579 break;
580
581 case '{':
582 container_just_begun_ = true;
583 if (!StartContainer(Json::Type::OBJECT)) {
584 return Status::GRPC_JSON_PARSE_ERROR;
585 }
586 state_ = State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
587 break;
588
589 case '[':
590 container_just_begun_ = true;
591 if (!StartContainer(Json::Type::ARRAY)) {
592 return Status::GRPC_JSON_PARSE_ERROR;
593 }
594 break;
595 default:
596 return Status::GRPC_JSON_PARSE_ERROR;
597 }
598 break;
599
600 case State::GRPC_JSON_STATE_STRING_ESCAPE:
601 if (escaped_string_was_key_) {
602 state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
603 } else {
604 state_ = State::GRPC_JSON_STATE_VALUE_STRING;
605 }
606 if (unicode_high_surrogate_ && c != 'u') {
607 return Status::GRPC_JSON_PARSE_ERROR;
608 }
609 switch (c) {
610 case '"':
611 case '/':
612 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
613 break;
614 case 'b':
615 if (!StringAddChar('\b')) return Status::GRPC_JSON_PARSE_ERROR;
616 break;
617 case 'f':
618 if (!StringAddChar('\f')) return Status::GRPC_JSON_PARSE_ERROR;
619 break;
620 case 'n':
621 if (!StringAddChar('\n')) return Status::GRPC_JSON_PARSE_ERROR;
622 break;
623 case 'r':
624 if (!StringAddChar('\r')) return Status::GRPC_JSON_PARSE_ERROR;
625 break;
626 case 't':
627 if (!StringAddChar('\t')) return Status::GRPC_JSON_PARSE_ERROR;
628 break;
629 case 'u':
630 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U1;
631 unicode_char_ = 0;
632 break;
633 default:
634 return Status::GRPC_JSON_PARSE_ERROR;
635 }
636 break;
637
638 case State::GRPC_JSON_STATE_STRING_ESCAPE_U1:
639 case State::GRPC_JSON_STATE_STRING_ESCAPE_U2:
640 case State::GRPC_JSON_STATE_STRING_ESCAPE_U3:
641 case State::GRPC_JSON_STATE_STRING_ESCAPE_U4:
642 if ((c >= '0') && (c <= '9')) {
643 c -= '0';
644 } else if ((c >= 'A') && (c <= 'F')) {
645 c -= 'A' - 10;
646 } else if ((c >= 'a') && (c <= 'f')) {
647 c -= 'a' - 10;
648 } else {
649 return Status::GRPC_JSON_PARSE_ERROR;
650 }
651 unicode_char_ = static_cast<uint16_t>(unicode_char_ << 4);
652 unicode_char_ = static_cast<uint16_t>(unicode_char_ | c);
653
654 switch (state_) {
655 case State::GRPC_JSON_STATE_STRING_ESCAPE_U1:
656 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U2;
657 break;
658 case State::GRPC_JSON_STATE_STRING_ESCAPE_U2:
659 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U3;
660 break;
661 case State::GRPC_JSON_STATE_STRING_ESCAPE_U3:
662 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U4;
663 break;
664 case State::GRPC_JSON_STATE_STRING_ESCAPE_U4:
665 /* See grpc_json_writer_escape_string to have a description
666 * of what's going on here.
667 */
668 if ((unicode_char_ & 0xfc00) == 0xd800) {
669 /* high surrogate utf-16 */
670 if (unicode_high_surrogate_ != 0) {
671 return Status::GRPC_JSON_PARSE_ERROR;
672 }
673 unicode_high_surrogate_ = unicode_char_;
674 } else if ((unicode_char_ & 0xfc00) == 0xdc00) {
675 /* low surrogate utf-16 */
676 uint32_t utf32;
677 if (unicode_high_surrogate_ == 0) {
678 return Status::GRPC_JSON_PARSE_ERROR;
679 }
680 utf32 = 0x10000;
681 utf32 += static_cast<uint32_t>(
682 (unicode_high_surrogate_ - 0xd800) * 0x400);
683 utf32 += static_cast<uint32_t>(unicode_char_ - 0xdc00);
684 if (!StringAddUtf32(utf32)) {
685 return Status::GRPC_JSON_PARSE_ERROR;
686 }
687 unicode_high_surrogate_ = 0;
688 } else {
689 /* anything else */
690 if (unicode_high_surrogate_ != 0) {
691 return Status::GRPC_JSON_PARSE_ERROR;
692 }
693 if (!StringAddUtf32(unicode_char_)) {
694 return Status::GRPC_JSON_PARSE_ERROR;
695 }
696 }
697 if (escaped_string_was_key_) {
698 state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
699 } else {
700 state_ = State::GRPC_JSON_STATE_VALUE_STRING;
701 }
702 break;
703 default:
704 GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
705 }
706 break;
707
708 case State::GRPC_JSON_STATE_VALUE_NUMBER:
709 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
710 switch (c) {
711 case '0':
712 case '1':
713 case '2':
714 case '3':
715 case '4':
716 case '5':
717 case '6':
718 case '7':
719 case '8':
720 case '9':
721 break;
722 case 'e':
723 case 'E':
724 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_E;
725 break;
726 case '.':
727 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_DOT;
728 break;
729 default:
730 return Status::GRPC_JSON_PARSE_ERROR;
731 }
732 break;
733
734 case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
735 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
736 switch (c) {
737 case '0':
738 case '1':
739 case '2':
740 case '3':
741 case '4':
742 case '5':
743 case '6':
744 case '7':
745 case '8':
746 case '9':
747 break;
748 case 'e':
749 case 'E':
750 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_E;
751 break;
752 default:
753 return Status::GRPC_JSON_PARSE_ERROR;
754 }
755 break;
756
757 case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
758 if (c != '.') return Status::GRPC_JSON_PARSE_ERROR;
759 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
760 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_DOT;
761 break;
762
763 case State::GRPC_JSON_STATE_VALUE_NUMBER_DOT:
764 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
765 switch (c) {
766 case '0':
767 case '1':
768 case '2':
769 case '3':
770 case '4':
771 case '5':
772 case '6':
773 case '7':
774 case '8':
775 case '9':
776 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL;
777 break;
778 default:
779 return Status::GRPC_JSON_PARSE_ERROR;
780 }
781 break;
782
783 case State::GRPC_JSON_STATE_VALUE_NUMBER_E:
784 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
785 switch (c) {
786 case '0':
787 case '1':
788 case '2':
789 case '3':
790 case '4':
791 case '5':
792 case '6':
793 case '7':
794 case '8':
795 case '9':
796 case '+':
797 case '-':
798 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_EPM;
799 break;
800 default:
801 return Status::GRPC_JSON_PARSE_ERROR;
802 }
803 break;
804
805 case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
806 if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
807 switch (c) {
808 case '0':
809 case '1':
810 case '2':
811 case '3':
812 case '4':
813 case '5':
814 case '6':
815 case '7':
816 case '8':
817 case '9':
818 break;
819 default:
820 return Status::GRPC_JSON_PARSE_ERROR;
821 }
822 break;
823
824 case State::GRPC_JSON_STATE_VALUE_TRUE_R:
825 if (c != 'r') return Status::GRPC_JSON_PARSE_ERROR;
826 state_ = State::GRPC_JSON_STATE_VALUE_TRUE_U;
827 break;
828
829 case State::GRPC_JSON_STATE_VALUE_TRUE_U:
830 if (c != 'u') return Status::GRPC_JSON_PARSE_ERROR;
831 state_ = State::GRPC_JSON_STATE_VALUE_TRUE_E;
832 break;
833
834 case State::GRPC_JSON_STATE_VALUE_TRUE_E:
835 if (c != 'e') return Status::GRPC_JSON_PARSE_ERROR;
836 SetTrue();
837 state_ = State::GRPC_JSON_STATE_VALUE_END;
838 break;
839
840 case State::GRPC_JSON_STATE_VALUE_FALSE_A:
841 if (c != 'a') return Status::GRPC_JSON_PARSE_ERROR;
842 state_ = State::GRPC_JSON_STATE_VALUE_FALSE_L;
843 break;
844
845 case State::GRPC_JSON_STATE_VALUE_FALSE_L:
846 if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
847 state_ = State::GRPC_JSON_STATE_VALUE_FALSE_S;
848 break;
849
850 case State::GRPC_JSON_STATE_VALUE_FALSE_S:
851 if (c != 's') return Status::GRPC_JSON_PARSE_ERROR;
852 state_ = State::GRPC_JSON_STATE_VALUE_FALSE_E;
853 break;
854
855 case State::GRPC_JSON_STATE_VALUE_FALSE_E:
856 if (c != 'e') return Status::GRPC_JSON_PARSE_ERROR;
857 SetFalse();
858 state_ = State::GRPC_JSON_STATE_VALUE_END;
859 break;
860
861 case State::GRPC_JSON_STATE_VALUE_NULL_U:
862 if (c != 'u') return Status::GRPC_JSON_PARSE_ERROR;
863 state_ = State::GRPC_JSON_STATE_VALUE_NULL_L1;
864 break;
865
866 case State::GRPC_JSON_STATE_VALUE_NULL_L1:
867 if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
868 state_ = State::GRPC_JSON_STATE_VALUE_NULL_L2;
869 break;
870
871 case State::GRPC_JSON_STATE_VALUE_NULL_L2:
872 if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
873 SetNull();
874 state_ = State::GRPC_JSON_STATE_VALUE_END;
875 break;
876
877 /* All of the VALUE_END cases are handled in the specialized case
878 * above. */
879 case State::GRPC_JSON_STATE_VALUE_END:
880 switch (c) {
881 case ',':
882 case '}':
883 case ']':
884 GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
885 break;
886
887 default:
888 return Status::GRPC_JSON_PARSE_ERROR;
889 }
890 break;
891
892 case State::GRPC_JSON_STATE_END:
893 return Status::GRPC_JSON_PARSE_ERROR;
894 }
895 }
896 }
897
898 GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
899 }
900
901 absl::StatusOr<Json> JsonReader::Parse(absl::string_view input) {
902 JsonReader reader(input);
903 Status status = reader.Run();
904 if (reader.truncated_errors_) {
905 reader.errors_.push_back(
906 "too many errors encountered during JSON parsing -- fix reported "
907 "errors and try again to see additional errors");
908 }
909 if (status == Status::GRPC_JSON_INTERNAL_ERROR) {
910 reader.errors_.push_back(absl::StrCat(
911 "internal error in JSON parser at index ", reader.CurrentIndex()));
912 } else if (status == Status::GRPC_JSON_PARSE_ERROR) {
913 reader.errors_.push_back(
914 absl::StrCat("JSON parse error at index ", reader.CurrentIndex()));
915 }
916 if (!reader.errors_.empty()) {
917 return absl::InvalidArgumentError(absl::StrCat(
918 "JSON parsing failed: [", absl::StrJoin(reader.errors_, "; "), "]"));
919 }
920 return std::move(reader.root_value_);
921 }
922
923 } // namespace
924
925 absl::StatusOr<Json> Json::Parse(absl::string_view json_str) {
926 return JsonReader::Parse(json_str);
927 }
928
929 } // namespace grpc_core