json_reader.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <string.h>
  34. #include <grpc/support/port_platform.h>
  35. #include <grpc/support/log.h>
  36. #include "src/core/lib/json/json_reader.h"
  37. static void json_reader_string_clear(grpc_json_reader *reader) {
  38. reader->vtable->string_clear(reader->userdata);
  39. }
  40. static void json_reader_string_add_char(grpc_json_reader *reader, uint32_t c) {
  41. reader->vtable->string_add_char(reader->userdata, c);
  42. }
  43. static void json_reader_string_add_utf32(grpc_json_reader *reader,
  44. uint32_t utf32) {
  45. reader->vtable->string_add_utf32(reader->userdata, utf32);
  46. }
  47. static uint32_t grpc_json_reader_read_char(grpc_json_reader *reader) {
  48. return reader->vtable->read_char(reader->userdata);
  49. }
  50. static void json_reader_container_begins(grpc_json_reader *reader,
  51. grpc_json_type type) {
  52. reader->vtable->container_begins(reader->userdata, type);
  53. }
  54. static grpc_json_type grpc_json_reader_container_ends(
  55. grpc_json_reader *reader) {
  56. return reader->vtable->container_ends(reader->userdata);
  57. }
  58. static void json_reader_set_key(grpc_json_reader *reader) {
  59. reader->vtable->set_key(reader->userdata);
  60. }
  61. static void json_reader_set_string(grpc_json_reader *reader) {
  62. reader->vtable->set_string(reader->userdata);
  63. }
  64. static int json_reader_set_number(grpc_json_reader *reader) {
  65. return reader->vtable->set_number(reader->userdata);
  66. }
  67. static void json_reader_set_true(grpc_json_reader *reader) {
  68. reader->vtable->set_true(reader->userdata);
  69. }
  70. static void json_reader_set_false(grpc_json_reader *reader) {
  71. reader->vtable->set_false(reader->userdata);
  72. }
  73. static void json_reader_set_null(grpc_json_reader *reader) {
  74. reader->vtable->set_null(reader->userdata);
  75. }
  76. /* Call this function to initialize the reader structure. */
  77. void grpc_json_reader_init(grpc_json_reader *reader,
  78. grpc_json_reader_vtable *vtable, void *userdata) {
  79. memset(reader, 0, sizeof(*reader));
  80. reader->vtable = vtable;
  81. reader->userdata = userdata;
  82. json_reader_string_clear(reader);
  83. reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
  84. }
  85. int grpc_json_reader_is_complete(grpc_json_reader *reader) {
  86. return ((reader->depth == 0) &&
  87. ((reader->state == GRPC_JSON_STATE_END) ||
  88. (reader->state == GRPC_JSON_STATE_VALUE_END)));
  89. }
  90. grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader) {
  91. uint32_t c, success;
  92. /* This state-machine is a strict implementation of ECMA-404 */
  93. for (;;) {
  94. c = grpc_json_reader_read_char(reader);
  95. switch (c) {
  96. /* Let's process the error cases first. */
  97. case GRPC_JSON_READ_CHAR_ERROR:
  98. return GRPC_JSON_READ_ERROR;
  99. case GRPC_JSON_READ_CHAR_EAGAIN:
  100. return GRPC_JSON_EAGAIN;
  101. case GRPC_JSON_READ_CHAR_EOF:
  102. if (grpc_json_reader_is_complete(reader)) {
  103. return GRPC_JSON_DONE;
  104. } else {
  105. return GRPC_JSON_PARSE_ERROR;
  106. }
  107. break;
  108. /* Processing whitespaces. */
  109. case ' ':
  110. case '\t':
  111. case '\n':
  112. case '\r':
  113. switch (reader->state) {
  114. case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
  115. case GRPC_JSON_STATE_OBJECT_KEY_END:
  116. case GRPC_JSON_STATE_VALUE_BEGIN:
  117. case GRPC_JSON_STATE_VALUE_END:
  118. case GRPC_JSON_STATE_END:
  119. break;
  120. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  121. case GRPC_JSON_STATE_VALUE_STRING:
  122. if (c != ' ') return GRPC_JSON_PARSE_ERROR;
  123. if (reader->unicode_high_surrogate != 0)
  124. return GRPC_JSON_PARSE_ERROR;
  125. json_reader_string_add_char(reader, c);
  126. break;
  127. case GRPC_JSON_STATE_VALUE_NUMBER:
  128. case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
  129. case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
  130. case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
  131. success = (uint32_t)json_reader_set_number(reader);
  132. if (!success) return GRPC_JSON_PARSE_ERROR;
  133. json_reader_string_clear(reader);
  134. reader->state = GRPC_JSON_STATE_VALUE_END;
  135. break;
  136. default:
  137. return GRPC_JSON_PARSE_ERROR;
  138. }
  139. break;
  140. /* Value, object or array terminations. */
  141. case ',':
  142. case '}':
  143. case ']':
  144. switch (reader->state) {
  145. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  146. case GRPC_JSON_STATE_VALUE_STRING:
  147. if (reader->unicode_high_surrogate != 0)
  148. return GRPC_JSON_PARSE_ERROR;
  149. json_reader_string_add_char(reader, c);
  150. break;
  151. case GRPC_JSON_STATE_VALUE_NUMBER:
  152. case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
  153. case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
  154. case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
  155. if (reader->depth == 0) {
  156. return GRPC_JSON_PARSE_ERROR;
  157. } else if ((c == '}') && !reader->in_object) {
  158. return GRPC_JSON_PARSE_ERROR;
  159. } else if ((c == ']') && !reader->in_array) {
  160. return GRPC_JSON_PARSE_ERROR;
  161. }
  162. success = (uint32_t)json_reader_set_number(reader);
  163. if (!success) return GRPC_JSON_PARSE_ERROR;
  164. json_reader_string_clear(reader);
  165. reader->state = GRPC_JSON_STATE_VALUE_END;
  166. /* The missing break here is intentional. */
  167. case GRPC_JSON_STATE_VALUE_END:
  168. case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
  169. case GRPC_JSON_STATE_VALUE_BEGIN:
  170. if (c == ',') {
  171. if (reader->state != GRPC_JSON_STATE_VALUE_END) {
  172. return GRPC_JSON_PARSE_ERROR;
  173. }
  174. if (reader->in_object) {
  175. reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
  176. } else if (reader->in_array) {
  177. reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
  178. } else {
  179. return GRPC_JSON_PARSE_ERROR;
  180. }
  181. } else {
  182. if (reader->depth-- == 0) return GRPC_JSON_PARSE_ERROR;
  183. if ((c == '}') && !reader->in_object) {
  184. return GRPC_JSON_PARSE_ERROR;
  185. }
  186. if ((c == '}') &&
  187. (reader->state == GRPC_JSON_STATE_OBJECT_KEY_BEGIN) &&
  188. !reader->container_just_begun) {
  189. return GRPC_JSON_PARSE_ERROR;
  190. }
  191. if ((c == ']') && !reader->in_array) return GRPC_JSON_PARSE_ERROR;
  192. if ((c == ']') &&
  193. (reader->state == GRPC_JSON_STATE_VALUE_BEGIN) &&
  194. !reader->container_just_begun) {
  195. return GRPC_JSON_PARSE_ERROR;
  196. }
  197. reader->state = GRPC_JSON_STATE_VALUE_END;
  198. switch (grpc_json_reader_container_ends(reader)) {
  199. case GRPC_JSON_OBJECT:
  200. reader->in_object = 1;
  201. reader->in_array = 0;
  202. break;
  203. case GRPC_JSON_ARRAY:
  204. reader->in_object = 0;
  205. reader->in_array = 1;
  206. break;
  207. case GRPC_JSON_TOP_LEVEL:
  208. GPR_ASSERT(reader->depth == 0);
  209. reader->in_object = 0;
  210. reader->in_array = 0;
  211. reader->state = GRPC_JSON_STATE_END;
  212. break;
  213. default:
  214. GPR_UNREACHABLE_CODE(return GRPC_JSON_INTERNAL_ERROR);
  215. }
  216. }
  217. break;
  218. default:
  219. return GRPC_JSON_PARSE_ERROR;
  220. }
  221. break;
  222. /* In-string escaping. */
  223. case '\\':
  224. switch (reader->state) {
  225. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  226. reader->escaped_string_was_key = 1;
  227. reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
  228. break;
  229. case GRPC_JSON_STATE_VALUE_STRING:
  230. reader->escaped_string_was_key = 0;
  231. reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
  232. break;
  233. /* This is the \\ case. */
  234. case GRPC_JSON_STATE_STRING_ESCAPE:
  235. if (reader->unicode_high_surrogate != 0)
  236. return GRPC_JSON_PARSE_ERROR;
  237. json_reader_string_add_char(reader, '\\');
  238. if (reader->escaped_string_was_key) {
  239. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  240. } else {
  241. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  242. }
  243. break;
  244. default:
  245. return GRPC_JSON_PARSE_ERROR;
  246. }
  247. break;
  248. default:
  249. reader->container_just_begun = 0;
  250. switch (reader->state) {
  251. case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
  252. if (c != '"') return GRPC_JSON_PARSE_ERROR;
  253. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  254. break;
  255. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  256. if (reader->unicode_high_surrogate != 0)
  257. return GRPC_JSON_PARSE_ERROR;
  258. if (c == '"') {
  259. reader->state = GRPC_JSON_STATE_OBJECT_KEY_END;
  260. json_reader_set_key(reader);
  261. json_reader_string_clear(reader);
  262. } else {
  263. if (c < 32) return GRPC_JSON_PARSE_ERROR;
  264. json_reader_string_add_char(reader, c);
  265. }
  266. break;
  267. case GRPC_JSON_STATE_VALUE_STRING:
  268. if (reader->unicode_high_surrogate != 0)
  269. return GRPC_JSON_PARSE_ERROR;
  270. if (c == '"') {
  271. reader->state = GRPC_JSON_STATE_VALUE_END;
  272. json_reader_set_string(reader);
  273. json_reader_string_clear(reader);
  274. } else {
  275. if (c < 32) return GRPC_JSON_PARSE_ERROR;
  276. json_reader_string_add_char(reader, c);
  277. }
  278. break;
  279. case GRPC_JSON_STATE_OBJECT_KEY_END:
  280. if (c != ':') return GRPC_JSON_PARSE_ERROR;
  281. reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
  282. break;
  283. case GRPC_JSON_STATE_VALUE_BEGIN:
  284. switch (c) {
  285. case 't':
  286. reader->state = GRPC_JSON_STATE_VALUE_TRUE_R;
  287. break;
  288. case 'f':
  289. reader->state = GRPC_JSON_STATE_VALUE_FALSE_A;
  290. break;
  291. case 'n':
  292. reader->state = GRPC_JSON_STATE_VALUE_NULL_U;
  293. break;
  294. case '"':
  295. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  296. break;
  297. case '0':
  298. json_reader_string_add_char(reader, c);
  299. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_ZERO;
  300. break;
  301. case '1':
  302. case '2':
  303. case '3':
  304. case '4':
  305. case '5':
  306. case '6':
  307. case '7':
  308. case '8':
  309. case '9':
  310. case '-':
  311. json_reader_string_add_char(reader, c);
  312. reader->state = GRPC_JSON_STATE_VALUE_NUMBER;
  313. break;
  314. case '{':
  315. reader->container_just_begun = 1;
  316. json_reader_container_begins(reader, GRPC_JSON_OBJECT);
  317. reader->depth++;
  318. reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
  319. reader->in_object = 1;
  320. reader->in_array = 0;
  321. break;
  322. case '[':
  323. reader->container_just_begun = 1;
  324. json_reader_container_begins(reader, GRPC_JSON_ARRAY);
  325. reader->depth++;
  326. reader->in_object = 0;
  327. reader->in_array = 1;
  328. break;
  329. default:
  330. return GRPC_JSON_PARSE_ERROR;
  331. }
  332. break;
  333. case GRPC_JSON_STATE_STRING_ESCAPE:
  334. if (reader->escaped_string_was_key) {
  335. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  336. } else {
  337. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  338. }
  339. if (reader->unicode_high_surrogate && c != 'u')
  340. return GRPC_JSON_PARSE_ERROR;
  341. switch (c) {
  342. case '"':
  343. case '/':
  344. json_reader_string_add_char(reader, c);
  345. break;
  346. case 'b':
  347. json_reader_string_add_char(reader, '\b');
  348. break;
  349. case 'f':
  350. json_reader_string_add_char(reader, '\f');
  351. break;
  352. case 'n':
  353. json_reader_string_add_char(reader, '\n');
  354. break;
  355. case 'r':
  356. json_reader_string_add_char(reader, '\r');
  357. break;
  358. case 't':
  359. json_reader_string_add_char(reader, '\t');
  360. break;
  361. case 'u':
  362. reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U1;
  363. reader->unicode_char = 0;
  364. break;
  365. default:
  366. return GRPC_JSON_PARSE_ERROR;
  367. }
  368. break;
  369. case GRPC_JSON_STATE_STRING_ESCAPE_U1:
  370. case GRPC_JSON_STATE_STRING_ESCAPE_U2:
  371. case GRPC_JSON_STATE_STRING_ESCAPE_U3:
  372. case GRPC_JSON_STATE_STRING_ESCAPE_U4:
  373. if ((c >= '0') && (c <= '9')) {
  374. c -= '0';
  375. } else if ((c >= 'A') && (c <= 'F')) {
  376. c -= 'A' - 10;
  377. } else if ((c >= 'a') && (c <= 'f')) {
  378. c -= 'a' - 10;
  379. } else {
  380. return GRPC_JSON_PARSE_ERROR;
  381. }
  382. reader->unicode_char = (uint16_t)(reader->unicode_char << 4);
  383. reader->unicode_char = (uint16_t)(reader->unicode_char | c);
  384. switch (reader->state) {
  385. case GRPC_JSON_STATE_STRING_ESCAPE_U1:
  386. reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U2;
  387. break;
  388. case GRPC_JSON_STATE_STRING_ESCAPE_U2:
  389. reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U3;
  390. break;
  391. case GRPC_JSON_STATE_STRING_ESCAPE_U3:
  392. reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U4;
  393. break;
  394. case GRPC_JSON_STATE_STRING_ESCAPE_U4:
  395. /* See grpc_json_writer_escape_string to have a description
  396. * of what's going on here.
  397. */
  398. if ((reader->unicode_char & 0xfc00) == 0xd800) {
  399. /* high surrogate utf-16 */
  400. if (reader->unicode_high_surrogate != 0)
  401. return GRPC_JSON_PARSE_ERROR;
  402. reader->unicode_high_surrogate = reader->unicode_char;
  403. } else if ((reader->unicode_char & 0xfc00) == 0xdc00) {
  404. /* low surrogate utf-16 */
  405. uint32_t utf32;
  406. if (reader->unicode_high_surrogate == 0)
  407. return GRPC_JSON_PARSE_ERROR;
  408. utf32 = 0x10000;
  409. utf32 += (uint32_t)(
  410. (reader->unicode_high_surrogate - 0xd800) * 0x400);
  411. utf32 += (uint32_t)(reader->unicode_char - 0xdc00);
  412. json_reader_string_add_utf32(reader, utf32);
  413. reader->unicode_high_surrogate = 0;
  414. } else {
  415. /* anything else */
  416. if (reader->unicode_high_surrogate != 0)
  417. return GRPC_JSON_PARSE_ERROR;
  418. json_reader_string_add_utf32(reader, reader->unicode_char);
  419. }
  420. if (reader->escaped_string_was_key) {
  421. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  422. } else {
  423. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  424. }
  425. break;
  426. default:
  427. GPR_UNREACHABLE_CODE(return GRPC_JSON_INTERNAL_ERROR);
  428. }
  429. break;
  430. case GRPC_JSON_STATE_VALUE_NUMBER:
  431. json_reader_string_add_char(reader, c);
  432. switch (c) {
  433. case '0':
  434. case '1':
  435. case '2':
  436. case '3':
  437. case '4':
  438. case '5':
  439. case '6':
  440. case '7':
  441. case '8':
  442. case '9':
  443. break;
  444. case 'e':
  445. case 'E':
  446. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
  447. break;
  448. case '.':
  449. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
  450. break;
  451. default:
  452. return GRPC_JSON_PARSE_ERROR;
  453. }
  454. break;
  455. case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
  456. json_reader_string_add_char(reader, c);
  457. switch (c) {
  458. case '0':
  459. case '1':
  460. case '2':
  461. case '3':
  462. case '4':
  463. case '5':
  464. case '6':
  465. case '7':
  466. case '8':
  467. case '9':
  468. break;
  469. case 'e':
  470. case 'E':
  471. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
  472. break;
  473. default:
  474. return GRPC_JSON_PARSE_ERROR;
  475. }
  476. break;
  477. case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
  478. if (c != '.') return GRPC_JSON_PARSE_ERROR;
  479. json_reader_string_add_char(reader, c);
  480. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
  481. break;
  482. case GRPC_JSON_STATE_VALUE_NUMBER_DOT:
  483. json_reader_string_add_char(reader, c);
  484. switch (c) {
  485. case '0':
  486. case '1':
  487. case '2':
  488. case '3':
  489. case '4':
  490. case '5':
  491. case '6':
  492. case '7':
  493. case '8':
  494. case '9':
  495. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL;
  496. break;
  497. default:
  498. return GRPC_JSON_PARSE_ERROR;
  499. }
  500. break;
  501. case GRPC_JSON_STATE_VALUE_NUMBER_E:
  502. json_reader_string_add_char(reader, c);
  503. switch (c) {
  504. case '0':
  505. case '1':
  506. case '2':
  507. case '3':
  508. case '4':
  509. case '5':
  510. case '6':
  511. case '7':
  512. case '8':
  513. case '9':
  514. case '+':
  515. case '-':
  516. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_EPM;
  517. break;
  518. default:
  519. return GRPC_JSON_PARSE_ERROR;
  520. }
  521. break;
  522. case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
  523. json_reader_string_add_char(reader, c);
  524. switch (c) {
  525. case '0':
  526. case '1':
  527. case '2':
  528. case '3':
  529. case '4':
  530. case '5':
  531. case '6':
  532. case '7':
  533. case '8':
  534. case '9':
  535. break;
  536. default:
  537. return GRPC_JSON_PARSE_ERROR;
  538. }
  539. break;
  540. case GRPC_JSON_STATE_VALUE_TRUE_R:
  541. if (c != 'r') return GRPC_JSON_PARSE_ERROR;
  542. reader->state = GRPC_JSON_STATE_VALUE_TRUE_U;
  543. break;
  544. case GRPC_JSON_STATE_VALUE_TRUE_U:
  545. if (c != 'u') return GRPC_JSON_PARSE_ERROR;
  546. reader->state = GRPC_JSON_STATE_VALUE_TRUE_E;
  547. break;
  548. case GRPC_JSON_STATE_VALUE_TRUE_E:
  549. if (c != 'e') return GRPC_JSON_PARSE_ERROR;
  550. json_reader_set_true(reader);
  551. reader->state = GRPC_JSON_STATE_VALUE_END;
  552. break;
  553. case GRPC_JSON_STATE_VALUE_FALSE_A:
  554. if (c != 'a') return GRPC_JSON_PARSE_ERROR;
  555. reader->state = GRPC_JSON_STATE_VALUE_FALSE_L;
  556. break;
  557. case GRPC_JSON_STATE_VALUE_FALSE_L:
  558. if (c != 'l') return GRPC_JSON_PARSE_ERROR;
  559. reader->state = GRPC_JSON_STATE_VALUE_FALSE_S;
  560. break;
  561. case GRPC_JSON_STATE_VALUE_FALSE_S:
  562. if (c != 's') return GRPC_JSON_PARSE_ERROR;
  563. reader->state = GRPC_JSON_STATE_VALUE_FALSE_E;
  564. break;
  565. case GRPC_JSON_STATE_VALUE_FALSE_E:
  566. if (c != 'e') return GRPC_JSON_PARSE_ERROR;
  567. json_reader_set_false(reader);
  568. reader->state = GRPC_JSON_STATE_VALUE_END;
  569. break;
  570. case GRPC_JSON_STATE_VALUE_NULL_U:
  571. if (c != 'u') return GRPC_JSON_PARSE_ERROR;
  572. reader->state = GRPC_JSON_STATE_VALUE_NULL_L1;
  573. break;
  574. case GRPC_JSON_STATE_VALUE_NULL_L1:
  575. if (c != 'l') return GRPC_JSON_PARSE_ERROR;
  576. reader->state = GRPC_JSON_STATE_VALUE_NULL_L2;
  577. break;
  578. case GRPC_JSON_STATE_VALUE_NULL_L2:
  579. if (c != 'l') return GRPC_JSON_PARSE_ERROR;
  580. json_reader_set_null(reader);
  581. reader->state = GRPC_JSON_STATE_VALUE_END;
  582. break;
  583. /* All of the VALUE_END cases are handled in the specialized case
  584. * above. */
  585. case GRPC_JSON_STATE_VALUE_END:
  586. switch (c) {
  587. case ',':
  588. case '}':
  589. case ']':
  590. GPR_UNREACHABLE_CODE(return GRPC_JSON_INTERNAL_ERROR);
  591. break;
  592. default:
  593. return GRPC_JSON_PARSE_ERROR;
  594. }
  595. break;
  596. case GRPC_JSON_STATE_END:
  597. return GRPC_JSON_PARSE_ERROR;
  598. }
  599. }
  600. }
  601. GPR_UNREACHABLE_CODE(return GRPC_JSON_INTERNAL_ERROR);
  602. }