| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693 |
- #pragma once
- #include <algorithm> // generate_n
- #include <array> // array
- #include <cassert> // assert
- #include <cmath> // ldexp
- #include <cstddef> // size_t
- #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
- #include <cstdio> // snprintf
- #include <cstring> // memcpy
- #include <iterator> // back_inserter
- #include <limits> // numeric_limits
- #include <string> // char_traits, string
- #include <utility> // make_pair, move
- #include <nlohmann/detail/input/input_adapters.hpp>
- #include <nlohmann/detail/input/json_sax.hpp>
- #include <nlohmann/detail/exceptions.hpp>
- #include <nlohmann/detail/macro_scope.hpp>
- #include <nlohmann/detail/value_t.hpp>
- namespace nlohmann
- {
- namespace detail
- {
- ///////////////////
- // binary reader //
- ///////////////////
- /*!
- @brief deserialization of CBOR, MessagePack, and UBJSON values
- */
- template<typename BasicJsonType>
- class binary_reader
- {
- using number_integer_t = typename BasicJsonType::number_integer_t;
- using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
- using number_float_t = typename BasicJsonType::number_float_t;
- using string_t = typename BasicJsonType::string_t;
- using json_sax_t = json_sax<BasicJsonType>;
- public:
- /*!
- @brief create a binary reader
- @param[in] adapter input adapter to read from
- */
- explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
- {
- assert(ia);
- }
- /*!
- @param[in] format the binary format to parse
- @param[in] sax_ a SAX event processor
- @param[in] strict whether to expect the input to be consumed completed
- @return
- */
- bool sax_parse(const input_format_t format,
- json_sax_t* sax_,
- const bool strict = true)
- {
- sax = sax_;
- bool result;
- switch (format)
- {
- case input_format_t::cbor:
- result = parse_cbor_internal();
- break;
- case input_format_t::msgpack:
- result = parse_msgpack_internal();
- break;
- case input_format_t::ubjson:
- result = parse_ubjson_internal();
- break;
- default:
- assert(false); // LCOV_EXCL_LINE
- }
- // strict mode: next byte must be EOF
- if (result and strict)
- {
- if (format == input_format_t::ubjson)
- {
- get_ignore_noop();
- }
- else
- {
- get();
- }
- if (JSON_UNLIKELY(current != std::char_traits<char>::eof()))
- {
- return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read, "expected end of input"));
- }
- }
- return result;
- }
- /*!
- @brief determine system byte order
- @return true if and only if system's byte order is little endian
- @note from http://stackoverflow.com/a/1001328/266378
- */
- static constexpr bool little_endianess(int num = 1) noexcept
- {
- return (*reinterpret_cast<char*>(&num) == 1);
- }
- private:
- /*!
- @param[in] get_char whether a new character should be retrieved from the
- input (true, default) or whether the last read
- character should be considered instead
- @return whether a valid CBOR value was passed to the SAX parser
- */
- bool parse_cbor_internal(const bool get_char = true)
- {
- switch (get_char ? get() : current)
- {
- // EOF
- case std::char_traits<char>::eof():
- return unexpect_eof();
- // Integer 0x00..0x17 (0..23)
- case 0x00:
- case 0x01:
- case 0x02:
- case 0x03:
- case 0x04:
- case 0x05:
- case 0x06:
- case 0x07:
- case 0x08:
- case 0x09:
- case 0x0A:
- case 0x0B:
- case 0x0C:
- case 0x0D:
- case 0x0E:
- case 0x0F:
- case 0x10:
- case 0x11:
- case 0x12:
- case 0x13:
- case 0x14:
- case 0x15:
- case 0x16:
- case 0x17:
- return sax->number_unsigned(static_cast<number_unsigned_t>(current));
- case 0x18: // Unsigned integer (one-byte uint8_t follows)
- {
- uint8_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- case 0x19: // Unsigned integer (two-byte uint16_t follows)
- {
- uint16_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- case 0x1A: // Unsigned integer (four-byte uint32_t follows)
- {
- uint32_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
- {
- uint64_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- // Negative integer -1-0x00..-1-0x17 (-1..-24)
- case 0x20:
- case 0x21:
- case 0x22:
- case 0x23:
- case 0x24:
- case 0x25:
- case 0x26:
- case 0x27:
- case 0x28:
- case 0x29:
- case 0x2A:
- case 0x2B:
- case 0x2C:
- case 0x2D:
- case 0x2E:
- case 0x2F:
- case 0x30:
- case 0x31:
- case 0x32:
- case 0x33:
- case 0x34:
- case 0x35:
- case 0x36:
- case 0x37:
- return sax->number_integer(static_cast<int8_t>(0x20 - 1 - current));
- case 0x38: // Negative integer (one-byte uint8_t follows)
- {
- uint8_t number;
- return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
- }
- case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
- {
- uint16_t number;
- return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
- }
- case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
- {
- uint32_t number;
- return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
- }
- case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
- {
- uint64_t number;
- return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1)
- - static_cast<number_integer_t>(number));
- }
- // UTF-8 string (0x00..0x17 bytes follow)
- case 0x60:
- case 0x61:
- case 0x62:
- case 0x63:
- case 0x64:
- case 0x65:
- case 0x66:
- case 0x67:
- case 0x68:
- case 0x69:
- case 0x6A:
- case 0x6B:
- case 0x6C:
- case 0x6D:
- case 0x6E:
- case 0x6F:
- case 0x70:
- case 0x71:
- case 0x72:
- case 0x73:
- case 0x74:
- case 0x75:
- case 0x76:
- case 0x77:
- case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
- case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
- case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
- case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
- case 0x7F: // UTF-8 string (indefinite length)
- {
- string_t s;
- return get_cbor_string(s) and sax->string(s);
- }
- // array (0x00..0x17 data items follow)
- case 0x80:
- case 0x81:
- case 0x82:
- case 0x83:
- case 0x84:
- case 0x85:
- case 0x86:
- case 0x87:
- case 0x88:
- case 0x89:
- case 0x8A:
- case 0x8B:
- case 0x8C:
- case 0x8D:
- case 0x8E:
- case 0x8F:
- case 0x90:
- case 0x91:
- case 0x92:
- case 0x93:
- case 0x94:
- case 0x95:
- case 0x96:
- case 0x97:
- return get_cbor_array(static_cast<std::size_t>(current & 0x1F));
- case 0x98: // array (one-byte uint8_t for n follows)
- {
- uint8_t len;
- return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
- }
- case 0x99: // array (two-byte uint16_t for n follow)
- {
- uint16_t len;
- return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
- }
- case 0x9A: // array (four-byte uint32_t for n follow)
- {
- uint32_t len;
- return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
- }
- case 0x9B: // array (eight-byte uint64_t for n follow)
- {
- uint64_t len;
- return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
- }
- case 0x9F: // array (indefinite length)
- return get_cbor_array(json_sax_t::no_limit);
- // map (0x00..0x17 pairs of data items follow)
- case 0xA0:
- case 0xA1:
- case 0xA2:
- case 0xA3:
- case 0xA4:
- case 0xA5:
- case 0xA6:
- case 0xA7:
- case 0xA8:
- case 0xA9:
- case 0xAA:
- case 0xAB:
- case 0xAC:
- case 0xAD:
- case 0xAE:
- case 0xAF:
- case 0xB0:
- case 0xB1:
- case 0xB2:
- case 0xB3:
- case 0xB4:
- case 0xB5:
- case 0xB6:
- case 0xB7:
- return get_cbor_object(static_cast<std::size_t>(current & 0x1F));
- case 0xB8: // map (one-byte uint8_t for n follows)
- {
- uint8_t len;
- return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
- }
- case 0xB9: // map (two-byte uint16_t for n follow)
- {
- uint16_t len;
- return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
- }
- case 0xBA: // map (four-byte uint32_t for n follow)
- {
- uint32_t len;
- return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
- }
- case 0xBB: // map (eight-byte uint64_t for n follow)
- {
- uint64_t len;
- return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
- }
- case 0xBF: // map (indefinite length)
- return get_cbor_object(json_sax_t::no_limit);
- case 0xF4: // false
- return sax->boolean(false);
- case 0xF5: // true
- return sax->boolean(true);
- case 0xF6: // null
- return sax->null();
- case 0xF9: // Half-Precision Float (two-byte IEEE 754)
- {
- const int byte1 = get();
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- const int byte2 = get();
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- // code from RFC 7049, Appendix D, Figure 3:
- // As half-precision floating-point numbers were only added
- // to IEEE 754 in 2008, today's programming platforms often
- // still only have limited support for them. It is very
- // easy to include at least decoding support for them even
- // without such support. An example of a small decoder for
- // half-precision floating-point numbers in the C language
- // is shown in Fig. 3.
- const int half = (byte1 << 8) + byte2;
- const double val = [&half]
- {
- const int exp = (half >> 10) & 0x1F;
- const int mant = half & 0x3FF;
- assert(0 <= exp and exp <= 32);
- assert(0 <= mant and mant <= 1024);
- switch (exp)
- {
- case 0:
- return std::ldexp(mant, -24);
- case 31:
- return (mant == 0)
- ? std::numeric_limits<double>::infinity()
- : std::numeric_limits<double>::quiet_NaN();
- default:
- return std::ldexp(mant + 1024, exp - 25);
- }
- }();
- return sax->number_float((half & 0x8000) != 0
- ? static_cast<number_float_t>(-val)
- : static_cast<number_float_t>(val), "");
- }
- case 0xFA: // Single-Precision Float (four-byte IEEE 754)
- {
- float number;
- return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
- }
- case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
- {
- double number;
- return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
- }
- default: // anything else (0xFF is handled inside the other types)
- {
- auto last_token = get_token_string();
- return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading CBOR; last byte: 0x" + last_token));
- }
- }
- }
- /*!
- @return whether a valid MessagePack value was passed to the SAX parser
- */
- bool parse_msgpack_internal()
- {
- switch (get())
- {
- // EOF
- case std::char_traits<char>::eof():
- return unexpect_eof();
- // positive fixint
- case 0x00:
- case 0x01:
- case 0x02:
- case 0x03:
- case 0x04:
- case 0x05:
- case 0x06:
- case 0x07:
- case 0x08:
- case 0x09:
- case 0x0A:
- case 0x0B:
- case 0x0C:
- case 0x0D:
- case 0x0E:
- case 0x0F:
- case 0x10:
- case 0x11:
- case 0x12:
- case 0x13:
- case 0x14:
- case 0x15:
- case 0x16:
- case 0x17:
- case 0x18:
- case 0x19:
- case 0x1A:
- case 0x1B:
- case 0x1C:
- case 0x1D:
- case 0x1E:
- case 0x1F:
- case 0x20:
- case 0x21:
- case 0x22:
- case 0x23:
- case 0x24:
- case 0x25:
- case 0x26:
- case 0x27:
- case 0x28:
- case 0x29:
- case 0x2A:
- case 0x2B:
- case 0x2C:
- case 0x2D:
- case 0x2E:
- case 0x2F:
- case 0x30:
- case 0x31:
- case 0x32:
- case 0x33:
- case 0x34:
- case 0x35:
- case 0x36:
- case 0x37:
- case 0x38:
- case 0x39:
- case 0x3A:
- case 0x3B:
- case 0x3C:
- case 0x3D:
- case 0x3E:
- case 0x3F:
- case 0x40:
- case 0x41:
- case 0x42:
- case 0x43:
- case 0x44:
- case 0x45:
- case 0x46:
- case 0x47:
- case 0x48:
- case 0x49:
- case 0x4A:
- case 0x4B:
- case 0x4C:
- case 0x4D:
- case 0x4E:
- case 0x4F:
- case 0x50:
- case 0x51:
- case 0x52:
- case 0x53:
- case 0x54:
- case 0x55:
- case 0x56:
- case 0x57:
- case 0x58:
- case 0x59:
- case 0x5A:
- case 0x5B:
- case 0x5C:
- case 0x5D:
- case 0x5E:
- case 0x5F:
- case 0x60:
- case 0x61:
- case 0x62:
- case 0x63:
- case 0x64:
- case 0x65:
- case 0x66:
- case 0x67:
- case 0x68:
- case 0x69:
- case 0x6A:
- case 0x6B:
- case 0x6C:
- case 0x6D:
- case 0x6E:
- case 0x6F:
- case 0x70:
- case 0x71:
- case 0x72:
- case 0x73:
- case 0x74:
- case 0x75:
- case 0x76:
- case 0x77:
- case 0x78:
- case 0x79:
- case 0x7A:
- case 0x7B:
- case 0x7C:
- case 0x7D:
- case 0x7E:
- case 0x7F:
- return sax->number_unsigned(static_cast<number_unsigned_t>(current));
- // fixmap
- case 0x80:
- case 0x81:
- case 0x82:
- case 0x83:
- case 0x84:
- case 0x85:
- case 0x86:
- case 0x87:
- case 0x88:
- case 0x89:
- case 0x8A:
- case 0x8B:
- case 0x8C:
- case 0x8D:
- case 0x8E:
- case 0x8F:
- return get_msgpack_object(static_cast<std::size_t>(current & 0x0F));
- // fixarray
- case 0x90:
- case 0x91:
- case 0x92:
- case 0x93:
- case 0x94:
- case 0x95:
- case 0x96:
- case 0x97:
- case 0x98:
- case 0x99:
- case 0x9A:
- case 0x9B:
- case 0x9C:
- case 0x9D:
- case 0x9E:
- case 0x9F:
- return get_msgpack_array(static_cast<std::size_t>(current & 0x0F));
- // fixstr
- case 0xA0:
- case 0xA1:
- case 0xA2:
- case 0xA3:
- case 0xA4:
- case 0xA5:
- case 0xA6:
- case 0xA7:
- case 0xA8:
- case 0xA9:
- case 0xAA:
- case 0xAB:
- case 0xAC:
- case 0xAD:
- case 0xAE:
- case 0xAF:
- case 0xB0:
- case 0xB1:
- case 0xB2:
- case 0xB3:
- case 0xB4:
- case 0xB5:
- case 0xB6:
- case 0xB7:
- case 0xB8:
- case 0xB9:
- case 0xBA:
- case 0xBB:
- case 0xBC:
- case 0xBD:
- case 0xBE:
- case 0xBF:
- {
- string_t s;
- return get_msgpack_string(s) and sax->string(s);
- }
- case 0xC0: // nil
- return sax->null();
- case 0xC2: // false
- return sax->boolean(false);
- case 0xC3: // true
- return sax->boolean(true);
- case 0xCA: // float 32
- {
- float number;
- return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
- }
- case 0xCB: // float 64
- {
- double number;
- return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
- }
- case 0xCC: // uint 8
- {
- uint8_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- case 0xCD: // uint 16
- {
- uint16_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- case 0xCE: // uint 32
- {
- uint32_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- case 0xCF: // uint 64
- {
- uint64_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- case 0xD0: // int 8
- {
- int8_t number;
- return get_number(number) and sax->number_integer(number);
- }
- case 0xD1: // int 16
- {
- int16_t number;
- return get_number(number) and sax->number_integer(number);
- }
- case 0xD2: // int 32
- {
- int32_t number;
- return get_number(number) and sax->number_integer(number);
- }
- case 0xD3: // int 64
- {
- int64_t number;
- return get_number(number) and sax->number_integer(number);
- }
- case 0xD9: // str 8
- case 0xDA: // str 16
- case 0xDB: // str 32
- {
- string_t s;
- return get_msgpack_string(s) and sax->string(s);
- }
- case 0xDC: // array 16
- {
- uint16_t len;
- return get_number(len) and get_msgpack_array(static_cast<std::size_t>(len));
- }
- case 0xDD: // array 32
- {
- uint32_t len;
- return get_number(len) and get_msgpack_array(static_cast<std::size_t>(len));
- }
- case 0xDE: // map 16
- {
- uint16_t len;
- return get_number(len) and get_msgpack_object(static_cast<std::size_t>(len));
- }
- case 0xDF: // map 32
- {
- uint32_t len;
- return get_number(len) and get_msgpack_object(static_cast<std::size_t>(len));
- }
- // negative fixint
- case 0xE0:
- case 0xE1:
- case 0xE2:
- case 0xE3:
- case 0xE4:
- case 0xE5:
- case 0xE6:
- case 0xE7:
- case 0xE8:
- case 0xE9:
- case 0xEA:
- case 0xEB:
- case 0xEC:
- case 0xED:
- case 0xEE:
- case 0xEF:
- case 0xF0:
- case 0xF1:
- case 0xF2:
- case 0xF3:
- case 0xF4:
- case 0xF5:
- case 0xF6:
- case 0xF7:
- case 0xF8:
- case 0xF9:
- case 0xFA:
- case 0xFB:
- case 0xFC:
- case 0xFD:
- case 0xFE:
- case 0xFF:
- return sax->number_integer(static_cast<int8_t>(current));
- default: // anything else
- {
- auto last_token = get_token_string();
- return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading MessagePack; last byte: 0x" + last_token));
- }
- }
- }
- /*!
- @param[in] get_char whether a new character should be retrieved from the
- input (true, default) or whether the last read
- character should be considered instead
- @return whether a valid UBJSON value was passed to the SAX parser
- */
- bool parse_ubjson_internal(const bool get_char = true)
- {
- return get_ubjson_value(get_char ? get_ignore_noop() : current);
- }
- /*!
- @brief get next character from the input
- This function provides the interface to the used input adapter. It does
- not throw in case the input reached EOF, but returns a -'ve valued
- `std::char_traits<char>::eof()` in that case.
- @return character read from the input
- */
- int get()
- {
- ++chars_read;
- return (current = ia->get_character());
- }
- /*!
- @return character read from the input after ignoring all 'N' entries
- */
- int get_ignore_noop()
- {
- do
- {
- get();
- }
- while (current == 'N');
- return current;
- }
- /*
- @brief read a number from the input
- @tparam NumberType the type of the number
- @param[out] result number of type @a NumberType
- @return whether conversion completed
- @note This function needs to respect the system's endianess, because
- bytes in CBOR, MessagePack, and UBJSON are stored in network order
- (big endian) and therefore need reordering on little endian systems.
- */
- template<typename NumberType>
- bool get_number(NumberType& result)
- {
- // step 1: read input into array with system's byte order
- std::array<uint8_t, sizeof(NumberType)> vec;
- for (std::size_t i = 0; i < sizeof(NumberType); ++i)
- {
- get();
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- // reverse byte order prior to conversion if necessary
- if (is_little_endian)
- {
- vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current);
- }
- else
- {
- vec[i] = static_cast<uint8_t>(current); // LCOV_EXCL_LINE
- }
- }
- // step 2: convert array into number of type T and return
- std::memcpy(&result, vec.data(), sizeof(NumberType));
- return true;
- }
- /*!
- @brief create a string by reading characters from the input
- @tparam NumberType the type of the number
- @param[in] len number of characters to read
- @param[out] string created by reading @a len bytes
- @return whether string creation completed
- @note We can not reserve @a len bytes for the result, because @a len
- may be too large. Usually, @ref unexpect_eof() detects the end of
- the input before we run out of string memory.
- */
- template<typename NumberType>
- bool get_string(const NumberType len, string_t& result)
- {
- bool success = true;
- std::generate_n(std::back_inserter(result), len, [this, &success]()
- {
- get();
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- success = false;
- }
- return static_cast<char>(current);
- });
- return success;
- }
- /*!
- @brief reads a CBOR string
- This function first reads starting bytes to determine the expected
- string length and then copies this number of bytes into a string.
- Additionally, CBOR's strings with indefinite lengths are supported.
- @param[out] result created string
- @return whether string creation completed
- */
- bool get_cbor_string(string_t& result)
- {
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- switch (current)
- {
- // UTF-8 string (0x00..0x17 bytes follow)
- case 0x60:
- case 0x61:
- case 0x62:
- case 0x63:
- case 0x64:
- case 0x65:
- case 0x66:
- case 0x67:
- case 0x68:
- case 0x69:
- case 0x6A:
- case 0x6B:
- case 0x6C:
- case 0x6D:
- case 0x6E:
- case 0x6F:
- case 0x70:
- case 0x71:
- case 0x72:
- case 0x73:
- case 0x74:
- case 0x75:
- case 0x76:
- case 0x77:
- {
- return get_string(current & 0x1F, result);
- }
- case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
- {
- uint8_t len;
- return get_number(len) and get_string(len, result);
- }
- case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
- {
- uint16_t len;
- return get_number(len) and get_string(len, result);
- }
- case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
- {
- uint32_t len;
- return get_number(len) and get_string(len, result);
- }
- case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
- {
- uint64_t len;
- return get_number(len) and get_string(len, result);
- }
- case 0x7F: // UTF-8 string (indefinite length)
- {
- while (get() != 0xFF)
- {
- string_t chunk;
- if (not get_cbor_string(chunk))
- {
- return false;
- }
- result.append(chunk);
- }
- return true;
- }
- default:
- {
- auto last_token = get_token_string();
- return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a CBOR string; last byte: 0x" + last_token));
- }
- }
- }
- /*!
- @param[in] len the length of the array or json_sax_t::no_limit for an
- array of indefinite size
- @return whether array creation completed
- */
- bool get_cbor_array(const std::size_t len)
- {
- if (JSON_UNLIKELY(not sax->start_array(len)))
- {
- return false;
- }
- if (len != json_sax_t::no_limit)
- for (std::size_t i = 0; i < len; ++i)
- {
- if (JSON_UNLIKELY(not parse_cbor_internal()))
- {
- return false;
- }
- }
- else
- {
- while (get() != 0xFF)
- {
- if (JSON_UNLIKELY(not parse_cbor_internal(false)))
- {
- return false;
- }
- }
- }
- return sax->end_array();
- }
- /*!
- @param[in] len the length of the object or json_sax_t::no_limit for an
- object of indefinite size
- @return whether object creation completed
- */
- bool get_cbor_object(const std::size_t len)
- {
- if (not JSON_UNLIKELY(sax->start_object(len)))
- {
- return false;
- }
- string_t key;
- if (len != json_sax_t::no_limit)
- {
- for (std::size_t i = 0; i < len; ++i)
- {
- get();
- if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
- {
- return false;
- }
- if (JSON_UNLIKELY(not parse_cbor_internal()))
- {
- return false;
- }
- key.clear();
- }
- }
- else
- {
- while (get() != 0xFF)
- {
- if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
- {
- return false;
- }
- if (JSON_UNLIKELY(not parse_cbor_internal()))
- {
- return false;
- }
- key.clear();
- }
- }
- return sax->end_object();
- }
- /*!
- @brief reads a MessagePack string
- This function first reads starting bytes to determine the expected
- string length and then copies this number of bytes into a string.
- @param[out] result created string
- @return whether string creation completed
- */
- bool get_msgpack_string(string_t& result)
- {
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- switch (current)
- {
- // fixstr
- case 0xA0:
- case 0xA1:
- case 0xA2:
- case 0xA3:
- case 0xA4:
- case 0xA5:
- case 0xA6:
- case 0xA7:
- case 0xA8:
- case 0xA9:
- case 0xAA:
- case 0xAB:
- case 0xAC:
- case 0xAD:
- case 0xAE:
- case 0xAF:
- case 0xB0:
- case 0xB1:
- case 0xB2:
- case 0xB3:
- case 0xB4:
- case 0xB5:
- case 0xB6:
- case 0xB7:
- case 0xB8:
- case 0xB9:
- case 0xBA:
- case 0xBB:
- case 0xBC:
- case 0xBD:
- case 0xBE:
- case 0xBF:
- {
- return get_string(current & 0x1F, result);
- }
- case 0xD9: // str 8
- {
- uint8_t len;
- return get_number(len) and get_string(len, result);
- }
- case 0xDA: // str 16
- {
- uint16_t len;
- return get_number(len) and get_string(len, result);
- }
- case 0xDB: // str 32
- {
- uint32_t len;
- return get_number(len) and get_string(len, result);
- }
- default:
- {
- auto last_token = get_token_string();
- return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a MessagePack string; last byte: 0x" + last_token));
- }
- }
- }
- /*!
- @param[in] len the length of the array
- @return whether array creation completed
- */
- bool get_msgpack_array(const std::size_t len)
- {
- if (JSON_UNLIKELY(not sax->start_array(len)))
- {
- return false;
- }
- for (std::size_t i = 0; i < len; ++i)
- {
- if (JSON_UNLIKELY(not parse_msgpack_internal()))
- {
- return false;
- }
- }
- return sax->end_array();
- }
- /*!
- @param[in] len the length of the object
- @return whether object creation completed
- */
- bool get_msgpack_object(const std::size_t len)
- {
- if (JSON_UNLIKELY(not sax->start_object(len)))
- {
- return false;
- }
- string_t key;
- for (std::size_t i = 0; i < len; ++i)
- {
- get();
- if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
- {
- return false;
- }
- if (JSON_UNLIKELY(not parse_msgpack_internal()))
- {
- return false;
- }
- key.clear();
- }
- return sax->end_object();
- }
- /*!
- @brief reads a UBJSON string
- This function is either called after reading the 'S' byte explicitly
- indicating a string, or in case of an object key where the 'S' byte can be
- left out.
- @param[out] result created string
- @param[in] get_char whether a new character should be retrieved from the
- input (true, default) or whether the last read
- character should be considered instead
- @return whether string creation completed
- */
- bool get_ubjson_string(string_t& result, const bool get_char = true)
- {
- if (get_char)
- {
- get(); // TODO: may we ignore N here?
- }
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- switch (current)
- {
- case 'U':
- {
- uint8_t len;
- return get_number(len) and get_string(len, result);
- }
- case 'i':
- {
- int8_t len;
- return get_number(len) and get_string(len, result);
- }
- case 'I':
- {
- int16_t len;
- return get_number(len) and get_string(len, result);
- }
- case 'l':
- {
- int32_t len;
- return get_number(len) and get_string(len, result);
- }
- case 'L':
- {
- int64_t len;
- return get_number(len) and get_string(len, result);
- }
- default:
- auto last_token = get_token_string();
- return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a UBJSON string; last byte: 0x" + last_token));
- }
- }
- /*!
- @param[out] result determined size
- @return whether size determination completed
- */
- bool get_ubjson_size_value(std::size_t& result)
- {
- switch (get_ignore_noop())
- {
- case 'U':
- {
- uint8_t number;
- if (JSON_UNLIKELY(not get_number(number)))
- {
- return false;
- }
- result = static_cast<std::size_t>(number);
- return true;
- }
- case 'i':
- {
- int8_t number;
- if (JSON_UNLIKELY(not get_number(number)))
- {
- return false;
- }
- result = static_cast<std::size_t>(number);
- return true;
- }
- case 'I':
- {
- int16_t number;
- if (JSON_UNLIKELY(not get_number(number)))
- {
- return false;
- }
- result = static_cast<std::size_t>(number);
- return true;
- }
- case 'l':
- {
- int32_t number;
- if (JSON_UNLIKELY(not get_number(number)))
- {
- return false;
- }
- result = static_cast<std::size_t>(number);
- return true;
- }
- case 'L':
- {
- int64_t number;
- if (JSON_UNLIKELY(not get_number(number)))
- {
- return false;
- }
- result = static_cast<std::size_t>(number);
- return true;
- }
- default:
- {
- auto last_token = get_token_string();
- 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));
- }
- }
- }
- /*!
- @brief determine the type and size for a container
- In the optimized UBJSON format, a type and a size can be provided to allow
- for a more compact representation.
- @param[out] result pair of the size and the type
- @return whether pair creation completed
- */
- bool get_ubjson_size_type(std::pair<std::size_t, int>& result)
- {
- result.first = string_t::npos; // size
- result.second = 0; // type
- get_ignore_noop();
- if (current == '$')
- {
- result.second = get(); // must not ignore 'N', because 'N' maybe the type
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- get_ignore_noop();
- if (JSON_UNLIKELY(current != '#'))
- {
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- auto last_token = get_token_string();
- return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "expected '#' after UBJSON type information; last byte: 0x" + last_token));
- }
- return get_ubjson_size_value(result.first);
- }
- else if (current == '#')
- {
- return get_ubjson_size_value(result.first);
- }
- return true;
- }
- /*!
- @param prefix the previously read or set type prefix
- @return whether value creation completed
- */
- bool get_ubjson_value(const int prefix)
- {
- switch (prefix)
- {
- case std::char_traits<char>::eof(): // EOF
- return unexpect_eof();
- case 'T': // true
- return sax->boolean(true);
- case 'F': // false
- return sax->boolean(false);
- case 'Z': // null
- return sax->null();
- case 'U':
- {
- uint8_t number;
- return get_number(number) and sax->number_unsigned(number);
- }
- case 'i':
- {
- int8_t number;
- return get_number(number) and sax->number_integer(number);
- }
- case 'I':
- {
- int16_t number;
- return get_number(number) and sax->number_integer(number);
- }
- case 'l':
- {
- int32_t number;
- return get_number(number) and sax->number_integer(number);
- }
- case 'L':
- {
- int64_t number;
- return get_number(number) and sax->number_integer(number);
- }
- case 'd':
- {
- float number;
- return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
- }
- case 'D':
- {
- double number;
- return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
- }
- case 'C': // char
- {
- get();
- if (JSON_UNLIKELY(not unexpect_eof()))
- {
- return false;
- }
- if (JSON_UNLIKELY(current > 127))
- {
- auto last_token = get_token_string();
- 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));
- }
- string_t s(1, static_cast<char>(current));
- return sax->string(s);
- }
- case 'S': // string
- {
- string_t s;
- return get_ubjson_string(s) and sax->string(s);
- }
- case '[': // array
- return get_ubjson_array();
- case '{': // object
- return get_ubjson_object();
- default: // anything else
- {
- auto last_token = get_token_string();
- return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading UBJSON; last byte: 0x" + last_token));
- }
- }
- }
- /*!
- @return whether array creation completed
- */
- bool get_ubjson_array()
- {
- std::pair<std::size_t, int> size_and_type;
- if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
- {
- return false;
- }
- if (size_and_type.first != string_t::npos)
- {
- if (JSON_UNLIKELY(not sax->start_array(size_and_type.first)))
- {
- return false;
- }
- if (size_and_type.second != 0)
- {
- if (size_and_type.second != 'N')
- {
- for (std::size_t i = 0; i < size_and_type.first; ++i)
- {
- if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
- {
- return false;
- }
- }
- }
- }
- else
- {
- for (std::size_t i = 0; i < size_and_type.first; ++i)
- {
- if (JSON_UNLIKELY(not parse_ubjson_internal()))
- {
- return false;
- }
- }
- }
- }
- else
- {
- if (JSON_UNLIKELY(not sax->start_array()))
- {
- return false;
- }
- while (current != ']')
- {
- if (JSON_UNLIKELY(not parse_ubjson_internal(false)))
- {
- return false;
- }
- get_ignore_noop();
- }
- }
- return sax->end_array();
- }
- /*!
- @return whether object creation completed
- */
- bool get_ubjson_object()
- {
- std::pair<std::size_t, int> size_and_type;
- if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
- {
- return false;
- }
- string_t key;
- if (size_and_type.first != string_t::npos)
- {
- if (JSON_UNLIKELY(not sax->start_object(size_and_type.first)))
- {
- return false;
- }
- if (size_and_type.second != 0)
- {
- for (std::size_t i = 0; i < size_and_type.first; ++i)
- {
- if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
- {
- return false;
- }
- if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
- {
- return false;
- }
- key.clear();
- }
- }
- else
- {
- for (std::size_t i = 0; i < size_and_type.first; ++i)
- {
- if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
- {
- return false;
- }
- if (JSON_UNLIKELY(not parse_ubjson_internal()))
- {
- return false;
- }
- key.clear();
- }
- }
- }
- else
- {
- if (JSON_UNLIKELY(not sax->start_object()))
- {
- return false;
- }
- while (current != '}')
- {
- if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
- {
- return false;
- }
- if (JSON_UNLIKELY(not parse_ubjson_internal()))
- {
- return false;
- }
- get_ignore_noop();
- key.clear();
- }
- }
- return sax->end_object();
- }
- /*!
- @return whether the last read character is not EOF
- */
- bool unexpect_eof() const
- {
- if (JSON_UNLIKELY(current == std::char_traits<char>::eof()))
- {
- return sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, "unexpected end of input"));
- }
- return true;
- }
- /*!
- @return a string representation of the last read byte
- */
- std::string get_token_string() const
- {
- char cr[3];
- snprintf(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
- return std::string{cr};
- }
- private:
- /// input adapter
- input_adapter_t ia = nullptr;
- /// the current character
- int current = std::char_traits<char>::eof();
- /// the number of characters read
- std::size_t chars_read = 0;
- /// whether we can assume little endianess
- const bool is_little_endian = little_endianess();
- /// the SAX parser
- json_sax_t* sax = nullptr;
- };
- }
- }
|