json_reader.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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/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,
  41. gpr_uint32 c) {
  42. reader->vtable->string_add_char(reader->userdata, c);
  43. }
  44. static void json_reader_string_add_utf32(grpc_json_reader *reader,
  45. gpr_uint32 utf32) {
  46. reader->vtable->string_add_utf32(reader->userdata, utf32);
  47. }
  48. static gpr_uint32 grpc_json_reader_read_char(grpc_json_reader *reader) {
  49. return reader->vtable->read_char(reader->userdata);
  50. }
  51. static void json_reader_container_begins(grpc_json_reader *reader,
  52. grpc_json_type type) {
  53. reader->vtable->container_begins(reader->userdata, type);
  54. }
  55. static grpc_json_type grpc_json_reader_container_ends(
  56. grpc_json_reader *reader) {
  57. return reader->vtable->container_ends(reader->userdata);
  58. }
  59. static void json_reader_set_key(grpc_json_reader *reader) {
  60. reader->vtable->set_key(reader->userdata);
  61. }
  62. static void json_reader_set_string(grpc_json_reader *reader) {
  63. reader->vtable->set_string(reader->userdata);
  64. }
  65. static int json_reader_set_number(grpc_json_reader *reader) {
  66. return reader->vtable->set_number(reader->userdata);
  67. }
  68. static void json_reader_set_true(grpc_json_reader *reader) {
  69. reader->vtable->set_true(reader->userdata);
  70. }
  71. static void json_reader_set_false(grpc_json_reader *reader) {
  72. reader->vtable->set_false(reader->userdata);
  73. }
  74. static void json_reader_set_null(grpc_json_reader *reader) {
  75. reader->vtable->set_null(reader->userdata);
  76. }
  77. /* Call this function to initialize the reader structure. */
  78. void grpc_json_reader_init(grpc_json_reader *reader,
  79. grpc_json_reader_vtable *vtable, void *userdata) {
  80. memset(reader, 0, sizeof(*reader));
  81. reader->vtable = vtable;
  82. reader->userdata = userdata;
  83. json_reader_string_clear(reader);
  84. reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
  85. }
  86. int grpc_json_reader_is_complete(grpc_json_reader *reader) {
  87. return ((reader->depth == 0) &&
  88. ((reader->state == GRPC_JSON_STATE_END) ||
  89. (reader->state == GRPC_JSON_STATE_VALUE_END)));
  90. }
  91. grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader) {
  92. gpr_uint32 c, success;
  93. /* This state-machine is a strict implementation of ECMA-404 */
  94. for (;;) {
  95. c = grpc_json_reader_read_char(reader);
  96. switch (c) {
  97. /* Let's process the error cases first. */
  98. case GRPC_JSON_READ_CHAR_ERROR:
  99. return GRPC_JSON_READ_ERROR;
  100. case GRPC_JSON_READ_CHAR_EAGAIN:
  101. return GRPC_JSON_EAGAIN;
  102. case GRPC_JSON_READ_CHAR_EOF:
  103. if (grpc_json_reader_is_complete(reader)) {
  104. return GRPC_JSON_DONE;
  105. } else {
  106. return GRPC_JSON_PARSE_ERROR;
  107. }
  108. break;
  109. /* Processing whitespaces. */
  110. case ' ':
  111. case '\t':
  112. case '\n':
  113. case '\r':
  114. switch (reader->state) {
  115. case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
  116. case GRPC_JSON_STATE_OBJECT_KEY_END:
  117. case GRPC_JSON_STATE_VALUE_BEGIN:
  118. case GRPC_JSON_STATE_VALUE_END:
  119. case GRPC_JSON_STATE_END:
  120. break;
  121. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  122. case GRPC_JSON_STATE_VALUE_STRING:
  123. if (c != ' ') return GRPC_JSON_PARSE_ERROR;
  124. if (reader->unicode_high_surrogate != 0)
  125. return GRPC_JSON_PARSE_ERROR;
  126. json_reader_string_add_char(reader, c);
  127. break;
  128. case GRPC_JSON_STATE_VALUE_NUMBER:
  129. case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
  130. case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
  131. case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
  132. success = (gpr_uint32)json_reader_set_number(reader);
  133. if (!success) return GRPC_JSON_PARSE_ERROR;
  134. json_reader_string_clear(reader);
  135. reader->state = GRPC_JSON_STATE_VALUE_END;
  136. break;
  137. default:
  138. return GRPC_JSON_PARSE_ERROR;
  139. }
  140. break;
  141. /* Value, object or array terminations. */
  142. case ',':
  143. case '}':
  144. case ']':
  145. switch (reader->state) {
  146. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  147. case GRPC_JSON_STATE_VALUE_STRING:
  148. if (reader->unicode_high_surrogate != 0)
  149. return GRPC_JSON_PARSE_ERROR;
  150. json_reader_string_add_char(reader, c);
  151. break;
  152. case GRPC_JSON_STATE_VALUE_NUMBER:
  153. case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
  154. case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
  155. case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
  156. success = (gpr_uint32)json_reader_set_number(reader);
  157. if (!success) return GRPC_JSON_PARSE_ERROR;
  158. json_reader_string_clear(reader);
  159. reader->state = GRPC_JSON_STATE_VALUE_END;
  160. /* The missing break here is intentional. */
  161. case GRPC_JSON_STATE_VALUE_END:
  162. case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
  163. case GRPC_JSON_STATE_VALUE_BEGIN:
  164. if (c == ',') {
  165. if (reader->state != GRPC_JSON_STATE_VALUE_END) {
  166. return GRPC_JSON_PARSE_ERROR;
  167. }
  168. if (reader->in_object) {
  169. reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
  170. } else {
  171. reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
  172. }
  173. } else {
  174. if (reader->depth-- == 0) return GRPC_JSON_PARSE_ERROR;
  175. if ((c == '}') && !reader->in_object) {
  176. return GRPC_JSON_PARSE_ERROR;
  177. }
  178. if ((c == '}') &&
  179. (reader->state == GRPC_JSON_STATE_OBJECT_KEY_BEGIN) &&
  180. !reader->container_just_begun) {
  181. return GRPC_JSON_PARSE_ERROR;
  182. }
  183. if ((c == ']') && !reader->in_array) return GRPC_JSON_PARSE_ERROR;
  184. if ((c == ']') &&
  185. (reader->state == GRPC_JSON_STATE_VALUE_BEGIN) &&
  186. !reader->container_just_begun) {
  187. return GRPC_JSON_PARSE_ERROR;
  188. }
  189. reader->state = GRPC_JSON_STATE_VALUE_END;
  190. switch (grpc_json_reader_container_ends(reader)) {
  191. case GRPC_JSON_OBJECT:
  192. reader->in_object = 1;
  193. reader->in_array = 0;
  194. break;
  195. case GRPC_JSON_ARRAY:
  196. reader->in_object = 0;
  197. reader->in_array = 1;
  198. break;
  199. case GRPC_JSON_TOP_LEVEL:
  200. GPR_ASSERT(reader->depth == 0);
  201. reader->in_object = 0;
  202. reader->in_array = 0;
  203. reader->state = GRPC_JSON_STATE_END;
  204. break;
  205. default:
  206. GPR_UNREACHABLE_CODE(return GRPC_JSON_INTERNAL_ERROR);
  207. }
  208. }
  209. break;
  210. default:
  211. return GRPC_JSON_PARSE_ERROR;
  212. }
  213. break;
  214. /* In-string escaping. */
  215. case '\\':
  216. switch (reader->state) {
  217. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  218. reader->escaped_string_was_key = 1;
  219. reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
  220. break;
  221. case GRPC_JSON_STATE_VALUE_STRING:
  222. reader->escaped_string_was_key = 0;
  223. reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
  224. break;
  225. /* This is the \\ case. */
  226. case GRPC_JSON_STATE_STRING_ESCAPE:
  227. if (reader->unicode_high_surrogate != 0)
  228. return GRPC_JSON_PARSE_ERROR;
  229. json_reader_string_add_char(reader, '\\');
  230. if (reader->escaped_string_was_key) {
  231. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  232. } else {
  233. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  234. }
  235. break;
  236. default:
  237. return GRPC_JSON_PARSE_ERROR;
  238. }
  239. break;
  240. default:
  241. reader->container_just_begun = 0;
  242. switch (reader->state) {
  243. case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
  244. if (c != '"') return GRPC_JSON_PARSE_ERROR;
  245. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  246. break;
  247. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  248. GPR_ASSERT(reader->unicode_high_surrogate == 0);
  249. if (c == '"') {
  250. reader->state = GRPC_JSON_STATE_OBJECT_KEY_END;
  251. json_reader_set_key(reader);
  252. json_reader_string_clear(reader);
  253. } else {
  254. if (c <= 0x001f) return GRPC_JSON_PARSE_ERROR;
  255. json_reader_string_add_char(reader, c);
  256. }
  257. break;
  258. case GRPC_JSON_STATE_VALUE_STRING:
  259. if (reader->unicode_high_surrogate != 0)
  260. return GRPC_JSON_PARSE_ERROR;
  261. if (c == '"') {
  262. reader->state = GRPC_JSON_STATE_VALUE_END;
  263. json_reader_set_string(reader);
  264. json_reader_string_clear(reader);
  265. } else {
  266. if (c < 32) return GRPC_JSON_PARSE_ERROR;
  267. json_reader_string_add_char(reader, c);
  268. }
  269. break;
  270. case GRPC_JSON_STATE_OBJECT_KEY_END:
  271. if (c != ':') return GRPC_JSON_PARSE_ERROR;
  272. reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
  273. break;
  274. case GRPC_JSON_STATE_VALUE_BEGIN:
  275. switch (c) {
  276. case 't':
  277. reader->state = GRPC_JSON_STATE_VALUE_TRUE_R;
  278. break;
  279. case 'f':
  280. reader->state = GRPC_JSON_STATE_VALUE_FALSE_A;
  281. break;
  282. case 'n':
  283. reader->state = GRPC_JSON_STATE_VALUE_NULL_U;
  284. break;
  285. case '"':
  286. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  287. break;
  288. case '0':
  289. json_reader_string_add_char(reader, c);
  290. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_ZERO;
  291. break;
  292. case '1':
  293. case '2':
  294. case '3':
  295. case '4':
  296. case '5':
  297. case '6':
  298. case '7':
  299. case '8':
  300. case '9':
  301. case '-':
  302. json_reader_string_add_char(reader, c);
  303. reader->state = GRPC_JSON_STATE_VALUE_NUMBER;
  304. break;
  305. case '{':
  306. reader->container_just_begun = 1;
  307. json_reader_container_begins(reader, GRPC_JSON_OBJECT);
  308. reader->depth++;
  309. reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
  310. reader->in_object = 1;
  311. reader->in_array = 0;
  312. break;
  313. case '[':
  314. reader->container_just_begun = 1;
  315. json_reader_container_begins(reader, GRPC_JSON_ARRAY);
  316. reader->depth++;
  317. reader->in_object = 0;
  318. reader->in_array = 1;
  319. break;
  320. }
  321. break;
  322. case GRPC_JSON_STATE_STRING_ESCAPE:
  323. if (reader->escaped_string_was_key) {
  324. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  325. } else {
  326. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  327. }
  328. if (reader->unicode_high_surrogate && c != 'u')
  329. return GRPC_JSON_PARSE_ERROR;
  330. switch (c) {
  331. case '"':
  332. case '/':
  333. json_reader_string_add_char(reader, c);
  334. break;
  335. case 'b':
  336. json_reader_string_add_char(reader, '\b');
  337. break;
  338. case 'f':
  339. json_reader_string_add_char(reader, '\f');
  340. break;
  341. case 'n':
  342. json_reader_string_add_char(reader, '\n');
  343. break;
  344. case 'r':
  345. json_reader_string_add_char(reader, '\r');
  346. break;
  347. case 't':
  348. json_reader_string_add_char(reader, '\t');
  349. break;
  350. case 'u':
  351. reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U1;
  352. reader->unicode_char = 0;
  353. break;
  354. default:
  355. return GRPC_JSON_PARSE_ERROR;
  356. }
  357. break;
  358. case GRPC_JSON_STATE_STRING_ESCAPE_U1:
  359. case GRPC_JSON_STATE_STRING_ESCAPE_U2:
  360. case GRPC_JSON_STATE_STRING_ESCAPE_U3:
  361. case GRPC_JSON_STATE_STRING_ESCAPE_U4:
  362. if ((c >= '0') && (c <= '9')) {
  363. c -= '0';
  364. } else if ((c >= 'A') && (c <= 'F')) {
  365. c -= 'A' - 10;
  366. } else if ((c >= 'a') && (c <= 'f')) {
  367. c -= 'a' - 10;
  368. } else {
  369. return GRPC_JSON_PARSE_ERROR;
  370. }
  371. reader->unicode_char = (gpr_uint16)(reader->unicode_char << 4);
  372. reader->unicode_char = (gpr_uint16)(reader->unicode_char | c);
  373. switch (reader->state) {
  374. case GRPC_JSON_STATE_STRING_ESCAPE_U1:
  375. reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U2;
  376. break;
  377. case GRPC_JSON_STATE_STRING_ESCAPE_U2:
  378. reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U3;
  379. break;
  380. case GRPC_JSON_STATE_STRING_ESCAPE_U3:
  381. reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U4;
  382. break;
  383. case GRPC_JSON_STATE_STRING_ESCAPE_U4:
  384. /* See grpc_json_writer_escape_string to have a description
  385. * of what's going on here.
  386. */
  387. if ((reader->unicode_char & 0xfc00) == 0xd800) {
  388. /* high surrogate utf-16 */
  389. if (reader->unicode_high_surrogate != 0)
  390. return GRPC_JSON_PARSE_ERROR;
  391. reader->unicode_high_surrogate = reader->unicode_char;
  392. } else if ((reader->unicode_char & 0xfc00) == 0xdc00) {
  393. /* low surrogate utf-16 */
  394. gpr_uint32 utf32;
  395. if (reader->unicode_high_surrogate == 0)
  396. return GRPC_JSON_PARSE_ERROR;
  397. utf32 = 0x10000;
  398. utf32 += (gpr_uint32)(
  399. (reader->unicode_high_surrogate - 0xd800) * 0x400);
  400. utf32 += (gpr_uint32)(reader->unicode_char - 0xdc00);
  401. json_reader_string_add_utf32(reader, utf32);
  402. reader->unicode_high_surrogate = 0;
  403. } else {
  404. /* anything else */
  405. if (reader->unicode_high_surrogate != 0)
  406. return GRPC_JSON_PARSE_ERROR;
  407. json_reader_string_add_utf32(reader, reader->unicode_char);
  408. }
  409. if (reader->escaped_string_was_key) {
  410. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  411. } else {
  412. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  413. }
  414. break;
  415. default:
  416. GPR_UNREACHABLE_CODE(return GRPC_JSON_INTERNAL_ERROR);
  417. }
  418. break;
  419. case GRPC_JSON_STATE_VALUE_NUMBER:
  420. json_reader_string_add_char(reader, c);
  421. switch (c) {
  422. case '0':
  423. case '1':
  424. case '2':
  425. case '3':
  426. case '4':
  427. case '5':
  428. case '6':
  429. case '7':
  430. case '8':
  431. case '9':
  432. break;
  433. case 'e':
  434. case 'E':
  435. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
  436. break;
  437. case '.':
  438. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
  439. break;
  440. default:
  441. return GRPC_JSON_PARSE_ERROR;
  442. }
  443. break;
  444. case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
  445. json_reader_string_add_char(reader, c);
  446. switch (c) {
  447. case '0':
  448. case '1':
  449. case '2':
  450. case '3':
  451. case '4':
  452. case '5':
  453. case '6':
  454. case '7':
  455. case '8':
  456. case '9':
  457. break;
  458. case 'e':
  459. case 'E':
  460. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
  461. break;
  462. default:
  463. return GRPC_JSON_PARSE_ERROR;
  464. }
  465. break;
  466. case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
  467. if (c != '.') return GRPC_JSON_PARSE_ERROR;
  468. json_reader_string_add_char(reader, c);
  469. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
  470. break;
  471. case GRPC_JSON_STATE_VALUE_NUMBER_DOT:
  472. json_reader_string_add_char(reader, c);
  473. switch (c) {
  474. case '0':
  475. case '1':
  476. case '2':
  477. case '3':
  478. case '4':
  479. case '5':
  480. case '6':
  481. case '7':
  482. case '8':
  483. case '9':
  484. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL;
  485. break;
  486. default:
  487. return GRPC_JSON_PARSE_ERROR;
  488. }
  489. break;
  490. case GRPC_JSON_STATE_VALUE_NUMBER_E:
  491. json_reader_string_add_char(reader, c);
  492. switch (c) {
  493. case '0':
  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. case '-':
  505. reader->state = GRPC_JSON_STATE_VALUE_NUMBER_EPM;
  506. break;
  507. default:
  508. return GRPC_JSON_PARSE_ERROR;
  509. }
  510. break;
  511. case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
  512. json_reader_string_add_char(reader, c);
  513. switch (c) {
  514. case '0':
  515. case '1':
  516. case '2':
  517. case '3':
  518. case '4':
  519. case '5':
  520. case '6':
  521. case '7':
  522. case '8':
  523. case '9':
  524. break;
  525. default:
  526. return GRPC_JSON_PARSE_ERROR;
  527. }
  528. break;
  529. case GRPC_JSON_STATE_VALUE_TRUE_R:
  530. if (c != 'r') return GRPC_JSON_PARSE_ERROR;
  531. reader->state = GRPC_JSON_STATE_VALUE_TRUE_U;
  532. break;
  533. case GRPC_JSON_STATE_VALUE_TRUE_U:
  534. if (c != 'u') return GRPC_JSON_PARSE_ERROR;
  535. reader->state = GRPC_JSON_STATE_VALUE_TRUE_E;
  536. break;
  537. case GRPC_JSON_STATE_VALUE_TRUE_E:
  538. if (c != 'e') return GRPC_JSON_PARSE_ERROR;
  539. json_reader_set_true(reader);
  540. reader->state = GRPC_JSON_STATE_VALUE_END;
  541. break;
  542. case GRPC_JSON_STATE_VALUE_FALSE_A:
  543. if (c != 'a') return GRPC_JSON_PARSE_ERROR;
  544. reader->state = GRPC_JSON_STATE_VALUE_FALSE_L;
  545. break;
  546. case GRPC_JSON_STATE_VALUE_FALSE_L:
  547. if (c != 'l') return GRPC_JSON_PARSE_ERROR;
  548. reader->state = GRPC_JSON_STATE_VALUE_FALSE_S;
  549. break;
  550. case GRPC_JSON_STATE_VALUE_FALSE_S:
  551. if (c != 's') return GRPC_JSON_PARSE_ERROR;
  552. reader->state = GRPC_JSON_STATE_VALUE_FALSE_E;
  553. break;
  554. case GRPC_JSON_STATE_VALUE_FALSE_E:
  555. if (c != 'e') return GRPC_JSON_PARSE_ERROR;
  556. json_reader_set_false(reader);
  557. reader->state = GRPC_JSON_STATE_VALUE_END;
  558. break;
  559. case GRPC_JSON_STATE_VALUE_NULL_U:
  560. if (c != 'u') return GRPC_JSON_PARSE_ERROR;
  561. reader->state = GRPC_JSON_STATE_VALUE_NULL_L1;
  562. break;
  563. case GRPC_JSON_STATE_VALUE_NULL_L1:
  564. if (c != 'l') return GRPC_JSON_PARSE_ERROR;
  565. reader->state = GRPC_JSON_STATE_VALUE_NULL_L2;
  566. break;
  567. case GRPC_JSON_STATE_VALUE_NULL_L2:
  568. if (c != 'l') return GRPC_JSON_PARSE_ERROR;
  569. json_reader_set_null(reader);
  570. reader->state = GRPC_JSON_STATE_VALUE_END;
  571. break;
  572. /* All of the VALUE_END cases are handled in the specialized case
  573. * above. */
  574. case GRPC_JSON_STATE_VALUE_END:
  575. switch (c) {
  576. case ',':
  577. case '}':
  578. case ']':
  579. GPR_UNREACHABLE_CODE(return GRPC_JSON_INTERNAL_ERROR);
  580. break;
  581. default:
  582. return GRPC_JSON_PARSE_ERROR;
  583. }
  584. break;
  585. case GRPC_JSON_STATE_END:
  586. return GRPC_JSON_PARSE_ERROR;
  587. }
  588. }
  589. }
  590. GPR_UNREACHABLE_CODE(return GRPC_JSON_INTERNAL_ERROR);
  591. }