binary_reader.hpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. #pragma once
  2. #include <algorithm> // generate_n
  3. #include <array> // array
  4. #include <cassert> // assert
  5. #include <cmath> // ldexp
  6. #include <cstddef> // size_t
  7. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  8. #include <cstdio> // snprintf
  9. #include <cstring> // memcpy
  10. #include <iterator> // back_inserter
  11. #include <limits> // numeric_limits
  12. #include <string> // char_traits, string
  13. #include <utility> // make_pair, move
  14. #include <nlohmann/detail/input/input_adapters.hpp>
  15. #include <nlohmann/detail/input/json_sax.hpp>
  16. #include <nlohmann/detail/exceptions.hpp>
  17. #include <nlohmann/detail/macro_scope.hpp>
  18. #include <nlohmann/detail/value_t.hpp>
  19. namespace nlohmann
  20. {
  21. namespace detail
  22. {
  23. ///////////////////
  24. // binary reader //
  25. ///////////////////
  26. /*!
  27. @brief deserialization of CBOR, MessagePack, and UBJSON values
  28. */
  29. template<typename BasicJsonType>
  30. class binary_reader
  31. {
  32. using number_integer_t = typename BasicJsonType::number_integer_t;
  33. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  34. using number_float_t = typename BasicJsonType::number_float_t;
  35. using string_t = typename BasicJsonType::string_t;
  36. using json_sax_t = json_sax<BasicJsonType>;
  37. public:
  38. /*!
  39. @brief create a binary reader
  40. @param[in] adapter input adapter to read from
  41. */
  42. explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
  43. {
  44. assert(ia);
  45. }
  46. /*!
  47. @param[in] format the binary format to parse
  48. @param[in] sax_ a SAX event processor
  49. @param[in] strict whether to expect the input to be consumed completed
  50. @return
  51. */
  52. bool sax_parse(const input_format_t format,
  53. json_sax_t* sax_,
  54. const bool strict = true)
  55. {
  56. sax = sax_;
  57. bool result;
  58. switch (format)
  59. {
  60. case input_format_t::cbor:
  61. result = parse_cbor_internal();
  62. break;
  63. case input_format_t::msgpack:
  64. result = parse_msgpack_internal();
  65. break;
  66. case input_format_t::ubjson:
  67. result = parse_ubjson_internal();
  68. break;
  69. default:
  70. assert(false); // LCOV_EXCL_LINE
  71. }
  72. // strict mode: next byte must be EOF
  73. if (result and strict)
  74. {
  75. if (format == input_format_t::ubjson)
  76. {
  77. get_ignore_noop();
  78. }
  79. else
  80. {
  81. get();
  82. }
  83. if (JSON_UNLIKELY(current != std::char_traits<char>::eof()))
  84. {
  85. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read, "expected end of input"));
  86. }
  87. }
  88. return result;
  89. }
  90. /*!
  91. @brief determine system byte order
  92. @return true if and only if system's byte order is little endian
  93. @note from http://stackoverflow.com/a/1001328/266378
  94. */
  95. static constexpr bool little_endianess(int num = 1) noexcept
  96. {
  97. return (*reinterpret_cast<char*>(&num) == 1);
  98. }
  99. private:
  100. /*!
  101. @param[in] get_char whether a new character should be retrieved from the
  102. input (true, default) or whether the last read
  103. character should be considered instead
  104. @return whether a valid CBOR value was passed to the SAX parser
  105. */
  106. bool parse_cbor_internal(const bool get_char = true)
  107. {
  108. switch (get_char ? get() : current)
  109. {
  110. // EOF
  111. case std::char_traits<char>::eof():
  112. return unexpect_eof();
  113. // Integer 0x00..0x17 (0..23)
  114. case 0x00:
  115. case 0x01:
  116. case 0x02:
  117. case 0x03:
  118. case 0x04:
  119. case 0x05:
  120. case 0x06:
  121. case 0x07:
  122. case 0x08:
  123. case 0x09:
  124. case 0x0A:
  125. case 0x0B:
  126. case 0x0C:
  127. case 0x0D:
  128. case 0x0E:
  129. case 0x0F:
  130. case 0x10:
  131. case 0x11:
  132. case 0x12:
  133. case 0x13:
  134. case 0x14:
  135. case 0x15:
  136. case 0x16:
  137. case 0x17:
  138. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  139. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  140. {
  141. uint8_t number;
  142. return get_number(number) and sax->number_unsigned(number);
  143. }
  144. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  145. {
  146. uint16_t number;
  147. return get_number(number) and sax->number_unsigned(number);
  148. }
  149. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  150. {
  151. uint32_t number;
  152. return get_number(number) and sax->number_unsigned(number);
  153. }
  154. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  155. {
  156. uint64_t number;
  157. return get_number(number) and sax->number_unsigned(number);
  158. }
  159. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  160. case 0x20:
  161. case 0x21:
  162. case 0x22:
  163. case 0x23:
  164. case 0x24:
  165. case 0x25:
  166. case 0x26:
  167. case 0x27:
  168. case 0x28:
  169. case 0x29:
  170. case 0x2A:
  171. case 0x2B:
  172. case 0x2C:
  173. case 0x2D:
  174. case 0x2E:
  175. case 0x2F:
  176. case 0x30:
  177. case 0x31:
  178. case 0x32:
  179. case 0x33:
  180. case 0x34:
  181. case 0x35:
  182. case 0x36:
  183. case 0x37:
  184. return sax->number_integer(static_cast<int8_t>(0x20 - 1 - current));
  185. case 0x38: // Negative integer (one-byte uint8_t follows)
  186. {
  187. uint8_t number;
  188. return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  189. }
  190. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  191. {
  192. uint16_t number;
  193. return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  194. }
  195. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  196. {
  197. uint32_t number;
  198. return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  199. }
  200. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  201. {
  202. uint64_t number;
  203. return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1)
  204. - static_cast<number_integer_t>(number));
  205. }
  206. // UTF-8 string (0x00..0x17 bytes follow)
  207. case 0x60:
  208. case 0x61:
  209. case 0x62:
  210. case 0x63:
  211. case 0x64:
  212. case 0x65:
  213. case 0x66:
  214. case 0x67:
  215. case 0x68:
  216. case 0x69:
  217. case 0x6A:
  218. case 0x6B:
  219. case 0x6C:
  220. case 0x6D:
  221. case 0x6E:
  222. case 0x6F:
  223. case 0x70:
  224. case 0x71:
  225. case 0x72:
  226. case 0x73:
  227. case 0x74:
  228. case 0x75:
  229. case 0x76:
  230. case 0x77:
  231. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  232. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  233. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  234. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  235. case 0x7F: // UTF-8 string (indefinite length)
  236. {
  237. string_t s;
  238. return get_cbor_string(s) and sax->string(s);
  239. }
  240. // array (0x00..0x17 data items follow)
  241. case 0x80:
  242. case 0x81:
  243. case 0x82:
  244. case 0x83:
  245. case 0x84:
  246. case 0x85:
  247. case 0x86:
  248. case 0x87:
  249. case 0x88:
  250. case 0x89:
  251. case 0x8A:
  252. case 0x8B:
  253. case 0x8C:
  254. case 0x8D:
  255. case 0x8E:
  256. case 0x8F:
  257. case 0x90:
  258. case 0x91:
  259. case 0x92:
  260. case 0x93:
  261. case 0x94:
  262. case 0x95:
  263. case 0x96:
  264. case 0x97:
  265. return get_cbor_array(static_cast<std::size_t>(current & 0x1F));
  266. case 0x98: // array (one-byte uint8_t for n follows)
  267. {
  268. uint8_t len;
  269. return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
  270. }
  271. case 0x99: // array (two-byte uint16_t for n follow)
  272. {
  273. uint16_t len;
  274. return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
  275. }
  276. case 0x9A: // array (four-byte uint32_t for n follow)
  277. {
  278. uint32_t len;
  279. return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
  280. }
  281. case 0x9B: // array (eight-byte uint64_t for n follow)
  282. {
  283. uint64_t len;
  284. return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
  285. }
  286. case 0x9F: // array (indefinite length)
  287. return get_cbor_array(json_sax_t::no_limit);
  288. // map (0x00..0x17 pairs of data items follow)
  289. case 0xA0:
  290. case 0xA1:
  291. case 0xA2:
  292. case 0xA3:
  293. case 0xA4:
  294. case 0xA5:
  295. case 0xA6:
  296. case 0xA7:
  297. case 0xA8:
  298. case 0xA9:
  299. case 0xAA:
  300. case 0xAB:
  301. case 0xAC:
  302. case 0xAD:
  303. case 0xAE:
  304. case 0xAF:
  305. case 0xB0:
  306. case 0xB1:
  307. case 0xB2:
  308. case 0xB3:
  309. case 0xB4:
  310. case 0xB5:
  311. case 0xB6:
  312. case 0xB7:
  313. return get_cbor_object(static_cast<std::size_t>(current & 0x1F));
  314. case 0xB8: // map (one-byte uint8_t for n follows)
  315. {
  316. uint8_t len;
  317. return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
  318. }
  319. case 0xB9: // map (two-byte uint16_t for n follow)
  320. {
  321. uint16_t len;
  322. return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
  323. }
  324. case 0xBA: // map (four-byte uint32_t for n follow)
  325. {
  326. uint32_t len;
  327. return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
  328. }
  329. case 0xBB: // map (eight-byte uint64_t for n follow)
  330. {
  331. uint64_t len;
  332. return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
  333. }
  334. case 0xBF: // map (indefinite length)
  335. return get_cbor_object(json_sax_t::no_limit);
  336. case 0xF4: // false
  337. return sax->boolean(false);
  338. case 0xF5: // true
  339. return sax->boolean(true);
  340. case 0xF6: // null
  341. return sax->null();
  342. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  343. {
  344. const int byte1 = get();
  345. if (JSON_UNLIKELY(not unexpect_eof()))
  346. {
  347. return false;
  348. }
  349. const int byte2 = get();
  350. if (JSON_UNLIKELY(not unexpect_eof()))
  351. {
  352. return false;
  353. }
  354. // code from RFC 7049, Appendix D, Figure 3:
  355. // As half-precision floating-point numbers were only added
  356. // to IEEE 754 in 2008, today's programming platforms often
  357. // still only have limited support for them. It is very
  358. // easy to include at least decoding support for them even
  359. // without such support. An example of a small decoder for
  360. // half-precision floating-point numbers in the C language
  361. // is shown in Fig. 3.
  362. const int half = (byte1 << 8) + byte2;
  363. const double val = [&half]
  364. {
  365. const int exp = (half >> 10) & 0x1F;
  366. const int mant = half & 0x3FF;
  367. assert(0 <= exp and exp <= 32);
  368. assert(0 <= mant and mant <= 1024);
  369. switch (exp)
  370. {
  371. case 0:
  372. return std::ldexp(mant, -24);
  373. case 31:
  374. return (mant == 0)
  375. ? std::numeric_limits<double>::infinity()
  376. : std::numeric_limits<double>::quiet_NaN();
  377. default:
  378. return std::ldexp(mant + 1024, exp - 25);
  379. }
  380. }();
  381. return sax->number_float((half & 0x8000) != 0
  382. ? static_cast<number_float_t>(-val)
  383. : static_cast<number_float_t>(val), "");
  384. }
  385. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  386. {
  387. float number;
  388. return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
  389. }
  390. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  391. {
  392. double number;
  393. return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
  394. }
  395. default: // anything else (0xFF is handled inside the other types)
  396. {
  397. auto last_token = get_token_string();
  398. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading CBOR; last byte: 0x" + last_token));
  399. }
  400. }
  401. }
  402. /*!
  403. @return whether a valid MessagePack value was passed to the SAX parser
  404. */
  405. bool parse_msgpack_internal()
  406. {
  407. switch (get())
  408. {
  409. // EOF
  410. case std::char_traits<char>::eof():
  411. return unexpect_eof();
  412. // positive fixint
  413. case 0x00:
  414. case 0x01:
  415. case 0x02:
  416. case 0x03:
  417. case 0x04:
  418. case 0x05:
  419. case 0x06:
  420. case 0x07:
  421. case 0x08:
  422. case 0x09:
  423. case 0x0A:
  424. case 0x0B:
  425. case 0x0C:
  426. case 0x0D:
  427. case 0x0E:
  428. case 0x0F:
  429. case 0x10:
  430. case 0x11:
  431. case 0x12:
  432. case 0x13:
  433. case 0x14:
  434. case 0x15:
  435. case 0x16:
  436. case 0x17:
  437. case 0x18:
  438. case 0x19:
  439. case 0x1A:
  440. case 0x1B:
  441. case 0x1C:
  442. case 0x1D:
  443. case 0x1E:
  444. case 0x1F:
  445. case 0x20:
  446. case 0x21:
  447. case 0x22:
  448. case 0x23:
  449. case 0x24:
  450. case 0x25:
  451. case 0x26:
  452. case 0x27:
  453. case 0x28:
  454. case 0x29:
  455. case 0x2A:
  456. case 0x2B:
  457. case 0x2C:
  458. case 0x2D:
  459. case 0x2E:
  460. case 0x2F:
  461. case 0x30:
  462. case 0x31:
  463. case 0x32:
  464. case 0x33:
  465. case 0x34:
  466. case 0x35:
  467. case 0x36:
  468. case 0x37:
  469. case 0x38:
  470. case 0x39:
  471. case 0x3A:
  472. case 0x3B:
  473. case 0x3C:
  474. case 0x3D:
  475. case 0x3E:
  476. case 0x3F:
  477. case 0x40:
  478. case 0x41:
  479. case 0x42:
  480. case 0x43:
  481. case 0x44:
  482. case 0x45:
  483. case 0x46:
  484. case 0x47:
  485. case 0x48:
  486. case 0x49:
  487. case 0x4A:
  488. case 0x4B:
  489. case 0x4C:
  490. case 0x4D:
  491. case 0x4E:
  492. case 0x4F:
  493. case 0x50:
  494. case 0x51:
  495. case 0x52:
  496. case 0x53:
  497. case 0x54:
  498. case 0x55:
  499. case 0x56:
  500. case 0x57:
  501. case 0x58:
  502. case 0x59:
  503. case 0x5A:
  504. case 0x5B:
  505. case 0x5C:
  506. case 0x5D:
  507. case 0x5E:
  508. case 0x5F:
  509. case 0x60:
  510. case 0x61:
  511. case 0x62:
  512. case 0x63:
  513. case 0x64:
  514. case 0x65:
  515. case 0x66:
  516. case 0x67:
  517. case 0x68:
  518. case 0x69:
  519. case 0x6A:
  520. case 0x6B:
  521. case 0x6C:
  522. case 0x6D:
  523. case 0x6E:
  524. case 0x6F:
  525. case 0x70:
  526. case 0x71:
  527. case 0x72:
  528. case 0x73:
  529. case 0x74:
  530. case 0x75:
  531. case 0x76:
  532. case 0x77:
  533. case 0x78:
  534. case 0x79:
  535. case 0x7A:
  536. case 0x7B:
  537. case 0x7C:
  538. case 0x7D:
  539. case 0x7E:
  540. case 0x7F:
  541. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  542. // fixmap
  543. case 0x80:
  544. case 0x81:
  545. case 0x82:
  546. case 0x83:
  547. case 0x84:
  548. case 0x85:
  549. case 0x86:
  550. case 0x87:
  551. case 0x88:
  552. case 0x89:
  553. case 0x8A:
  554. case 0x8B:
  555. case 0x8C:
  556. case 0x8D:
  557. case 0x8E:
  558. case 0x8F:
  559. return get_msgpack_object(static_cast<std::size_t>(current & 0x0F));
  560. // fixarray
  561. case 0x90:
  562. case 0x91:
  563. case 0x92:
  564. case 0x93:
  565. case 0x94:
  566. case 0x95:
  567. case 0x96:
  568. case 0x97:
  569. case 0x98:
  570. case 0x99:
  571. case 0x9A:
  572. case 0x9B:
  573. case 0x9C:
  574. case 0x9D:
  575. case 0x9E:
  576. case 0x9F:
  577. return get_msgpack_array(static_cast<std::size_t>(current & 0x0F));
  578. // fixstr
  579. case 0xA0:
  580. case 0xA1:
  581. case 0xA2:
  582. case 0xA3:
  583. case 0xA4:
  584. case 0xA5:
  585. case 0xA6:
  586. case 0xA7:
  587. case 0xA8:
  588. case 0xA9:
  589. case 0xAA:
  590. case 0xAB:
  591. case 0xAC:
  592. case 0xAD:
  593. case 0xAE:
  594. case 0xAF:
  595. case 0xB0:
  596. case 0xB1:
  597. case 0xB2:
  598. case 0xB3:
  599. case 0xB4:
  600. case 0xB5:
  601. case 0xB6:
  602. case 0xB7:
  603. case 0xB8:
  604. case 0xB9:
  605. case 0xBA:
  606. case 0xBB:
  607. case 0xBC:
  608. case 0xBD:
  609. case 0xBE:
  610. case 0xBF:
  611. {
  612. string_t s;
  613. return get_msgpack_string(s) and sax->string(s);
  614. }
  615. case 0xC0: // nil
  616. return sax->null();
  617. case 0xC2: // false
  618. return sax->boolean(false);
  619. case 0xC3: // true
  620. return sax->boolean(true);
  621. case 0xCA: // float 32
  622. {
  623. float number;
  624. return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
  625. }
  626. case 0xCB: // float 64
  627. {
  628. double number;
  629. return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
  630. }
  631. case 0xCC: // uint 8
  632. {
  633. uint8_t number;
  634. return get_number(number) and sax->number_unsigned(number);
  635. }
  636. case 0xCD: // uint 16
  637. {
  638. uint16_t number;
  639. return get_number(number) and sax->number_unsigned(number);
  640. }
  641. case 0xCE: // uint 32
  642. {
  643. uint32_t number;
  644. return get_number(number) and sax->number_unsigned(number);
  645. }
  646. case 0xCF: // uint 64
  647. {
  648. uint64_t number;
  649. return get_number(number) and sax->number_unsigned(number);
  650. }
  651. case 0xD0: // int 8
  652. {
  653. int8_t number;
  654. return get_number(number) and sax->number_integer(number);
  655. }
  656. case 0xD1: // int 16
  657. {
  658. int16_t number;
  659. return get_number(number) and sax->number_integer(number);
  660. }
  661. case 0xD2: // int 32
  662. {
  663. int32_t number;
  664. return get_number(number) and sax->number_integer(number);
  665. }
  666. case 0xD3: // int 64
  667. {
  668. int64_t number;
  669. return get_number(number) and sax->number_integer(number);
  670. }
  671. case 0xD9: // str 8
  672. case 0xDA: // str 16
  673. case 0xDB: // str 32
  674. {
  675. string_t s;
  676. return get_msgpack_string(s) and sax->string(s);
  677. }
  678. case 0xDC: // array 16
  679. {
  680. uint16_t len;
  681. return get_number(len) and get_msgpack_array(static_cast<std::size_t>(len));
  682. }
  683. case 0xDD: // array 32
  684. {
  685. uint32_t len;
  686. return get_number(len) and get_msgpack_array(static_cast<std::size_t>(len));
  687. }
  688. case 0xDE: // map 16
  689. {
  690. uint16_t len;
  691. return get_number(len) and get_msgpack_object(static_cast<std::size_t>(len));
  692. }
  693. case 0xDF: // map 32
  694. {
  695. uint32_t len;
  696. return get_number(len) and get_msgpack_object(static_cast<std::size_t>(len));
  697. }
  698. // negative fixint
  699. case 0xE0:
  700. case 0xE1:
  701. case 0xE2:
  702. case 0xE3:
  703. case 0xE4:
  704. case 0xE5:
  705. case 0xE6:
  706. case 0xE7:
  707. case 0xE8:
  708. case 0xE9:
  709. case 0xEA:
  710. case 0xEB:
  711. case 0xEC:
  712. case 0xED:
  713. case 0xEE:
  714. case 0xEF:
  715. case 0xF0:
  716. case 0xF1:
  717. case 0xF2:
  718. case 0xF3:
  719. case 0xF4:
  720. case 0xF5:
  721. case 0xF6:
  722. case 0xF7:
  723. case 0xF8:
  724. case 0xF9:
  725. case 0xFA:
  726. case 0xFB:
  727. case 0xFC:
  728. case 0xFD:
  729. case 0xFE:
  730. case 0xFF:
  731. return sax->number_integer(static_cast<int8_t>(current));
  732. default: // anything else
  733. {
  734. auto last_token = get_token_string();
  735. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading MessagePack; last byte: 0x" + last_token));
  736. }
  737. }
  738. }
  739. /*!
  740. @param[in] get_char whether a new character should be retrieved from the
  741. input (true, default) or whether the last read
  742. character should be considered instead
  743. @return whether a valid UBJSON value was passed to the SAX parser
  744. */
  745. bool parse_ubjson_internal(const bool get_char = true)
  746. {
  747. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  748. }
  749. /*!
  750. @brief get next character from the input
  751. This function provides the interface to the used input adapter. It does
  752. not throw in case the input reached EOF, but returns a -'ve valued
  753. `std::char_traits<char>::eof()` in that case.
  754. @return character read from the input
  755. */
  756. int get()
  757. {
  758. ++chars_read;
  759. return (current = ia->get_character());
  760. }
  761. /*!
  762. @return character read from the input after ignoring all 'N' entries
  763. */
  764. int get_ignore_noop()
  765. {
  766. do
  767. {
  768. get();
  769. }
  770. while (current == 'N');
  771. return current;
  772. }
  773. /*
  774. @brief read a number from the input
  775. @tparam NumberType the type of the number
  776. @param[out] result number of type @a NumberType
  777. @return whether conversion completed
  778. @note This function needs to respect the system's endianess, because
  779. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  780. (big endian) and therefore need reordering on little endian systems.
  781. */
  782. template<typename NumberType>
  783. bool get_number(NumberType& result)
  784. {
  785. // step 1: read input into array with system's byte order
  786. std::array<uint8_t, sizeof(NumberType)> vec;
  787. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  788. {
  789. get();
  790. if (JSON_UNLIKELY(not unexpect_eof()))
  791. {
  792. return false;
  793. }
  794. // reverse byte order prior to conversion if necessary
  795. if (is_little_endian)
  796. {
  797. vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current);
  798. }
  799. else
  800. {
  801. vec[i] = static_cast<uint8_t>(current); // LCOV_EXCL_LINE
  802. }
  803. }
  804. // step 2: convert array into number of type T and return
  805. std::memcpy(&result, vec.data(), sizeof(NumberType));
  806. return true;
  807. }
  808. /*!
  809. @brief create a string by reading characters from the input
  810. @tparam NumberType the type of the number
  811. @param[in] len number of characters to read
  812. @param[out] string created by reading @a len bytes
  813. @return whether string creation completed
  814. @note We can not reserve @a len bytes for the result, because @a len
  815. may be too large. Usually, @ref unexpect_eof() detects the end of
  816. the input before we run out of string memory.
  817. */
  818. template<typename NumberType>
  819. bool get_string(const NumberType len, string_t& result)
  820. {
  821. bool success = true;
  822. std::generate_n(std::back_inserter(result), len, [this, &success]()
  823. {
  824. get();
  825. if (JSON_UNLIKELY(not unexpect_eof()))
  826. {
  827. success = false;
  828. }
  829. return static_cast<char>(current);
  830. });
  831. return success;
  832. }
  833. /*!
  834. @brief reads a CBOR string
  835. This function first reads starting bytes to determine the expected
  836. string length and then copies this number of bytes into a string.
  837. Additionally, CBOR's strings with indefinite lengths are supported.
  838. @param[out] result created string
  839. @return whether string creation completed
  840. */
  841. bool get_cbor_string(string_t& result)
  842. {
  843. if (JSON_UNLIKELY(not unexpect_eof()))
  844. {
  845. return false;
  846. }
  847. switch (current)
  848. {
  849. // UTF-8 string (0x00..0x17 bytes follow)
  850. case 0x60:
  851. case 0x61:
  852. case 0x62:
  853. case 0x63:
  854. case 0x64:
  855. case 0x65:
  856. case 0x66:
  857. case 0x67:
  858. case 0x68:
  859. case 0x69:
  860. case 0x6A:
  861. case 0x6B:
  862. case 0x6C:
  863. case 0x6D:
  864. case 0x6E:
  865. case 0x6F:
  866. case 0x70:
  867. case 0x71:
  868. case 0x72:
  869. case 0x73:
  870. case 0x74:
  871. case 0x75:
  872. case 0x76:
  873. case 0x77:
  874. {
  875. return get_string(current & 0x1F, result);
  876. }
  877. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  878. {
  879. uint8_t len;
  880. return get_number(len) and get_string(len, result);
  881. }
  882. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  883. {
  884. uint16_t len;
  885. return get_number(len) and get_string(len, result);
  886. }
  887. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  888. {
  889. uint32_t len;
  890. return get_number(len) and get_string(len, result);
  891. }
  892. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  893. {
  894. uint64_t len;
  895. return get_number(len) and get_string(len, result);
  896. }
  897. case 0x7F: // UTF-8 string (indefinite length)
  898. {
  899. while (get() != 0xFF)
  900. {
  901. string_t chunk;
  902. if (not get_cbor_string(chunk))
  903. {
  904. return false;
  905. }
  906. result.append(chunk);
  907. }
  908. return true;
  909. }
  910. default:
  911. {
  912. auto last_token = get_token_string();
  913. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a CBOR string; last byte: 0x" + last_token));
  914. }
  915. }
  916. }
  917. /*!
  918. @param[in] len the length of the array or json_sax_t::no_limit for an
  919. array of indefinite size
  920. @return whether array creation completed
  921. */
  922. bool get_cbor_array(const std::size_t len)
  923. {
  924. if (JSON_UNLIKELY(not sax->start_array(len)))
  925. {
  926. return false;
  927. }
  928. if (len != json_sax_t::no_limit)
  929. for (std::size_t i = 0; i < len; ++i)
  930. {
  931. if (JSON_UNLIKELY(not parse_cbor_internal()))
  932. {
  933. return false;
  934. }
  935. }
  936. else
  937. {
  938. while (get() != 0xFF)
  939. {
  940. if (JSON_UNLIKELY(not parse_cbor_internal(false)))
  941. {
  942. return false;
  943. }
  944. }
  945. }
  946. return sax->end_array();
  947. }
  948. /*!
  949. @param[in] len the length of the object or json_sax_t::no_limit for an
  950. object of indefinite size
  951. @return whether object creation completed
  952. */
  953. bool get_cbor_object(const std::size_t len)
  954. {
  955. if (not JSON_UNLIKELY(sax->start_object(len)))
  956. {
  957. return false;
  958. }
  959. string_t key;
  960. if (len != json_sax_t::no_limit)
  961. {
  962. for (std::size_t i = 0; i < len; ++i)
  963. {
  964. get();
  965. if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
  966. {
  967. return false;
  968. }
  969. if (JSON_UNLIKELY(not parse_cbor_internal()))
  970. {
  971. return false;
  972. }
  973. key.clear();
  974. }
  975. }
  976. else
  977. {
  978. while (get() != 0xFF)
  979. {
  980. if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
  981. {
  982. return false;
  983. }
  984. if (JSON_UNLIKELY(not parse_cbor_internal()))
  985. {
  986. return false;
  987. }
  988. key.clear();
  989. }
  990. }
  991. return sax->end_object();
  992. }
  993. /*!
  994. @brief reads a MessagePack string
  995. This function first reads starting bytes to determine the expected
  996. string length and then copies this number of bytes into a string.
  997. @param[out] result created string
  998. @return whether string creation completed
  999. */
  1000. bool get_msgpack_string(string_t& result)
  1001. {
  1002. if (JSON_UNLIKELY(not unexpect_eof()))
  1003. {
  1004. return false;
  1005. }
  1006. switch (current)
  1007. {
  1008. // fixstr
  1009. case 0xA0:
  1010. case 0xA1:
  1011. case 0xA2:
  1012. case 0xA3:
  1013. case 0xA4:
  1014. case 0xA5:
  1015. case 0xA6:
  1016. case 0xA7:
  1017. case 0xA8:
  1018. case 0xA9:
  1019. case 0xAA:
  1020. case 0xAB:
  1021. case 0xAC:
  1022. case 0xAD:
  1023. case 0xAE:
  1024. case 0xAF:
  1025. case 0xB0:
  1026. case 0xB1:
  1027. case 0xB2:
  1028. case 0xB3:
  1029. case 0xB4:
  1030. case 0xB5:
  1031. case 0xB6:
  1032. case 0xB7:
  1033. case 0xB8:
  1034. case 0xB9:
  1035. case 0xBA:
  1036. case 0xBB:
  1037. case 0xBC:
  1038. case 0xBD:
  1039. case 0xBE:
  1040. case 0xBF:
  1041. {
  1042. return get_string(current & 0x1F, result);
  1043. }
  1044. case 0xD9: // str 8
  1045. {
  1046. uint8_t len;
  1047. return get_number(len) and get_string(len, result);
  1048. }
  1049. case 0xDA: // str 16
  1050. {
  1051. uint16_t len;
  1052. return get_number(len) and get_string(len, result);
  1053. }
  1054. case 0xDB: // str 32
  1055. {
  1056. uint32_t len;
  1057. return get_number(len) and get_string(len, result);
  1058. }
  1059. default:
  1060. {
  1061. auto last_token = get_token_string();
  1062. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a MessagePack string; last byte: 0x" + last_token));
  1063. }
  1064. }
  1065. }
  1066. /*!
  1067. @param[in] len the length of the array
  1068. @return whether array creation completed
  1069. */
  1070. bool get_msgpack_array(const std::size_t len)
  1071. {
  1072. if (JSON_UNLIKELY(not sax->start_array(len)))
  1073. {
  1074. return false;
  1075. }
  1076. for (std::size_t i = 0; i < len; ++i)
  1077. {
  1078. if (JSON_UNLIKELY(not parse_msgpack_internal()))
  1079. {
  1080. return false;
  1081. }
  1082. }
  1083. return sax->end_array();
  1084. }
  1085. /*!
  1086. @param[in] len the length of the object
  1087. @return whether object creation completed
  1088. */
  1089. bool get_msgpack_object(const std::size_t len)
  1090. {
  1091. if (JSON_UNLIKELY(not sax->start_object(len)))
  1092. {
  1093. return false;
  1094. }
  1095. string_t key;
  1096. for (std::size_t i = 0; i < len; ++i)
  1097. {
  1098. get();
  1099. if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
  1100. {
  1101. return false;
  1102. }
  1103. if (JSON_UNLIKELY(not parse_msgpack_internal()))
  1104. {
  1105. return false;
  1106. }
  1107. key.clear();
  1108. }
  1109. return sax->end_object();
  1110. }
  1111. /*!
  1112. @brief reads a UBJSON string
  1113. This function is either called after reading the 'S' byte explicitly
  1114. indicating a string, or in case of an object key where the 'S' byte can be
  1115. left out.
  1116. @param[out] result created string
  1117. @param[in] get_char whether a new character should be retrieved from the
  1118. input (true, default) or whether the last read
  1119. character should be considered instead
  1120. @return whether string creation completed
  1121. */
  1122. bool get_ubjson_string(string_t& result, const bool get_char = true)
  1123. {
  1124. if (get_char)
  1125. {
  1126. get(); // TODO: may we ignore N here?
  1127. }
  1128. if (JSON_UNLIKELY(not unexpect_eof()))
  1129. {
  1130. return false;
  1131. }
  1132. switch (current)
  1133. {
  1134. case 'U':
  1135. {
  1136. uint8_t len;
  1137. return get_number(len) and get_string(len, result);
  1138. }
  1139. case 'i':
  1140. {
  1141. int8_t len;
  1142. return get_number(len) and get_string(len, result);
  1143. }
  1144. case 'I':
  1145. {
  1146. int16_t len;
  1147. return get_number(len) and get_string(len, result);
  1148. }
  1149. case 'l':
  1150. {
  1151. int32_t len;
  1152. return get_number(len) and get_string(len, result);
  1153. }
  1154. case 'L':
  1155. {
  1156. int64_t len;
  1157. return get_number(len) and get_string(len, result);
  1158. }
  1159. default:
  1160. auto last_token = get_token_string();
  1161. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a UBJSON string; last byte: 0x" + last_token));
  1162. }
  1163. }
  1164. /*!
  1165. @param[out] result determined size
  1166. @return whether size determination completed
  1167. */
  1168. bool get_ubjson_size_value(std::size_t& result)
  1169. {
  1170. switch (get_ignore_noop())
  1171. {
  1172. case 'U':
  1173. {
  1174. uint8_t number;
  1175. if (JSON_UNLIKELY(not get_number(number)))
  1176. {
  1177. return false;
  1178. }
  1179. result = static_cast<std::size_t>(number);
  1180. return true;
  1181. }
  1182. case 'i':
  1183. {
  1184. int8_t number;
  1185. if (JSON_UNLIKELY(not get_number(number)))
  1186. {
  1187. return false;
  1188. }
  1189. result = static_cast<std::size_t>(number);
  1190. return true;
  1191. }
  1192. case 'I':
  1193. {
  1194. int16_t number;
  1195. if (JSON_UNLIKELY(not get_number(number)))
  1196. {
  1197. return false;
  1198. }
  1199. result = static_cast<std::size_t>(number);
  1200. return true;
  1201. }
  1202. case 'l':
  1203. {
  1204. int32_t number;
  1205. if (JSON_UNLIKELY(not get_number(number)))
  1206. {
  1207. return false;
  1208. }
  1209. result = static_cast<std::size_t>(number);
  1210. return true;
  1211. }
  1212. case 'L':
  1213. {
  1214. int64_t number;
  1215. if (JSON_UNLIKELY(not get_number(number)))
  1216. {
  1217. return false;
  1218. }
  1219. result = static_cast<std::size_t>(number);
  1220. return true;
  1221. }
  1222. default:
  1223. {
  1224. auto last_token = get_token_string();
  1225. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "byte after '#' must denote a number type; last byte: 0x" + last_token));
  1226. }
  1227. }
  1228. }
  1229. /*!
  1230. @brief determine the type and size for a container
  1231. In the optimized UBJSON format, a type and a size can be provided to allow
  1232. for a more compact representation.
  1233. @param[out] result pair of the size and the type
  1234. @return whether pair creation completed
  1235. */
  1236. bool get_ubjson_size_type(std::pair<std::size_t, int>& result)
  1237. {
  1238. result.first = string_t::npos; // size
  1239. result.second = 0; // type
  1240. get_ignore_noop();
  1241. if (current == '$')
  1242. {
  1243. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  1244. if (JSON_UNLIKELY(not unexpect_eof()))
  1245. {
  1246. return false;
  1247. }
  1248. get_ignore_noop();
  1249. if (JSON_UNLIKELY(current != '#'))
  1250. {
  1251. if (JSON_UNLIKELY(not unexpect_eof()))
  1252. {
  1253. return false;
  1254. }
  1255. auto last_token = get_token_string();
  1256. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "expected '#' after UBJSON type information; last byte: 0x" + last_token));
  1257. }
  1258. return get_ubjson_size_value(result.first);
  1259. }
  1260. else if (current == '#')
  1261. {
  1262. return get_ubjson_size_value(result.first);
  1263. }
  1264. return true;
  1265. }
  1266. /*!
  1267. @param prefix the previously read or set type prefix
  1268. @return whether value creation completed
  1269. */
  1270. bool get_ubjson_value(const int prefix)
  1271. {
  1272. switch (prefix)
  1273. {
  1274. case std::char_traits<char>::eof(): // EOF
  1275. return unexpect_eof();
  1276. case 'T': // true
  1277. return sax->boolean(true);
  1278. case 'F': // false
  1279. return sax->boolean(false);
  1280. case 'Z': // null
  1281. return sax->null();
  1282. case 'U':
  1283. {
  1284. uint8_t number;
  1285. return get_number(number) and sax->number_unsigned(number);
  1286. }
  1287. case 'i':
  1288. {
  1289. int8_t number;
  1290. return get_number(number) and sax->number_integer(number);
  1291. }
  1292. case 'I':
  1293. {
  1294. int16_t number;
  1295. return get_number(number) and sax->number_integer(number);
  1296. }
  1297. case 'l':
  1298. {
  1299. int32_t number;
  1300. return get_number(number) and sax->number_integer(number);
  1301. }
  1302. case 'L':
  1303. {
  1304. int64_t number;
  1305. return get_number(number) and sax->number_integer(number);
  1306. }
  1307. case 'd':
  1308. {
  1309. float number;
  1310. return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
  1311. }
  1312. case 'D':
  1313. {
  1314. double number;
  1315. return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
  1316. }
  1317. case 'C': // char
  1318. {
  1319. get();
  1320. if (JSON_UNLIKELY(not unexpect_eof()))
  1321. {
  1322. return false;
  1323. }
  1324. if (JSON_UNLIKELY(current > 127))
  1325. {
  1326. auto last_token = get_token_string();
  1327. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token));
  1328. }
  1329. string_t s(1, static_cast<char>(current));
  1330. return sax->string(s);
  1331. }
  1332. case 'S': // string
  1333. {
  1334. string_t s;
  1335. return get_ubjson_string(s) and sax->string(s);
  1336. }
  1337. case '[': // array
  1338. return get_ubjson_array();
  1339. case '{': // object
  1340. return get_ubjson_object();
  1341. default: // anything else
  1342. {
  1343. auto last_token = get_token_string();
  1344. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading UBJSON; last byte: 0x" + last_token));
  1345. }
  1346. }
  1347. }
  1348. /*!
  1349. @return whether array creation completed
  1350. */
  1351. bool get_ubjson_array()
  1352. {
  1353. std::pair<std::size_t, int> size_and_type;
  1354. if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
  1355. {
  1356. return false;
  1357. }
  1358. if (size_and_type.first != string_t::npos)
  1359. {
  1360. if (JSON_UNLIKELY(not sax->start_array(size_and_type.first)))
  1361. {
  1362. return false;
  1363. }
  1364. if (size_and_type.second != 0)
  1365. {
  1366. if (size_and_type.second != 'N')
  1367. {
  1368. for (std::size_t i = 0; i < size_and_type.first; ++i)
  1369. {
  1370. if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
  1371. {
  1372. return false;
  1373. }
  1374. }
  1375. }
  1376. }
  1377. else
  1378. {
  1379. for (std::size_t i = 0; i < size_and_type.first; ++i)
  1380. {
  1381. if (JSON_UNLIKELY(not parse_ubjson_internal()))
  1382. {
  1383. return false;
  1384. }
  1385. }
  1386. }
  1387. }
  1388. else
  1389. {
  1390. if (JSON_UNLIKELY(not sax->start_array()))
  1391. {
  1392. return false;
  1393. }
  1394. while (current != ']')
  1395. {
  1396. if (JSON_UNLIKELY(not parse_ubjson_internal(false)))
  1397. {
  1398. return false;
  1399. }
  1400. get_ignore_noop();
  1401. }
  1402. }
  1403. return sax->end_array();
  1404. }
  1405. /*!
  1406. @return whether object creation completed
  1407. */
  1408. bool get_ubjson_object()
  1409. {
  1410. std::pair<std::size_t, int> size_and_type;
  1411. if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
  1412. {
  1413. return false;
  1414. }
  1415. string_t key;
  1416. if (size_and_type.first != string_t::npos)
  1417. {
  1418. if (JSON_UNLIKELY(not sax->start_object(size_and_type.first)))
  1419. {
  1420. return false;
  1421. }
  1422. if (size_and_type.second != 0)
  1423. {
  1424. for (std::size_t i = 0; i < size_and_type.first; ++i)
  1425. {
  1426. if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
  1427. {
  1428. return false;
  1429. }
  1430. if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
  1431. {
  1432. return false;
  1433. }
  1434. key.clear();
  1435. }
  1436. }
  1437. else
  1438. {
  1439. for (std::size_t i = 0; i < size_and_type.first; ++i)
  1440. {
  1441. if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
  1442. {
  1443. return false;
  1444. }
  1445. if (JSON_UNLIKELY(not parse_ubjson_internal()))
  1446. {
  1447. return false;
  1448. }
  1449. key.clear();
  1450. }
  1451. }
  1452. }
  1453. else
  1454. {
  1455. if (JSON_UNLIKELY(not sax->start_object()))
  1456. {
  1457. return false;
  1458. }
  1459. while (current != '}')
  1460. {
  1461. if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
  1462. {
  1463. return false;
  1464. }
  1465. if (JSON_UNLIKELY(not parse_ubjson_internal()))
  1466. {
  1467. return false;
  1468. }
  1469. get_ignore_noop();
  1470. key.clear();
  1471. }
  1472. }
  1473. return sax->end_object();
  1474. }
  1475. /*!
  1476. @return whether the last read character is not EOF
  1477. */
  1478. bool unexpect_eof() const
  1479. {
  1480. if (JSON_UNLIKELY(current == std::char_traits<char>::eof()))
  1481. {
  1482. return sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, "unexpected end of input"));
  1483. }
  1484. return true;
  1485. }
  1486. /*!
  1487. @return a string representation of the last read byte
  1488. */
  1489. std::string get_token_string() const
  1490. {
  1491. char cr[3];
  1492. snprintf(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
  1493. return std::string{cr};
  1494. }
  1495. private:
  1496. /// input adapter
  1497. input_adapter_t ia = nullptr;
  1498. /// the current character
  1499. int current = std::char_traits<char>::eof();
  1500. /// the number of characters read
  1501. std::size_t chars_read = 0;
  1502. /// whether we can assume little endianess
  1503. const bool is_little_endian = little_endianess();
  1504. /// the SAX parser
  1505. json_sax_t* sax = nullptr;
  1506. };
  1507. }
  1508. }