json_reader.cc 27 KB

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