binary_reader.hpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299
  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/exceptions.hpp>
  15. #include <nlohmann/detail/input/input_adapters.hpp>
  16. #include <nlohmann/detail/input/json_sax.hpp>
  17. #include <nlohmann/detail/macro_scope.hpp>
  18. #include <nlohmann/detail/meta/is_sax.hpp>
  19. #include <nlohmann/detail/value_t.hpp>
  20. namespace nlohmann
  21. {
  22. namespace detail
  23. {
  24. ///////////////////
  25. // binary reader //
  26. ///////////////////
  27. /*!
  28. @brief deserialization of CBOR, MessagePack, and UBJSON values
  29. */
  30. template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  31. class binary_reader
  32. {
  33. using number_integer_t = typename BasicJsonType::number_integer_t;
  34. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  35. using number_float_t = typename BasicJsonType::number_float_t;
  36. using string_t = typename BasicJsonType::string_t;
  37. using internal_binary_t = typename BasicJsonType::internal_binary_t;
  38. using json_sax_t = SAX;
  39. public:
  40. /*!
  41. @brief create a binary reader
  42. @param[in] adapter input adapter to read from
  43. */
  44. explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
  45. {
  46. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  47. assert(ia);
  48. }
  49. // make class move-only
  50. binary_reader(const binary_reader&) = delete;
  51. binary_reader(binary_reader&&) = default;
  52. binary_reader& operator=(const binary_reader&) = delete;
  53. binary_reader& operator=(binary_reader&&) = default;
  54. ~binary_reader() = default;
  55. /*!
  56. @param[in] format the binary format to parse
  57. @param[in] sax_ a SAX event processor
  58. @param[in] strict whether to expect the input to be consumed completed
  59. @return
  60. */
  61. JSON_HEDLEY_NON_NULL(3)
  62. bool sax_parse(const input_format_t format,
  63. json_sax_t* sax_,
  64. const bool strict = true)
  65. {
  66. sax = sax_;
  67. bool result = false;
  68. switch (format)
  69. {
  70. case input_format_t::bson:
  71. result = parse_bson_internal();
  72. break;
  73. case input_format_t::cbor:
  74. result = parse_cbor_internal();
  75. break;
  76. case input_format_t::msgpack:
  77. result = parse_msgpack_internal();
  78. break;
  79. case input_format_t::ubjson:
  80. result = parse_ubjson_internal();
  81. break;
  82. default: // LCOV_EXCL_LINE
  83. assert(false); // LCOV_EXCL_LINE
  84. }
  85. // strict mode: next byte must be EOF
  86. if (result and strict)
  87. {
  88. if (format == input_format_t::ubjson)
  89. {
  90. get_ignore_noop();
  91. }
  92. else
  93. {
  94. get();
  95. }
  96. if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char>::eof()))
  97. {
  98. return sax->parse_error(chars_read, get_token_string(),
  99. parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value")));
  100. }
  101. }
  102. return result;
  103. }
  104. /*!
  105. @brief determine system byte order
  106. @return true if and only if system's byte order is little endian
  107. @note from https://stackoverflow.com/a/1001328/266378
  108. */
  109. static constexpr bool little_endianess(int num = 1) noexcept
  110. {
  111. return *reinterpret_cast<char*>(&num) == 1;
  112. }
  113. private:
  114. //////////
  115. // BSON //
  116. //////////
  117. /*!
  118. @brief Reads in a BSON-object and passes it to the SAX-parser.
  119. @return whether a valid BSON-value was passed to the SAX parser
  120. */
  121. bool parse_bson_internal()
  122. {
  123. std::int32_t document_size;
  124. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  125. if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1))))
  126. {
  127. return false;
  128. }
  129. if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_list(/*is_array*/false)))
  130. {
  131. return false;
  132. }
  133. return sax->end_object();
  134. }
  135. /*!
  136. @brief Parses a C-style string from the BSON input.
  137. @param[in, out] result A reference to the string variable where the read
  138. string is to be stored.
  139. @return `true` if the \x00-byte indicating the end of the string was
  140. encountered before the EOF; false` indicates an unexpected EOF.
  141. */
  142. bool get_bson_cstr(string_t& result)
  143. {
  144. auto out = std::back_inserter(result);
  145. while (true)
  146. {
  147. get();
  148. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::bson, "cstring")))
  149. {
  150. return false;
  151. }
  152. if (current == 0x00)
  153. {
  154. return true;
  155. }
  156. *out++ = static_cast<char>(current);
  157. }
  158. return true;
  159. }
  160. /*!
  161. @brief Parses a zero-terminated string of length @a len from the BSON
  162. input.
  163. @param[in] len The length (including the zero-byte at the end) of the
  164. string to be read.
  165. @param[in, out] result A reference to the string variable where the read
  166. string is to be stored.
  167. @tparam NumberType The type of the length @a len
  168. @pre len >= 1
  169. @return `true` if the string was successfully parsed
  170. */
  171. template<typename NumberType>
  172. bool get_bson_string(const NumberType len, string_t& result)
  173. {
  174. if (JSON_HEDLEY_UNLIKELY(len < 1))
  175. {
  176. auto last_token = get_token_string();
  177. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string")));
  178. }
  179. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) and get() != std::char_traits<char>::eof();
  180. }
  181. /*!
  182. @brief Parses a byte array input of length @a len from the BSON input.
  183. @param[in] len The length of the byte array to be read.
  184. @param[in, out] result A reference to the binary variable where the read
  185. array is to be stored.
  186. @tparam NumberType The type of the length @a len
  187. @pre len >= 0
  188. @return `true` if the byte array was successfully parsed
  189. */
  190. template<typename NumberType>
  191. bool get_bson_binary(const NumberType len, internal_binary_t& result)
  192. {
  193. if (JSON_HEDLEY_UNLIKELY(len < 0))
  194. {
  195. auto last_token = get_token_string();
  196. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary")));
  197. }
  198. result.has_subtype = true; // All BSON binary values have a subtype
  199. get_number<std::uint8_t>(input_format_t::bson, result.subtype);
  200. return get_binary(input_format_t::bson, len, result);
  201. }
  202. /*!
  203. @brief Read a BSON document element of the given @a element_type.
  204. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  205. @param[in] element_type_parse_position The position in the input stream,
  206. where the `element_type` was read.
  207. @warning Not all BSON element types are supported yet. An unsupported
  208. @a element_type will give rise to a parse_error.114:
  209. Unsupported BSON record type 0x...
  210. @return whether a valid BSON-object/array was passed to the SAX parser
  211. */
  212. bool parse_bson_element_internal(const int element_type,
  213. const std::size_t element_type_parse_position)
  214. {
  215. switch (element_type)
  216. {
  217. case 0x01: // double
  218. {
  219. double number;
  220. return get_number<double, true>(input_format_t::bson, number) and sax->number_float(static_cast<number_float_t>(number), "");
  221. }
  222. case 0x02: // string
  223. {
  224. std::int32_t len;
  225. string_t value;
  226. return get_number<std::int32_t, true>(input_format_t::bson, len) and get_bson_string(len, value) and sax->string(value);
  227. }
  228. case 0x03: // object
  229. {
  230. return parse_bson_internal();
  231. }
  232. case 0x04: // array
  233. {
  234. return parse_bson_array();
  235. }
  236. case 0x05: // binary
  237. {
  238. std::int32_t len;
  239. internal_binary_t value;
  240. return get_number<std::int32_t, true>(input_format_t::bson, len) and get_bson_binary(len, value) and sax->binary(value);
  241. }
  242. case 0x08: // boolean
  243. {
  244. return sax->boolean(get() != 0);
  245. }
  246. case 0x0A: // null
  247. {
  248. return sax->null();
  249. }
  250. case 0x10: // int32
  251. {
  252. std::int32_t value;
  253. return get_number<std::int32_t, true>(input_format_t::bson, value) and sax->number_integer(value);
  254. }
  255. case 0x12: // int64
  256. {
  257. std::int64_t value;
  258. return get_number<std::int64_t, true>(input_format_t::bson, value) and sax->number_integer(value);
  259. }
  260. default: // anything else not supported (yet)
  261. {
  262. std::array<char, 3> cr{{}};
  263. (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type));
  264. return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data())));
  265. }
  266. }
  267. }
  268. /*!
  269. @brief Read a BSON element list (as specified in the BSON-spec)
  270. The same binary layout is used for objects and arrays, hence it must be
  271. indicated with the argument @a is_array which one is expected
  272. (true --> array, false --> object).
  273. @param[in] is_array Determines if the element list being read is to be
  274. treated as an object (@a is_array == false), or as an
  275. array (@a is_array == true).
  276. @return whether a valid BSON-object/array was passed to the SAX parser
  277. */
  278. bool parse_bson_element_list(const bool is_array)
  279. {
  280. string_t key;
  281. while (int element_type = get())
  282. {
  283. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::bson, "element list")))
  284. {
  285. return false;
  286. }
  287. const std::size_t element_type_parse_position = chars_read;
  288. if (JSON_HEDLEY_UNLIKELY(not get_bson_cstr(key)))
  289. {
  290. return false;
  291. }
  292. if (not is_array and not sax->key(key))
  293. {
  294. return false;
  295. }
  296. if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_internal(element_type, element_type_parse_position)))
  297. {
  298. return false;
  299. }
  300. // get_bson_cstr only appends
  301. key.clear();
  302. }
  303. return true;
  304. }
  305. /*!
  306. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  307. @return whether a valid BSON-array was passed to the SAX parser
  308. */
  309. bool parse_bson_array()
  310. {
  311. std::int32_t document_size;
  312. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  313. if (JSON_HEDLEY_UNLIKELY(not sax->start_array(std::size_t(-1))))
  314. {
  315. return false;
  316. }
  317. if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_list(/*is_array*/true)))
  318. {
  319. return false;
  320. }
  321. return sax->end_array();
  322. }
  323. //////////
  324. // CBOR //
  325. //////////
  326. /*!
  327. @param[in] get_char whether a new character should be retrieved from the
  328. input (true, default) or whether the last read
  329. character should be considered instead
  330. @return whether a valid CBOR value was passed to the SAX parser
  331. */
  332. bool parse_cbor_internal(const bool get_char = true)
  333. {
  334. switch (get_char ? get() : current)
  335. {
  336. // EOF
  337. case std::char_traits<char>::eof():
  338. return unexpect_eof(input_format_t::cbor, "value");
  339. // Integer 0x00..0x17 (0..23)
  340. case 0x00:
  341. case 0x01:
  342. case 0x02:
  343. case 0x03:
  344. case 0x04:
  345. case 0x05:
  346. case 0x06:
  347. case 0x07:
  348. case 0x08:
  349. case 0x09:
  350. case 0x0A:
  351. case 0x0B:
  352. case 0x0C:
  353. case 0x0D:
  354. case 0x0E:
  355. case 0x0F:
  356. case 0x10:
  357. case 0x11:
  358. case 0x12:
  359. case 0x13:
  360. case 0x14:
  361. case 0x15:
  362. case 0x16:
  363. case 0x17:
  364. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  365. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  366. {
  367. std::uint8_t number;
  368. return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
  369. }
  370. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  371. {
  372. std::uint16_t number;
  373. return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
  374. }
  375. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  376. {
  377. std::uint32_t number;
  378. return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
  379. }
  380. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  381. {
  382. std::uint64_t number;
  383. return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
  384. }
  385. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  386. case 0x20:
  387. case 0x21:
  388. case 0x22:
  389. case 0x23:
  390. case 0x24:
  391. case 0x25:
  392. case 0x26:
  393. case 0x27:
  394. case 0x28:
  395. case 0x29:
  396. case 0x2A:
  397. case 0x2B:
  398. case 0x2C:
  399. case 0x2D:
  400. case 0x2E:
  401. case 0x2F:
  402. case 0x30:
  403. case 0x31:
  404. case 0x32:
  405. case 0x33:
  406. case 0x34:
  407. case 0x35:
  408. case 0x36:
  409. case 0x37:
  410. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  411. case 0x38: // Negative integer (one-byte uint8_t follows)
  412. {
  413. std::uint8_t number;
  414. return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  415. }
  416. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  417. {
  418. std::uint16_t number;
  419. return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  420. }
  421. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  422. {
  423. std::uint32_t number;
  424. return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  425. }
  426. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  427. {
  428. std::uint64_t number;
  429. return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1)
  430. - static_cast<number_integer_t>(number));
  431. }
  432. // Binary data (0x00..0x17 bytes follow)
  433. case 0x40:
  434. case 0x41:
  435. case 0x42:
  436. case 0x43:
  437. case 0x44:
  438. case 0x45:
  439. case 0x46:
  440. case 0x47:
  441. case 0x48:
  442. case 0x49:
  443. case 0x4A:
  444. case 0x4B:
  445. case 0x4C:
  446. case 0x4D:
  447. case 0x4E:
  448. case 0x4F:
  449. case 0x50:
  450. case 0x51:
  451. case 0x52:
  452. case 0x53:
  453. case 0x54:
  454. case 0x55:
  455. case 0x56:
  456. case 0x57:
  457. case 0x58: // Binary data (one-byte uint8_t for n follows)
  458. case 0x59: // Binary data (two-byte uint16_t for n follow)
  459. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  460. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  461. case 0x5F: // Binary data (indefinite length)
  462. {
  463. internal_binary_t b;
  464. return get_cbor_binary(b) and sax->binary(b);
  465. }
  466. // UTF-8 string (0x00..0x17 bytes follow)
  467. case 0x60:
  468. case 0x61:
  469. case 0x62:
  470. case 0x63:
  471. case 0x64:
  472. case 0x65:
  473. case 0x66:
  474. case 0x67:
  475. case 0x68:
  476. case 0x69:
  477. case 0x6A:
  478. case 0x6B:
  479. case 0x6C:
  480. case 0x6D:
  481. case 0x6E:
  482. case 0x6F:
  483. case 0x70:
  484. case 0x71:
  485. case 0x72:
  486. case 0x73:
  487. case 0x74:
  488. case 0x75:
  489. case 0x76:
  490. case 0x77:
  491. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  492. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  493. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  494. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  495. case 0x7F: // UTF-8 string (indefinite length)
  496. {
  497. string_t s;
  498. return get_cbor_string(s) and sax->string(s);
  499. }
  500. // array (0x00..0x17 data items follow)
  501. case 0x80:
  502. case 0x81:
  503. case 0x82:
  504. case 0x83:
  505. case 0x84:
  506. case 0x85:
  507. case 0x86:
  508. case 0x87:
  509. case 0x88:
  510. case 0x89:
  511. case 0x8A:
  512. case 0x8B:
  513. case 0x8C:
  514. case 0x8D:
  515. case 0x8E:
  516. case 0x8F:
  517. case 0x90:
  518. case 0x91:
  519. case 0x92:
  520. case 0x93:
  521. case 0x94:
  522. case 0x95:
  523. case 0x96:
  524. case 0x97:
  525. return get_cbor_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
  526. case 0x98: // array (one-byte uint8_t for n follows)
  527. {
  528. std::uint8_t len;
  529. return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
  530. }
  531. case 0x99: // array (two-byte uint16_t for n follow)
  532. {
  533. std::uint16_t len;
  534. return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
  535. }
  536. case 0x9A: // array (four-byte uint32_t for n follow)
  537. {
  538. std::uint32_t len;
  539. return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
  540. }
  541. case 0x9B: // array (eight-byte uint64_t for n follow)
  542. {
  543. std::uint64_t len;
  544. return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
  545. }
  546. case 0x9F: // array (indefinite length)
  547. return get_cbor_array(std::size_t(-1));
  548. // map (0x00..0x17 pairs of data items follow)
  549. case 0xA0:
  550. case 0xA1:
  551. case 0xA2:
  552. case 0xA3:
  553. case 0xA4:
  554. case 0xA5:
  555. case 0xA6:
  556. case 0xA7:
  557. case 0xA8:
  558. case 0xA9:
  559. case 0xAA:
  560. case 0xAB:
  561. case 0xAC:
  562. case 0xAD:
  563. case 0xAE:
  564. case 0xAF:
  565. case 0xB0:
  566. case 0xB1:
  567. case 0xB2:
  568. case 0xB3:
  569. case 0xB4:
  570. case 0xB5:
  571. case 0xB6:
  572. case 0xB7:
  573. return get_cbor_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
  574. case 0xB8: // map (one-byte uint8_t for n follows)
  575. {
  576. std::uint8_t len;
  577. return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
  578. }
  579. case 0xB9: // map (two-byte uint16_t for n follow)
  580. {
  581. std::uint16_t len;
  582. return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
  583. }
  584. case 0xBA: // map (four-byte uint32_t for n follow)
  585. {
  586. std::uint32_t len;
  587. return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
  588. }
  589. case 0xBB: // map (eight-byte uint64_t for n follow)
  590. {
  591. std::uint64_t len;
  592. return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
  593. }
  594. case 0xBF: // map (indefinite length)
  595. return get_cbor_object(std::size_t(-1));
  596. case 0xF4: // false
  597. return sax->boolean(false);
  598. case 0xF5: // true
  599. return sax->boolean(true);
  600. case 0xF6: // null
  601. return sax->null();
  602. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  603. {
  604. const int byte1_raw = get();
  605. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
  606. {
  607. return false;
  608. }
  609. const int byte2_raw = get();
  610. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
  611. {
  612. return false;
  613. }
  614. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  615. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  616. // code from RFC 7049, Appendix D, Figure 3:
  617. // As half-precision floating-point numbers were only added
  618. // to IEEE 754 in 2008, today's programming platforms often
  619. // still only have limited support for them. It is very
  620. // easy to include at least decoding support for them even
  621. // without such support. An example of a small decoder for
  622. // half-precision floating-point numbers in the C language
  623. // is shown in Fig. 3.
  624. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  625. const double val = [&half]
  626. {
  627. const int exp = (half >> 10u) & 0x1Fu;
  628. const unsigned int mant = half & 0x3FFu;
  629. assert(0 <= exp and exp <= 32);
  630. assert(mant <= 1024);
  631. switch (exp)
  632. {
  633. case 0:
  634. return std::ldexp(mant, -24);
  635. case 31:
  636. return (mant == 0)
  637. ? std::numeric_limits<double>::infinity()
  638. : std::numeric_limits<double>::quiet_NaN();
  639. default:
  640. return std::ldexp(mant + 1024, exp - 25);
  641. }
  642. }();
  643. return sax->number_float((half & 0x8000u) != 0
  644. ? static_cast<number_float_t>(-val)
  645. : static_cast<number_float_t>(val), "");
  646. }
  647. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  648. {
  649. float number;
  650. return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
  651. }
  652. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  653. {
  654. double number;
  655. return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
  656. }
  657. default: // anything else (0xFF is handled inside the other types)
  658. {
  659. auto last_token = get_token_string();
  660. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value")));
  661. }
  662. }
  663. }
  664. /*!
  665. @brief reads a CBOR string
  666. This function first reads starting bytes to determine the expected
  667. string length and then copies this number of bytes into a string.
  668. Additionally, CBOR's strings with indefinite lengths are supported.
  669. @param[out] result created string
  670. @return whether string creation completed
  671. */
  672. bool get_cbor_string(string_t& result)
  673. {
  674. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "string")))
  675. {
  676. return false;
  677. }
  678. switch (current)
  679. {
  680. // UTF-8 string (0x00..0x17 bytes follow)
  681. case 0x60:
  682. case 0x61:
  683. case 0x62:
  684. case 0x63:
  685. case 0x64:
  686. case 0x65:
  687. case 0x66:
  688. case 0x67:
  689. case 0x68:
  690. case 0x69:
  691. case 0x6A:
  692. case 0x6B:
  693. case 0x6C:
  694. case 0x6D:
  695. case 0x6E:
  696. case 0x6F:
  697. case 0x70:
  698. case 0x71:
  699. case 0x72:
  700. case 0x73:
  701. case 0x74:
  702. case 0x75:
  703. case 0x76:
  704. case 0x77:
  705. {
  706. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  707. }
  708. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  709. {
  710. std::uint8_t len;
  711. return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
  712. }
  713. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  714. {
  715. std::uint16_t len;
  716. return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
  717. }
  718. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  719. {
  720. std::uint32_t len;
  721. return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
  722. }
  723. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  724. {
  725. std::uint64_t len;
  726. return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
  727. }
  728. case 0x7F: // UTF-8 string (indefinite length)
  729. {
  730. while (get() != 0xFF)
  731. {
  732. string_t chunk;
  733. if (not get_cbor_string(chunk))
  734. {
  735. return false;
  736. }
  737. result.append(chunk);
  738. }
  739. return true;
  740. }
  741. default:
  742. {
  743. auto last_token = get_token_string();
  744. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string")));
  745. }
  746. }
  747. }
  748. /*!
  749. @brief reads a CBOR byte array
  750. This function first reads starting bytes to determine the expected
  751. byte array length and then copies this number of bytes into the byte array.
  752. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  753. @param[out] result created byte array
  754. @return whether byte array creation completed
  755. */
  756. bool get_cbor_binary(internal_binary_t& result)
  757. {
  758. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "binary")))
  759. {
  760. return false;
  761. }
  762. switch (current)
  763. {
  764. // Binary data (0x00..0x17 bytes follow)
  765. case 0x40:
  766. case 0x41:
  767. case 0x42:
  768. case 0x43:
  769. case 0x44:
  770. case 0x45:
  771. case 0x46:
  772. case 0x47:
  773. case 0x48:
  774. case 0x49:
  775. case 0x4A:
  776. case 0x4B:
  777. case 0x4C:
  778. case 0x4D:
  779. case 0x4E:
  780. case 0x4F:
  781. case 0x50:
  782. case 0x51:
  783. case 0x52:
  784. case 0x53:
  785. case 0x54:
  786. case 0x55:
  787. case 0x56:
  788. case 0x57:
  789. {
  790. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  791. }
  792. case 0x58: // Binary data (one-byte uint8_t for n follows)
  793. {
  794. std::uint8_t len;
  795. return get_number(input_format_t::cbor, len) and get_binary(input_format_t::cbor, len, result);
  796. }
  797. case 0x59: // Binary data (two-byte uint16_t for n follow)
  798. {
  799. std::uint16_t len;
  800. return get_number(input_format_t::cbor, len) and get_binary(input_format_t::cbor, len, result);
  801. }
  802. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  803. {
  804. std::uint32_t len;
  805. return get_number(input_format_t::cbor, len) and get_binary(input_format_t::cbor, len, result);
  806. }
  807. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  808. {
  809. std::uint64_t len;
  810. return get_number(input_format_t::cbor, len) and get_binary(input_format_t::cbor, len, result);
  811. }
  812. case 0x5F: // Binary data (indefinite length)
  813. {
  814. while (get() != 0xFF)
  815. {
  816. internal_binary_t chunk;
  817. if (not get_cbor_binary(chunk))
  818. {
  819. return false;
  820. }
  821. result.insert(result.end(), chunk.begin(), chunk.end());
  822. }
  823. return true;
  824. }
  825. default:
  826. {
  827. auto last_token = get_token_string();
  828. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary")));
  829. }
  830. }
  831. }
  832. /*!
  833. @param[in] len the length of the array or std::size_t(-1) for an
  834. array of indefinite size
  835. @return whether array creation completed
  836. */
  837. bool get_cbor_array(const std::size_t len)
  838. {
  839. if (JSON_HEDLEY_UNLIKELY(not sax->start_array(len)))
  840. {
  841. return false;
  842. }
  843. if (len != std::size_t(-1))
  844. {
  845. for (std::size_t i = 0; i < len; ++i)
  846. {
  847. if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal()))
  848. {
  849. return false;
  850. }
  851. }
  852. }
  853. else
  854. {
  855. while (get() != 0xFF)
  856. {
  857. if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal(false)))
  858. {
  859. return false;
  860. }
  861. }
  862. }
  863. return sax->end_array();
  864. }
  865. /*!
  866. @param[in] len the length of the object or std::size_t(-1) for an
  867. object of indefinite size
  868. @return whether object creation completed
  869. */
  870. bool get_cbor_object(const std::size_t len)
  871. {
  872. if (JSON_HEDLEY_UNLIKELY(not sax->start_object(len)))
  873. {
  874. return false;
  875. }
  876. string_t key;
  877. if (len != std::size_t(-1))
  878. {
  879. for (std::size_t i = 0; i < len; ++i)
  880. {
  881. get();
  882. if (JSON_HEDLEY_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
  883. {
  884. return false;
  885. }
  886. if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal()))
  887. {
  888. return false;
  889. }
  890. key.clear();
  891. }
  892. }
  893. else
  894. {
  895. while (get() != 0xFF)
  896. {
  897. if (JSON_HEDLEY_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
  898. {
  899. return false;
  900. }
  901. if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal()))
  902. {
  903. return false;
  904. }
  905. key.clear();
  906. }
  907. }
  908. return sax->end_object();
  909. }
  910. /////////////
  911. // MsgPack //
  912. /////////////
  913. /*!
  914. @return whether a valid MessagePack value was passed to the SAX parser
  915. */
  916. bool parse_msgpack_internal()
  917. {
  918. switch (get())
  919. {
  920. // EOF
  921. case std::char_traits<char>::eof():
  922. return unexpect_eof(input_format_t::msgpack, "value");
  923. // positive fixint
  924. case 0x00:
  925. case 0x01:
  926. case 0x02:
  927. case 0x03:
  928. case 0x04:
  929. case 0x05:
  930. case 0x06:
  931. case 0x07:
  932. case 0x08:
  933. case 0x09:
  934. case 0x0A:
  935. case 0x0B:
  936. case 0x0C:
  937. case 0x0D:
  938. case 0x0E:
  939. case 0x0F:
  940. case 0x10:
  941. case 0x11:
  942. case 0x12:
  943. case 0x13:
  944. case 0x14:
  945. case 0x15:
  946. case 0x16:
  947. case 0x17:
  948. case 0x18:
  949. case 0x19:
  950. case 0x1A:
  951. case 0x1B:
  952. case 0x1C:
  953. case 0x1D:
  954. case 0x1E:
  955. case 0x1F:
  956. case 0x20:
  957. case 0x21:
  958. case 0x22:
  959. case 0x23:
  960. case 0x24:
  961. case 0x25:
  962. case 0x26:
  963. case 0x27:
  964. case 0x28:
  965. case 0x29:
  966. case 0x2A:
  967. case 0x2B:
  968. case 0x2C:
  969. case 0x2D:
  970. case 0x2E:
  971. case 0x2F:
  972. case 0x30:
  973. case 0x31:
  974. case 0x32:
  975. case 0x33:
  976. case 0x34:
  977. case 0x35:
  978. case 0x36:
  979. case 0x37:
  980. case 0x38:
  981. case 0x39:
  982. case 0x3A:
  983. case 0x3B:
  984. case 0x3C:
  985. case 0x3D:
  986. case 0x3E:
  987. case 0x3F:
  988. case 0x40:
  989. case 0x41:
  990. case 0x42:
  991. case 0x43:
  992. case 0x44:
  993. case 0x45:
  994. case 0x46:
  995. case 0x47:
  996. case 0x48:
  997. case 0x49:
  998. case 0x4A:
  999. case 0x4B:
  1000. case 0x4C:
  1001. case 0x4D:
  1002. case 0x4E:
  1003. case 0x4F:
  1004. case 0x50:
  1005. case 0x51:
  1006. case 0x52:
  1007. case 0x53:
  1008. case 0x54:
  1009. case 0x55:
  1010. case 0x56:
  1011. case 0x57:
  1012. case 0x58:
  1013. case 0x59:
  1014. case 0x5A:
  1015. case 0x5B:
  1016. case 0x5C:
  1017. case 0x5D:
  1018. case 0x5E:
  1019. case 0x5F:
  1020. case 0x60:
  1021. case 0x61:
  1022. case 0x62:
  1023. case 0x63:
  1024. case 0x64:
  1025. case 0x65:
  1026. case 0x66:
  1027. case 0x67:
  1028. case 0x68:
  1029. case 0x69:
  1030. case 0x6A:
  1031. case 0x6B:
  1032. case 0x6C:
  1033. case 0x6D:
  1034. case 0x6E:
  1035. case 0x6F:
  1036. case 0x70:
  1037. case 0x71:
  1038. case 0x72:
  1039. case 0x73:
  1040. case 0x74:
  1041. case 0x75:
  1042. case 0x76:
  1043. case 0x77:
  1044. case 0x78:
  1045. case 0x79:
  1046. case 0x7A:
  1047. case 0x7B:
  1048. case 0x7C:
  1049. case 0x7D:
  1050. case 0x7E:
  1051. case 0x7F:
  1052. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  1053. // fixmap
  1054. case 0x80:
  1055. case 0x81:
  1056. case 0x82:
  1057. case 0x83:
  1058. case 0x84:
  1059. case 0x85:
  1060. case 0x86:
  1061. case 0x87:
  1062. case 0x88:
  1063. case 0x89:
  1064. case 0x8A:
  1065. case 0x8B:
  1066. case 0x8C:
  1067. case 0x8D:
  1068. case 0x8E:
  1069. case 0x8F:
  1070. return get_msgpack_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  1071. // fixarray
  1072. case 0x90:
  1073. case 0x91:
  1074. case 0x92:
  1075. case 0x93:
  1076. case 0x94:
  1077. case 0x95:
  1078. case 0x96:
  1079. case 0x97:
  1080. case 0x98:
  1081. case 0x99:
  1082. case 0x9A:
  1083. case 0x9B:
  1084. case 0x9C:
  1085. case 0x9D:
  1086. case 0x9E:
  1087. case 0x9F:
  1088. return get_msgpack_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  1089. // fixstr
  1090. case 0xA0:
  1091. case 0xA1:
  1092. case 0xA2:
  1093. case 0xA3:
  1094. case 0xA4:
  1095. case 0xA5:
  1096. case 0xA6:
  1097. case 0xA7:
  1098. case 0xA8:
  1099. case 0xA9:
  1100. case 0xAA:
  1101. case 0xAB:
  1102. case 0xAC:
  1103. case 0xAD:
  1104. case 0xAE:
  1105. case 0xAF:
  1106. case 0xB0:
  1107. case 0xB1:
  1108. case 0xB2:
  1109. case 0xB3:
  1110. case 0xB4:
  1111. case 0xB5:
  1112. case 0xB6:
  1113. case 0xB7:
  1114. case 0xB8:
  1115. case 0xB9:
  1116. case 0xBA:
  1117. case 0xBB:
  1118. case 0xBC:
  1119. case 0xBD:
  1120. case 0xBE:
  1121. case 0xBF:
  1122. case 0xD9: // str 8
  1123. case 0xDA: // str 16
  1124. case 0xDB: // str 32
  1125. {
  1126. string_t s;
  1127. return get_msgpack_string(s) and sax->string(s);
  1128. }
  1129. case 0xC0: // nil
  1130. return sax->null();
  1131. case 0xC2: // false
  1132. return sax->boolean(false);
  1133. case 0xC3: // true
  1134. return sax->boolean(true);
  1135. case 0xC4: // bin 8
  1136. case 0xC5: // bin 16
  1137. case 0xC6: // bin 32
  1138. case 0xC7: // ext 8
  1139. case 0xC8: // ext 16
  1140. case 0xC9: // ext 32
  1141. case 0xD4: // fixext 1
  1142. case 0xD5: // fixext 2
  1143. case 0xD6: // fixext 4
  1144. case 0xD7: // fixext 8
  1145. case 0xD8: // fixext 16
  1146. {
  1147. internal_binary_t b;
  1148. return get_msgpack_binary(b) and sax->binary(b);
  1149. }
  1150. case 0xCA: // float 32
  1151. {
  1152. float number;
  1153. return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
  1154. }
  1155. case 0xCB: // float 64
  1156. {
  1157. double number;
  1158. return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
  1159. }
  1160. case 0xCC: // uint 8
  1161. {
  1162. std::uint8_t number;
  1163. return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
  1164. }
  1165. case 0xCD: // uint 16
  1166. {
  1167. std::uint16_t number;
  1168. return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
  1169. }
  1170. case 0xCE: // uint 32
  1171. {
  1172. std::uint32_t number;
  1173. return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
  1174. }
  1175. case 0xCF: // uint 64
  1176. {
  1177. std::uint64_t number;
  1178. return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
  1179. }
  1180. case 0xD0: // int 8
  1181. {
  1182. std::int8_t number;
  1183. return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
  1184. }
  1185. case 0xD1: // int 16
  1186. {
  1187. std::int16_t number;
  1188. return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
  1189. }
  1190. case 0xD2: // int 32
  1191. {
  1192. std::int32_t number;
  1193. return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
  1194. }
  1195. case 0xD3: // int 64
  1196. {
  1197. std::int64_t number;
  1198. return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
  1199. }
  1200. case 0xDC: // array 16
  1201. {
  1202. std::uint16_t len;
  1203. return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
  1204. }
  1205. case 0xDD: // array 32
  1206. {
  1207. std::uint32_t len;
  1208. return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
  1209. }
  1210. case 0xDE: // map 16
  1211. {
  1212. std::uint16_t len;
  1213. return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
  1214. }
  1215. case 0xDF: // map 32
  1216. {
  1217. std::uint32_t len;
  1218. return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
  1219. }
  1220. // negative fixint
  1221. case 0xE0:
  1222. case 0xE1:
  1223. case 0xE2:
  1224. case 0xE3:
  1225. case 0xE4:
  1226. case 0xE5:
  1227. case 0xE6:
  1228. case 0xE7:
  1229. case 0xE8:
  1230. case 0xE9:
  1231. case 0xEA:
  1232. case 0xEB:
  1233. case 0xEC:
  1234. case 0xED:
  1235. case 0xEE:
  1236. case 0xEF:
  1237. case 0xF0:
  1238. case 0xF1:
  1239. case 0xF2:
  1240. case 0xF3:
  1241. case 0xF4:
  1242. case 0xF5:
  1243. case 0xF6:
  1244. case 0xF7:
  1245. case 0xF8:
  1246. case 0xF9:
  1247. case 0xFA:
  1248. case 0xFB:
  1249. case 0xFC:
  1250. case 0xFD:
  1251. case 0xFE:
  1252. case 0xFF:
  1253. return sax->number_integer(static_cast<std::int8_t>(current));
  1254. default: // anything else
  1255. {
  1256. auto last_token = get_token_string();
  1257. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value")));
  1258. }
  1259. }
  1260. }
  1261. /*!
  1262. @brief reads a MessagePack string
  1263. This function first reads starting bytes to determine the expected
  1264. string length and then copies this number of bytes into a string.
  1265. @param[out] result created string
  1266. @return whether string creation completed
  1267. */
  1268. bool get_msgpack_string(string_t& result)
  1269. {
  1270. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "string")))
  1271. {
  1272. return false;
  1273. }
  1274. switch (current)
  1275. {
  1276. // fixstr
  1277. case 0xA0:
  1278. case 0xA1:
  1279. case 0xA2:
  1280. case 0xA3:
  1281. case 0xA4:
  1282. case 0xA5:
  1283. case 0xA6:
  1284. case 0xA7:
  1285. case 0xA8:
  1286. case 0xA9:
  1287. case 0xAA:
  1288. case 0xAB:
  1289. case 0xAC:
  1290. case 0xAD:
  1291. case 0xAE:
  1292. case 0xAF:
  1293. case 0xB0:
  1294. case 0xB1:
  1295. case 0xB2:
  1296. case 0xB3:
  1297. case 0xB4:
  1298. case 0xB5:
  1299. case 0xB6:
  1300. case 0xB7:
  1301. case 0xB8:
  1302. case 0xB9:
  1303. case 0xBA:
  1304. case 0xBB:
  1305. case 0xBC:
  1306. case 0xBD:
  1307. case 0xBE:
  1308. case 0xBF:
  1309. {
  1310. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  1311. }
  1312. case 0xD9: // str 8
  1313. {
  1314. std::uint8_t len;
  1315. return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
  1316. }
  1317. case 0xDA: // str 16
  1318. {
  1319. std::uint16_t len;
  1320. return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
  1321. }
  1322. case 0xDB: // str 32
  1323. {
  1324. std::uint32_t len;
  1325. return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
  1326. }
  1327. default:
  1328. {
  1329. auto last_token = get_token_string();
  1330. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string")));
  1331. }
  1332. }
  1333. }
  1334. /*!
  1335. @brief reads a MessagePack byte array
  1336. This function first reads starting bytes to determine the expected
  1337. byte array length and then copies this number of bytes into a byte array.
  1338. @param[out] result created byte array
  1339. @return whether byte array creation completed
  1340. */
  1341. bool get_msgpack_binary(internal_binary_t& result)
  1342. {
  1343. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "binary")))
  1344. {
  1345. return false;
  1346. }
  1347. switch (current)
  1348. {
  1349. case 0xC4: // bin 8
  1350. {
  1351. std::uint8_t len;
  1352. return get_number(input_format_t::msgpack, len) and get_binary(input_format_t::msgpack, len, result);
  1353. }
  1354. case 0xC5: // bin 16
  1355. {
  1356. std::uint16_t len;
  1357. return get_number(input_format_t::msgpack, len) and get_binary(input_format_t::msgpack, len, result);
  1358. }
  1359. case 0xC6: // bin 32
  1360. {
  1361. std::uint32_t len;
  1362. return get_number(input_format_t::msgpack, len) and get_binary(input_format_t::msgpack, len, result);
  1363. }
  1364. case 0xC7: // ext 8
  1365. {
  1366. std::uint8_t len;
  1367. result.has_subtype = true;
  1368. return get_number(input_format_t::msgpack, len) and
  1369. get_number(input_format_t::msgpack, result.subtype) and
  1370. get_binary(input_format_t::msgpack, len, result);
  1371. }
  1372. case 0xC8: // ext 16
  1373. {
  1374. std::uint16_t len;
  1375. result.has_subtype = true;
  1376. return get_number(input_format_t::msgpack, len) and
  1377. get_number(input_format_t::msgpack, result.subtype) and
  1378. get_binary(input_format_t::msgpack, len, result);
  1379. }
  1380. case 0xC9: // ext 32
  1381. {
  1382. std::uint32_t len;
  1383. result.has_subtype = true;
  1384. return get_number(input_format_t::msgpack, len) and
  1385. get_number(input_format_t::msgpack, result.subtype) and
  1386. get_binary(input_format_t::msgpack, len, result);
  1387. }
  1388. case 0xD4: // fixext 1
  1389. {
  1390. result.has_subtype = true;
  1391. return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 1, result);
  1392. }
  1393. case 0xD5: // fixext 2
  1394. {
  1395. result.has_subtype = true;
  1396. return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 2, result);
  1397. }
  1398. case 0xD6: // fixext 4
  1399. {
  1400. result.has_subtype = true;
  1401. return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 4, result);
  1402. }
  1403. case 0xD7: // fixext 8
  1404. {
  1405. result.has_subtype = true;
  1406. return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 8, result);
  1407. }
  1408. case 0xD8: // fixext 16
  1409. {
  1410. result.has_subtype = true;
  1411. return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 16, result);
  1412. }
  1413. default:
  1414. {
  1415. auto last_token = get_token_string();
  1416. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected binary type specification (0xC4-0xC9, 0xD4-0xD8); last byte: 0x" + last_token, "binary")));
  1417. }
  1418. }
  1419. }
  1420. /*!
  1421. @param[in] len the length of the array
  1422. @return whether array creation completed
  1423. */
  1424. bool get_msgpack_array(const std::size_t len)
  1425. {
  1426. if (JSON_HEDLEY_UNLIKELY(not sax->start_array(len)))
  1427. {
  1428. return false;
  1429. }
  1430. for (std::size_t i = 0; i < len; ++i)
  1431. {
  1432. if (JSON_HEDLEY_UNLIKELY(not parse_msgpack_internal()))
  1433. {
  1434. return false;
  1435. }
  1436. }
  1437. return sax->end_array();
  1438. }
  1439. /*!
  1440. @param[in] len the length of the object
  1441. @return whether object creation completed
  1442. */
  1443. bool get_msgpack_object(const std::size_t len)
  1444. {
  1445. if (JSON_HEDLEY_UNLIKELY(not sax->start_object(len)))
  1446. {
  1447. return false;
  1448. }
  1449. string_t key;
  1450. for (std::size_t i = 0; i < len; ++i)
  1451. {
  1452. get();
  1453. if (JSON_HEDLEY_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
  1454. {
  1455. return false;
  1456. }
  1457. if (JSON_HEDLEY_UNLIKELY(not parse_msgpack_internal()))
  1458. {
  1459. return false;
  1460. }
  1461. key.clear();
  1462. }
  1463. return sax->end_object();
  1464. }
  1465. ////////////
  1466. // UBJSON //
  1467. ////////////
  1468. /*!
  1469. @param[in] get_char whether a new character should be retrieved from the
  1470. input (true, default) or whether the last read
  1471. character should be considered instead
  1472. @return whether a valid UBJSON value was passed to the SAX parser
  1473. */
  1474. bool parse_ubjson_internal(const bool get_char = true)
  1475. {
  1476. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  1477. }
  1478. /*!
  1479. @brief reads a UBJSON string
  1480. This function is either called after reading the 'S' byte explicitly
  1481. indicating a string, or in case of an object key where the 'S' byte can be
  1482. left out.
  1483. @param[out] result created string
  1484. @param[in] get_char whether a new character should be retrieved from the
  1485. input (true, default) or whether the last read
  1486. character should be considered instead
  1487. @return whether string creation completed
  1488. */
  1489. bool get_ubjson_string(string_t& result, const bool get_char = true)
  1490. {
  1491. if (get_char)
  1492. {
  1493. get(); // TODO(niels): may we ignore N here?
  1494. }
  1495. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
  1496. {
  1497. return false;
  1498. }
  1499. switch (current)
  1500. {
  1501. case 'U':
  1502. {
  1503. std::uint8_t len;
  1504. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  1505. }
  1506. case 'i':
  1507. {
  1508. std::int8_t len;
  1509. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  1510. }
  1511. case 'I':
  1512. {
  1513. std::int16_t len;
  1514. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  1515. }
  1516. case 'l':
  1517. {
  1518. std::int32_t len;
  1519. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  1520. }
  1521. case 'L':
  1522. {
  1523. std::int64_t len;
  1524. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  1525. }
  1526. default:
  1527. auto last_token = get_token_string();
  1528. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string")));
  1529. }
  1530. }
  1531. /*!
  1532. @param[out] result determined size
  1533. @return whether size determination completed
  1534. */
  1535. bool get_ubjson_size_value(std::size_t& result)
  1536. {
  1537. switch (get_ignore_noop())
  1538. {
  1539. case 'U':
  1540. {
  1541. std::uint8_t number;
  1542. if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  1543. {
  1544. return false;
  1545. }
  1546. result = static_cast<std::size_t>(number);
  1547. return true;
  1548. }
  1549. case 'i':
  1550. {
  1551. std::int8_t number;
  1552. if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  1553. {
  1554. return false;
  1555. }
  1556. result = static_cast<std::size_t>(number);
  1557. return true;
  1558. }
  1559. case 'I':
  1560. {
  1561. std::int16_t number;
  1562. if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  1563. {
  1564. return false;
  1565. }
  1566. result = static_cast<std::size_t>(number);
  1567. return true;
  1568. }
  1569. case 'l':
  1570. {
  1571. std::int32_t number;
  1572. if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  1573. {
  1574. return false;
  1575. }
  1576. result = static_cast<std::size_t>(number);
  1577. return true;
  1578. }
  1579. case 'L':
  1580. {
  1581. std::int64_t number;
  1582. if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  1583. {
  1584. return false;
  1585. }
  1586. result = static_cast<std::size_t>(number);
  1587. return true;
  1588. }
  1589. default:
  1590. {
  1591. auto last_token = get_token_string();
  1592. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size")));
  1593. }
  1594. }
  1595. }
  1596. /*!
  1597. @brief determine the type and size for a container
  1598. In the optimized UBJSON format, a type and a size can be provided to allow
  1599. for a more compact representation.
  1600. @param[out] result pair of the size and the type
  1601. @return whether pair creation completed
  1602. */
  1603. bool get_ubjson_size_type(std::pair<std::size_t, int>& result)
  1604. {
  1605. result.first = string_t::npos; // size
  1606. result.second = 0; // type
  1607. get_ignore_noop();
  1608. if (current == '$')
  1609. {
  1610. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  1611. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "type")))
  1612. {
  1613. return false;
  1614. }
  1615. get_ignore_noop();
  1616. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  1617. {
  1618. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
  1619. {
  1620. return false;
  1621. }
  1622. auto last_token = get_token_string();
  1623. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size")));
  1624. }
  1625. return get_ubjson_size_value(result.first);
  1626. }
  1627. if (current == '#')
  1628. {
  1629. return get_ubjson_size_value(result.first);
  1630. }
  1631. return true;
  1632. }
  1633. /*!
  1634. @param prefix the previously read or set type prefix
  1635. @return whether value creation completed
  1636. */
  1637. bool get_ubjson_value(const int prefix)
  1638. {
  1639. switch (prefix)
  1640. {
  1641. case std::char_traits<char>::eof(): // EOF
  1642. return unexpect_eof(input_format_t::ubjson, "value");
  1643. case 'T': // true
  1644. return sax->boolean(true);
  1645. case 'F': // false
  1646. return sax->boolean(false);
  1647. case 'Z': // null
  1648. return sax->null();
  1649. case 'U':
  1650. {
  1651. std::uint8_t number;
  1652. return get_number(input_format_t::ubjson, number) and sax->number_unsigned(number);
  1653. }
  1654. case 'i':
  1655. {
  1656. std::int8_t number;
  1657. return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
  1658. }
  1659. case 'I':
  1660. {
  1661. std::int16_t number;
  1662. return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
  1663. }
  1664. case 'l':
  1665. {
  1666. std::int32_t number;
  1667. return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
  1668. }
  1669. case 'L':
  1670. {
  1671. std::int64_t number;
  1672. return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
  1673. }
  1674. case 'd':
  1675. {
  1676. float number;
  1677. return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
  1678. }
  1679. case 'D':
  1680. {
  1681. double number;
  1682. return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
  1683. }
  1684. case 'C': // char
  1685. {
  1686. get();
  1687. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "char")))
  1688. {
  1689. return false;
  1690. }
  1691. if (JSON_HEDLEY_UNLIKELY(current > 127))
  1692. {
  1693. auto last_token = get_token_string();
  1694. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char")));
  1695. }
  1696. string_t s(1, static_cast<char>(current));
  1697. return sax->string(s);
  1698. }
  1699. case 'S': // string
  1700. {
  1701. string_t s;
  1702. return get_ubjson_string(s) and sax->string(s);
  1703. }
  1704. case '[': // array
  1705. return get_ubjson_array();
  1706. case '{': // object
  1707. return get_ubjson_object();
  1708. default: // anything else
  1709. {
  1710. auto last_token = get_token_string();
  1711. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value")));
  1712. }
  1713. }
  1714. }
  1715. /*!
  1716. @return whether array creation completed
  1717. */
  1718. bool get_ubjson_array()
  1719. {
  1720. std::pair<std::size_t, int> size_and_type;
  1721. if (JSON_HEDLEY_UNLIKELY(not get_ubjson_size_type(size_and_type)))
  1722. {
  1723. return false;
  1724. }
  1725. if (size_and_type.first != string_t::npos)
  1726. {
  1727. if (JSON_HEDLEY_UNLIKELY(not sax->start_array(size_and_type.first)))
  1728. {
  1729. return false;
  1730. }
  1731. if (size_and_type.second != 0)
  1732. {
  1733. if (size_and_type.second != 'N')
  1734. {
  1735. for (std::size_t i = 0; i < size_and_type.first; ++i)
  1736. {
  1737. if (JSON_HEDLEY_UNLIKELY(not get_ubjson_value(size_and_type.second)))
  1738. {
  1739. return false;
  1740. }
  1741. }
  1742. }
  1743. }
  1744. else
  1745. {
  1746. for (std::size_t i = 0; i < size_and_type.first; ++i)
  1747. {
  1748. if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal()))
  1749. {
  1750. return false;
  1751. }
  1752. }
  1753. }
  1754. }
  1755. else
  1756. {
  1757. if (JSON_HEDLEY_UNLIKELY(not sax->start_array(std::size_t(-1))))
  1758. {
  1759. return false;
  1760. }
  1761. while (current != ']')
  1762. {
  1763. if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal(false)))
  1764. {
  1765. return false;
  1766. }
  1767. get_ignore_noop();
  1768. }
  1769. }
  1770. return sax->end_array();
  1771. }
  1772. /*!
  1773. @return whether object creation completed
  1774. */
  1775. bool get_ubjson_object()
  1776. {
  1777. std::pair<std::size_t, int> size_and_type;
  1778. if (JSON_HEDLEY_UNLIKELY(not get_ubjson_size_type(size_and_type)))
  1779. {
  1780. return false;
  1781. }
  1782. string_t key;
  1783. if (size_and_type.first != string_t::npos)
  1784. {
  1785. if (JSON_HEDLEY_UNLIKELY(not sax->start_object(size_and_type.first)))
  1786. {
  1787. return false;
  1788. }
  1789. if (size_and_type.second != 0)
  1790. {
  1791. for (std::size_t i = 0; i < size_and_type.first; ++i)
  1792. {
  1793. if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
  1794. {
  1795. return false;
  1796. }
  1797. if (JSON_HEDLEY_UNLIKELY(not get_ubjson_value(size_and_type.second)))
  1798. {
  1799. return false;
  1800. }
  1801. key.clear();
  1802. }
  1803. }
  1804. else
  1805. {
  1806. for (std::size_t i = 0; i < size_and_type.first; ++i)
  1807. {
  1808. if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
  1809. {
  1810. return false;
  1811. }
  1812. if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal()))
  1813. {
  1814. return false;
  1815. }
  1816. key.clear();
  1817. }
  1818. }
  1819. }
  1820. else
  1821. {
  1822. if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1))))
  1823. {
  1824. return false;
  1825. }
  1826. while (current != '}')
  1827. {
  1828. if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
  1829. {
  1830. return false;
  1831. }
  1832. if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal()))
  1833. {
  1834. return false;
  1835. }
  1836. get_ignore_noop();
  1837. key.clear();
  1838. }
  1839. }
  1840. return sax->end_object();
  1841. }
  1842. // Note, no reader for UBJSON binary types is implemented because they do
  1843. // not exist
  1844. ///////////////////////
  1845. // Utility functions //
  1846. ///////////////////////
  1847. /*!
  1848. @brief get next character from the input
  1849. This function provides the interface to the used input adapter. It does
  1850. not throw in case the input reached EOF, but returns a -'ve valued
  1851. `std::char_traits<char>::eof()` in that case.
  1852. @return character read from the input
  1853. */
  1854. int get()
  1855. {
  1856. ++chars_read;
  1857. return current = ia->get_character();
  1858. }
  1859. /*!
  1860. @return character read from the input after ignoring all 'N' entries
  1861. */
  1862. int get_ignore_noop()
  1863. {
  1864. do
  1865. {
  1866. get();
  1867. }
  1868. while (current == 'N');
  1869. return current;
  1870. }
  1871. /*
  1872. @brief read a number from the input
  1873. @tparam NumberType the type of the number
  1874. @param[in] format the current format (for diagnostics)
  1875. @param[out] result number of type @a NumberType
  1876. @return whether conversion completed
  1877. @note This function needs to respect the system's endianess, because
  1878. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  1879. (big endian) and therefore need reordering on little endian systems.
  1880. */
  1881. template<typename NumberType, bool InputIsLittleEndian = false>
  1882. bool get_number(const input_format_t format, NumberType& result)
  1883. {
  1884. // step 1: read input into array with system's byte order
  1885. std::array<std::uint8_t, sizeof(NumberType)> vec;
  1886. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  1887. {
  1888. get();
  1889. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(format, "number")))
  1890. {
  1891. return false;
  1892. }
  1893. // reverse byte order prior to conversion if necessary
  1894. if (is_little_endian != InputIsLittleEndian)
  1895. {
  1896. vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
  1897. }
  1898. else
  1899. {
  1900. vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
  1901. }
  1902. }
  1903. // step 2: convert array into number of type T and return
  1904. std::memcpy(&result, vec.data(), sizeof(NumberType));
  1905. return true;
  1906. }
  1907. /*!
  1908. @brief create a string by reading characters from the input
  1909. @tparam NumberType the type of the number
  1910. @param[in] format the current format (for diagnostics)
  1911. @param[in] len number of characters to read
  1912. @param[out] result string created by reading @a len bytes
  1913. @return whether string creation completed
  1914. @note We can not reserve @a len bytes for the result, because @a len
  1915. may be too large. Usually, @ref unexpect_eof() detects the end of
  1916. the input before we run out of string memory.
  1917. */
  1918. template<typename NumberType>
  1919. bool get_string(const input_format_t format,
  1920. const NumberType len,
  1921. string_t& result)
  1922. {
  1923. bool success = true;
  1924. std::generate_n(std::back_inserter(result), len, [this, &success, &format]()
  1925. {
  1926. get();
  1927. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(format, "string")))
  1928. {
  1929. success = false;
  1930. }
  1931. return static_cast<char>(current);
  1932. });
  1933. return success;
  1934. }
  1935. /*!
  1936. @brief create a byte array by reading bytes from the input
  1937. @tparam NumberType the type of the number
  1938. @param[in] format the current format (for diagnostics)
  1939. @param[in] len number of bytes to read
  1940. @param[out] result byte array created by reading @a len bytes
  1941. @return whether byte array creation completed
  1942. @note We can not reserve @a len bytes for the result, because @a len
  1943. may be too large. Usually, @ref unexpect_eof() detects the end of
  1944. the input before we run out of memory.
  1945. */
  1946. template<typename NumberType>
  1947. bool get_binary(const input_format_t format,
  1948. const NumberType len,
  1949. internal_binary_t& result)
  1950. {
  1951. bool success = true;
  1952. std::generate_n(std::back_inserter(result), len, [this, &success, &format]()
  1953. {
  1954. get();
  1955. if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(format, "binary")))
  1956. {
  1957. success = false;
  1958. }
  1959. return static_cast<uint8_t>(current);
  1960. });
  1961. return success;
  1962. }
  1963. /*!
  1964. @param[in] format the current format (for diagnostics)
  1965. @param[in] context further context information (for diagnostics)
  1966. @return whether the last read character is not EOF
  1967. */
  1968. JSON_HEDLEY_NON_NULL(3)
  1969. bool unexpect_eof(const input_format_t format, const char* context) const
  1970. {
  1971. if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char>::eof()))
  1972. {
  1973. return sax->parse_error(chars_read, "<end of file>",
  1974. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context)));
  1975. }
  1976. return true;
  1977. }
  1978. /*!
  1979. @return a string representation of the last read byte
  1980. */
  1981. std::string get_token_string() const
  1982. {
  1983. std::array<char, 3> cr{{}};
  1984. (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current));
  1985. return std::string{cr.data()};
  1986. }
  1987. /*!
  1988. @param[in] format the current format
  1989. @param[in] detail a detailed error message
  1990. @param[in] context further context information
  1991. @return a message string to use in the parse_error exceptions
  1992. */
  1993. std::string exception_message(const input_format_t format,
  1994. const std::string& detail,
  1995. const std::string& context) const
  1996. {
  1997. std::string error_msg = "syntax error while parsing ";
  1998. switch (format)
  1999. {
  2000. case input_format_t::cbor:
  2001. error_msg += "CBOR";
  2002. break;
  2003. case input_format_t::msgpack:
  2004. error_msg += "MessagePack";
  2005. break;
  2006. case input_format_t::ubjson:
  2007. error_msg += "UBJSON";
  2008. break;
  2009. case input_format_t::bson:
  2010. error_msg += "BSON";
  2011. break;
  2012. default: // LCOV_EXCL_LINE
  2013. assert(false); // LCOV_EXCL_LINE
  2014. }
  2015. return error_msg + " " + context + ": " + detail;
  2016. }
  2017. private:
  2018. /// input adapter
  2019. input_adapter_t ia = nullptr;
  2020. /// the current character
  2021. int current = std::char_traits<char>::eof();
  2022. /// the number of characters read
  2023. std::size_t chars_read = 0;
  2024. /// whether we can assume little endianess
  2025. const bool is_little_endian = little_endianess();
  2026. /// the SAX parser
  2027. json_sax_t* sax = nullptr;
  2028. };
  2029. } // namespace detail
  2030. } // namespace nlohmann