mqt-core 3.3.2__cp314-cp314t-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (546) hide show
  1. mqt/core/__init__.py +77 -0
  2. mqt/core/__main__.py +55 -0
  3. mqt/core/_commands.py +52 -0
  4. mqt/core/_compat/__init__.py +11 -0
  5. mqt/core/_compat/typing.py +29 -0
  6. mqt/core/_version.py +34 -0
  7. mqt/core/_version.pyi +12 -0
  8. mqt/core/dd.cpython-314t-darwin.so +0 -0
  9. mqt/core/dd.pyi +1016 -0
  10. mqt/core/dd_evaluation.py +368 -0
  11. mqt/core/fomac.cpython-314t-darwin.so +0 -0
  12. mqt/core/fomac.pyi +125 -0
  13. mqt/core/include/mqt-core/algorithms/BernsteinVazirani.hpp +39 -0
  14. mqt/core/include/mqt-core/algorithms/GHZState.hpp +18 -0
  15. mqt/core/include/mqt-core/algorithms/Grover.hpp +33 -0
  16. mqt/core/include/mqt-core/algorithms/QFT.hpp +21 -0
  17. mqt/core/include/mqt-core/algorithms/QPE.hpp +30 -0
  18. mqt/core/include/mqt-core/algorithms/RandomCliffordCircuit.hpp +22 -0
  19. mqt/core/include/mqt-core/algorithms/StatePreparation.hpp +43 -0
  20. mqt/core/include/mqt-core/algorithms/WState.hpp +18 -0
  21. mqt/core/include/mqt-core/algorithms/mqt_core_algorithms_export.h +43 -0
  22. mqt/core/include/mqt-core/boost/config/abi/borland_prefix.hpp +27 -0
  23. mqt/core/include/mqt-core/boost/config/abi/borland_suffix.hpp +12 -0
  24. mqt/core/include/mqt-core/boost/config/abi/msvc_prefix.hpp +22 -0
  25. mqt/core/include/mqt-core/boost/config/abi/msvc_suffix.hpp +8 -0
  26. mqt/core/include/mqt-core/boost/config/abi_prefix.hpp +25 -0
  27. mqt/core/include/mqt-core/boost/config/abi_suffix.hpp +25 -0
  28. mqt/core/include/mqt-core/boost/config/assert_cxx03.hpp +211 -0
  29. mqt/core/include/mqt-core/boost/config/assert_cxx11.hpp +212 -0
  30. mqt/core/include/mqt-core/boost/config/assert_cxx14.hpp +47 -0
  31. mqt/core/include/mqt-core/boost/config/assert_cxx17.hpp +65 -0
  32. mqt/core/include/mqt-core/boost/config/assert_cxx20.hpp +59 -0
  33. mqt/core/include/mqt-core/boost/config/assert_cxx23.hpp +41 -0
  34. mqt/core/include/mqt-core/boost/config/assert_cxx98.hpp +23 -0
  35. mqt/core/include/mqt-core/boost/config/auto_link.hpp +525 -0
  36. mqt/core/include/mqt-core/boost/config/compiler/borland.hpp +342 -0
  37. mqt/core/include/mqt-core/boost/config/compiler/clang.hpp +370 -0
  38. mqt/core/include/mqt-core/boost/config/compiler/clang_version.hpp +89 -0
  39. mqt/core/include/mqt-core/boost/config/compiler/codegear.hpp +389 -0
  40. mqt/core/include/mqt-core/boost/config/compiler/comeau.hpp +59 -0
  41. mqt/core/include/mqt-core/boost/config/compiler/common_edg.hpp +185 -0
  42. mqt/core/include/mqt-core/boost/config/compiler/compaq_cxx.hpp +19 -0
  43. mqt/core/include/mqt-core/boost/config/compiler/cray.hpp +446 -0
  44. mqt/core/include/mqt-core/boost/config/compiler/diab.hpp +26 -0
  45. mqt/core/include/mqt-core/boost/config/compiler/digitalmars.hpp +146 -0
  46. mqt/core/include/mqt-core/boost/config/compiler/gcc.hpp +386 -0
  47. mqt/core/include/mqt-core/boost/config/compiler/gcc_xml.hpp +115 -0
  48. mqt/core/include/mqt-core/boost/config/compiler/greenhills.hpp +28 -0
  49. mqt/core/include/mqt-core/boost/config/compiler/hp_acc.hpp +153 -0
  50. mqt/core/include/mqt-core/boost/config/compiler/intel.hpp +577 -0
  51. mqt/core/include/mqt-core/boost/config/compiler/kai.hpp +33 -0
  52. mqt/core/include/mqt-core/boost/config/compiler/metrowerks.hpp +201 -0
  53. mqt/core/include/mqt-core/boost/config/compiler/mpw.hpp +143 -0
  54. mqt/core/include/mqt-core/boost/config/compiler/nvcc.hpp +64 -0
  55. mqt/core/include/mqt-core/boost/config/compiler/pathscale.hpp +141 -0
  56. mqt/core/include/mqt-core/boost/config/compiler/pgi.hpp +23 -0
  57. mqt/core/include/mqt-core/boost/config/compiler/sgi_mipspro.hpp +29 -0
  58. mqt/core/include/mqt-core/boost/config/compiler/sunpro_cc.hpp +225 -0
  59. mqt/core/include/mqt-core/boost/config/compiler/vacpp.hpp +189 -0
  60. mqt/core/include/mqt-core/boost/config/compiler/visualc.hpp +398 -0
  61. mqt/core/include/mqt-core/boost/config/compiler/xlcpp.hpp +303 -0
  62. mqt/core/include/mqt-core/boost/config/compiler/xlcpp_zos.hpp +174 -0
  63. mqt/core/include/mqt-core/boost/config/detail/cxx_composite.hpp +218 -0
  64. mqt/core/include/mqt-core/boost/config/detail/posix_features.hpp +95 -0
  65. mqt/core/include/mqt-core/boost/config/detail/select_compiler_config.hpp +157 -0
  66. mqt/core/include/mqt-core/boost/config/detail/select_platform_config.hpp +147 -0
  67. mqt/core/include/mqt-core/boost/config/detail/select_stdlib_config.hpp +121 -0
  68. mqt/core/include/mqt-core/boost/config/detail/suffix.hpp +1334 -0
  69. mqt/core/include/mqt-core/boost/config/header_deprecated.hpp +26 -0
  70. mqt/core/include/mqt-core/boost/config/helper_macros.hpp +37 -0
  71. mqt/core/include/mqt-core/boost/config/no_tr1/cmath.hpp +28 -0
  72. mqt/core/include/mqt-core/boost/config/no_tr1/complex.hpp +28 -0
  73. mqt/core/include/mqt-core/boost/config/no_tr1/functional.hpp +28 -0
  74. mqt/core/include/mqt-core/boost/config/no_tr1/memory.hpp +28 -0
  75. mqt/core/include/mqt-core/boost/config/no_tr1/utility.hpp +28 -0
  76. mqt/core/include/mqt-core/boost/config/platform/aix.hpp +33 -0
  77. mqt/core/include/mqt-core/boost/config/platform/amigaos.hpp +15 -0
  78. mqt/core/include/mqt-core/boost/config/platform/beos.hpp +26 -0
  79. mqt/core/include/mqt-core/boost/config/platform/bsd.hpp +83 -0
  80. mqt/core/include/mqt-core/boost/config/platform/cloudabi.hpp +18 -0
  81. mqt/core/include/mqt-core/boost/config/platform/cray.hpp +18 -0
  82. mqt/core/include/mqt-core/boost/config/platform/cygwin.hpp +71 -0
  83. mqt/core/include/mqt-core/boost/config/platform/haiku.hpp +31 -0
  84. mqt/core/include/mqt-core/boost/config/platform/hpux.hpp +87 -0
  85. mqt/core/include/mqt-core/boost/config/platform/irix.hpp +31 -0
  86. mqt/core/include/mqt-core/boost/config/platform/linux.hpp +106 -0
  87. mqt/core/include/mqt-core/boost/config/platform/macos.hpp +87 -0
  88. mqt/core/include/mqt-core/boost/config/platform/qnxnto.hpp +31 -0
  89. mqt/core/include/mqt-core/boost/config/platform/solaris.hpp +31 -0
  90. mqt/core/include/mqt-core/boost/config/platform/symbian.hpp +97 -0
  91. mqt/core/include/mqt-core/boost/config/platform/vms.hpp +25 -0
  92. mqt/core/include/mqt-core/boost/config/platform/vxworks.hpp +422 -0
  93. mqt/core/include/mqt-core/boost/config/platform/wasm.hpp +23 -0
  94. mqt/core/include/mqt-core/boost/config/platform/win32.hpp +90 -0
  95. mqt/core/include/mqt-core/boost/config/platform/zos.hpp +32 -0
  96. mqt/core/include/mqt-core/boost/config/pragma_message.hpp +31 -0
  97. mqt/core/include/mqt-core/boost/config/requires_threads.hpp +92 -0
  98. mqt/core/include/mqt-core/boost/config/stdlib/dinkumware.hpp +324 -0
  99. mqt/core/include/mqt-core/boost/config/stdlib/libcomo.hpp +93 -0
  100. mqt/core/include/mqt-core/boost/config/stdlib/libcpp.hpp +180 -0
  101. mqt/core/include/mqt-core/boost/config/stdlib/libstdcpp3.hpp +482 -0
  102. mqt/core/include/mqt-core/boost/config/stdlib/modena.hpp +79 -0
  103. mqt/core/include/mqt-core/boost/config/stdlib/msl.hpp +98 -0
  104. mqt/core/include/mqt-core/boost/config/stdlib/roguewave.hpp +208 -0
  105. mqt/core/include/mqt-core/boost/config/stdlib/sgi.hpp +168 -0
  106. mqt/core/include/mqt-core/boost/config/stdlib/stlport.hpp +258 -0
  107. mqt/core/include/mqt-core/boost/config/stdlib/vacpp.hpp +74 -0
  108. mqt/core/include/mqt-core/boost/config/stdlib/xlcpp_zos.hpp +61 -0
  109. mqt/core/include/mqt-core/boost/config/user.hpp +133 -0
  110. mqt/core/include/mqt-core/boost/config/warning_disable.hpp +47 -0
  111. mqt/core/include/mqt-core/boost/config/workaround.hpp +305 -0
  112. mqt/core/include/mqt-core/boost/config.hpp +67 -0
  113. mqt/core/include/mqt-core/boost/cstdint.hpp +556 -0
  114. mqt/core/include/mqt-core/boost/cxx11_char_types.hpp +70 -0
  115. mqt/core/include/mqt-core/boost/detail/workaround.hpp +10 -0
  116. mqt/core/include/mqt-core/boost/limits.hpp +146 -0
  117. mqt/core/include/mqt-core/boost/multiprecision/complex128.hpp +24 -0
  118. mqt/core/include/mqt-core/boost/multiprecision/complex_adaptor.hpp +1046 -0
  119. mqt/core/include/mqt-core/boost/multiprecision/concepts/mp_number_archetypes.hpp +257 -0
  120. mqt/core/include/mqt-core/boost/multiprecision/cpp_bin_float/io.hpp +698 -0
  121. mqt/core/include/mqt-core/boost/multiprecision/cpp_bin_float/transcendental.hpp +157 -0
  122. mqt/core/include/mqt-core/boost/multiprecision/cpp_bin_float.hpp +2297 -0
  123. mqt/core/include/mqt-core/boost/multiprecision/cpp_complex.hpp +12 -0
  124. mqt/core/include/mqt-core/boost/multiprecision/cpp_dec_float.hpp +3690 -0
  125. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/add.hpp +368 -0
  126. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/add_unsigned.hpp +387 -0
  127. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/bitwise.hpp +889 -0
  128. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/checked.hpp +178 -0
  129. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/comparison.hpp +374 -0
  130. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/cpp_int_config.hpp +161 -0
  131. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/divide.hpp +703 -0
  132. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/import_export.hpp +248 -0
  133. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/intel_intrinsics.hpp +138 -0
  134. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/limits.hpp +282 -0
  135. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/literals.hpp +295 -0
  136. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/misc.hpp +1457 -0
  137. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/multiply.hpp +848 -0
  138. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/serialize.hpp +211 -0
  139. mqt/core/include/mqt-core/boost/multiprecision/cpp_int/value_pack.hpp +42 -0
  140. mqt/core/include/mqt-core/boost/multiprecision/cpp_int.hpp +2360 -0
  141. mqt/core/include/mqt-core/boost/multiprecision/debug_adaptor.hpp +760 -0
  142. mqt/core/include/mqt-core/boost/multiprecision/detail/assert.hpp +29 -0
  143. mqt/core/include/mqt-core/boost/multiprecision/detail/atomic.hpp +62 -0
  144. mqt/core/include/mqt-core/boost/multiprecision/detail/bitscan.hpp +317 -0
  145. mqt/core/include/mqt-core/boost/multiprecision/detail/check_cpp11_config.hpp +64 -0
  146. mqt/core/include/mqt-core/boost/multiprecision/detail/constexpr.hpp +88 -0
  147. mqt/core/include/mqt-core/boost/multiprecision/detail/default_ops.hpp +4052 -0
  148. mqt/core/include/mqt-core/boost/multiprecision/detail/digits.hpp +49 -0
  149. mqt/core/include/mqt-core/boost/multiprecision/detail/dynamic_array.hpp +44 -0
  150. mqt/core/include/mqt-core/boost/multiprecision/detail/empty_value.hpp +87 -0
  151. mqt/core/include/mqt-core/boost/multiprecision/detail/endian.hpp +35 -0
  152. mqt/core/include/mqt-core/boost/multiprecision/detail/et_ops.hpp +1831 -0
  153. mqt/core/include/mqt-core/boost/multiprecision/detail/float128_functions.hpp +95 -0
  154. mqt/core/include/mqt-core/boost/multiprecision/detail/float_string_cvt.hpp +333 -0
  155. mqt/core/include/mqt-core/boost/multiprecision/detail/fpclassify.hpp +101 -0
  156. mqt/core/include/mqt-core/boost/multiprecision/detail/functions/constants.hpp +288 -0
  157. mqt/core/include/mqt-core/boost/multiprecision/detail/functions/pow.hpp +905 -0
  158. mqt/core/include/mqt-core/boost/multiprecision/detail/functions/trig.hpp +1058 -0
  159. mqt/core/include/mqt-core/boost/multiprecision/detail/functions/trunc.hpp +82 -0
  160. mqt/core/include/mqt-core/boost/multiprecision/detail/generic_interconvert.hpp +687 -0
  161. mqt/core/include/mqt-core/boost/multiprecision/detail/hash.hpp +56 -0
  162. mqt/core/include/mqt-core/boost/multiprecision/detail/integer_ops.hpp +474 -0
  163. mqt/core/include/mqt-core/boost/multiprecision/detail/itos.hpp +39 -0
  164. mqt/core/include/mqt-core/boost/multiprecision/detail/min_max.hpp +106 -0
  165. mqt/core/include/mqt-core/boost/multiprecision/detail/no_et_ops.hpp +661 -0
  166. mqt/core/include/mqt-core/boost/multiprecision/detail/no_exceptions_support.hpp +55 -0
  167. mqt/core/include/mqt-core/boost/multiprecision/detail/number_base.hpp +1656 -0
  168. mqt/core/include/mqt-core/boost/multiprecision/detail/number_compare.hpp +848 -0
  169. mqt/core/include/mqt-core/boost/multiprecision/detail/precision.hpp +313 -0
  170. mqt/core/include/mqt-core/boost/multiprecision/detail/rebind.hpp +19 -0
  171. mqt/core/include/mqt-core/boost/multiprecision/detail/standalone_config.hpp +148 -0
  172. mqt/core/include/mqt-core/boost/multiprecision/detail/static_array.hpp +42 -0
  173. mqt/core/include/mqt-core/boost/multiprecision/detail/string_helpers.hpp +48 -0
  174. mqt/core/include/mqt-core/boost/multiprecision/detail/tables.hpp +80 -0
  175. mqt/core/include/mqt-core/boost/multiprecision/detail/ublas_interop.hpp +75 -0
  176. mqt/core/include/mqt-core/boost/multiprecision/detail/uniform_int_distribution.hpp +212 -0
  177. mqt/core/include/mqt-core/boost/multiprecision/detail/utype_helper.hpp +374 -0
  178. mqt/core/include/mqt-core/boost/multiprecision/eigen.hpp +248 -0
  179. mqt/core/include/mqt-core/boost/multiprecision/float128.hpp +920 -0
  180. mqt/core/include/mqt-core/boost/multiprecision/fwd.hpp +268 -0
  181. mqt/core/include/mqt-core/boost/multiprecision/gmp.hpp +4060 -0
  182. mqt/core/include/mqt-core/boost/multiprecision/integer.hpp +363 -0
  183. mqt/core/include/mqt-core/boost/multiprecision/logged_adaptor.hpp +834 -0
  184. mqt/core/include/mqt-core/boost/multiprecision/miller_rabin.hpp +221 -0
  185. mqt/core/include/mqt-core/boost/multiprecision/mpc.hpp +1721 -0
  186. mqt/core/include/mqt-core/boost/multiprecision/mpfi.hpp +2559 -0
  187. mqt/core/include/mqt-core/boost/multiprecision/mpfr.hpp +3644 -0
  188. mqt/core/include/mqt-core/boost/multiprecision/number.hpp +2500 -0
  189. mqt/core/include/mqt-core/boost/multiprecision/random.hpp +23 -0
  190. mqt/core/include/mqt-core/boost/multiprecision/rational_adaptor.hpp +1289 -0
  191. mqt/core/include/mqt-core/boost/multiprecision/tommath.hpp +1034 -0
  192. mqt/core/include/mqt-core/boost/multiprecision/traits/explicit_conversion.hpp +67 -0
  193. mqt/core/include/mqt-core/boost/multiprecision/traits/extract_exponent_type.hpp +28 -0
  194. mqt/core/include/mqt-core/boost/multiprecision/traits/is_backend.hpp +91 -0
  195. mqt/core/include/mqt-core/boost/multiprecision/traits/is_byte_container.hpp +51 -0
  196. mqt/core/include/mqt-core/boost/multiprecision/traits/is_complex.hpp +22 -0
  197. mqt/core/include/mqt-core/boost/multiprecision/traits/is_convertible_arithmetic.hpp +51 -0
  198. mqt/core/include/mqt-core/boost/multiprecision/traits/is_restricted_conversion.hpp +47 -0
  199. mqt/core/include/mqt-core/boost/multiprecision/traits/is_variable_precision.hpp +25 -0
  200. mqt/core/include/mqt-core/boost/multiprecision/traits/max_digits10.hpp +79 -0
  201. mqt/core/include/mqt-core/boost/multiprecision/traits/std_integer_traits.hpp +90 -0
  202. mqt/core/include/mqt-core/boost/multiprecision/traits/transcendental_reduction_type.hpp +21 -0
  203. mqt/core/include/mqt-core/boost/version.hpp +32 -0
  204. mqt/core/include/mqt-core/circuit_optimizer/CircuitOptimizer.hpp +119 -0
  205. mqt/core/include/mqt-core/circuit_optimizer/mqt_core_circuit_optimizer_export.h +43 -0
  206. mqt/core/include/mqt-core/datastructures/DirectedAcyclicGraph.hpp +117 -0
  207. mqt/core/include/mqt-core/datastructures/DirectedGraph.hpp +158 -0
  208. mqt/core/include/mqt-core/datastructures/DisjointSet.hpp +50 -0
  209. mqt/core/include/mqt-core/datastructures/Layer.hpp +172 -0
  210. mqt/core/include/mqt-core/datastructures/SymmetricMatrix.hpp +57 -0
  211. mqt/core/include/mqt-core/datastructures/UndirectedGraph.hpp +227 -0
  212. mqt/core/include/mqt-core/datastructures/mqt_core_ds_export.h +43 -0
  213. mqt/core/include/mqt-core/dd/Approximation.hpp +45 -0
  214. mqt/core/include/mqt-core/dd/CachedEdge.hpp +174 -0
  215. mqt/core/include/mqt-core/dd/Complex.hpp +165 -0
  216. mqt/core/include/mqt-core/dd/ComplexNumbers.hpp +150 -0
  217. mqt/core/include/mqt-core/dd/ComplexValue.hpp +184 -0
  218. mqt/core/include/mqt-core/dd/ComputeTable.hpp +183 -0
  219. mqt/core/include/mqt-core/dd/DDDefinitions.hpp +139 -0
  220. mqt/core/include/mqt-core/dd/DDpackageConfig.hpp +104 -0
  221. mqt/core/include/mqt-core/dd/DensityNoiseTable.hpp +114 -0
  222. mqt/core/include/mqt-core/dd/Edge.hpp +416 -0
  223. mqt/core/include/mqt-core/dd/Export.hpp +438 -0
  224. mqt/core/include/mqt-core/dd/FunctionalityConstruction.hpp +75 -0
  225. mqt/core/include/mqt-core/dd/GateMatrixDefinitions.hpp +43 -0
  226. mqt/core/include/mqt-core/dd/LinkedListBase.hpp +45 -0
  227. mqt/core/include/mqt-core/dd/MemoryManager.hpp +193 -0
  228. mqt/core/include/mqt-core/dd/Node.hpp +223 -0
  229. mqt/core/include/mqt-core/dd/NoiseFunctionality.hpp +144 -0
  230. mqt/core/include/mqt-core/dd/Operations.hpp +306 -0
  231. mqt/core/include/mqt-core/dd/Package.hpp +2036 -0
  232. mqt/core/include/mqt-core/dd/Package_fwd.hpp +22 -0
  233. mqt/core/include/mqt-core/dd/RealNumber.hpp +255 -0
  234. mqt/core/include/mqt-core/dd/RealNumberUniqueTable.hpp +217 -0
  235. mqt/core/include/mqt-core/dd/Simulation.hpp +98 -0
  236. mqt/core/include/mqt-core/dd/StateGeneration.hpp +143 -0
  237. mqt/core/include/mqt-core/dd/StochasticNoiseOperationTable.hpp +88 -0
  238. mqt/core/include/mqt-core/dd/UnaryComputeTable.hpp +121 -0
  239. mqt/core/include/mqt-core/dd/UniqueTable.hpp +243 -0
  240. mqt/core/include/mqt-core/dd/mqt_core_dd_export.h +43 -0
  241. mqt/core/include/mqt-core/dd/statistics/MemoryManagerStatistics.hpp +84 -0
  242. mqt/core/include/mqt-core/dd/statistics/PackageStatistics.hpp +55 -0
  243. mqt/core/include/mqt-core/dd/statistics/Statistics.hpp +48 -0
  244. mqt/core/include/mqt-core/dd/statistics/TableStatistics.hpp +79 -0
  245. mqt/core/include/mqt-core/dd/statistics/UniqueTableStatistics.hpp +31 -0
  246. mqt/core/include/mqt-core/fomac/FoMaC.hpp +568 -0
  247. mqt/core/include/mqt-core/ir/Definitions.hpp +108 -0
  248. mqt/core/include/mqt-core/ir/Permutation.hpp +213 -0
  249. mqt/core/include/mqt-core/ir/QuantumComputation.hpp +596 -0
  250. mqt/core/include/mqt-core/ir/Register.hpp +125 -0
  251. mqt/core/include/mqt-core/ir/mqt_core_ir_export.h +43 -0
  252. mqt/core/include/mqt-core/ir/operations/AodOperation.hpp +92 -0
  253. mqt/core/include/mqt-core/ir/operations/CompoundOperation.hpp +212 -0
  254. mqt/core/include/mqt-core/ir/operations/Control.hpp +142 -0
  255. mqt/core/include/mqt-core/ir/operations/Expression.hpp +847 -0
  256. mqt/core/include/mqt-core/ir/operations/IfElseOperation.hpp +169 -0
  257. mqt/core/include/mqt-core/ir/operations/NonUnitaryOperation.hpp +118 -0
  258. mqt/core/include/mqt-core/ir/operations/OpType.hpp +120 -0
  259. mqt/core/include/mqt-core/ir/operations/OpType.inc +76 -0
  260. mqt/core/include/mqt-core/ir/operations/Operation.hpp +247 -0
  261. mqt/core/include/mqt-core/ir/operations/StandardOperation.hpp +140 -0
  262. mqt/core/include/mqt-core/ir/operations/SymbolicOperation.hpp +144 -0
  263. mqt/core/include/mqt-core/mqt_na_qdmi/device.h +602 -0
  264. mqt/core/include/mqt-core/mqt_na_qdmi/types.h +78 -0
  265. mqt/core/include/mqt-core/na/NAComputation.hpp +185 -0
  266. mqt/core/include/mqt-core/na/device/Device.hpp +410 -0
  267. mqt/core/include/mqt-core/na/device/DeviceMemberInitializers.hpp +724 -0
  268. mqt/core/include/mqt-core/na/device/Generator.hpp +447 -0
  269. mqt/core/include/mqt-core/na/entities/Atom.hpp +62 -0
  270. mqt/core/include/mqt-core/na/entities/Location.hpp +154 -0
  271. mqt/core/include/mqt-core/na/entities/Zone.hpp +95 -0
  272. mqt/core/include/mqt-core/na/fomac/Device.hpp +169 -0
  273. mqt/core/include/mqt-core/na/mqt_core_na_export.h +43 -0
  274. mqt/core/include/mqt-core/na/operations/GlobalCZOp.hpp +38 -0
  275. mqt/core/include/mqt-core/na/operations/GlobalOp.hpp +58 -0
  276. mqt/core/include/mqt-core/na/operations/GlobalRYOp.hpp +42 -0
  277. mqt/core/include/mqt-core/na/operations/LoadOp.hpp +89 -0
  278. mqt/core/include/mqt-core/na/operations/LocalOp.hpp +56 -0
  279. mqt/core/include/mqt-core/na/operations/LocalRZOp.hpp +42 -0
  280. mqt/core/include/mqt-core/na/operations/LocalUOp.hpp +49 -0
  281. mqt/core/include/mqt-core/na/operations/MoveOp.hpp +66 -0
  282. mqt/core/include/mqt-core/na/operations/Op.hpp +62 -0
  283. mqt/core/include/mqt-core/na/operations/ShuttlingOp.hpp +51 -0
  284. mqt/core/include/mqt-core/na/operations/StoreOp.hpp +87 -0
  285. mqt/core/include/mqt-core/qasm3/Exception.hpp +85 -0
  286. mqt/core/include/mqt-core/qasm3/Gate.hpp +65 -0
  287. mqt/core/include/mqt-core/qasm3/Importer.hpp +192 -0
  288. mqt/core/include/mqt-core/qasm3/InstVisitor.hpp +145 -0
  289. mqt/core/include/mqt-core/qasm3/NestedEnvironment.hpp +41 -0
  290. mqt/core/include/mqt-core/qasm3/Parser.hpp +170 -0
  291. mqt/core/include/mqt-core/qasm3/Scanner.hpp +73 -0
  292. mqt/core/include/mqt-core/qasm3/Statement.hpp +486 -0
  293. mqt/core/include/mqt-core/qasm3/Statement_fwd.hpp +39 -0
  294. mqt/core/include/mqt-core/qasm3/StdGates.hpp +232 -0
  295. mqt/core/include/mqt-core/qasm3/Token.hpp +198 -0
  296. mqt/core/include/mqt-core/qasm3/Types.hpp +238 -0
  297. mqt/core/include/mqt-core/qasm3/Types_fwd.hpp +22 -0
  298. mqt/core/include/mqt-core/qasm3/mqt_core_qasm_export.h +43 -0
  299. mqt/core/include/mqt-core/qasm3/passes/CompilerPass.hpp +22 -0
  300. mqt/core/include/mqt-core/qasm3/passes/ConstEvalPass.hpp +102 -0
  301. mqt/core/include/mqt-core/qasm3/passes/TypeCheckPass.hpp +124 -0
  302. mqt/core/include/mqt-core/qdmi/Driver.hpp +431 -0
  303. mqt/core/include/mqt-core/zx/FunctionalityConstruction.hpp +125 -0
  304. mqt/core/include/mqt-core/zx/Rational.hpp +318 -0
  305. mqt/core/include/mqt-core/zx/Rules.hpp +132 -0
  306. mqt/core/include/mqt-core/zx/Simplify.hpp +182 -0
  307. mqt/core/include/mqt-core/zx/Utils.hpp +212 -0
  308. mqt/core/include/mqt-core/zx/ZXDefinitions.hpp +93 -0
  309. mqt/core/include/mqt-core/zx/ZXDiagram.hpp +480 -0
  310. mqt/core/include/mqt-core/zx/mqt_core_zx_export.h +43 -0
  311. mqt/core/include/nlohmann/adl_serializer.hpp +55 -0
  312. mqt/core/include/nlohmann/byte_container_with_subtype.hpp +103 -0
  313. mqt/core/include/nlohmann/detail/abi_macros.hpp +111 -0
  314. mqt/core/include/nlohmann/detail/conversions/from_json.hpp +577 -0
  315. mqt/core/include/nlohmann/detail/conversions/to_chars.hpp +1118 -0
  316. mqt/core/include/nlohmann/detail/conversions/to_json.hpp +479 -0
  317. mqt/core/include/nlohmann/detail/exceptions.hpp +291 -0
  318. mqt/core/include/nlohmann/detail/hash.hpp +129 -0
  319. mqt/core/include/nlohmann/detail/input/binary_reader.hpp +3068 -0
  320. mqt/core/include/nlohmann/detail/input/input_adapters.hpp +549 -0
  321. mqt/core/include/nlohmann/detail/input/json_sax.hpp +986 -0
  322. mqt/core/include/nlohmann/detail/input/lexer.hpp +1643 -0
  323. mqt/core/include/nlohmann/detail/input/parser.hpp +519 -0
  324. mqt/core/include/nlohmann/detail/input/position_t.hpp +37 -0
  325. mqt/core/include/nlohmann/detail/iterators/internal_iterator.hpp +35 -0
  326. mqt/core/include/nlohmann/detail/iterators/iter_impl.hpp +760 -0
  327. mqt/core/include/nlohmann/detail/iterators/iteration_proxy.hpp +235 -0
  328. mqt/core/include/nlohmann/detail/iterators/iterator_traits.hpp +61 -0
  329. mqt/core/include/nlohmann/detail/iterators/json_reverse_iterator.hpp +130 -0
  330. mqt/core/include/nlohmann/detail/iterators/primitive_iterator.hpp +132 -0
  331. mqt/core/include/nlohmann/detail/json_custom_base_class.hpp +39 -0
  332. mqt/core/include/nlohmann/detail/json_pointer.hpp +988 -0
  333. mqt/core/include/nlohmann/detail/json_ref.hpp +78 -0
  334. mqt/core/include/nlohmann/detail/macro_scope.hpp +595 -0
  335. mqt/core/include/nlohmann/detail/macro_unscope.hpp +46 -0
  336. mqt/core/include/nlohmann/detail/meta/call_std/begin.hpp +17 -0
  337. mqt/core/include/nlohmann/detail/meta/call_std/end.hpp +17 -0
  338. mqt/core/include/nlohmann/detail/meta/cpp_future.hpp +171 -0
  339. mqt/core/include/nlohmann/detail/meta/detected.hpp +70 -0
  340. mqt/core/include/nlohmann/detail/meta/identity_tag.hpp +21 -0
  341. mqt/core/include/nlohmann/detail/meta/is_sax.hpp +159 -0
  342. mqt/core/include/nlohmann/detail/meta/std_fs.hpp +29 -0
  343. mqt/core/include/nlohmann/detail/meta/type_traits.hpp +795 -0
  344. mqt/core/include/nlohmann/detail/meta/void_t.hpp +24 -0
  345. mqt/core/include/nlohmann/detail/output/binary_writer.hpp +1850 -0
  346. mqt/core/include/nlohmann/detail/output/output_adapters.hpp +147 -0
  347. mqt/core/include/nlohmann/detail/output/serializer.hpp +988 -0
  348. mqt/core/include/nlohmann/detail/string_concat.hpp +146 -0
  349. mqt/core/include/nlohmann/detail/string_escape.hpp +72 -0
  350. mqt/core/include/nlohmann/detail/string_utils.hpp +37 -0
  351. mqt/core/include/nlohmann/detail/value_t.hpp +118 -0
  352. mqt/core/include/nlohmann/json.hpp +5306 -0
  353. mqt/core/include/nlohmann/json_fwd.hpp +75 -0
  354. mqt/core/include/nlohmann/ordered_map.hpp +359 -0
  355. mqt/core/include/nlohmann/thirdparty/hedley/hedley.hpp +2045 -0
  356. mqt/core/include/nlohmann/thirdparty/hedley/hedley_undef.hpp +158 -0
  357. mqt/core/include/qdmi/qdmi/client.h +990 -0
  358. mqt/core/include/qdmi/qdmi/constants.h +1139 -0
  359. mqt/core/include/qdmi/qdmi/device.h +602 -0
  360. mqt/core/include/qdmi/qdmi/types.h +78 -0
  361. mqt/core/include/spdlog/async.h +99 -0
  362. mqt/core/include/spdlog/async_logger-inl.h +84 -0
  363. mqt/core/include/spdlog/async_logger.h +74 -0
  364. mqt/core/include/spdlog/cfg/argv.h +40 -0
  365. mqt/core/include/spdlog/cfg/env.h +36 -0
  366. mqt/core/include/spdlog/cfg/helpers-inl.h +107 -0
  367. mqt/core/include/spdlog/cfg/helpers.h +29 -0
  368. mqt/core/include/spdlog/common-inl.h +68 -0
  369. mqt/core/include/spdlog/common.h +406 -0
  370. mqt/core/include/spdlog/details/backtracer-inl.h +63 -0
  371. mqt/core/include/spdlog/details/backtracer.h +45 -0
  372. mqt/core/include/spdlog/details/circular_q.h +115 -0
  373. mqt/core/include/spdlog/details/console_globals.h +28 -0
  374. mqt/core/include/spdlog/details/file_helper-inl.h +153 -0
  375. mqt/core/include/spdlog/details/file_helper.h +61 -0
  376. mqt/core/include/spdlog/details/fmt_helper.h +141 -0
  377. mqt/core/include/spdlog/details/log_msg-inl.h +44 -0
  378. mqt/core/include/spdlog/details/log_msg.h +40 -0
  379. mqt/core/include/spdlog/details/log_msg_buffer-inl.h +54 -0
  380. mqt/core/include/spdlog/details/log_msg_buffer.h +32 -0
  381. mqt/core/include/spdlog/details/mpmc_blocking_q.h +177 -0
  382. mqt/core/include/spdlog/details/null_mutex.h +35 -0
  383. mqt/core/include/spdlog/details/os-inl.h +606 -0
  384. mqt/core/include/spdlog/details/os.h +127 -0
  385. mqt/core/include/spdlog/details/periodic_worker-inl.h +26 -0
  386. mqt/core/include/spdlog/details/periodic_worker.h +58 -0
  387. mqt/core/include/spdlog/details/registry-inl.h +270 -0
  388. mqt/core/include/spdlog/details/registry.h +131 -0
  389. mqt/core/include/spdlog/details/synchronous_factory.h +22 -0
  390. mqt/core/include/spdlog/details/tcp_client-windows.h +135 -0
  391. mqt/core/include/spdlog/details/tcp_client.h +127 -0
  392. mqt/core/include/spdlog/details/thread_pool-inl.h +126 -0
  393. mqt/core/include/spdlog/details/thread_pool.h +117 -0
  394. mqt/core/include/spdlog/details/udp_client-windows.h +98 -0
  395. mqt/core/include/spdlog/details/udp_client.h +81 -0
  396. mqt/core/include/spdlog/details/windows_include.h +11 -0
  397. mqt/core/include/spdlog/fmt/bin_to_hex.h +224 -0
  398. mqt/core/include/spdlog/fmt/bundled/args.h +220 -0
  399. mqt/core/include/spdlog/fmt/bundled/base.h +2989 -0
  400. mqt/core/include/spdlog/fmt/bundled/chrono.h +2330 -0
  401. mqt/core/include/spdlog/fmt/bundled/color.h +637 -0
  402. mqt/core/include/spdlog/fmt/bundled/compile.h +539 -0
  403. mqt/core/include/spdlog/fmt/bundled/core.h +5 -0
  404. mqt/core/include/spdlog/fmt/bundled/fmt.license.rst +27 -0
  405. mqt/core/include/spdlog/fmt/bundled/format-inl.h +1948 -0
  406. mqt/core/include/spdlog/fmt/bundled/format.h +4244 -0
  407. mqt/core/include/spdlog/fmt/bundled/os.h +427 -0
  408. mqt/core/include/spdlog/fmt/bundled/ostream.h +167 -0
  409. mqt/core/include/spdlog/fmt/bundled/printf.h +633 -0
  410. mqt/core/include/spdlog/fmt/bundled/ranges.h +850 -0
  411. mqt/core/include/spdlog/fmt/bundled/std.h +728 -0
  412. mqt/core/include/spdlog/fmt/bundled/xchar.h +369 -0
  413. mqt/core/include/spdlog/fmt/chrono.h +23 -0
  414. mqt/core/include/spdlog/fmt/compile.h +23 -0
  415. mqt/core/include/spdlog/fmt/fmt.h +30 -0
  416. mqt/core/include/spdlog/fmt/ostr.h +23 -0
  417. mqt/core/include/spdlog/fmt/ranges.h +23 -0
  418. mqt/core/include/spdlog/fmt/std.h +24 -0
  419. mqt/core/include/spdlog/fmt/xchar.h +23 -0
  420. mqt/core/include/spdlog/formatter.h +17 -0
  421. mqt/core/include/spdlog/fwd.h +18 -0
  422. mqt/core/include/spdlog/logger-inl.h +198 -0
  423. mqt/core/include/spdlog/logger.h +379 -0
  424. mqt/core/include/spdlog/mdc.h +52 -0
  425. mqt/core/include/spdlog/pattern_formatter-inl.h +1340 -0
  426. mqt/core/include/spdlog/pattern_formatter.h +118 -0
  427. mqt/core/include/spdlog/sinks/android_sink.h +137 -0
  428. mqt/core/include/spdlog/sinks/ansicolor_sink-inl.h +142 -0
  429. mqt/core/include/spdlog/sinks/ansicolor_sink.h +116 -0
  430. mqt/core/include/spdlog/sinks/base_sink-inl.h +59 -0
  431. mqt/core/include/spdlog/sinks/base_sink.h +51 -0
  432. mqt/core/include/spdlog/sinks/basic_file_sink-inl.h +48 -0
  433. mqt/core/include/spdlog/sinks/basic_file_sink.h +66 -0
  434. mqt/core/include/spdlog/sinks/callback_sink.h +56 -0
  435. mqt/core/include/spdlog/sinks/daily_file_sink.h +254 -0
  436. mqt/core/include/spdlog/sinks/dist_sink.h +81 -0
  437. mqt/core/include/spdlog/sinks/dup_filter_sink.h +91 -0
  438. mqt/core/include/spdlog/sinks/hourly_file_sink.h +193 -0
  439. mqt/core/include/spdlog/sinks/kafka_sink.h +119 -0
  440. mqt/core/include/spdlog/sinks/mongo_sink.h +108 -0
  441. mqt/core/include/spdlog/sinks/msvc_sink.h +68 -0
  442. mqt/core/include/spdlog/sinks/null_sink.h +41 -0
  443. mqt/core/include/spdlog/sinks/ostream_sink.h +43 -0
  444. mqt/core/include/spdlog/sinks/qt_sinks.h +304 -0
  445. mqt/core/include/spdlog/sinks/ringbuffer_sink.h +67 -0
  446. mqt/core/include/spdlog/sinks/rotating_file_sink-inl.h +179 -0
  447. mqt/core/include/spdlog/sinks/rotating_file_sink.h +93 -0
  448. mqt/core/include/spdlog/sinks/sink-inl.h +22 -0
  449. mqt/core/include/spdlog/sinks/sink.h +34 -0
  450. mqt/core/include/spdlog/sinks/stdout_color_sinks-inl.h +38 -0
  451. mqt/core/include/spdlog/sinks/stdout_color_sinks.h +49 -0
  452. mqt/core/include/spdlog/sinks/stdout_sinks-inl.h +127 -0
  453. mqt/core/include/spdlog/sinks/stdout_sinks.h +84 -0
  454. mqt/core/include/spdlog/sinks/syslog_sink.h +104 -0
  455. mqt/core/include/spdlog/sinks/systemd_sink.h +121 -0
  456. mqt/core/include/spdlog/sinks/tcp_sink.h +75 -0
  457. mqt/core/include/spdlog/sinks/udp_sink.h +69 -0
  458. mqt/core/include/spdlog/sinks/win_eventlog_sink.h +260 -0
  459. mqt/core/include/spdlog/sinks/wincolor_sink-inl.h +172 -0
  460. mqt/core/include/spdlog/sinks/wincolor_sink.h +82 -0
  461. mqt/core/include/spdlog/spdlog-inl.h +96 -0
  462. mqt/core/include/spdlog/spdlog.h +357 -0
  463. mqt/core/include/spdlog/stopwatch.h +66 -0
  464. mqt/core/include/spdlog/tweakme.h +148 -0
  465. mqt/core/include/spdlog/version.h +11 -0
  466. mqt/core/ir/__init__.pyi +2078 -0
  467. mqt/core/ir/operations.pyi +1011 -0
  468. mqt/core/ir/registers.pyi +91 -0
  469. mqt/core/ir/symbolic.pyi +177 -0
  470. mqt/core/ir.cpython-314t-darwin.so +0 -0
  471. mqt/core/lib/libmqt-core-algorithms.3.3.2.dylib +0 -0
  472. mqt/core/lib/libmqt-core-algorithms.3.3.dylib +0 -0
  473. mqt/core/lib/libmqt-core-algorithms.dylib +0 -0
  474. mqt/core/lib/libmqt-core-circuit-optimizer.3.3.2.dylib +0 -0
  475. mqt/core/lib/libmqt-core-circuit-optimizer.3.3.dylib +0 -0
  476. mqt/core/lib/libmqt-core-circuit-optimizer.dylib +0 -0
  477. mqt/core/lib/libmqt-core-dd.3.3.2.dylib +0 -0
  478. mqt/core/lib/libmqt-core-dd.3.3.dylib +0 -0
  479. mqt/core/lib/libmqt-core-dd.dylib +0 -0
  480. mqt/core/lib/libmqt-core-ds.3.3.2.dylib +0 -0
  481. mqt/core/lib/libmqt-core-ds.3.3.dylib +0 -0
  482. mqt/core/lib/libmqt-core-ds.dylib +0 -0
  483. mqt/core/lib/libmqt-core-fomac.3.3.2.dylib +0 -0
  484. mqt/core/lib/libmqt-core-fomac.3.3.dylib +0 -0
  485. mqt/core/lib/libmqt-core-fomac.dylib +0 -0
  486. mqt/core/lib/libmqt-core-ir.3.3.2.dylib +0 -0
  487. mqt/core/lib/libmqt-core-ir.3.3.dylib +0 -0
  488. mqt/core/lib/libmqt-core-ir.dylib +0 -0
  489. mqt/core/lib/libmqt-core-na-fomac.3.3.2.dylib +0 -0
  490. mqt/core/lib/libmqt-core-na-fomac.3.3.dylib +0 -0
  491. mqt/core/lib/libmqt-core-na-fomac.dylib +0 -0
  492. mqt/core/lib/libmqt-core-na.3.3.2.dylib +0 -0
  493. mqt/core/lib/libmqt-core-na.3.3.dylib +0 -0
  494. mqt/core/lib/libmqt-core-na.dylib +0 -0
  495. mqt/core/lib/libmqt-core-qasm.3.3.2.dylib +0 -0
  496. mqt/core/lib/libmqt-core-qasm.3.3.dylib +0 -0
  497. mqt/core/lib/libmqt-core-qasm.dylib +0 -0
  498. mqt/core/lib/libmqt-core-qdmi-driver.3.3.2.dylib +0 -0
  499. mqt/core/lib/libmqt-core-qdmi-driver.3.3.dylib +0 -0
  500. mqt/core/lib/libmqt-core-qdmi-driver.dylib +0 -0
  501. mqt/core/lib/libmqt-core-qdmi-na-device-gen.a +0 -0
  502. mqt/core/lib/libmqt-core-qdmi-na-device.3.3.2.dylib +0 -0
  503. mqt/core/lib/libmqt-core-qdmi-na-device.3.3.dylib +0 -0
  504. mqt/core/lib/libmqt-core-qdmi-na-device.dylib +0 -0
  505. mqt/core/lib/libmqt-core-zx.3.3.2.dylib +0 -0
  506. mqt/core/lib/libmqt-core-zx.3.3.dylib +0 -0
  507. mqt/core/lib/libmqt-core-zx.dylib +0 -0
  508. mqt/core/lib/libspdlog.a +0 -0
  509. mqt/core/lib/pkgconfig/spdlog.pc +13 -0
  510. mqt/core/na/__init__.py +12 -0
  511. mqt/core/na/fomac.cpython-314t-darwin.so +0 -0
  512. mqt/core/na/fomac.pyi +117 -0
  513. mqt/core/plugins/__init__.py +9 -0
  514. mqt/core/plugins/qiskit/__init__.py +19 -0
  515. mqt/core/plugins/qiskit/mqt_to_qiskit.py +420 -0
  516. mqt/core/plugins/qiskit/qiskit_to_mqt.py +562 -0
  517. mqt/core/py.typed +2 -0
  518. mqt/core/share/cmake/mqt-core/AddMQTPythonBinding.cmake +55 -0
  519. mqt/core/share/cmake/mqt-core/Cache.cmake +33 -0
  520. mqt/core/share/cmake/mqt-core/FindGMP.cmake +103 -0
  521. mqt/core/share/cmake/mqt-core/PackageAddTest.cmake +46 -0
  522. mqt/core/share/cmake/mqt-core/PreventInSourceBuilds.cmake +25 -0
  523. mqt/core/share/cmake/mqt-core/StandardProjectSettings.cmake +87 -0
  524. mqt/core/share/cmake/mqt-core/mqt-core-config-version.cmake +85 -0
  525. mqt/core/share/cmake/mqt-core/mqt-core-config.cmake +52 -0
  526. mqt/core/share/cmake/mqt-core/mqt-core-targets-release.cmake +141 -0
  527. mqt/core/share/cmake/mqt-core/mqt-core-targets.cmake +446 -0
  528. mqt/core/share/cmake/nlohmann_json/nlohmann_jsonConfig.cmake +15 -0
  529. mqt/core/share/cmake/nlohmann_json/nlohmann_jsonConfigVersion.cmake +20 -0
  530. mqt/core/share/cmake/nlohmann_json/nlohmann_jsonTargets.cmake +109 -0
  531. mqt/core/share/cmake/qdmi/Cache.cmake +44 -0
  532. mqt/core/share/cmake/qdmi/PrefixHandling.cmake +78 -0
  533. mqt/core/share/cmake/qdmi/prefix_defs.txt +26 -0
  534. mqt/core/share/cmake/qdmi/qdmi-config-version.cmake +85 -0
  535. mqt/core/share/cmake/qdmi/qdmi-config.cmake +42 -0
  536. mqt/core/share/cmake/qdmi/qdmi-targets.cmake +129 -0
  537. mqt/core/share/cmake/spdlog/spdlogConfig.cmake +44 -0
  538. mqt/core/share/cmake/spdlog/spdlogConfigTargets-release.cmake +19 -0
  539. mqt/core/share/cmake/spdlog/spdlogConfigTargets.cmake +118 -0
  540. mqt/core/share/cmake/spdlog/spdlogConfigVersion.cmake +65 -0
  541. mqt/core/share/pkgconfig/nlohmann_json.pc +7 -0
  542. mqt_core-3.3.2.dist-info/METADATA +210 -0
  543. mqt_core-3.3.2.dist-info/RECORD +546 -0
  544. mqt_core-3.3.2.dist-info/WHEEL +6 -0
  545. mqt_core-3.3.2.dist-info/entry_points.txt +4 -0
  546. mqt_core-3.3.2.dist-info/licenses/LICENSE.md +22 -0
@@ -0,0 +1,3068 @@
1
+ // __ _____ _____ _____
2
+ // __| | __| | | | JSON for Modern C++
3
+ // | | |__ | | | | | | version 3.12.0
4
+ // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5
+ //
6
+ // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7
+ // SPDX-License-Identifier: MIT
8
+
9
+ #pragma once
10
+
11
+ #include <algorithm> // generate_n
12
+ #include <array> // array
13
+ #include <cmath> // ldexp
14
+ #include <cstddef> // size_t
15
+ #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
16
+ #include <cstdio> // snprintf
17
+ #include <cstring> // memcpy
18
+ #include <iterator> // back_inserter
19
+ #include <limits> // numeric_limits
20
+ #include <string> // char_traits, string
21
+ #include <utility> // make_pair, move
22
+ #include <vector> // vector
23
+ #ifdef __cpp_lib_byteswap
24
+ #include <bit> //byteswap
25
+ #endif
26
+
27
+ #include <nlohmann/detail/exceptions.hpp>
28
+ #include <nlohmann/detail/input/input_adapters.hpp>
29
+ #include <nlohmann/detail/input/json_sax.hpp>
30
+ #include <nlohmann/detail/input/lexer.hpp>
31
+ #include <nlohmann/detail/macro_scope.hpp>
32
+ #include <nlohmann/detail/meta/is_sax.hpp>
33
+ #include <nlohmann/detail/meta/type_traits.hpp>
34
+ #include <nlohmann/detail/string_concat.hpp>
35
+ #include <nlohmann/detail/value_t.hpp>
36
+
37
+ NLOHMANN_JSON_NAMESPACE_BEGIN
38
+ namespace detail
39
+ {
40
+
41
+ /// how to treat CBOR tags
42
+ enum class cbor_tag_handler_t
43
+ {
44
+ error, ///< throw a parse_error exception in case of a tag
45
+ ignore, ///< ignore tags
46
+ store ///< store tags as binary type
47
+ };
48
+
49
+ /*!
50
+ @brief determine system byte order
51
+
52
+ @return true if and only if system's byte order is little endian
53
+
54
+ @note from https://stackoverflow.com/a/1001328/266378
55
+ */
56
+ static inline bool little_endianness(int num = 1) noexcept
57
+ {
58
+ return *reinterpret_cast<char*>(&num) == 1;
59
+ }
60
+
61
+ ///////////////////
62
+ // binary reader //
63
+ ///////////////////
64
+
65
+ /*!
66
+ @brief deserialization of CBOR, MessagePack, and UBJSON values
67
+ */
68
+ template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType, InputAdapterType>>
69
+ class binary_reader
70
+ {
71
+ using number_integer_t = typename BasicJsonType::number_integer_t;
72
+ using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
73
+ using number_float_t = typename BasicJsonType::number_float_t;
74
+ using string_t = typename BasicJsonType::string_t;
75
+ using binary_t = typename BasicJsonType::binary_t;
76
+ using json_sax_t = SAX;
77
+ using char_type = typename InputAdapterType::char_type;
78
+ using char_int_type = typename char_traits<char_type>::int_type;
79
+
80
+ public:
81
+ /*!
82
+ @brief create a binary reader
83
+
84
+ @param[in] adapter input adapter to read from
85
+ */
86
+ explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
87
+ {
88
+ (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
89
+ }
90
+
91
+ // make class move-only
92
+ binary_reader(const binary_reader&) = delete;
93
+ binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
94
+ binary_reader& operator=(const binary_reader&) = delete;
95
+ binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
96
+ ~binary_reader() = default;
97
+
98
+ /*!
99
+ @param[in] format the binary format to parse
100
+ @param[in] sax_ a SAX event processor
101
+ @param[in] strict whether to expect the input to be consumed completed
102
+ @param[in] tag_handler how to treat CBOR tags
103
+
104
+ @return whether parsing was successful
105
+ */
106
+ JSON_HEDLEY_NON_NULL(3)
107
+ bool sax_parse(const input_format_t format,
108
+ json_sax_t* sax_,
109
+ const bool strict = true,
110
+ const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
111
+ {
112
+ sax = sax_;
113
+ bool result = false;
114
+
115
+ switch (format)
116
+ {
117
+ case input_format_t::bson:
118
+ result = parse_bson_internal();
119
+ break;
120
+
121
+ case input_format_t::cbor:
122
+ result = parse_cbor_internal(true, tag_handler);
123
+ break;
124
+
125
+ case input_format_t::msgpack:
126
+ result = parse_msgpack_internal();
127
+ break;
128
+
129
+ case input_format_t::ubjson:
130
+ case input_format_t::bjdata:
131
+ result = parse_ubjson_internal();
132
+ break;
133
+
134
+ case input_format_t::json: // LCOV_EXCL_LINE
135
+ default: // LCOV_EXCL_LINE
136
+ JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
137
+ }
138
+
139
+ // strict mode: next byte must be EOF
140
+ if (result && strict)
141
+ {
142
+ if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
143
+ {
144
+ get_ignore_noop();
145
+ }
146
+ else
147
+ {
148
+ get();
149
+ }
150
+
151
+ if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
152
+ {
153
+ return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
154
+ exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
155
+ }
156
+ }
157
+
158
+ return result;
159
+ }
160
+
161
+ private:
162
+ //////////
163
+ // BSON //
164
+ //////////
165
+
166
+ /*!
167
+ @brief Reads in a BSON-object and passes it to the SAX-parser.
168
+ @return whether a valid BSON-value was passed to the SAX parser
169
+ */
170
+ bool parse_bson_internal()
171
+ {
172
+ std::int32_t document_size{};
173
+ get_number<std::int32_t, true>(input_format_t::bson, document_size);
174
+
175
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
176
+ {
177
+ return false;
178
+ }
179
+
180
+ if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
181
+ {
182
+ return false;
183
+ }
184
+
185
+ return sax->end_object();
186
+ }
187
+
188
+ /*!
189
+ @brief Parses a C-style string from the BSON input.
190
+ @param[in,out] result A reference to the string variable where the read
191
+ string is to be stored.
192
+ @return `true` if the \x00-byte indicating the end of the string was
193
+ encountered before the EOF; false` indicates an unexpected EOF.
194
+ */
195
+ bool get_bson_cstr(string_t& result)
196
+ {
197
+ auto out = std::back_inserter(result);
198
+ while (true)
199
+ {
200
+ get();
201
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
202
+ {
203
+ return false;
204
+ }
205
+ if (current == 0x00)
206
+ {
207
+ return true;
208
+ }
209
+ *out++ = static_cast<typename string_t::value_type>(current);
210
+ }
211
+ }
212
+
213
+ /*!
214
+ @brief Parses a zero-terminated string of length @a len from the BSON
215
+ input.
216
+ @param[in] len The length (including the zero-byte at the end) of the
217
+ string to be read.
218
+ @param[in,out] result A reference to the string variable where the read
219
+ string is to be stored.
220
+ @tparam NumberType The type of the length @a len
221
+ @pre len >= 1
222
+ @return `true` if the string was successfully parsed
223
+ */
224
+ template<typename NumberType>
225
+ bool get_bson_string(const NumberType len, string_t& result)
226
+ {
227
+ if (JSON_HEDLEY_UNLIKELY(len < 1))
228
+ {
229
+ auto last_token = get_token_string();
230
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
231
+ exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
232
+ }
233
+
234
+ return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
235
+ }
236
+
237
+ /*!
238
+ @brief Parses a byte array input of length @a len from the BSON input.
239
+ @param[in] len The length of the byte array to be read.
240
+ @param[in,out] result A reference to the binary variable where the read
241
+ array is to be stored.
242
+ @tparam NumberType The type of the length @a len
243
+ @pre len >= 0
244
+ @return `true` if the byte array was successfully parsed
245
+ */
246
+ template<typename NumberType>
247
+ bool get_bson_binary(const NumberType len, binary_t& result)
248
+ {
249
+ if (JSON_HEDLEY_UNLIKELY(len < 0))
250
+ {
251
+ auto last_token = get_token_string();
252
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
253
+ exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
254
+ }
255
+
256
+ // All BSON binary values have a subtype
257
+ std::uint8_t subtype{};
258
+ get_number<std::uint8_t>(input_format_t::bson, subtype);
259
+ result.set_subtype(subtype);
260
+
261
+ return get_binary(input_format_t::bson, len, result);
262
+ }
263
+
264
+ /*!
265
+ @brief Read a BSON document element of the given @a element_type.
266
+ @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
267
+ @param[in] element_type_parse_position The position in the input stream,
268
+ where the `element_type` was read.
269
+ @warning Not all BSON element types are supported yet. An unsupported
270
+ @a element_type will give rise to a parse_error.114:
271
+ Unsupported BSON record type 0x...
272
+ @return whether a valid BSON-object/array was passed to the SAX parser
273
+ */
274
+ bool parse_bson_element_internal(const char_int_type element_type,
275
+ const std::size_t element_type_parse_position)
276
+ {
277
+ switch (element_type)
278
+ {
279
+ case 0x01: // double
280
+ {
281
+ double number{};
282
+ return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
283
+ }
284
+
285
+ case 0x02: // string
286
+ {
287
+ std::int32_t len{};
288
+ string_t value;
289
+ return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
290
+ }
291
+
292
+ case 0x03: // object
293
+ {
294
+ return parse_bson_internal();
295
+ }
296
+
297
+ case 0x04: // array
298
+ {
299
+ return parse_bson_array();
300
+ }
301
+
302
+ case 0x05: // binary
303
+ {
304
+ std::int32_t len{};
305
+ binary_t value;
306
+ return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
307
+ }
308
+
309
+ case 0x08: // boolean
310
+ {
311
+ return sax->boolean(get() != 0);
312
+ }
313
+
314
+ case 0x0A: // null
315
+ {
316
+ return sax->null();
317
+ }
318
+
319
+ case 0x10: // int32
320
+ {
321
+ std::int32_t value{};
322
+ return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
323
+ }
324
+
325
+ case 0x12: // int64
326
+ {
327
+ std::int64_t value{};
328
+ return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
329
+ }
330
+
331
+ case 0x11: // uint64
332
+ {
333
+ std::uint64_t value{};
334
+ return get_number<std::uint64_t, true>(input_format_t::bson, value) && sax->number_unsigned(value);
335
+ }
336
+
337
+ default: // anything else not supported (yet)
338
+ {
339
+ std::array<char, 3> cr{{}};
340
+ static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
341
+ const std::string cr_str{cr.data()};
342
+ return sax->parse_error(element_type_parse_position, cr_str,
343
+ parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
344
+ }
345
+ }
346
+ }
347
+
348
+ /*!
349
+ @brief Read a BSON element list (as specified in the BSON-spec)
350
+
351
+ The same binary layout is used for objects and arrays, hence it must be
352
+ indicated with the argument @a is_array which one is expected
353
+ (true --> array, false --> object).
354
+
355
+ @param[in] is_array Determines if the element list being read is to be
356
+ treated as an object (@a is_array == false), or as an
357
+ array (@a is_array == true).
358
+ @return whether a valid BSON-object/array was passed to the SAX parser
359
+ */
360
+ bool parse_bson_element_list(const bool is_array)
361
+ {
362
+ string_t key;
363
+
364
+ while (auto element_type = get())
365
+ {
366
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
367
+ {
368
+ return false;
369
+ }
370
+
371
+ const std::size_t element_type_parse_position = chars_read;
372
+ if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
373
+ {
374
+ return false;
375
+ }
376
+
377
+ if (!is_array && !sax->key(key))
378
+ {
379
+ return false;
380
+ }
381
+
382
+ if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
383
+ {
384
+ return false;
385
+ }
386
+
387
+ // get_bson_cstr only appends
388
+ key.clear();
389
+ }
390
+
391
+ return true;
392
+ }
393
+
394
+ /*!
395
+ @brief Reads an array from the BSON input and passes it to the SAX-parser.
396
+ @return whether a valid BSON-array was passed to the SAX parser
397
+ */
398
+ bool parse_bson_array()
399
+ {
400
+ std::int32_t document_size{};
401
+ get_number<std::int32_t, true>(input_format_t::bson, document_size);
402
+
403
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
404
+ {
405
+ return false;
406
+ }
407
+
408
+ if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
409
+ {
410
+ return false;
411
+ }
412
+
413
+ return sax->end_array();
414
+ }
415
+
416
+ //////////
417
+ // CBOR //
418
+ //////////
419
+
420
+ /*!
421
+ @param[in] get_char whether a new character should be retrieved from the
422
+ input (true) or whether the last read character should
423
+ be considered instead (false)
424
+ @param[in] tag_handler how CBOR tags should be treated
425
+
426
+ @return whether a valid CBOR value was passed to the SAX parser
427
+ */
428
+ bool parse_cbor_internal(const bool get_char,
429
+ const cbor_tag_handler_t tag_handler)
430
+ {
431
+ switch (get_char ? get() : current)
432
+ {
433
+ // EOF
434
+ case char_traits<char_type>::eof():
435
+ return unexpect_eof(input_format_t::cbor, "value");
436
+
437
+ // Integer 0x00..0x17 (0..23)
438
+ case 0x00:
439
+ case 0x01:
440
+ case 0x02:
441
+ case 0x03:
442
+ case 0x04:
443
+ case 0x05:
444
+ case 0x06:
445
+ case 0x07:
446
+ case 0x08:
447
+ case 0x09:
448
+ case 0x0A:
449
+ case 0x0B:
450
+ case 0x0C:
451
+ case 0x0D:
452
+ case 0x0E:
453
+ case 0x0F:
454
+ case 0x10:
455
+ case 0x11:
456
+ case 0x12:
457
+ case 0x13:
458
+ case 0x14:
459
+ case 0x15:
460
+ case 0x16:
461
+ case 0x17:
462
+ return sax->number_unsigned(static_cast<number_unsigned_t>(current));
463
+
464
+ case 0x18: // Unsigned integer (one-byte uint8_t follows)
465
+ {
466
+ std::uint8_t number{};
467
+ return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
468
+ }
469
+
470
+ case 0x19: // Unsigned integer (two-byte uint16_t follows)
471
+ {
472
+ std::uint16_t number{};
473
+ return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
474
+ }
475
+
476
+ case 0x1A: // Unsigned integer (four-byte uint32_t follows)
477
+ {
478
+ std::uint32_t number{};
479
+ return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
480
+ }
481
+
482
+ case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
483
+ {
484
+ std::uint64_t number{};
485
+ return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
486
+ }
487
+
488
+ // Negative integer -1-0x00..-1-0x17 (-1..-24)
489
+ case 0x20:
490
+ case 0x21:
491
+ case 0x22:
492
+ case 0x23:
493
+ case 0x24:
494
+ case 0x25:
495
+ case 0x26:
496
+ case 0x27:
497
+ case 0x28:
498
+ case 0x29:
499
+ case 0x2A:
500
+ case 0x2B:
501
+ case 0x2C:
502
+ case 0x2D:
503
+ case 0x2E:
504
+ case 0x2F:
505
+ case 0x30:
506
+ case 0x31:
507
+ case 0x32:
508
+ case 0x33:
509
+ case 0x34:
510
+ case 0x35:
511
+ case 0x36:
512
+ case 0x37:
513
+ return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
514
+
515
+ case 0x38: // Negative integer (one-byte uint8_t follows)
516
+ {
517
+ std::uint8_t number{};
518
+ return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
519
+ }
520
+
521
+ case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
522
+ {
523
+ std::uint16_t number{};
524
+ return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
525
+ }
526
+
527
+ case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
528
+ {
529
+ std::uint32_t number{};
530
+ return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
531
+ }
532
+
533
+ case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
534
+ {
535
+ std::uint64_t number{};
536
+ return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
537
+ - static_cast<number_integer_t>(number));
538
+ }
539
+
540
+ // Binary data (0x00..0x17 bytes follow)
541
+ case 0x40:
542
+ case 0x41:
543
+ case 0x42:
544
+ case 0x43:
545
+ case 0x44:
546
+ case 0x45:
547
+ case 0x46:
548
+ case 0x47:
549
+ case 0x48:
550
+ case 0x49:
551
+ case 0x4A:
552
+ case 0x4B:
553
+ case 0x4C:
554
+ case 0x4D:
555
+ case 0x4E:
556
+ case 0x4F:
557
+ case 0x50:
558
+ case 0x51:
559
+ case 0x52:
560
+ case 0x53:
561
+ case 0x54:
562
+ case 0x55:
563
+ case 0x56:
564
+ case 0x57:
565
+ case 0x58: // Binary data (one-byte uint8_t for n follows)
566
+ case 0x59: // Binary data (two-byte uint16_t for n follow)
567
+ case 0x5A: // Binary data (four-byte uint32_t for n follow)
568
+ case 0x5B: // Binary data (eight-byte uint64_t for n follow)
569
+ case 0x5F: // Binary data (indefinite length)
570
+ {
571
+ binary_t b;
572
+ return get_cbor_binary(b) && sax->binary(b);
573
+ }
574
+
575
+ // UTF-8 string (0x00..0x17 bytes follow)
576
+ case 0x60:
577
+ case 0x61:
578
+ case 0x62:
579
+ case 0x63:
580
+ case 0x64:
581
+ case 0x65:
582
+ case 0x66:
583
+ case 0x67:
584
+ case 0x68:
585
+ case 0x69:
586
+ case 0x6A:
587
+ case 0x6B:
588
+ case 0x6C:
589
+ case 0x6D:
590
+ case 0x6E:
591
+ case 0x6F:
592
+ case 0x70:
593
+ case 0x71:
594
+ case 0x72:
595
+ case 0x73:
596
+ case 0x74:
597
+ case 0x75:
598
+ case 0x76:
599
+ case 0x77:
600
+ case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
601
+ case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
602
+ case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
603
+ case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
604
+ case 0x7F: // UTF-8 string (indefinite length)
605
+ {
606
+ string_t s;
607
+ return get_cbor_string(s) && sax->string(s);
608
+ }
609
+
610
+ // array (0x00..0x17 data items follow)
611
+ case 0x80:
612
+ case 0x81:
613
+ case 0x82:
614
+ case 0x83:
615
+ case 0x84:
616
+ case 0x85:
617
+ case 0x86:
618
+ case 0x87:
619
+ case 0x88:
620
+ case 0x89:
621
+ case 0x8A:
622
+ case 0x8B:
623
+ case 0x8C:
624
+ case 0x8D:
625
+ case 0x8E:
626
+ case 0x8F:
627
+ case 0x90:
628
+ case 0x91:
629
+ case 0x92:
630
+ case 0x93:
631
+ case 0x94:
632
+ case 0x95:
633
+ case 0x96:
634
+ case 0x97:
635
+ return get_cbor_array(
636
+ conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
637
+
638
+ case 0x98: // array (one-byte uint8_t for n follows)
639
+ {
640
+ std::uint8_t len{};
641
+ return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
642
+ }
643
+
644
+ case 0x99: // array (two-byte uint16_t for n follow)
645
+ {
646
+ std::uint16_t len{};
647
+ return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
648
+ }
649
+
650
+ case 0x9A: // array (four-byte uint32_t for n follow)
651
+ {
652
+ std::uint32_t len{};
653
+ return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
654
+ }
655
+
656
+ case 0x9B: // array (eight-byte uint64_t for n follow)
657
+ {
658
+ std::uint64_t len{};
659
+ return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
660
+ }
661
+
662
+ case 0x9F: // array (indefinite length)
663
+ return get_cbor_array(detail::unknown_size(), tag_handler);
664
+
665
+ // map (0x00..0x17 pairs of data items follow)
666
+ case 0xA0:
667
+ case 0xA1:
668
+ case 0xA2:
669
+ case 0xA3:
670
+ case 0xA4:
671
+ case 0xA5:
672
+ case 0xA6:
673
+ case 0xA7:
674
+ case 0xA8:
675
+ case 0xA9:
676
+ case 0xAA:
677
+ case 0xAB:
678
+ case 0xAC:
679
+ case 0xAD:
680
+ case 0xAE:
681
+ case 0xAF:
682
+ case 0xB0:
683
+ case 0xB1:
684
+ case 0xB2:
685
+ case 0xB3:
686
+ case 0xB4:
687
+ case 0xB5:
688
+ case 0xB6:
689
+ case 0xB7:
690
+ return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
691
+
692
+ case 0xB8: // map (one-byte uint8_t for n follows)
693
+ {
694
+ std::uint8_t len{};
695
+ return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
696
+ }
697
+
698
+ case 0xB9: // map (two-byte uint16_t for n follow)
699
+ {
700
+ std::uint16_t len{};
701
+ return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
702
+ }
703
+
704
+ case 0xBA: // map (four-byte uint32_t for n follow)
705
+ {
706
+ std::uint32_t len{};
707
+ return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
708
+ }
709
+
710
+ case 0xBB: // map (eight-byte uint64_t for n follow)
711
+ {
712
+ std::uint64_t len{};
713
+ return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
714
+ }
715
+
716
+ case 0xBF: // map (indefinite length)
717
+ return get_cbor_object(detail::unknown_size(), tag_handler);
718
+
719
+ case 0xC6: // tagged item
720
+ case 0xC7:
721
+ case 0xC8:
722
+ case 0xC9:
723
+ case 0xCA:
724
+ case 0xCB:
725
+ case 0xCC:
726
+ case 0xCD:
727
+ case 0xCE:
728
+ case 0xCF:
729
+ case 0xD0:
730
+ case 0xD1:
731
+ case 0xD2:
732
+ case 0xD3:
733
+ case 0xD4:
734
+ case 0xD8: // tagged item (1 bytes follow)
735
+ case 0xD9: // tagged item (2 bytes follow)
736
+ case 0xDA: // tagged item (4 bytes follow)
737
+ case 0xDB: // tagged item (8 bytes follow)
738
+ {
739
+ switch (tag_handler)
740
+ {
741
+ case cbor_tag_handler_t::error:
742
+ {
743
+ auto last_token = get_token_string();
744
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
745
+ exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
746
+ }
747
+
748
+ case cbor_tag_handler_t::ignore:
749
+ {
750
+ // ignore binary subtype
751
+ switch (current)
752
+ {
753
+ case 0xD8:
754
+ {
755
+ std::uint8_t subtype_to_ignore{};
756
+ get_number(input_format_t::cbor, subtype_to_ignore);
757
+ break;
758
+ }
759
+ case 0xD9:
760
+ {
761
+ std::uint16_t subtype_to_ignore{};
762
+ get_number(input_format_t::cbor, subtype_to_ignore);
763
+ break;
764
+ }
765
+ case 0xDA:
766
+ {
767
+ std::uint32_t subtype_to_ignore{};
768
+ get_number(input_format_t::cbor, subtype_to_ignore);
769
+ break;
770
+ }
771
+ case 0xDB:
772
+ {
773
+ std::uint64_t subtype_to_ignore{};
774
+ get_number(input_format_t::cbor, subtype_to_ignore);
775
+ break;
776
+ }
777
+ default:
778
+ break;
779
+ }
780
+ return parse_cbor_internal(true, tag_handler);
781
+ }
782
+
783
+ case cbor_tag_handler_t::store:
784
+ {
785
+ binary_t b;
786
+ // use binary subtype and store in binary container
787
+ switch (current)
788
+ {
789
+ case 0xD8:
790
+ {
791
+ std::uint8_t subtype{};
792
+ get_number(input_format_t::cbor, subtype);
793
+ b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
794
+ break;
795
+ }
796
+ case 0xD9:
797
+ {
798
+ std::uint16_t subtype{};
799
+ get_number(input_format_t::cbor, subtype);
800
+ b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
801
+ break;
802
+ }
803
+ case 0xDA:
804
+ {
805
+ std::uint32_t subtype{};
806
+ get_number(input_format_t::cbor, subtype);
807
+ b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
808
+ break;
809
+ }
810
+ case 0xDB:
811
+ {
812
+ std::uint64_t subtype{};
813
+ get_number(input_format_t::cbor, subtype);
814
+ b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
815
+ break;
816
+ }
817
+ default:
818
+ return parse_cbor_internal(true, tag_handler);
819
+ }
820
+ get();
821
+ return get_cbor_binary(b) && sax->binary(b);
822
+ }
823
+
824
+ default: // LCOV_EXCL_LINE
825
+ JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
826
+ return false; // LCOV_EXCL_LINE
827
+ }
828
+ }
829
+
830
+ case 0xF4: // false
831
+ return sax->boolean(false);
832
+
833
+ case 0xF5: // true
834
+ return sax->boolean(true);
835
+
836
+ case 0xF6: // null
837
+ return sax->null();
838
+
839
+ case 0xF9: // Half-Precision Float (two-byte IEEE 754)
840
+ {
841
+ const auto byte1_raw = get();
842
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
843
+ {
844
+ return false;
845
+ }
846
+ const auto byte2_raw = get();
847
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
848
+ {
849
+ return false;
850
+ }
851
+
852
+ const auto byte1 = static_cast<unsigned char>(byte1_raw);
853
+ const auto byte2 = static_cast<unsigned char>(byte2_raw);
854
+
855
+ // code from RFC 7049, Appendix D, Figure 3:
856
+ // As half-precision floating-point numbers were only added
857
+ // to IEEE 754 in 2008, today's programming platforms often
858
+ // still only have limited support for them. It is very
859
+ // easy to include at least decoding support for them even
860
+ // without such support. An example of a small decoder for
861
+ // half-precision floating-point numbers in the C language
862
+ // is shown in Fig. 3.
863
+ const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
864
+ const double val = [&half]
865
+ {
866
+ const int exp = (half >> 10u) & 0x1Fu;
867
+ const unsigned int mant = half & 0x3FFu;
868
+ JSON_ASSERT(0 <= exp&& exp <= 32);
869
+ JSON_ASSERT(mant <= 1024);
870
+ switch (exp)
871
+ {
872
+ case 0:
873
+ return std::ldexp(mant, -24);
874
+ case 31:
875
+ return (mant == 0)
876
+ ? std::numeric_limits<double>::infinity()
877
+ : std::numeric_limits<double>::quiet_NaN();
878
+ default:
879
+ return std::ldexp(mant + 1024, exp - 25);
880
+ }
881
+ }();
882
+ return sax->number_float((half & 0x8000u) != 0
883
+ ? static_cast<number_float_t>(-val)
884
+ : static_cast<number_float_t>(val), "");
885
+ }
886
+
887
+ case 0xFA: // Single-Precision Float (four-byte IEEE 754)
888
+ {
889
+ float number{};
890
+ return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
891
+ }
892
+
893
+ case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
894
+ {
895
+ double number{};
896
+ return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
897
+ }
898
+
899
+ default: // anything else (0xFF is handled inside the other types)
900
+ {
901
+ auto last_token = get_token_string();
902
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
903
+ exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
904
+ }
905
+ }
906
+ }
907
+
908
+ /*!
909
+ @brief reads a CBOR string
910
+
911
+ This function first reads starting bytes to determine the expected
912
+ string length and then copies this number of bytes into a string.
913
+ Additionally, CBOR's strings with indefinite lengths are supported.
914
+
915
+ @param[out] result created string
916
+
917
+ @return whether string creation completed
918
+ */
919
+ bool get_cbor_string(string_t& result)
920
+ {
921
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
922
+ {
923
+ return false;
924
+ }
925
+
926
+ switch (current)
927
+ {
928
+ // UTF-8 string (0x00..0x17 bytes follow)
929
+ case 0x60:
930
+ case 0x61:
931
+ case 0x62:
932
+ case 0x63:
933
+ case 0x64:
934
+ case 0x65:
935
+ case 0x66:
936
+ case 0x67:
937
+ case 0x68:
938
+ case 0x69:
939
+ case 0x6A:
940
+ case 0x6B:
941
+ case 0x6C:
942
+ case 0x6D:
943
+ case 0x6E:
944
+ case 0x6F:
945
+ case 0x70:
946
+ case 0x71:
947
+ case 0x72:
948
+ case 0x73:
949
+ case 0x74:
950
+ case 0x75:
951
+ case 0x76:
952
+ case 0x77:
953
+ {
954
+ return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
955
+ }
956
+
957
+ case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
958
+ {
959
+ std::uint8_t len{};
960
+ return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
961
+ }
962
+
963
+ case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
964
+ {
965
+ std::uint16_t len{};
966
+ return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
967
+ }
968
+
969
+ case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
970
+ {
971
+ std::uint32_t len{};
972
+ return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
973
+ }
974
+
975
+ case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
976
+ {
977
+ std::uint64_t len{};
978
+ return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
979
+ }
980
+
981
+ case 0x7F: // UTF-8 string (indefinite length)
982
+ {
983
+ while (get() != 0xFF)
984
+ {
985
+ string_t chunk;
986
+ if (!get_cbor_string(chunk))
987
+ {
988
+ return false;
989
+ }
990
+ result.append(chunk);
991
+ }
992
+ return true;
993
+ }
994
+
995
+ default:
996
+ {
997
+ auto last_token = get_token_string();
998
+ return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
999
+ exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
1000
+ }
1001
+ }
1002
+ }
1003
+
1004
+ /*!
1005
+ @brief reads a CBOR byte array
1006
+
1007
+ This function first reads starting bytes to determine the expected
1008
+ byte array length and then copies this number of bytes into the byte array.
1009
+ Additionally, CBOR's byte arrays with indefinite lengths are supported.
1010
+
1011
+ @param[out] result created byte array
1012
+
1013
+ @return whether byte array creation completed
1014
+ */
1015
+ bool get_cbor_binary(binary_t& result)
1016
+ {
1017
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
1018
+ {
1019
+ return false;
1020
+ }
1021
+
1022
+ switch (current)
1023
+ {
1024
+ // Binary data (0x00..0x17 bytes follow)
1025
+ case 0x40:
1026
+ case 0x41:
1027
+ case 0x42:
1028
+ case 0x43:
1029
+ case 0x44:
1030
+ case 0x45:
1031
+ case 0x46:
1032
+ case 0x47:
1033
+ case 0x48:
1034
+ case 0x49:
1035
+ case 0x4A:
1036
+ case 0x4B:
1037
+ case 0x4C:
1038
+ case 0x4D:
1039
+ case 0x4E:
1040
+ case 0x4F:
1041
+ case 0x50:
1042
+ case 0x51:
1043
+ case 0x52:
1044
+ case 0x53:
1045
+ case 0x54:
1046
+ case 0x55:
1047
+ case 0x56:
1048
+ case 0x57:
1049
+ {
1050
+ return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
1051
+ }
1052
+
1053
+ case 0x58: // Binary data (one-byte uint8_t for n follows)
1054
+ {
1055
+ std::uint8_t len{};
1056
+ return get_number(input_format_t::cbor, len) &&
1057
+ get_binary(input_format_t::cbor, len, result);
1058
+ }
1059
+
1060
+ case 0x59: // Binary data (two-byte uint16_t for n follow)
1061
+ {
1062
+ std::uint16_t len{};
1063
+ return get_number(input_format_t::cbor, len) &&
1064
+ get_binary(input_format_t::cbor, len, result);
1065
+ }
1066
+
1067
+ case 0x5A: // Binary data (four-byte uint32_t for n follow)
1068
+ {
1069
+ std::uint32_t len{};
1070
+ return get_number(input_format_t::cbor, len) &&
1071
+ get_binary(input_format_t::cbor, len, result);
1072
+ }
1073
+
1074
+ case 0x5B: // Binary data (eight-byte uint64_t for n follow)
1075
+ {
1076
+ std::uint64_t len{};
1077
+ return get_number(input_format_t::cbor, len) &&
1078
+ get_binary(input_format_t::cbor, len, result);
1079
+ }
1080
+
1081
+ case 0x5F: // Binary data (indefinite length)
1082
+ {
1083
+ while (get() != 0xFF)
1084
+ {
1085
+ binary_t chunk;
1086
+ if (!get_cbor_binary(chunk))
1087
+ {
1088
+ return false;
1089
+ }
1090
+ result.insert(result.end(), chunk.begin(), chunk.end());
1091
+ }
1092
+ return true;
1093
+ }
1094
+
1095
+ default:
1096
+ {
1097
+ auto last_token = get_token_string();
1098
+ return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
1099
+ exception_message(input_format_t::cbor, concat("expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x", last_token), "binary"), nullptr));
1100
+ }
1101
+ }
1102
+ }
1103
+
1104
+ /*!
1105
+ @param[in] len the length of the array or detail::unknown_size() for an
1106
+ array of indefinite size
1107
+ @param[in] tag_handler how CBOR tags should be treated
1108
+ @return whether array creation completed
1109
+ */
1110
+ bool get_cbor_array(const std::size_t len,
1111
+ const cbor_tag_handler_t tag_handler)
1112
+ {
1113
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
1114
+ {
1115
+ return false;
1116
+ }
1117
+
1118
+ if (len != detail::unknown_size())
1119
+ {
1120
+ for (std::size_t i = 0; i < len; ++i)
1121
+ {
1122
+ if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
1123
+ {
1124
+ return false;
1125
+ }
1126
+ }
1127
+ }
1128
+ else
1129
+ {
1130
+ while (get() != 0xFF)
1131
+ {
1132
+ if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
1133
+ {
1134
+ return false;
1135
+ }
1136
+ }
1137
+ }
1138
+
1139
+ return sax->end_array();
1140
+ }
1141
+
1142
+ /*!
1143
+ @param[in] len the length of the object or detail::unknown_size() for an
1144
+ object of indefinite size
1145
+ @param[in] tag_handler how CBOR tags should be treated
1146
+ @return whether object creation completed
1147
+ */
1148
+ bool get_cbor_object(const std::size_t len,
1149
+ const cbor_tag_handler_t tag_handler)
1150
+ {
1151
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
1152
+ {
1153
+ return false;
1154
+ }
1155
+
1156
+ if (len != 0)
1157
+ {
1158
+ string_t key;
1159
+ if (len != detail::unknown_size())
1160
+ {
1161
+ for (std::size_t i = 0; i < len; ++i)
1162
+ {
1163
+ get();
1164
+ if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
1165
+ {
1166
+ return false;
1167
+ }
1168
+
1169
+ if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
1170
+ {
1171
+ return false;
1172
+ }
1173
+ key.clear();
1174
+ }
1175
+ }
1176
+ else
1177
+ {
1178
+ while (get() != 0xFF)
1179
+ {
1180
+ if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
1181
+ {
1182
+ return false;
1183
+ }
1184
+
1185
+ if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
1186
+ {
1187
+ return false;
1188
+ }
1189
+ key.clear();
1190
+ }
1191
+ }
1192
+ }
1193
+
1194
+ return sax->end_object();
1195
+ }
1196
+
1197
+ /////////////
1198
+ // MsgPack //
1199
+ /////////////
1200
+
1201
+ /*!
1202
+ @return whether a valid MessagePack value was passed to the SAX parser
1203
+ */
1204
+ bool parse_msgpack_internal()
1205
+ {
1206
+ switch (get())
1207
+ {
1208
+ // EOF
1209
+ case char_traits<char_type>::eof():
1210
+ return unexpect_eof(input_format_t::msgpack, "value");
1211
+
1212
+ // positive fixint
1213
+ case 0x00:
1214
+ case 0x01:
1215
+ case 0x02:
1216
+ case 0x03:
1217
+ case 0x04:
1218
+ case 0x05:
1219
+ case 0x06:
1220
+ case 0x07:
1221
+ case 0x08:
1222
+ case 0x09:
1223
+ case 0x0A:
1224
+ case 0x0B:
1225
+ case 0x0C:
1226
+ case 0x0D:
1227
+ case 0x0E:
1228
+ case 0x0F:
1229
+ case 0x10:
1230
+ case 0x11:
1231
+ case 0x12:
1232
+ case 0x13:
1233
+ case 0x14:
1234
+ case 0x15:
1235
+ case 0x16:
1236
+ case 0x17:
1237
+ case 0x18:
1238
+ case 0x19:
1239
+ case 0x1A:
1240
+ case 0x1B:
1241
+ case 0x1C:
1242
+ case 0x1D:
1243
+ case 0x1E:
1244
+ case 0x1F:
1245
+ case 0x20:
1246
+ case 0x21:
1247
+ case 0x22:
1248
+ case 0x23:
1249
+ case 0x24:
1250
+ case 0x25:
1251
+ case 0x26:
1252
+ case 0x27:
1253
+ case 0x28:
1254
+ case 0x29:
1255
+ case 0x2A:
1256
+ case 0x2B:
1257
+ case 0x2C:
1258
+ case 0x2D:
1259
+ case 0x2E:
1260
+ case 0x2F:
1261
+ case 0x30:
1262
+ case 0x31:
1263
+ case 0x32:
1264
+ case 0x33:
1265
+ case 0x34:
1266
+ case 0x35:
1267
+ case 0x36:
1268
+ case 0x37:
1269
+ case 0x38:
1270
+ case 0x39:
1271
+ case 0x3A:
1272
+ case 0x3B:
1273
+ case 0x3C:
1274
+ case 0x3D:
1275
+ case 0x3E:
1276
+ case 0x3F:
1277
+ case 0x40:
1278
+ case 0x41:
1279
+ case 0x42:
1280
+ case 0x43:
1281
+ case 0x44:
1282
+ case 0x45:
1283
+ case 0x46:
1284
+ case 0x47:
1285
+ case 0x48:
1286
+ case 0x49:
1287
+ case 0x4A:
1288
+ case 0x4B:
1289
+ case 0x4C:
1290
+ case 0x4D:
1291
+ case 0x4E:
1292
+ case 0x4F:
1293
+ case 0x50:
1294
+ case 0x51:
1295
+ case 0x52:
1296
+ case 0x53:
1297
+ case 0x54:
1298
+ case 0x55:
1299
+ case 0x56:
1300
+ case 0x57:
1301
+ case 0x58:
1302
+ case 0x59:
1303
+ case 0x5A:
1304
+ case 0x5B:
1305
+ case 0x5C:
1306
+ case 0x5D:
1307
+ case 0x5E:
1308
+ case 0x5F:
1309
+ case 0x60:
1310
+ case 0x61:
1311
+ case 0x62:
1312
+ case 0x63:
1313
+ case 0x64:
1314
+ case 0x65:
1315
+ case 0x66:
1316
+ case 0x67:
1317
+ case 0x68:
1318
+ case 0x69:
1319
+ case 0x6A:
1320
+ case 0x6B:
1321
+ case 0x6C:
1322
+ case 0x6D:
1323
+ case 0x6E:
1324
+ case 0x6F:
1325
+ case 0x70:
1326
+ case 0x71:
1327
+ case 0x72:
1328
+ case 0x73:
1329
+ case 0x74:
1330
+ case 0x75:
1331
+ case 0x76:
1332
+ case 0x77:
1333
+ case 0x78:
1334
+ case 0x79:
1335
+ case 0x7A:
1336
+ case 0x7B:
1337
+ case 0x7C:
1338
+ case 0x7D:
1339
+ case 0x7E:
1340
+ case 0x7F:
1341
+ return sax->number_unsigned(static_cast<number_unsigned_t>(current));
1342
+
1343
+ // fixmap
1344
+ case 0x80:
1345
+ case 0x81:
1346
+ case 0x82:
1347
+ case 0x83:
1348
+ case 0x84:
1349
+ case 0x85:
1350
+ case 0x86:
1351
+ case 0x87:
1352
+ case 0x88:
1353
+ case 0x89:
1354
+ case 0x8A:
1355
+ case 0x8B:
1356
+ case 0x8C:
1357
+ case 0x8D:
1358
+ case 0x8E:
1359
+ case 0x8F:
1360
+ return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
1361
+
1362
+ // fixarray
1363
+ case 0x90:
1364
+ case 0x91:
1365
+ case 0x92:
1366
+ case 0x93:
1367
+ case 0x94:
1368
+ case 0x95:
1369
+ case 0x96:
1370
+ case 0x97:
1371
+ case 0x98:
1372
+ case 0x99:
1373
+ case 0x9A:
1374
+ case 0x9B:
1375
+ case 0x9C:
1376
+ case 0x9D:
1377
+ case 0x9E:
1378
+ case 0x9F:
1379
+ return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
1380
+
1381
+ // fixstr
1382
+ case 0xA0:
1383
+ case 0xA1:
1384
+ case 0xA2:
1385
+ case 0xA3:
1386
+ case 0xA4:
1387
+ case 0xA5:
1388
+ case 0xA6:
1389
+ case 0xA7:
1390
+ case 0xA8:
1391
+ case 0xA9:
1392
+ case 0xAA:
1393
+ case 0xAB:
1394
+ case 0xAC:
1395
+ case 0xAD:
1396
+ case 0xAE:
1397
+ case 0xAF:
1398
+ case 0xB0:
1399
+ case 0xB1:
1400
+ case 0xB2:
1401
+ case 0xB3:
1402
+ case 0xB4:
1403
+ case 0xB5:
1404
+ case 0xB6:
1405
+ case 0xB7:
1406
+ case 0xB8:
1407
+ case 0xB9:
1408
+ case 0xBA:
1409
+ case 0xBB:
1410
+ case 0xBC:
1411
+ case 0xBD:
1412
+ case 0xBE:
1413
+ case 0xBF:
1414
+ case 0xD9: // str 8
1415
+ case 0xDA: // str 16
1416
+ case 0xDB: // str 32
1417
+ {
1418
+ string_t s;
1419
+ return get_msgpack_string(s) && sax->string(s);
1420
+ }
1421
+
1422
+ case 0xC0: // nil
1423
+ return sax->null();
1424
+
1425
+ case 0xC2: // false
1426
+ return sax->boolean(false);
1427
+
1428
+ case 0xC3: // true
1429
+ return sax->boolean(true);
1430
+
1431
+ case 0xC4: // bin 8
1432
+ case 0xC5: // bin 16
1433
+ case 0xC6: // bin 32
1434
+ case 0xC7: // ext 8
1435
+ case 0xC8: // ext 16
1436
+ case 0xC9: // ext 32
1437
+ case 0xD4: // fixext 1
1438
+ case 0xD5: // fixext 2
1439
+ case 0xD6: // fixext 4
1440
+ case 0xD7: // fixext 8
1441
+ case 0xD8: // fixext 16
1442
+ {
1443
+ binary_t b;
1444
+ return get_msgpack_binary(b) && sax->binary(b);
1445
+ }
1446
+
1447
+ case 0xCA: // float 32
1448
+ {
1449
+ float number{};
1450
+ return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
1451
+ }
1452
+
1453
+ case 0xCB: // float 64
1454
+ {
1455
+ double number{};
1456
+ return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
1457
+ }
1458
+
1459
+ case 0xCC: // uint 8
1460
+ {
1461
+ std::uint8_t number{};
1462
+ return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
1463
+ }
1464
+
1465
+ case 0xCD: // uint 16
1466
+ {
1467
+ std::uint16_t number{};
1468
+ return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
1469
+ }
1470
+
1471
+ case 0xCE: // uint 32
1472
+ {
1473
+ std::uint32_t number{};
1474
+ return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
1475
+ }
1476
+
1477
+ case 0xCF: // uint 64
1478
+ {
1479
+ std::uint64_t number{};
1480
+ return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
1481
+ }
1482
+
1483
+ case 0xD0: // int 8
1484
+ {
1485
+ std::int8_t number{};
1486
+ return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
1487
+ }
1488
+
1489
+ case 0xD1: // int 16
1490
+ {
1491
+ std::int16_t number{};
1492
+ return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
1493
+ }
1494
+
1495
+ case 0xD2: // int 32
1496
+ {
1497
+ std::int32_t number{};
1498
+ return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
1499
+ }
1500
+
1501
+ case 0xD3: // int 64
1502
+ {
1503
+ std::int64_t number{};
1504
+ return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
1505
+ }
1506
+
1507
+ case 0xDC: // array 16
1508
+ {
1509
+ std::uint16_t len{};
1510
+ return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
1511
+ }
1512
+
1513
+ case 0xDD: // array 32
1514
+ {
1515
+ std::uint32_t len{};
1516
+ return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
1517
+ }
1518
+
1519
+ case 0xDE: // map 16
1520
+ {
1521
+ std::uint16_t len{};
1522
+ return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
1523
+ }
1524
+
1525
+ case 0xDF: // map 32
1526
+ {
1527
+ std::uint32_t len{};
1528
+ return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
1529
+ }
1530
+
1531
+ // negative fixint
1532
+ case 0xE0:
1533
+ case 0xE1:
1534
+ case 0xE2:
1535
+ case 0xE3:
1536
+ case 0xE4:
1537
+ case 0xE5:
1538
+ case 0xE6:
1539
+ case 0xE7:
1540
+ case 0xE8:
1541
+ case 0xE9:
1542
+ case 0xEA:
1543
+ case 0xEB:
1544
+ case 0xEC:
1545
+ case 0xED:
1546
+ case 0xEE:
1547
+ case 0xEF:
1548
+ case 0xF0:
1549
+ case 0xF1:
1550
+ case 0xF2:
1551
+ case 0xF3:
1552
+ case 0xF4:
1553
+ case 0xF5:
1554
+ case 0xF6:
1555
+ case 0xF7:
1556
+ case 0xF8:
1557
+ case 0xF9:
1558
+ case 0xFA:
1559
+ case 0xFB:
1560
+ case 0xFC:
1561
+ case 0xFD:
1562
+ case 0xFE:
1563
+ case 0xFF:
1564
+ return sax->number_integer(static_cast<std::int8_t>(current));
1565
+
1566
+ default: // anything else
1567
+ {
1568
+ auto last_token = get_token_string();
1569
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
1570
+ exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
1571
+ }
1572
+ }
1573
+ }
1574
+
1575
+ /*!
1576
+ @brief reads a MessagePack string
1577
+
1578
+ This function first reads starting bytes to determine the expected
1579
+ string length and then copies this number of bytes into a string.
1580
+
1581
+ @param[out] result created string
1582
+
1583
+ @return whether string creation completed
1584
+ */
1585
+ bool get_msgpack_string(string_t& result)
1586
+ {
1587
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
1588
+ {
1589
+ return false;
1590
+ }
1591
+
1592
+ switch (current)
1593
+ {
1594
+ // fixstr
1595
+ case 0xA0:
1596
+ case 0xA1:
1597
+ case 0xA2:
1598
+ case 0xA3:
1599
+ case 0xA4:
1600
+ case 0xA5:
1601
+ case 0xA6:
1602
+ case 0xA7:
1603
+ case 0xA8:
1604
+ case 0xA9:
1605
+ case 0xAA:
1606
+ case 0xAB:
1607
+ case 0xAC:
1608
+ case 0xAD:
1609
+ case 0xAE:
1610
+ case 0xAF:
1611
+ case 0xB0:
1612
+ case 0xB1:
1613
+ case 0xB2:
1614
+ case 0xB3:
1615
+ case 0xB4:
1616
+ case 0xB5:
1617
+ case 0xB6:
1618
+ case 0xB7:
1619
+ case 0xB8:
1620
+ case 0xB9:
1621
+ case 0xBA:
1622
+ case 0xBB:
1623
+ case 0xBC:
1624
+ case 0xBD:
1625
+ case 0xBE:
1626
+ case 0xBF:
1627
+ {
1628
+ return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
1629
+ }
1630
+
1631
+ case 0xD9: // str 8
1632
+ {
1633
+ std::uint8_t len{};
1634
+ return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
1635
+ }
1636
+
1637
+ case 0xDA: // str 16
1638
+ {
1639
+ std::uint16_t len{};
1640
+ return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
1641
+ }
1642
+
1643
+ case 0xDB: // str 32
1644
+ {
1645
+ std::uint32_t len{};
1646
+ return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
1647
+ }
1648
+
1649
+ default:
1650
+ {
1651
+ auto last_token = get_token_string();
1652
+ return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
1653
+ exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
1654
+ }
1655
+ }
1656
+ }
1657
+
1658
+ /*!
1659
+ @brief reads a MessagePack byte array
1660
+
1661
+ This function first reads starting bytes to determine the expected
1662
+ byte array length and then copies this number of bytes into a byte array.
1663
+
1664
+ @param[out] result created byte array
1665
+
1666
+ @return whether byte array creation completed
1667
+ */
1668
+ bool get_msgpack_binary(binary_t& result)
1669
+ {
1670
+ // helper function to set the subtype
1671
+ auto assign_and_return_true = [&result](std::int8_t subtype)
1672
+ {
1673
+ result.set_subtype(static_cast<std::uint8_t>(subtype));
1674
+ return true;
1675
+ };
1676
+
1677
+ switch (current)
1678
+ {
1679
+ case 0xC4: // bin 8
1680
+ {
1681
+ std::uint8_t len{};
1682
+ return get_number(input_format_t::msgpack, len) &&
1683
+ get_binary(input_format_t::msgpack, len, result);
1684
+ }
1685
+
1686
+ case 0xC5: // bin 16
1687
+ {
1688
+ std::uint16_t len{};
1689
+ return get_number(input_format_t::msgpack, len) &&
1690
+ get_binary(input_format_t::msgpack, len, result);
1691
+ }
1692
+
1693
+ case 0xC6: // bin 32
1694
+ {
1695
+ std::uint32_t len{};
1696
+ return get_number(input_format_t::msgpack, len) &&
1697
+ get_binary(input_format_t::msgpack, len, result);
1698
+ }
1699
+
1700
+ case 0xC7: // ext 8
1701
+ {
1702
+ std::uint8_t len{};
1703
+ std::int8_t subtype{};
1704
+ return get_number(input_format_t::msgpack, len) &&
1705
+ get_number(input_format_t::msgpack, subtype) &&
1706
+ get_binary(input_format_t::msgpack, len, result) &&
1707
+ assign_and_return_true(subtype);
1708
+ }
1709
+
1710
+ case 0xC8: // ext 16
1711
+ {
1712
+ std::uint16_t len{};
1713
+ std::int8_t subtype{};
1714
+ return get_number(input_format_t::msgpack, len) &&
1715
+ get_number(input_format_t::msgpack, subtype) &&
1716
+ get_binary(input_format_t::msgpack, len, result) &&
1717
+ assign_and_return_true(subtype);
1718
+ }
1719
+
1720
+ case 0xC9: // ext 32
1721
+ {
1722
+ std::uint32_t len{};
1723
+ std::int8_t subtype{};
1724
+ return get_number(input_format_t::msgpack, len) &&
1725
+ get_number(input_format_t::msgpack, subtype) &&
1726
+ get_binary(input_format_t::msgpack, len, result) &&
1727
+ assign_and_return_true(subtype);
1728
+ }
1729
+
1730
+ case 0xD4: // fixext 1
1731
+ {
1732
+ std::int8_t subtype{};
1733
+ return get_number(input_format_t::msgpack, subtype) &&
1734
+ get_binary(input_format_t::msgpack, 1, result) &&
1735
+ assign_and_return_true(subtype);
1736
+ }
1737
+
1738
+ case 0xD5: // fixext 2
1739
+ {
1740
+ std::int8_t subtype{};
1741
+ return get_number(input_format_t::msgpack, subtype) &&
1742
+ get_binary(input_format_t::msgpack, 2, result) &&
1743
+ assign_and_return_true(subtype);
1744
+ }
1745
+
1746
+ case 0xD6: // fixext 4
1747
+ {
1748
+ std::int8_t subtype{};
1749
+ return get_number(input_format_t::msgpack, subtype) &&
1750
+ get_binary(input_format_t::msgpack, 4, result) &&
1751
+ assign_and_return_true(subtype);
1752
+ }
1753
+
1754
+ case 0xD7: // fixext 8
1755
+ {
1756
+ std::int8_t subtype{};
1757
+ return get_number(input_format_t::msgpack, subtype) &&
1758
+ get_binary(input_format_t::msgpack, 8, result) &&
1759
+ assign_and_return_true(subtype);
1760
+ }
1761
+
1762
+ case 0xD8: // fixext 16
1763
+ {
1764
+ std::int8_t subtype{};
1765
+ return get_number(input_format_t::msgpack, subtype) &&
1766
+ get_binary(input_format_t::msgpack, 16, result) &&
1767
+ assign_and_return_true(subtype);
1768
+ }
1769
+
1770
+ default: // LCOV_EXCL_LINE
1771
+ return false; // LCOV_EXCL_LINE
1772
+ }
1773
+ }
1774
+
1775
+ /*!
1776
+ @param[in] len the length of the array
1777
+ @return whether array creation completed
1778
+ */
1779
+ bool get_msgpack_array(const std::size_t len)
1780
+ {
1781
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
1782
+ {
1783
+ return false;
1784
+ }
1785
+
1786
+ for (std::size_t i = 0; i < len; ++i)
1787
+ {
1788
+ if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
1789
+ {
1790
+ return false;
1791
+ }
1792
+ }
1793
+
1794
+ return sax->end_array();
1795
+ }
1796
+
1797
+ /*!
1798
+ @param[in] len the length of the object
1799
+ @return whether object creation completed
1800
+ */
1801
+ bool get_msgpack_object(const std::size_t len)
1802
+ {
1803
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
1804
+ {
1805
+ return false;
1806
+ }
1807
+
1808
+ string_t key;
1809
+ for (std::size_t i = 0; i < len; ++i)
1810
+ {
1811
+ get();
1812
+ if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
1813
+ {
1814
+ return false;
1815
+ }
1816
+
1817
+ if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
1818
+ {
1819
+ return false;
1820
+ }
1821
+ key.clear();
1822
+ }
1823
+
1824
+ return sax->end_object();
1825
+ }
1826
+
1827
+ ////////////
1828
+ // UBJSON //
1829
+ ////////////
1830
+
1831
+ /*!
1832
+ @param[in] get_char whether a new character should be retrieved from the
1833
+ input (true, default) or whether the last read
1834
+ character should be considered instead
1835
+
1836
+ @return whether a valid UBJSON value was passed to the SAX parser
1837
+ */
1838
+ bool parse_ubjson_internal(const bool get_char = true)
1839
+ {
1840
+ return get_ubjson_value(get_char ? get_ignore_noop() : current);
1841
+ }
1842
+
1843
+ /*!
1844
+ @brief reads a UBJSON string
1845
+
1846
+ This function is either called after reading the 'S' byte explicitly
1847
+ indicating a string, or in case of an object key where the 'S' byte can be
1848
+ left out.
1849
+
1850
+ @param[out] result created string
1851
+ @param[in] get_char whether a new character should be retrieved from the
1852
+ input (true, default) or whether the last read
1853
+ character should be considered instead
1854
+
1855
+ @return whether string creation completed
1856
+ */
1857
+ bool get_ubjson_string(string_t& result, const bool get_char = true)
1858
+ {
1859
+ if (get_char)
1860
+ {
1861
+ get(); // TODO(niels): may we ignore N here?
1862
+ }
1863
+
1864
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
1865
+ {
1866
+ return false;
1867
+ }
1868
+
1869
+ switch (current)
1870
+ {
1871
+ case 'U':
1872
+ {
1873
+ std::uint8_t len{};
1874
+ return get_number(input_format, len) && get_string(input_format, len, result);
1875
+ }
1876
+
1877
+ case 'i':
1878
+ {
1879
+ std::int8_t len{};
1880
+ return get_number(input_format, len) && get_string(input_format, len, result);
1881
+ }
1882
+
1883
+ case 'I':
1884
+ {
1885
+ std::int16_t len{};
1886
+ return get_number(input_format, len) && get_string(input_format, len, result);
1887
+ }
1888
+
1889
+ case 'l':
1890
+ {
1891
+ std::int32_t len{};
1892
+ return get_number(input_format, len) && get_string(input_format, len, result);
1893
+ }
1894
+
1895
+ case 'L':
1896
+ {
1897
+ std::int64_t len{};
1898
+ return get_number(input_format, len) && get_string(input_format, len, result);
1899
+ }
1900
+
1901
+ case 'u':
1902
+ {
1903
+ if (input_format != input_format_t::bjdata)
1904
+ {
1905
+ break;
1906
+ }
1907
+ std::uint16_t len{};
1908
+ return get_number(input_format, len) && get_string(input_format, len, result);
1909
+ }
1910
+
1911
+ case 'm':
1912
+ {
1913
+ if (input_format != input_format_t::bjdata)
1914
+ {
1915
+ break;
1916
+ }
1917
+ std::uint32_t len{};
1918
+ return get_number(input_format, len) && get_string(input_format, len, result);
1919
+ }
1920
+
1921
+ case 'M':
1922
+ {
1923
+ if (input_format != input_format_t::bjdata)
1924
+ {
1925
+ break;
1926
+ }
1927
+ std::uint64_t len{};
1928
+ return get_number(input_format, len) && get_string(input_format, len, result);
1929
+ }
1930
+
1931
+ default:
1932
+ break;
1933
+ }
1934
+ auto last_token = get_token_string();
1935
+ std::string message;
1936
+
1937
+ if (input_format != input_format_t::bjdata)
1938
+ {
1939
+ message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
1940
+ }
1941
+ else
1942
+ {
1943
+ message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
1944
+ }
1945
+ return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
1946
+ }
1947
+
1948
+ /*!
1949
+ @param[out] dim an integer vector storing the ND array dimensions
1950
+ @return whether reading ND array size vector is successful
1951
+ */
1952
+ bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
1953
+ {
1954
+ std::pair<std::size_t, char_int_type> size_and_type;
1955
+ size_t dimlen = 0;
1956
+ bool no_ndarray = true;
1957
+
1958
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
1959
+ {
1960
+ return false;
1961
+ }
1962
+
1963
+ if (size_and_type.first != npos)
1964
+ {
1965
+ if (size_and_type.second != 0)
1966
+ {
1967
+ if (size_and_type.second != 'N')
1968
+ {
1969
+ for (std::size_t i = 0; i < size_and_type.first; ++i)
1970
+ {
1971
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
1972
+ {
1973
+ return false;
1974
+ }
1975
+ dim.push_back(dimlen);
1976
+ }
1977
+ }
1978
+ }
1979
+ else
1980
+ {
1981
+ for (std::size_t i = 0; i < size_and_type.first; ++i)
1982
+ {
1983
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
1984
+ {
1985
+ return false;
1986
+ }
1987
+ dim.push_back(dimlen);
1988
+ }
1989
+ }
1990
+ }
1991
+ else
1992
+ {
1993
+ while (current != ']')
1994
+ {
1995
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
1996
+ {
1997
+ return false;
1998
+ }
1999
+ dim.push_back(dimlen);
2000
+ get_ignore_noop();
2001
+ }
2002
+ }
2003
+ return true;
2004
+ }
2005
+
2006
+ /*!
2007
+ @param[out] result determined size
2008
+ @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
2009
+ or ndarray dimension is not allowed; `false` means ndarray
2010
+ is allowed; for output, `true` means an ndarray is found;
2011
+ is_ndarray can only return `true` when its initial value
2012
+ is `false`
2013
+ @param[in] prefix type marker if already read, otherwise set to 0
2014
+
2015
+ @return whether size determination completed
2016
+ */
2017
+ bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
2018
+ {
2019
+ if (prefix == 0)
2020
+ {
2021
+ prefix = get_ignore_noop();
2022
+ }
2023
+
2024
+ switch (prefix)
2025
+ {
2026
+ case 'U':
2027
+ {
2028
+ std::uint8_t number{};
2029
+ if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
2030
+ {
2031
+ return false;
2032
+ }
2033
+ result = static_cast<std::size_t>(number);
2034
+ return true;
2035
+ }
2036
+
2037
+ case 'i':
2038
+ {
2039
+ std::int8_t number{};
2040
+ if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
2041
+ {
2042
+ return false;
2043
+ }
2044
+ if (number < 0)
2045
+ {
2046
+ return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
2047
+ exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
2048
+ }
2049
+ result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
2050
+ return true;
2051
+ }
2052
+
2053
+ case 'I':
2054
+ {
2055
+ std::int16_t number{};
2056
+ if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
2057
+ {
2058
+ return false;
2059
+ }
2060
+ if (number < 0)
2061
+ {
2062
+ return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
2063
+ exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
2064
+ }
2065
+ result = static_cast<std::size_t>(number);
2066
+ return true;
2067
+ }
2068
+
2069
+ case 'l':
2070
+ {
2071
+ std::int32_t number{};
2072
+ if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
2073
+ {
2074
+ return false;
2075
+ }
2076
+ if (number < 0)
2077
+ {
2078
+ return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
2079
+ exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
2080
+ }
2081
+ result = static_cast<std::size_t>(number);
2082
+ return true;
2083
+ }
2084
+
2085
+ case 'L':
2086
+ {
2087
+ std::int64_t number{};
2088
+ if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
2089
+ {
2090
+ return false;
2091
+ }
2092
+ if (number < 0)
2093
+ {
2094
+ return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
2095
+ exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
2096
+ }
2097
+ if (!value_in_range_of<std::size_t>(number))
2098
+ {
2099
+ return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
2100
+ exception_message(input_format, "integer value overflow", "size"), nullptr));
2101
+ }
2102
+ result = static_cast<std::size_t>(number);
2103
+ return true;
2104
+ }
2105
+
2106
+ case 'u':
2107
+ {
2108
+ if (input_format != input_format_t::bjdata)
2109
+ {
2110
+ break;
2111
+ }
2112
+ std::uint16_t number{};
2113
+ if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
2114
+ {
2115
+ return false;
2116
+ }
2117
+ result = static_cast<std::size_t>(number);
2118
+ return true;
2119
+ }
2120
+
2121
+ case 'm':
2122
+ {
2123
+ if (input_format != input_format_t::bjdata)
2124
+ {
2125
+ break;
2126
+ }
2127
+ std::uint32_t number{};
2128
+ if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
2129
+ {
2130
+ return false;
2131
+ }
2132
+ result = conditional_static_cast<std::size_t>(number);
2133
+ return true;
2134
+ }
2135
+
2136
+ case 'M':
2137
+ {
2138
+ if (input_format != input_format_t::bjdata)
2139
+ {
2140
+ break;
2141
+ }
2142
+ std::uint64_t number{};
2143
+ if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
2144
+ {
2145
+ return false;
2146
+ }
2147
+ if (!value_in_range_of<std::size_t>(number))
2148
+ {
2149
+ return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
2150
+ exception_message(input_format, "integer value overflow", "size"), nullptr));
2151
+ }
2152
+ result = detail::conditional_static_cast<std::size_t>(number);
2153
+ return true;
2154
+ }
2155
+
2156
+ case '[':
2157
+ {
2158
+ if (input_format != input_format_t::bjdata)
2159
+ {
2160
+ break;
2161
+ }
2162
+ if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
2163
+ {
2164
+ return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimensional vector is not allowed", "size"), nullptr));
2165
+ }
2166
+ std::vector<size_t> dim;
2167
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
2168
+ {
2169
+ return false;
2170
+ }
2171
+ if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
2172
+ {
2173
+ result = dim.at(dim.size() - 1);
2174
+ return true;
2175
+ }
2176
+ if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
2177
+ {
2178
+ for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
2179
+ {
2180
+ if ( i == 0 )
2181
+ {
2182
+ result = 0;
2183
+ return true;
2184
+ }
2185
+ }
2186
+
2187
+ string_t key = "_ArraySize_";
2188
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
2189
+ {
2190
+ return false;
2191
+ }
2192
+ result = 1;
2193
+ for (auto i : dim)
2194
+ {
2195
+ result *= i;
2196
+ if (result == 0 || result == npos) // because dim elements shall not have zeros, result = 0 means overflow happened; it also can't be npos as it is used to initialize size in get_ubjson_size_type()
2197
+ {
2198
+ return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408, exception_message(input_format, "excessive ndarray size caused overflow", "size"), nullptr));
2199
+ }
2200
+ if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
2201
+ {
2202
+ return false;
2203
+ }
2204
+ }
2205
+ is_ndarray = true;
2206
+ return sax->end_array();
2207
+ }
2208
+ result = 0;
2209
+ return true;
2210
+ }
2211
+
2212
+ default:
2213
+ break;
2214
+ }
2215
+ auto last_token = get_token_string();
2216
+ std::string message;
2217
+
2218
+ if (input_format != input_format_t::bjdata)
2219
+ {
2220
+ message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
2221
+ }
2222
+ else
2223
+ {
2224
+ message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
2225
+ }
2226
+ return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
2227
+ }
2228
+
2229
+ /*!
2230
+ @brief determine the type and size for a container
2231
+
2232
+ In the optimized UBJSON format, a type and a size can be provided to allow
2233
+ for a more compact representation.
2234
+
2235
+ @param[out] result pair of the size and the type
2236
+ @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
2237
+
2238
+ @return whether pair creation completed
2239
+ */
2240
+ bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
2241
+ {
2242
+ result.first = npos; // size
2243
+ result.second = 0; // type
2244
+ bool is_ndarray = false;
2245
+
2246
+ get_ignore_noop();
2247
+
2248
+ if (current == '$')
2249
+ {
2250
+ result.second = get(); // must not ignore 'N', because 'N' maybe the type
2251
+ if (input_format == input_format_t::bjdata
2252
+ && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
2253
+ {
2254
+ auto last_token = get_token_string();
2255
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
2256
+ exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
2257
+ }
2258
+
2259
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
2260
+ {
2261
+ return false;
2262
+ }
2263
+
2264
+ get_ignore_noop();
2265
+ if (JSON_HEDLEY_UNLIKELY(current != '#'))
2266
+ {
2267
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
2268
+ {
2269
+ return false;
2270
+ }
2271
+ auto last_token = get_token_string();
2272
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
2273
+ exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
2274
+ }
2275
+
2276
+ const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
2277
+ if (input_format == input_format_t::bjdata && is_ndarray)
2278
+ {
2279
+ if (inside_ndarray)
2280
+ {
2281
+ return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
2282
+ exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
2283
+ }
2284
+ result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
2285
+ }
2286
+ return is_error;
2287
+ }
2288
+
2289
+ if (current == '#')
2290
+ {
2291
+ const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
2292
+ if (input_format == input_format_t::bjdata && is_ndarray)
2293
+ {
2294
+ return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
2295
+ exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
2296
+ }
2297
+ return is_error;
2298
+ }
2299
+
2300
+ return true;
2301
+ }
2302
+
2303
+ /*!
2304
+ @param prefix the previously read or set type prefix
2305
+ @return whether value creation completed
2306
+ */
2307
+ bool get_ubjson_value(const char_int_type prefix)
2308
+ {
2309
+ switch (prefix)
2310
+ {
2311
+ case char_traits<char_type>::eof(): // EOF
2312
+ return unexpect_eof(input_format, "value");
2313
+
2314
+ case 'T': // true
2315
+ return sax->boolean(true);
2316
+ case 'F': // false
2317
+ return sax->boolean(false);
2318
+
2319
+ case 'Z': // null
2320
+ return sax->null();
2321
+
2322
+ case 'B': // byte
2323
+ {
2324
+ if (input_format != input_format_t::bjdata)
2325
+ {
2326
+ break;
2327
+ }
2328
+ std::uint8_t number{};
2329
+ return get_number(input_format, number) && sax->number_unsigned(number);
2330
+ }
2331
+
2332
+ case 'U':
2333
+ {
2334
+ std::uint8_t number{};
2335
+ return get_number(input_format, number) && sax->number_unsigned(number);
2336
+ }
2337
+
2338
+ case 'i':
2339
+ {
2340
+ std::int8_t number{};
2341
+ return get_number(input_format, number) && sax->number_integer(number);
2342
+ }
2343
+
2344
+ case 'I':
2345
+ {
2346
+ std::int16_t number{};
2347
+ return get_number(input_format, number) && sax->number_integer(number);
2348
+ }
2349
+
2350
+ case 'l':
2351
+ {
2352
+ std::int32_t number{};
2353
+ return get_number(input_format, number) && sax->number_integer(number);
2354
+ }
2355
+
2356
+ case 'L':
2357
+ {
2358
+ std::int64_t number{};
2359
+ return get_number(input_format, number) && sax->number_integer(number);
2360
+ }
2361
+
2362
+ case 'u':
2363
+ {
2364
+ if (input_format != input_format_t::bjdata)
2365
+ {
2366
+ break;
2367
+ }
2368
+ std::uint16_t number{};
2369
+ return get_number(input_format, number) && sax->number_unsigned(number);
2370
+ }
2371
+
2372
+ case 'm':
2373
+ {
2374
+ if (input_format != input_format_t::bjdata)
2375
+ {
2376
+ break;
2377
+ }
2378
+ std::uint32_t number{};
2379
+ return get_number(input_format, number) && sax->number_unsigned(number);
2380
+ }
2381
+
2382
+ case 'M':
2383
+ {
2384
+ if (input_format != input_format_t::bjdata)
2385
+ {
2386
+ break;
2387
+ }
2388
+ std::uint64_t number{};
2389
+ return get_number(input_format, number) && sax->number_unsigned(number);
2390
+ }
2391
+
2392
+ case 'h':
2393
+ {
2394
+ if (input_format != input_format_t::bjdata)
2395
+ {
2396
+ break;
2397
+ }
2398
+ const auto byte1_raw = get();
2399
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
2400
+ {
2401
+ return false;
2402
+ }
2403
+ const auto byte2_raw = get();
2404
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
2405
+ {
2406
+ return false;
2407
+ }
2408
+
2409
+ const auto byte1 = static_cast<unsigned char>(byte1_raw);
2410
+ const auto byte2 = static_cast<unsigned char>(byte2_raw);
2411
+
2412
+ // code from RFC 7049, Appendix D, Figure 3:
2413
+ // As half-precision floating-point numbers were only added
2414
+ // to IEEE 754 in 2008, today's programming platforms often
2415
+ // still only have limited support for them. It is very
2416
+ // easy to include at least decoding support for them even
2417
+ // without such support. An example of a small decoder for
2418
+ // half-precision floating-point numbers in the C language
2419
+ // is shown in Fig. 3.
2420
+ const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
2421
+ const double val = [&half]
2422
+ {
2423
+ const int exp = (half >> 10u) & 0x1Fu;
2424
+ const unsigned int mant = half & 0x3FFu;
2425
+ JSON_ASSERT(0 <= exp&& exp <= 32);
2426
+ JSON_ASSERT(mant <= 1024);
2427
+ switch (exp)
2428
+ {
2429
+ case 0:
2430
+ return std::ldexp(mant, -24);
2431
+ case 31:
2432
+ return (mant == 0)
2433
+ ? std::numeric_limits<double>::infinity()
2434
+ : std::numeric_limits<double>::quiet_NaN();
2435
+ default:
2436
+ return std::ldexp(mant + 1024, exp - 25);
2437
+ }
2438
+ }();
2439
+ return sax->number_float((half & 0x8000u) != 0
2440
+ ? static_cast<number_float_t>(-val)
2441
+ : static_cast<number_float_t>(val), "");
2442
+ }
2443
+
2444
+ case 'd':
2445
+ {
2446
+ float number{};
2447
+ return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
2448
+ }
2449
+
2450
+ case 'D':
2451
+ {
2452
+ double number{};
2453
+ return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
2454
+ }
2455
+
2456
+ case 'H':
2457
+ {
2458
+ return get_ubjson_high_precision_number();
2459
+ }
2460
+
2461
+ case 'C': // char
2462
+ {
2463
+ get();
2464
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
2465
+ {
2466
+ return false;
2467
+ }
2468
+ if (JSON_HEDLEY_UNLIKELY(current > 127))
2469
+ {
2470
+ auto last_token = get_token_string();
2471
+ return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
2472
+ exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
2473
+ }
2474
+ string_t s(1, static_cast<typename string_t::value_type>(current));
2475
+ return sax->string(s);
2476
+ }
2477
+
2478
+ case 'S': // string
2479
+ {
2480
+ string_t s;
2481
+ return get_ubjson_string(s) && sax->string(s);
2482
+ }
2483
+
2484
+ case '[': // array
2485
+ return get_ubjson_array();
2486
+
2487
+ case '{': // object
2488
+ return get_ubjson_object();
2489
+
2490
+ default: // anything else
2491
+ break;
2492
+ }
2493
+ auto last_token = get_token_string();
2494
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), nullptr));
2495
+ }
2496
+
2497
+ /*!
2498
+ @return whether array creation completed
2499
+ */
2500
+ bool get_ubjson_array()
2501
+ {
2502
+ std::pair<std::size_t, char_int_type> size_and_type;
2503
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
2504
+ {
2505
+ return false;
2506
+ }
2507
+
2508
+ // if bit-8 of size_and_type.second is set to 1, encode bjdata ndarray as an object in JData annotated array format (https://github.com/NeuroJSON/jdata):
2509
+ // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
2510
+
2511
+ if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
2512
+ {
2513
+ size_and_type.second &= ~(static_cast<char_int_type>(1) << 8); // use bit 8 to indicate ndarray, here we remove the bit to restore the type marker
2514
+ auto it = std::lower_bound(bjd_types_map.begin(), bjd_types_map.end(), size_and_type.second, [](const bjd_type & p, char_int_type t)
2515
+ {
2516
+ return p.first < t;
2517
+ });
2518
+ string_t key = "_ArrayType_";
2519
+ if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
2520
+ {
2521
+ auto last_token = get_token_string();
2522
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
2523
+ exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
2524
+ }
2525
+
2526
+ string_t type = it->second; // sax->string() takes a reference
2527
+ if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
2528
+ {
2529
+ return false;
2530
+ }
2531
+
2532
+ if (size_and_type.second == 'C' || size_and_type.second == 'B')
2533
+ {
2534
+ size_and_type.second = 'U';
2535
+ }
2536
+
2537
+ key = "_ArrayData_";
2538
+ if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
2539
+ {
2540
+ return false;
2541
+ }
2542
+
2543
+ for (std::size_t i = 0; i < size_and_type.first; ++i)
2544
+ {
2545
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
2546
+ {
2547
+ return false;
2548
+ }
2549
+ }
2550
+
2551
+ return (sax->end_array() && sax->end_object());
2552
+ }
2553
+
2554
+ // If BJData type marker is 'B' decode as binary
2555
+ if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
2556
+ {
2557
+ binary_t result;
2558
+ return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
2559
+ }
2560
+
2561
+ if (size_and_type.first != npos)
2562
+ {
2563
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
2564
+ {
2565
+ return false;
2566
+ }
2567
+
2568
+ if (size_and_type.second != 0)
2569
+ {
2570
+ if (size_and_type.second != 'N')
2571
+ {
2572
+ for (std::size_t i = 0; i < size_and_type.first; ++i)
2573
+ {
2574
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
2575
+ {
2576
+ return false;
2577
+ }
2578
+ }
2579
+ }
2580
+ }
2581
+ else
2582
+ {
2583
+ for (std::size_t i = 0; i < size_and_type.first; ++i)
2584
+ {
2585
+ if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
2586
+ {
2587
+ return false;
2588
+ }
2589
+ }
2590
+ }
2591
+ }
2592
+ else
2593
+ {
2594
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
2595
+ {
2596
+ return false;
2597
+ }
2598
+
2599
+ while (current != ']')
2600
+ {
2601
+ if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
2602
+ {
2603
+ return false;
2604
+ }
2605
+ get_ignore_noop();
2606
+ }
2607
+ }
2608
+
2609
+ return sax->end_array();
2610
+ }
2611
+
2612
+ /*!
2613
+ @return whether object creation completed
2614
+ */
2615
+ bool get_ubjson_object()
2616
+ {
2617
+ std::pair<std::size_t, char_int_type> size_and_type;
2618
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
2619
+ {
2620
+ return false;
2621
+ }
2622
+
2623
+ // do not accept ND-array size in objects in BJData
2624
+ if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
2625
+ {
2626
+ auto last_token = get_token_string();
2627
+ return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
2628
+ exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
2629
+ }
2630
+
2631
+ string_t key;
2632
+ if (size_and_type.first != npos)
2633
+ {
2634
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
2635
+ {
2636
+ return false;
2637
+ }
2638
+
2639
+ if (size_and_type.second != 0)
2640
+ {
2641
+ for (std::size_t i = 0; i < size_and_type.first; ++i)
2642
+ {
2643
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
2644
+ {
2645
+ return false;
2646
+ }
2647
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
2648
+ {
2649
+ return false;
2650
+ }
2651
+ key.clear();
2652
+ }
2653
+ }
2654
+ else
2655
+ {
2656
+ for (std::size_t i = 0; i < size_and_type.first; ++i)
2657
+ {
2658
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
2659
+ {
2660
+ return false;
2661
+ }
2662
+ if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
2663
+ {
2664
+ return false;
2665
+ }
2666
+ key.clear();
2667
+ }
2668
+ }
2669
+ }
2670
+ else
2671
+ {
2672
+ if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
2673
+ {
2674
+ return false;
2675
+ }
2676
+
2677
+ while (current != '}')
2678
+ {
2679
+ if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
2680
+ {
2681
+ return false;
2682
+ }
2683
+ if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
2684
+ {
2685
+ return false;
2686
+ }
2687
+ get_ignore_noop();
2688
+ key.clear();
2689
+ }
2690
+ }
2691
+
2692
+ return sax->end_object();
2693
+ }
2694
+
2695
+ // Note, no reader for UBJSON binary types is implemented because they do
2696
+ // not exist
2697
+
2698
+ bool get_ubjson_high_precision_number()
2699
+ {
2700
+ // get size of following number string
2701
+ std::size_t size{};
2702
+ bool no_ndarray = true;
2703
+ auto res = get_ubjson_size_value(size, no_ndarray);
2704
+ if (JSON_HEDLEY_UNLIKELY(!res))
2705
+ {
2706
+ return res;
2707
+ }
2708
+
2709
+ // get number string
2710
+ std::vector<char> number_vector;
2711
+ for (std::size_t i = 0; i < size; ++i)
2712
+ {
2713
+ get();
2714
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
2715
+ {
2716
+ return false;
2717
+ }
2718
+ number_vector.push_back(static_cast<char>(current));
2719
+ }
2720
+
2721
+ // parse number string
2722
+ using ia_type = decltype(detail::input_adapter(number_vector));
2723
+ auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
2724
+ const auto result_number = number_lexer.scan();
2725
+ const auto number_string = number_lexer.get_token_string();
2726
+ const auto result_remainder = number_lexer.scan();
2727
+
2728
+ using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
2729
+
2730
+ if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
2731
+ {
2732
+ return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
2733
+ exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
2734
+ }
2735
+
2736
+ switch (result_number)
2737
+ {
2738
+ case token_type::value_integer:
2739
+ return sax->number_integer(number_lexer.get_number_integer());
2740
+ case token_type::value_unsigned:
2741
+ return sax->number_unsigned(number_lexer.get_number_unsigned());
2742
+ case token_type::value_float:
2743
+ return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
2744
+ case token_type::uninitialized:
2745
+ case token_type::literal_true:
2746
+ case token_type::literal_false:
2747
+ case token_type::literal_null:
2748
+ case token_type::value_string:
2749
+ case token_type::begin_array:
2750
+ case token_type::begin_object:
2751
+ case token_type::end_array:
2752
+ case token_type::end_object:
2753
+ case token_type::name_separator:
2754
+ case token_type::value_separator:
2755
+ case token_type::parse_error:
2756
+ case token_type::end_of_input:
2757
+ case token_type::literal_or_value:
2758
+ default:
2759
+ return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
2760
+ exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
2761
+ }
2762
+ }
2763
+
2764
+ ///////////////////////
2765
+ // Utility functions //
2766
+ ///////////////////////
2767
+
2768
+ /*!
2769
+ @brief get next character from the input
2770
+
2771
+ This function provides the interface to the used input adapter. It does
2772
+ not throw in case the input reached EOF, but returns a -'ve valued
2773
+ `char_traits<char_type>::eof()` in that case.
2774
+
2775
+ @return character read from the input
2776
+ */
2777
+ char_int_type get()
2778
+ {
2779
+ ++chars_read;
2780
+ return current = ia.get_character();
2781
+ }
2782
+
2783
+ /*!
2784
+ @brief get_to read into a primitive type
2785
+
2786
+ This function provides the interface to the used input adapter. It does
2787
+ not throw in case the input reached EOF, but returns false instead
2788
+
2789
+ @return bool, whether the read was successful
2790
+ */
2791
+ template<class T>
2792
+ bool get_to(T& dest, const input_format_t format, const char* context)
2793
+ {
2794
+ auto new_chars_read = ia.get_elements(&dest);
2795
+ chars_read += new_chars_read;
2796
+ if (JSON_HEDLEY_UNLIKELY(new_chars_read < sizeof(T)))
2797
+ {
2798
+ // in case of failure, advance position by 1 to report failing location
2799
+ ++chars_read;
2800
+ sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
2801
+ return false;
2802
+ }
2803
+ return true;
2804
+ }
2805
+
2806
+ /*!
2807
+ @return character read from the input after ignoring all 'N' entries
2808
+ */
2809
+ char_int_type get_ignore_noop()
2810
+ {
2811
+ do
2812
+ {
2813
+ get();
2814
+ }
2815
+ while (current == 'N');
2816
+
2817
+ return current;
2818
+ }
2819
+
2820
+ template<class NumberType>
2821
+ static void byte_swap(NumberType& number)
2822
+ {
2823
+ constexpr std::size_t sz = sizeof(number);
2824
+ #ifdef __cpp_lib_byteswap
2825
+ if constexpr (sz == 1)
2826
+ {
2827
+ return;
2828
+ }
2829
+ if constexpr(std::is_integral_v<NumberType>)
2830
+ {
2831
+ number = std::byteswap(number);
2832
+ return;
2833
+ }
2834
+ #endif
2835
+ auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
2836
+ for (std::size_t i = 0; i < sz / 2; ++i)
2837
+ {
2838
+ std::swap(ptr[i], ptr[sz - i - 1]);
2839
+ }
2840
+ }
2841
+
2842
+ /*
2843
+ @brief read a number from the input
2844
+
2845
+ @tparam NumberType the type of the number
2846
+ @param[in] format the current format (for diagnostics)
2847
+ @param[out] result number of type @a NumberType
2848
+
2849
+ @return whether conversion completed
2850
+
2851
+ @note This function needs to respect the system's endianness, because
2852
+ bytes in CBOR, MessagePack, and UBJSON are stored in network order
2853
+ (big endian) and therefore need reordering on little endian systems.
2854
+ On the other hand, BSON and BJData use little endian and should reorder
2855
+ on big endian systems.
2856
+ */
2857
+ template<typename NumberType, bool InputIsLittleEndian = false>
2858
+ bool get_number(const input_format_t format, NumberType& result)
2859
+ {
2860
+ // read in the original format
2861
+
2862
+ if (JSON_HEDLEY_UNLIKELY(!get_to(result, format, "number")))
2863
+ {
2864
+ return false;
2865
+ }
2866
+ if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
2867
+ {
2868
+ byte_swap(result);
2869
+ }
2870
+ return true;
2871
+ }
2872
+
2873
+ /*!
2874
+ @brief create a string by reading characters from the input
2875
+
2876
+ @tparam NumberType the type of the number
2877
+ @param[in] format the current format (for diagnostics)
2878
+ @param[in] len number of characters to read
2879
+ @param[out] result string created by reading @a len bytes
2880
+
2881
+ @return whether string creation completed
2882
+
2883
+ @note We can not reserve @a len bytes for the result, because @a len
2884
+ may be too large. Usually, @ref unexpect_eof() detects the end of
2885
+ the input before we run out of string memory.
2886
+ */
2887
+ template<typename NumberType>
2888
+ bool get_string(const input_format_t format,
2889
+ const NumberType len,
2890
+ string_t& result)
2891
+ {
2892
+ bool success = true;
2893
+ for (NumberType i = 0; i < len; i++)
2894
+ {
2895
+ get();
2896
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
2897
+ {
2898
+ success = false;
2899
+ break;
2900
+ }
2901
+ result.push_back(static_cast<typename string_t::value_type>(current));
2902
+ }
2903
+ return success;
2904
+ }
2905
+
2906
+ /*!
2907
+ @brief create a byte array by reading bytes from the input
2908
+
2909
+ @tparam NumberType the type of the number
2910
+ @param[in] format the current format (for diagnostics)
2911
+ @param[in] len number of bytes to read
2912
+ @param[out] result byte array created by reading @a len bytes
2913
+
2914
+ @return whether byte array creation completed
2915
+
2916
+ @note We can not reserve @a len bytes for the result, because @a len
2917
+ may be too large. Usually, @ref unexpect_eof() detects the end of
2918
+ the input before we run out of memory.
2919
+ */
2920
+ template<typename NumberType>
2921
+ bool get_binary(const input_format_t format,
2922
+ const NumberType len,
2923
+ binary_t& result)
2924
+ {
2925
+ bool success = true;
2926
+ for (NumberType i = 0; i < len; i++)
2927
+ {
2928
+ get();
2929
+ if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
2930
+ {
2931
+ success = false;
2932
+ break;
2933
+ }
2934
+ result.push_back(static_cast<std::uint8_t>(current));
2935
+ }
2936
+ return success;
2937
+ }
2938
+
2939
+ /*!
2940
+ @param[in] format the current format (for diagnostics)
2941
+ @param[in] context further context information (for diagnostics)
2942
+ @return whether the last read character is not EOF
2943
+ */
2944
+ JSON_HEDLEY_NON_NULL(3)
2945
+ bool unexpect_eof(const input_format_t format, const char* context) const
2946
+ {
2947
+ if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
2948
+ {
2949
+ return sax->parse_error(chars_read, "<end of file>",
2950
+ parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
2951
+ }
2952
+ return true;
2953
+ }
2954
+
2955
+ /*!
2956
+ @return a string representation of the last read byte
2957
+ */
2958
+ std::string get_token_string() const
2959
+ {
2960
+ std::array<char, 3> cr{{}};
2961
+ static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
2962
+ return std::string{cr.data()};
2963
+ }
2964
+
2965
+ /*!
2966
+ @param[in] format the current format
2967
+ @param[in] detail a detailed error message
2968
+ @param[in] context further context information
2969
+ @return a message string to use in the parse_error exceptions
2970
+ */
2971
+ std::string exception_message(const input_format_t format,
2972
+ const std::string& detail,
2973
+ const std::string& context) const
2974
+ {
2975
+ std::string error_msg = "syntax error while parsing ";
2976
+
2977
+ switch (format)
2978
+ {
2979
+ case input_format_t::cbor:
2980
+ error_msg += "CBOR";
2981
+ break;
2982
+
2983
+ case input_format_t::msgpack:
2984
+ error_msg += "MessagePack";
2985
+ break;
2986
+
2987
+ case input_format_t::ubjson:
2988
+ error_msg += "UBJSON";
2989
+ break;
2990
+
2991
+ case input_format_t::bson:
2992
+ error_msg += "BSON";
2993
+ break;
2994
+
2995
+ case input_format_t::bjdata:
2996
+ error_msg += "BJData";
2997
+ break;
2998
+
2999
+ case input_format_t::json: // LCOV_EXCL_LINE
3000
+ default: // LCOV_EXCL_LINE
3001
+ JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
3002
+ }
3003
+
3004
+ return concat(error_msg, ' ', context, ": ", detail);
3005
+ }
3006
+
3007
+ private:
3008
+ static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
3009
+
3010
+ /// input adapter
3011
+ InputAdapterType ia;
3012
+
3013
+ /// the current character
3014
+ char_int_type current = char_traits<char_type>::eof();
3015
+
3016
+ /// the number of characters read
3017
+ std::size_t chars_read = 0;
3018
+
3019
+ /// whether we can assume little endianness
3020
+ const bool is_little_endian = little_endianness();
3021
+
3022
+ /// input format
3023
+ const input_format_t input_format = input_format_t::json;
3024
+
3025
+ /// the SAX parser
3026
+ json_sax_t* sax = nullptr;
3027
+
3028
+ // excluded markers in bjdata optimized type
3029
+ #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
3030
+ make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
3031
+
3032
+ #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
3033
+ make_array<bjd_type>( \
3034
+ bjd_type{'B', "byte"}, \
3035
+ bjd_type{'C', "char"}, \
3036
+ bjd_type{'D', "double"}, \
3037
+ bjd_type{'I', "int16"}, \
3038
+ bjd_type{'L', "int64"}, \
3039
+ bjd_type{'M', "uint64"}, \
3040
+ bjd_type{'U', "uint8"}, \
3041
+ bjd_type{'d', "single"}, \
3042
+ bjd_type{'i', "int8"}, \
3043
+ bjd_type{'l', "int32"}, \
3044
+ bjd_type{'m', "uint32"}, \
3045
+ bjd_type{'u', "uint16"})
3046
+
3047
+ JSON_PRIVATE_UNLESS_TESTED:
3048
+ // lookup tables
3049
+ // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
3050
+ const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
3051
+ JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
3052
+
3053
+ using bjd_type = std::pair<char_int_type, string_t>;
3054
+ // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
3055
+ const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
3056
+ JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
3057
+
3058
+ #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
3059
+ #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
3060
+ };
3061
+
3062
+ #ifndef JSON_HAS_CPP_17
3063
+ template<typename BasicJsonType, typename InputAdapterType, typename SAX>
3064
+ constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
3065
+ #endif
3066
+
3067
+ } // namespace detail
3068
+ NLOHMANN_JSON_NAMESPACE_END