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