json_reader.c 18 KB

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