json_reader.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 "src/core/json/json_reader.h"
  36. static void json_reader_string_clear(grpc_json_reader *reader) {
  37. reader->vtable->string_clear(reader->userdata);
  38. }
  39. static void json_reader_string_add_char(grpc_json_reader *reader,
  40. gpr_uint32 c) {
  41. reader->vtable->string_add_char(reader->userdata, c);
  42. }
  43. static void json_reader_string_add_utf32(grpc_json_reader *reader,
  44. gpr_uint32 utf32) {
  45. reader->vtable->string_add_utf32(reader->userdata, utf32);
  46. }
  47. static gpr_uint32 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. gpr_uint32 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 = (gpr_uint32)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. success = (gpr_uint32)json_reader_set_number(reader);
  156. if (!success) return GRPC_JSON_PARSE_ERROR;
  157. json_reader_string_clear(reader);
  158. reader->state = GRPC_JSON_STATE_VALUE_END;
  159. /* The missing break here is intentional. */
  160. case GRPC_JSON_STATE_VALUE_END:
  161. case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
  162. case GRPC_JSON_STATE_VALUE_BEGIN:
  163. if (c == ',') {
  164. if (reader->state != GRPC_JSON_STATE_VALUE_END) {
  165. return GRPC_JSON_PARSE_ERROR;
  166. }
  167. if (reader->in_object) {
  168. reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
  169. } else {
  170. reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
  171. }
  172. } else {
  173. if (reader->depth-- == 0) return GRPC_JSON_PARSE_ERROR;
  174. if ((c == '}') && !reader->in_object) {
  175. return GRPC_JSON_PARSE_ERROR;
  176. }
  177. if ((c == '}') &&
  178. (reader->state == GRPC_JSON_STATE_OBJECT_KEY_BEGIN) &&
  179. !reader->container_just_begun) {
  180. return GRPC_JSON_PARSE_ERROR;
  181. }
  182. if ((c == ']') && !reader->in_array) return GRPC_JSON_PARSE_ERROR;
  183. if ((c == ']') &&
  184. (reader->state == GRPC_JSON_STATE_VALUE_BEGIN) &&
  185. !reader->container_just_begun) {
  186. return GRPC_JSON_PARSE_ERROR;
  187. }
  188. reader->state = GRPC_JSON_STATE_VALUE_END;
  189. switch (grpc_json_reader_container_ends(reader)) {
  190. case GRPC_JSON_OBJECT:
  191. reader->in_object = 1;
  192. reader->in_array = 0;
  193. break;
  194. case GRPC_JSON_ARRAY:
  195. reader->in_object = 0;
  196. reader->in_array = 1;
  197. break;
  198. case GRPC_JSON_TOP_LEVEL:
  199. if (reader->depth != 0) return GRPC_JSON_INTERNAL_ERROR;
  200. reader->in_object = 0;
  201. reader->in_array = 0;
  202. reader->state = GRPC_JSON_STATE_END;
  203. break;
  204. default:
  205. return GRPC_JSON_INTERNAL_ERROR;
  206. }
  207. }
  208. break;
  209. default:
  210. return GRPC_JSON_PARSE_ERROR;
  211. }
  212. break;
  213. /* In-string escaping. */
  214. case '\\':
  215. switch (reader->state) {
  216. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  217. reader->escaped_string_was_key = 1;
  218. reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
  219. break;
  220. case GRPC_JSON_STATE_VALUE_STRING:
  221. reader->escaped_string_was_key = 0;
  222. reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
  223. break;
  224. /* This is the \\ case. */
  225. case GRPC_JSON_STATE_STRING_ESCAPE:
  226. if (reader->unicode_high_surrogate != 0)
  227. return GRPC_JSON_PARSE_ERROR;
  228. json_reader_string_add_char(reader, '\\');
  229. if (reader->escaped_string_was_key) {
  230. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  231. } else {
  232. reader->state = GRPC_JSON_STATE_VALUE_STRING;
  233. }
  234. break;
  235. default:
  236. return GRPC_JSON_PARSE_ERROR;
  237. }
  238. break;
  239. default:
  240. reader->container_just_begun = 0;
  241. switch (reader->state) {
  242. case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
  243. if (c != '"') return GRPC_JSON_PARSE_ERROR;
  244. reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
  245. break;
  246. case GRPC_JSON_STATE_OBJECT_KEY_STRING:
  247. if (reader->unicode_high_surrogate != 0)
  248. return GRPC_JSON_PARSE_ERROR;
  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. 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. 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. return GRPC_JSON_INTERNAL_ERROR;
  591. }