• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             StringAddChar('\\');
419             if (escaped_string_was_key_) {
420               state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
421             } else {
422               state_ = State::GRPC_JSON_STATE_VALUE_STRING;
423             }
424             break;
425 
426           default:
427             return Status::GRPC_JSON_PARSE_ERROR;
428         }
429         break;
430 
431       default:
432         container_just_begun_ = false;
433         switch (state_) {
434           case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
435             if (c != '"') return Status::GRPC_JSON_PARSE_ERROR;
436             state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
437             break;
438 
439           case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
440             if (unicode_high_surrogate_ != 0) {
441               return Status::GRPC_JSON_PARSE_ERROR;
442             }
443             if (c == '"') {
444               state_ = State::GRPC_JSON_STATE_OBJECT_KEY_END;
445               SetKey();
446             } else {
447               if (c < 32) return Status::GRPC_JSON_PARSE_ERROR;
448               StringAddChar(c);
449             }
450             break;
451 
452           case State::GRPC_JSON_STATE_VALUE_STRING:
453             if (unicode_high_surrogate_ != 0) {
454               return Status::GRPC_JSON_PARSE_ERROR;
455             }
456             if (c == '"') {
457               state_ = State::GRPC_JSON_STATE_VALUE_END;
458               SetString();
459             } else {
460               if (c < 32) return Status::GRPC_JSON_PARSE_ERROR;
461               StringAddChar(c);
462             }
463             break;
464 
465           case State::GRPC_JSON_STATE_OBJECT_KEY_END:
466             if (c != ':') return Status::GRPC_JSON_PARSE_ERROR;
467             state_ = State::GRPC_JSON_STATE_VALUE_BEGIN;
468             break;
469 
470           case State::GRPC_JSON_STATE_VALUE_BEGIN:
471             switch (c) {
472               case 't':
473                 state_ = State::GRPC_JSON_STATE_VALUE_TRUE_R;
474                 break;
475 
476               case 'f':
477                 state_ = State::GRPC_JSON_STATE_VALUE_FALSE_A;
478                 break;
479 
480               case 'n':
481                 state_ = State::GRPC_JSON_STATE_VALUE_NULL_U;
482                 break;
483 
484               case '"':
485                 state_ = State::GRPC_JSON_STATE_VALUE_STRING;
486                 break;
487 
488               case '0':
489                 StringAddChar(c);
490                 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO;
491                 break;
492 
493               case '1':
494               case '2':
495               case '3':
496               case '4':
497               case '5':
498               case '6':
499               case '7':
500               case '8':
501               case '9':
502               case '-':
503                 StringAddChar(c);
504                 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER;
505                 break;
506 
507               case '{':
508                 container_just_begun_ = true;
509                 if (!StartContainer(Json::Type::OBJECT)) {
510                   return Status::GRPC_JSON_PARSE_ERROR;
511                 }
512                 state_ = State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
513                 break;
514 
515               case '[':
516                 container_just_begun_ = true;
517                 if (!StartContainer(Json::Type::ARRAY)) {
518                   return Status::GRPC_JSON_PARSE_ERROR;
519                 }
520                 break;
521               default:
522                 return Status::GRPC_JSON_PARSE_ERROR;
523             }
524             break;
525 
526           case State::GRPC_JSON_STATE_STRING_ESCAPE:
527             if (escaped_string_was_key_) {
528               state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
529             } else {
530               state_ = State::GRPC_JSON_STATE_VALUE_STRING;
531             }
532             if (unicode_high_surrogate_ && c != 'u') {
533               return Status::GRPC_JSON_PARSE_ERROR;
534             }
535             switch (c) {
536               case '"':
537               case '/':
538                 StringAddChar(c);
539                 break;
540               case 'b':
541                 StringAddChar('\b');
542                 break;
543               case 'f':
544                 StringAddChar('\f');
545                 break;
546               case 'n':
547                 StringAddChar('\n');
548                 break;
549               case 'r':
550                 StringAddChar('\r');
551                 break;
552               case 't':
553                 StringAddChar('\t');
554                 break;
555               case 'u':
556                 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U1;
557                 unicode_char_ = 0;
558                 break;
559               default:
560                 return Status::GRPC_JSON_PARSE_ERROR;
561             }
562             break;
563 
564           case State::GRPC_JSON_STATE_STRING_ESCAPE_U1:
565           case State::GRPC_JSON_STATE_STRING_ESCAPE_U2:
566           case State::GRPC_JSON_STATE_STRING_ESCAPE_U3:
567           case State::GRPC_JSON_STATE_STRING_ESCAPE_U4:
568             if ((c >= '0') && (c <= '9')) {
569               c -= '0';
570             } else if ((c >= 'A') && (c <= 'F')) {
571               c -= 'A' - 10;
572             } else if ((c >= 'a') && (c <= 'f')) {
573               c -= 'a' - 10;
574             } else {
575               return Status::GRPC_JSON_PARSE_ERROR;
576             }
577             unicode_char_ = static_cast<uint16_t>(unicode_char_ << 4);
578             unicode_char_ = static_cast<uint16_t>(unicode_char_ | c);
579 
580             switch (state_) {
581               case State::GRPC_JSON_STATE_STRING_ESCAPE_U1:
582                 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U2;
583                 break;
584               case State::GRPC_JSON_STATE_STRING_ESCAPE_U2:
585                 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U3;
586                 break;
587               case State::GRPC_JSON_STATE_STRING_ESCAPE_U3:
588                 state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U4;
589                 break;
590               case State::GRPC_JSON_STATE_STRING_ESCAPE_U4:
591                 /* See grpc_json_writer_escape_string to have a description
592                  * of what's going on here.
593                  */
594                 if ((unicode_char_ & 0xfc00) == 0xd800) {
595                   /* high surrogate utf-16 */
596                   if (unicode_high_surrogate_ != 0)
597                     return Status::GRPC_JSON_PARSE_ERROR;
598                   unicode_high_surrogate_ = unicode_char_;
599                 } else if ((unicode_char_ & 0xfc00) == 0xdc00) {
600                   /* low surrogate utf-16 */
601                   uint32_t utf32;
602                   if (unicode_high_surrogate_ == 0)
603                     return Status::GRPC_JSON_PARSE_ERROR;
604                   utf32 = 0x10000;
605                   utf32 += static_cast<uint32_t>(
606                       (unicode_high_surrogate_ - 0xd800) * 0x400);
607                   utf32 += static_cast<uint32_t>(unicode_char_ - 0xdc00);
608                   StringAddUtf32(utf32);
609                   unicode_high_surrogate_ = 0;
610                 } else {
611                   /* anything else */
612                   if (unicode_high_surrogate_ != 0)
613                     return Status::GRPC_JSON_PARSE_ERROR;
614                   StringAddUtf32(unicode_char_);
615                 }
616                 if (escaped_string_was_key_) {
617                   state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
618                 } else {
619                   state_ = State::GRPC_JSON_STATE_VALUE_STRING;
620                 }
621                 break;
622               default:
623                 GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
624             }
625             break;
626 
627           case State::GRPC_JSON_STATE_VALUE_NUMBER:
628             StringAddChar(c);
629             switch (c) {
630               case '0':
631               case '1':
632               case '2':
633               case '3':
634               case '4':
635               case '5':
636               case '6':
637               case '7':
638               case '8':
639               case '9':
640                 break;
641               case 'e':
642               case 'E':
643                 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_E;
644                 break;
645               case '.':
646                 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_DOT;
647                 break;
648               default:
649                 return Status::GRPC_JSON_PARSE_ERROR;
650             }
651             break;
652 
653           case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
654             StringAddChar(c);
655             switch (c) {
656               case '0':
657               case '1':
658               case '2':
659               case '3':
660               case '4':
661               case '5':
662               case '6':
663               case '7':
664               case '8':
665               case '9':
666                 break;
667               case 'e':
668               case 'E':
669                 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_E;
670                 break;
671               default:
672                 return Status::GRPC_JSON_PARSE_ERROR;
673             }
674             break;
675 
676           case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
677             if (c != '.') return Status::GRPC_JSON_PARSE_ERROR;
678             StringAddChar(c);
679             state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_DOT;
680             break;
681 
682           case State::GRPC_JSON_STATE_VALUE_NUMBER_DOT:
683             StringAddChar(c);
684             switch (c) {
685               case '0':
686               case '1':
687               case '2':
688               case '3':
689               case '4':
690               case '5':
691               case '6':
692               case '7':
693               case '8':
694               case '9':
695                 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL;
696                 break;
697               default:
698                 return Status::GRPC_JSON_PARSE_ERROR;
699             }
700             break;
701 
702           case State::GRPC_JSON_STATE_VALUE_NUMBER_E:
703             StringAddChar(c);
704             switch (c) {
705               case '0':
706               case '1':
707               case '2':
708               case '3':
709               case '4':
710               case '5':
711               case '6':
712               case '7':
713               case '8':
714               case '9':
715               case '+':
716               case '-':
717                 state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_EPM;
718                 break;
719               default:
720                 return Status::GRPC_JSON_PARSE_ERROR;
721             }
722             break;
723 
724           case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
725             StringAddChar(c);
726             switch (c) {
727               case '0':
728               case '1':
729               case '2':
730               case '3':
731               case '4':
732               case '5':
733               case '6':
734               case '7':
735               case '8':
736               case '9':
737                 break;
738               default:
739                 return Status::GRPC_JSON_PARSE_ERROR;
740             }
741             break;
742 
743           case State::GRPC_JSON_STATE_VALUE_TRUE_R:
744             if (c != 'r') return Status::GRPC_JSON_PARSE_ERROR;
745             state_ = State::GRPC_JSON_STATE_VALUE_TRUE_U;
746             break;
747 
748           case State::GRPC_JSON_STATE_VALUE_TRUE_U:
749             if (c != 'u') return Status::GRPC_JSON_PARSE_ERROR;
750             state_ = State::GRPC_JSON_STATE_VALUE_TRUE_E;
751             break;
752 
753           case State::GRPC_JSON_STATE_VALUE_TRUE_E:
754             if (c != 'e') return Status::GRPC_JSON_PARSE_ERROR;
755             SetTrue();
756             state_ = State::GRPC_JSON_STATE_VALUE_END;
757             break;
758 
759           case State::GRPC_JSON_STATE_VALUE_FALSE_A:
760             if (c != 'a') return Status::GRPC_JSON_PARSE_ERROR;
761             state_ = State::GRPC_JSON_STATE_VALUE_FALSE_L;
762             break;
763 
764           case State::GRPC_JSON_STATE_VALUE_FALSE_L:
765             if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
766             state_ = State::GRPC_JSON_STATE_VALUE_FALSE_S;
767             break;
768 
769           case State::GRPC_JSON_STATE_VALUE_FALSE_S:
770             if (c != 's') return Status::GRPC_JSON_PARSE_ERROR;
771             state_ = State::GRPC_JSON_STATE_VALUE_FALSE_E;
772             break;
773 
774           case State::GRPC_JSON_STATE_VALUE_FALSE_E:
775             if (c != 'e') return Status::GRPC_JSON_PARSE_ERROR;
776             SetFalse();
777             state_ = State::GRPC_JSON_STATE_VALUE_END;
778             break;
779 
780           case State::GRPC_JSON_STATE_VALUE_NULL_U:
781             if (c != 'u') return Status::GRPC_JSON_PARSE_ERROR;
782             state_ = State::GRPC_JSON_STATE_VALUE_NULL_L1;
783             break;
784 
785           case State::GRPC_JSON_STATE_VALUE_NULL_L1:
786             if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
787             state_ = State::GRPC_JSON_STATE_VALUE_NULL_L2;
788             break;
789 
790           case State::GRPC_JSON_STATE_VALUE_NULL_L2:
791             if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
792             SetNull();
793             state_ = State::GRPC_JSON_STATE_VALUE_END;
794             break;
795 
796           /* All of the VALUE_END cases are handled in the specialized case
797            * above. */
798           case State::GRPC_JSON_STATE_VALUE_END:
799             switch (c) {
800               case ',':
801               case '}':
802               case ']':
803                 GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
804                 break;
805 
806               default:
807                 return Status::GRPC_JSON_PARSE_ERROR;
808             }
809             break;
810 
811           case State::GRPC_JSON_STATE_END:
812             return Status::GRPC_JSON_PARSE_ERROR;
813         }
814     }
815   }
816 
817   GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
818 }
819 
Parse(absl::string_view input,Json * output)820 grpc_error* JsonReader::Parse(absl::string_view input, Json* output) {
821   JsonReader reader(input);
822   Status status = reader.Run();
823   if (reader.truncated_errors_) {
824     reader.errors_.push_back(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
825         "too many errors encountered during JSON parsing -- fix reported "
826         "errors and try again to see additional errors"));
827   }
828   if (status == Status::GRPC_JSON_INTERNAL_ERROR) {
829     reader.errors_.push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
830         absl::StrCat("internal error in JSON parser at index ",
831                      reader.CurrentIndex())
832             .c_str()));
833   } else if (status == Status::GRPC_JSON_PARSE_ERROR) {
834     reader.errors_.push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
835         absl::StrCat("JSON parse error at index ", reader.CurrentIndex())
836             .c_str()));
837   }
838   if (!reader.errors_.empty()) {
839     return GRPC_ERROR_CREATE_FROM_VECTOR("JSON parsing failed",
840                                          &reader.errors_);
841   }
842   *output = std::move(reader.root_value_);
843   return GRPC_ERROR_NONE;
844 }
845 
846 }  // namespace
847 
Parse(absl::string_view json_str,grpc_error ** error)848 Json Json::Parse(absl::string_view json_str, grpc_error** error) {
849   Json value;
850   *error = JsonReader::Parse(json_str, &value);
851   return value;
852 }
853 
854 }  // namespace grpc_core
855