flexbuffers.h 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  1. /*
  2. * Copyright 2017 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FLATBUFFERS_FLEXBUFFERS_H_
  17. #define FLATBUFFERS_FLEXBUFFERS_H_
  18. #include <map>
  19. // Used to select STL variant.
  20. #include "flatbuffers/base.h"
  21. // We use the basic binary writing functions from the regular FlatBuffers.
  22. #include "flatbuffers/util.h"
  23. #ifdef _MSC_VER
  24. # include <intrin.h>
  25. #endif
  26. #if defined(_MSC_VER)
  27. # pragma warning(push)
  28. # pragma warning(disable : 4127) // C4127: conditional expression is constant
  29. #endif
  30. namespace flexbuffers {
  31. class Reference;
  32. class Map;
  33. // These are used in the lower 2 bits of a type field to determine the size of
  34. // the elements (and or size field) of the item pointed to (e.g. vector).
  35. enum BitWidth {
  36. BIT_WIDTH_8 = 0,
  37. BIT_WIDTH_16 = 1,
  38. BIT_WIDTH_32 = 2,
  39. BIT_WIDTH_64 = 3,
  40. };
  41. // These are used as the upper 6 bits of a type field to indicate the actual
  42. // type.
  43. enum Type {
  44. FBT_NULL = 0,
  45. FBT_INT = 1,
  46. FBT_UINT = 2,
  47. FBT_FLOAT = 3,
  48. // Types above stored inline, types below (except FBT_BOOL) store an offset.
  49. FBT_KEY = 4,
  50. FBT_STRING = 5,
  51. FBT_INDIRECT_INT = 6,
  52. FBT_INDIRECT_UINT = 7,
  53. FBT_INDIRECT_FLOAT = 8,
  54. FBT_MAP = 9,
  55. FBT_VECTOR = 10, // Untyped.
  56. FBT_VECTOR_INT = 11, // Typed any size (stores no type table).
  57. FBT_VECTOR_UINT = 12,
  58. FBT_VECTOR_FLOAT = 13,
  59. FBT_VECTOR_KEY = 14,
  60. // DEPRECATED, use FBT_VECTOR or FBT_VECTOR_KEY instead.
  61. // Read test.cpp/FlexBuffersDeprecatedTest() for details on why.
  62. FBT_VECTOR_STRING_DEPRECATED = 15,
  63. FBT_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field).
  64. FBT_VECTOR_UINT2 = 17,
  65. FBT_VECTOR_FLOAT2 = 18,
  66. FBT_VECTOR_INT3 = 19, // Typed triple (no type table, no size field).
  67. FBT_VECTOR_UINT3 = 20,
  68. FBT_VECTOR_FLOAT3 = 21,
  69. FBT_VECTOR_INT4 = 22, // Typed quad (no type table, no size field).
  70. FBT_VECTOR_UINT4 = 23,
  71. FBT_VECTOR_FLOAT4 = 24,
  72. FBT_BLOB = 25,
  73. FBT_BOOL = 26,
  74. FBT_VECTOR_BOOL =
  75. 36, // To Allow the same type of conversion of type to vector type
  76. FBT_MAX_TYPE = 37
  77. };
  78. inline bool IsInline(Type t) { return t <= FBT_FLOAT || t == FBT_BOOL; }
  79. inline bool IsTypedVectorElementType(Type t) {
  80. return (t >= FBT_INT && t <= FBT_STRING) || t == FBT_BOOL;
  81. }
  82. inline bool IsTypedVector(Type t) {
  83. return (t >= FBT_VECTOR_INT && t <= FBT_VECTOR_STRING_DEPRECATED) ||
  84. t == FBT_VECTOR_BOOL;
  85. }
  86. inline bool IsFixedTypedVector(Type t) {
  87. return t >= FBT_VECTOR_INT2 && t <= FBT_VECTOR_FLOAT4;
  88. }
  89. inline Type ToTypedVector(Type t, size_t fixed_len = 0) {
  90. FLATBUFFERS_ASSERT(IsTypedVectorElementType(t));
  91. switch (fixed_len) {
  92. case 0: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT);
  93. case 2: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT2);
  94. case 3: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT3);
  95. case 4: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT4);
  96. default: FLATBUFFERS_ASSERT(0); return FBT_NULL;
  97. }
  98. }
  99. inline Type ToTypedVectorElementType(Type t) {
  100. FLATBUFFERS_ASSERT(IsTypedVector(t));
  101. return static_cast<Type>(t - FBT_VECTOR_INT + FBT_INT);
  102. }
  103. inline Type ToFixedTypedVectorElementType(Type t, uint8_t *len) {
  104. FLATBUFFERS_ASSERT(IsFixedTypedVector(t));
  105. auto fixed_type = t - FBT_VECTOR_INT2;
  106. *len = static_cast<uint8_t>(fixed_type / 3 +
  107. 2); // 3 types each, starting from length 2.
  108. return static_cast<Type>(fixed_type % 3 + FBT_INT);
  109. }
  110. // TODO: implement proper support for 8/16bit floats, or decide not to
  111. // support them.
  112. typedef int16_t half;
  113. typedef int8_t quarter;
  114. // TODO: can we do this without conditionals using intrinsics or inline asm
  115. // on some platforms? Given branch prediction the method below should be
  116. // decently quick, but it is the most frequently executed function.
  117. // We could do an (unaligned) 64-bit read if we ifdef out the platforms for
  118. // which that doesn't work (or where we'd read into un-owned memory).
  119. template<typename R, typename T1, typename T2, typename T4, typename T8>
  120. R ReadSizedScalar(const uint8_t *data, uint8_t byte_width) {
  121. return byte_width < 4
  122. ? (byte_width < 2
  123. ? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
  124. : static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
  125. : (byte_width < 8
  126. ? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
  127. : static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
  128. }
  129. inline int64_t ReadInt64(const uint8_t *data, uint8_t byte_width) {
  130. return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(
  131. data, byte_width);
  132. }
  133. inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
  134. // This is the "hottest" function (all offset lookups use this), so worth
  135. // optimizing if possible.
  136. // TODO: GCC apparently replaces memcpy by a rep movsb, but only if count is a
  137. // constant, which here it isn't. Test if memcpy is still faster than
  138. // the conditionals in ReadSizedScalar. Can also use inline asm.
  139. // clang-format off
  140. #if defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC)
  141. // This is 64-bit Windows only, __movsb does not work on 32-bit Windows.
  142. uint64_t u = 0;
  143. __movsb(reinterpret_cast<uint8_t *>(&u),
  144. reinterpret_cast<const uint8_t *>(data), byte_width);
  145. return flatbuffers::EndianScalar(u);
  146. #else
  147. return ReadSizedScalar<uint64_t, uint8_t, uint16_t, uint32_t, uint64_t>(
  148. data, byte_width);
  149. #endif
  150. // clang-format on
  151. }
  152. inline double ReadDouble(const uint8_t *data, uint8_t byte_width) {
  153. return ReadSizedScalar<double, quarter, half, float, double>(data,
  154. byte_width);
  155. }
  156. inline const uint8_t *Indirect(const uint8_t *offset, uint8_t byte_width) {
  157. return offset - ReadUInt64(offset, byte_width);
  158. }
  159. template<typename T> const uint8_t *Indirect(const uint8_t *offset) {
  160. return offset - flatbuffers::ReadScalar<T>(offset);
  161. }
  162. inline BitWidth WidthU(uint64_t u) {
  163. #define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) \
  164. { \
  165. if (!((u) & ~((1ULL << (width)) - 1ULL))) return BIT_WIDTH_##width; \
  166. }
  167. FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 8);
  168. FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 16);
  169. FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 32);
  170. #undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
  171. return BIT_WIDTH_64;
  172. }
  173. inline BitWidth WidthI(int64_t i) {
  174. auto u = static_cast<uint64_t>(i) << 1;
  175. return WidthU(i >= 0 ? u : ~u);
  176. }
  177. inline BitWidth WidthF(double f) {
  178. return static_cast<double>(static_cast<float>(f)) == f ? BIT_WIDTH_32
  179. : BIT_WIDTH_64;
  180. }
  181. // Base class of all types below.
  182. // Points into the data buffer and allows access to one type.
  183. class Object {
  184. public:
  185. Object(const uint8_t *data, uint8_t byte_width)
  186. : data_(data), byte_width_(byte_width) {}
  187. protected:
  188. const uint8_t *data_;
  189. uint8_t byte_width_;
  190. };
  191. // Object that has a size, obtained either from size prefix, or elsewhere.
  192. class Sized : public Object {
  193. public:
  194. // Size prefix.
  195. Sized(const uint8_t *data, uint8_t byte_width)
  196. : Object(data, byte_width), size_(read_size()) {}
  197. // Manual size.
  198. Sized(const uint8_t *data, uint8_t byte_width, size_t sz)
  199. : Object(data, byte_width), size_(sz) {}
  200. size_t size() const { return size_; }
  201. // Access size stored in `byte_width_` bytes before data_ pointer.
  202. size_t read_size() const {
  203. return static_cast<size_t>(ReadUInt64(data_ - byte_width_, byte_width_));
  204. }
  205. protected:
  206. size_t size_;
  207. };
  208. class String : public Sized {
  209. public:
  210. // Size prefix.
  211. String(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
  212. // Manual size.
  213. String(const uint8_t *data, uint8_t byte_width, size_t sz)
  214. : Sized(data, byte_width, sz) {}
  215. size_t length() const { return size(); }
  216. const char *c_str() const { return reinterpret_cast<const char *>(data_); }
  217. std::string str() const { return std::string(c_str(), size()); }
  218. static String EmptyString() {
  219. static const char *empty_string = "";
  220. return String(reinterpret_cast<const uint8_t *>(empty_string), 1, 0);
  221. }
  222. bool IsTheEmptyString() const { return data_ == EmptyString().data_; }
  223. };
  224. class Blob : public Sized {
  225. public:
  226. Blob(const uint8_t *data_buf, uint8_t byte_width)
  227. : Sized(data_buf, byte_width) {}
  228. static Blob EmptyBlob() {
  229. static const uint8_t empty_blob[] = { 0 /*len*/ };
  230. return Blob(empty_blob + 1, 1);
  231. }
  232. bool IsTheEmptyBlob() const { return data_ == EmptyBlob().data_; }
  233. const uint8_t *data() const { return data_; }
  234. };
  235. class Vector : public Sized {
  236. public:
  237. Vector(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
  238. Reference operator[](size_t i) const;
  239. static Vector EmptyVector() {
  240. static const uint8_t empty_vector[] = { 0 /*len*/ };
  241. return Vector(empty_vector + 1, 1);
  242. }
  243. bool IsTheEmptyVector() const { return data_ == EmptyVector().data_; }
  244. };
  245. class TypedVector : public Sized {
  246. public:
  247. TypedVector(const uint8_t *data, uint8_t byte_width, Type element_type)
  248. : Sized(data, byte_width), type_(element_type) {}
  249. Reference operator[](size_t i) const;
  250. static TypedVector EmptyTypedVector() {
  251. static const uint8_t empty_typed_vector[] = { 0 /*len*/ };
  252. return TypedVector(empty_typed_vector + 1, 1, FBT_INT);
  253. }
  254. bool IsTheEmptyVector() const {
  255. return data_ == TypedVector::EmptyTypedVector().data_;
  256. }
  257. Type ElementType() { return type_; }
  258. friend Reference;
  259. private:
  260. Type type_;
  261. friend Map;
  262. };
  263. class FixedTypedVector : public Object {
  264. public:
  265. FixedTypedVector(const uint8_t *data, uint8_t byte_width, Type element_type,
  266. uint8_t len)
  267. : Object(data, byte_width), type_(element_type), len_(len) {}
  268. Reference operator[](size_t i) const;
  269. static FixedTypedVector EmptyFixedTypedVector() {
  270. static const uint8_t fixed_empty_vector[] = { 0 /* unused */ };
  271. return FixedTypedVector(fixed_empty_vector, 1, FBT_INT, 0);
  272. }
  273. bool IsTheEmptyFixedTypedVector() const {
  274. return data_ == FixedTypedVector::EmptyFixedTypedVector().data_;
  275. }
  276. Type ElementType() const { return type_; }
  277. uint8_t size() const { return len_; }
  278. private:
  279. Type type_;
  280. uint8_t len_;
  281. };
  282. class Map : public Vector {
  283. public:
  284. Map(const uint8_t *data, uint8_t byte_width) : Vector(data, byte_width) {}
  285. Reference operator[](const char *key) const;
  286. Reference operator[](const std::string &key) const;
  287. Vector Values() const { return Vector(data_, byte_width_); }
  288. TypedVector Keys() const {
  289. const size_t num_prefixed_fields = 3;
  290. auto keys_offset = data_ - byte_width_ * num_prefixed_fields;
  291. return TypedVector(Indirect(keys_offset, byte_width_),
  292. static_cast<uint8_t>(
  293. ReadUInt64(keys_offset + byte_width_, byte_width_)),
  294. FBT_KEY);
  295. }
  296. static Map EmptyMap() {
  297. static const uint8_t empty_map[] = {
  298. 0 /*keys_len*/, 0 /*keys_offset*/, 1 /*keys_width*/, 0 /*len*/
  299. };
  300. return Map(empty_map + 4, 1);
  301. }
  302. bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; }
  303. };
  304. template<typename T>
  305. void AppendToString(std::string &s, T &&v, bool keys_quoted) {
  306. s += "[ ";
  307. for (size_t i = 0; i < v.size(); i++) {
  308. if (i) s += ", ";
  309. v[i].ToString(true, keys_quoted, s);
  310. }
  311. s += " ]";
  312. }
  313. class Reference {
  314. public:
  315. Reference()
  316. : data_(nullptr),
  317. parent_width_(0),
  318. byte_width_(0),
  319. type_(FBT_NULL) {}
  320. Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
  321. Type type)
  322. : data_(data),
  323. parent_width_(parent_width),
  324. byte_width_(byte_width),
  325. type_(type) {}
  326. Reference(const uint8_t *data, uint8_t parent_width, uint8_t packed_type)
  327. : data_(data), parent_width_(parent_width) {
  328. byte_width_ = 1U << static_cast<BitWidth>(packed_type & 3);
  329. type_ = static_cast<Type>(packed_type >> 2);
  330. }
  331. Type GetType() const { return type_; }
  332. bool IsNull() const { return type_ == FBT_NULL; }
  333. bool IsBool() const { return type_ == FBT_BOOL; }
  334. bool IsInt() const { return type_ == FBT_INT || type_ == FBT_INDIRECT_INT; }
  335. bool IsUInt() const {
  336. return type_ == FBT_UINT || type_ == FBT_INDIRECT_UINT;
  337. }
  338. bool IsIntOrUint() const { return IsInt() || IsUInt(); }
  339. bool IsFloat() const {
  340. return type_ == FBT_FLOAT || type_ == FBT_INDIRECT_FLOAT;
  341. }
  342. bool IsNumeric() const { return IsIntOrUint() || IsFloat(); }
  343. bool IsString() const { return type_ == FBT_STRING; }
  344. bool IsKey() const { return type_ == FBT_KEY; }
  345. bool IsVector() const { return type_ == FBT_VECTOR || type_ == FBT_MAP; }
  346. bool IsUntypedVector() const { return type_ == FBT_VECTOR; }
  347. bool IsTypedVector() const { return flexbuffers::IsTypedVector(type_); }
  348. bool IsFixedTypedVector() const {
  349. return flexbuffers::IsFixedTypedVector(type_);
  350. }
  351. bool IsAnyVector() const {
  352. return (IsTypedVector() || IsFixedTypedVector() || IsVector());
  353. }
  354. bool IsMap() const { return type_ == FBT_MAP; }
  355. bool IsBlob() const { return type_ == FBT_BLOB; }
  356. bool AsBool() const {
  357. return (type_ == FBT_BOOL ? ReadUInt64(data_, parent_width_)
  358. : AsUInt64()) != 0;
  359. }
  360. // Reads any type as a int64_t. Never fails, does most sensible conversion.
  361. // Truncates floats, strings are attempted to be parsed for a number,
  362. // vectors/maps return their size. Returns 0 if all else fails.
  363. int64_t AsInt64() const {
  364. if (type_ == FBT_INT) {
  365. // A fast path for the common case.
  366. return ReadInt64(data_, parent_width_);
  367. } else
  368. switch (type_) {
  369. case FBT_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
  370. case FBT_UINT: return ReadUInt64(data_, parent_width_);
  371. case FBT_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
  372. case FBT_FLOAT:
  373. return static_cast<int64_t>(ReadDouble(data_, parent_width_));
  374. case FBT_INDIRECT_FLOAT:
  375. return static_cast<int64_t>(ReadDouble(Indirect(), byte_width_));
  376. case FBT_NULL: return 0;
  377. case FBT_STRING: return flatbuffers::StringToInt(AsString().c_str());
  378. case FBT_VECTOR: return static_cast<int64_t>(AsVector().size());
  379. case FBT_BOOL: return ReadInt64(data_, parent_width_);
  380. default:
  381. // Convert other things to int.
  382. return 0;
  383. }
  384. }
  385. // TODO: could specialize these to not use AsInt64() if that saves
  386. // extension ops in generated code, and use a faster op than ReadInt64.
  387. int32_t AsInt32() const { return static_cast<int32_t>(AsInt64()); }
  388. int16_t AsInt16() const { return static_cast<int16_t>(AsInt64()); }
  389. int8_t AsInt8() const { return static_cast<int8_t>(AsInt64()); }
  390. uint64_t AsUInt64() const {
  391. if (type_ == FBT_UINT) {
  392. // A fast path for the common case.
  393. return ReadUInt64(data_, parent_width_);
  394. } else
  395. switch (type_) {
  396. case FBT_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
  397. case FBT_INT: return ReadInt64(data_, parent_width_);
  398. case FBT_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
  399. case FBT_FLOAT:
  400. return static_cast<uint64_t>(ReadDouble(data_, parent_width_));
  401. case FBT_INDIRECT_FLOAT:
  402. return static_cast<uint64_t>(ReadDouble(Indirect(), byte_width_));
  403. case FBT_NULL: return 0;
  404. case FBT_STRING: return flatbuffers::StringToUInt(AsString().c_str());
  405. case FBT_VECTOR: return static_cast<uint64_t>(AsVector().size());
  406. case FBT_BOOL: return ReadUInt64(data_, parent_width_);
  407. default:
  408. // Convert other things to uint.
  409. return 0;
  410. }
  411. }
  412. uint32_t AsUInt32() const { return static_cast<uint32_t>(AsUInt64()); }
  413. uint16_t AsUInt16() const { return static_cast<uint16_t>(AsUInt64()); }
  414. uint8_t AsUInt8() const { return static_cast<uint8_t>(AsUInt64()); }
  415. double AsDouble() const {
  416. if (type_ == FBT_FLOAT) {
  417. // A fast path for the common case.
  418. return ReadDouble(data_, parent_width_);
  419. } else
  420. switch (type_) {
  421. case FBT_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
  422. case FBT_INT:
  423. return static_cast<double>(ReadInt64(data_, parent_width_));
  424. case FBT_UINT:
  425. return static_cast<double>(ReadUInt64(data_, parent_width_));
  426. case FBT_INDIRECT_INT:
  427. return static_cast<double>(ReadInt64(Indirect(), byte_width_));
  428. case FBT_INDIRECT_UINT:
  429. return static_cast<double>(ReadUInt64(Indirect(), byte_width_));
  430. case FBT_NULL: return 0.0;
  431. case FBT_STRING: {
  432. #if 1
  433. #if !defined( _MSC_VER)
  434. #pragma GCC diagnostic push
  435. #pragma GCC diagnostic ignored "-Wnull-dereference"
  436. #endif
  437. // See b/173239141 for additional context. Patched via
  438. // micro/tools/make/flexbuffers_download.sh
  439. // Introduce a segfault for an unsupported code path for TFLM.
  440. return *(static_cast<double*>(nullptr));
  441. #if !defined( _MSC_VER)
  442. #pragma GCC diagnostic pop
  443. #endif
  444. #else
  445. // This is the original code
  446. double d;
  447. flatbuffers::StringToNumber(AsString().c_str(), &d);
  448. return d;
  449. #endif
  450. }
  451. case FBT_VECTOR: return static_cast<double>(AsVector().size());
  452. case FBT_BOOL:
  453. return static_cast<double>(ReadUInt64(data_, parent_width_));
  454. default:
  455. // Convert strings and other things to float.
  456. return 0;
  457. }
  458. }
  459. float AsFloat() const { return static_cast<float>(AsDouble()); }
  460. const char *AsKey() const {
  461. if (type_ == FBT_KEY || type_ == FBT_STRING) {
  462. return reinterpret_cast<const char *>(Indirect());
  463. } else {
  464. return "";
  465. }
  466. }
  467. // This function returns the empty string if you try to read something that
  468. // is not a string or key.
  469. String AsString() const {
  470. if (type_ == FBT_STRING) {
  471. return String(Indirect(), byte_width_);
  472. } else if (type_ == FBT_KEY) {
  473. auto key = Indirect();
  474. return String(key, byte_width_,
  475. strlen(reinterpret_cast<const char *>(key)));
  476. } else {
  477. return String::EmptyString();
  478. }
  479. }
  480. // Unlike AsString(), this will convert any type to a std::string.
  481. std::string ToString() const {
  482. std::string s;
  483. ToString(false, false, s);
  484. return s;
  485. }
  486. // Convert any type to a JSON-like string. strings_quoted determines if
  487. // string values at the top level receive "" quotes (inside other values
  488. // they always do). keys_quoted determines if keys are quoted, at any level.
  489. // TODO(wvo): add further options to have indentation/newlines.
  490. void ToString(bool strings_quoted, bool keys_quoted, std::string &s) const {
  491. if (type_ == FBT_STRING) {
  492. String str(Indirect(), byte_width_);
  493. if (strings_quoted) {
  494. flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, false);
  495. } else {
  496. s.append(str.c_str(), str.length());
  497. }
  498. } else if (IsKey()) {
  499. auto str = AsKey();
  500. if (keys_quoted) {
  501. flatbuffers::EscapeString(str, strlen(str), &s, true, false);
  502. } else {
  503. s += str;
  504. }
  505. } else if (IsInt()) {
  506. s += flatbuffers::NumToString(AsInt64());
  507. } else if (IsUInt()) {
  508. s += flatbuffers::NumToString(AsUInt64());
  509. } else if (IsFloat()) {
  510. s += flatbuffers::NumToString(AsDouble());
  511. } else if (IsNull()) {
  512. s += "null";
  513. } else if (IsBool()) {
  514. s += AsBool() ? "true" : "false";
  515. } else if (IsMap()) {
  516. s += "{ ";
  517. auto m = AsMap();
  518. auto keys = m.Keys();
  519. auto vals = m.Values();
  520. for (size_t i = 0; i < keys.size(); i++) {
  521. keys[i].ToString(true, keys_quoted, s);
  522. s += ": ";
  523. vals[i].ToString(true, keys_quoted, s);
  524. if (i < keys.size() - 1) s += ", ";
  525. }
  526. s += " }";
  527. } else if (IsVector()) {
  528. AppendToString<Vector>(s, AsVector(), keys_quoted);
  529. } else if (IsTypedVector()) {
  530. AppendToString<TypedVector>(s, AsTypedVector(), keys_quoted);
  531. } else if (IsFixedTypedVector()) {
  532. AppendToString<FixedTypedVector>(s, AsFixedTypedVector(), keys_quoted);
  533. } else if (IsBlob()) {
  534. auto blob = AsBlob();
  535. flatbuffers::EscapeString(reinterpret_cast<const char *>(blob.data()),
  536. blob.size(), &s, true, false);
  537. } else {
  538. s += "(?)";
  539. }
  540. }
  541. // This function returns the empty blob if you try to read a not-blob.
  542. // Strings can be viewed as blobs too.
  543. Blob AsBlob() const {
  544. if (type_ == FBT_BLOB || type_ == FBT_STRING) {
  545. return Blob(Indirect(), byte_width_);
  546. } else {
  547. return Blob::EmptyBlob();
  548. }
  549. }
  550. // This function returns the empty vector if you try to read a not-vector.
  551. // Maps can be viewed as vectors too.
  552. Vector AsVector() const {
  553. if (type_ == FBT_VECTOR || type_ == FBT_MAP) {
  554. return Vector(Indirect(), byte_width_);
  555. } else {
  556. return Vector::EmptyVector();
  557. }
  558. }
  559. TypedVector AsTypedVector() const {
  560. if (IsTypedVector()) {
  561. auto tv =
  562. TypedVector(Indirect(), byte_width_, ToTypedVectorElementType(type_));
  563. if (tv.type_ == FBT_STRING) {
  564. // These can't be accessed as strings, since we don't know the bit-width
  565. // of the size field, see the declaration of
  566. // FBT_VECTOR_STRING_DEPRECATED above for details.
  567. // We change the type here to be keys, which are a subtype of strings,
  568. // and will ignore the size field. This will truncate strings with
  569. // embedded nulls.
  570. tv.type_ = FBT_KEY;
  571. }
  572. return tv;
  573. } else {
  574. return TypedVector::EmptyTypedVector();
  575. }
  576. }
  577. FixedTypedVector AsFixedTypedVector() const {
  578. if (IsFixedTypedVector()) {
  579. uint8_t len = 0;
  580. auto vtype = ToFixedTypedVectorElementType(type_, &len);
  581. return FixedTypedVector(Indirect(), byte_width_, vtype, len);
  582. } else {
  583. return FixedTypedVector::EmptyFixedTypedVector();
  584. }
  585. }
  586. Map AsMap() const {
  587. if (type_ == FBT_MAP) {
  588. return Map(Indirect(), byte_width_);
  589. } else {
  590. return Map::EmptyMap();
  591. }
  592. }
  593. template<typename T> T As() const;
  594. // Experimental: Mutation functions.
  595. // These allow scalars in an already created buffer to be updated in-place.
  596. // Since by default scalars are stored in the smallest possible space,
  597. // the new value may not fit, in which case these functions return false.
  598. // To avoid this, you can construct the values you intend to mutate using
  599. // Builder::ForceMinimumBitWidth.
  600. bool MutateInt(int64_t i) {
  601. if (type_ == FBT_INT) {
  602. return Mutate(data_, i, parent_width_, WidthI(i));
  603. } else if (type_ == FBT_INDIRECT_INT) {
  604. return Mutate(Indirect(), i, byte_width_, WidthI(i));
  605. } else if (type_ == FBT_UINT) {
  606. auto u = static_cast<uint64_t>(i);
  607. return Mutate(data_, u, parent_width_, WidthU(u));
  608. } else if (type_ == FBT_INDIRECT_UINT) {
  609. auto u = static_cast<uint64_t>(i);
  610. return Mutate(Indirect(), u, byte_width_, WidthU(u));
  611. } else {
  612. return false;
  613. }
  614. }
  615. bool MutateBool(bool b) {
  616. return type_ == FBT_BOOL && Mutate(data_, b, parent_width_, BIT_WIDTH_8);
  617. }
  618. bool MutateUInt(uint64_t u) {
  619. if (type_ == FBT_UINT) {
  620. return Mutate(data_, u, parent_width_, WidthU(u));
  621. } else if (type_ == FBT_INDIRECT_UINT) {
  622. return Mutate(Indirect(), u, byte_width_, WidthU(u));
  623. } else if (type_ == FBT_INT) {
  624. auto i = static_cast<int64_t>(u);
  625. return Mutate(data_, i, parent_width_, WidthI(i));
  626. } else if (type_ == FBT_INDIRECT_INT) {
  627. auto i = static_cast<int64_t>(u);
  628. return Mutate(Indirect(), i, byte_width_, WidthI(i));
  629. } else {
  630. return false;
  631. }
  632. }
  633. bool MutateFloat(float f) {
  634. if (type_ == FBT_FLOAT) {
  635. return MutateF(data_, f, parent_width_, BIT_WIDTH_32);
  636. } else if (type_ == FBT_INDIRECT_FLOAT) {
  637. return MutateF(Indirect(), f, byte_width_, BIT_WIDTH_32);
  638. } else {
  639. return false;
  640. }
  641. }
  642. bool MutateFloat(double d) {
  643. if (type_ == FBT_FLOAT) {
  644. return MutateF(data_, d, parent_width_, WidthF(d));
  645. } else if (type_ == FBT_INDIRECT_FLOAT) {
  646. return MutateF(Indirect(), d, byte_width_, WidthF(d));
  647. } else {
  648. return false;
  649. }
  650. }
  651. bool MutateString(const char *str, size_t len) {
  652. auto s = AsString();
  653. if (s.IsTheEmptyString()) return false;
  654. // This is very strict, could allow shorter strings, but that creates
  655. // garbage.
  656. if (s.length() != len) return false;
  657. memcpy(const_cast<char *>(s.c_str()), str, len);
  658. return true;
  659. }
  660. bool MutateString(const char *str) { return MutateString(str, strlen(str)); }
  661. bool MutateString(const std::string &str) {
  662. return MutateString(str.data(), str.length());
  663. }
  664. private:
  665. const uint8_t *Indirect() const {
  666. return flexbuffers::Indirect(data_, parent_width_);
  667. }
  668. template<typename T>
  669. bool Mutate(const uint8_t *dest, T t, size_t byte_width,
  670. BitWidth value_width) {
  671. auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <=
  672. byte_width;
  673. if (fits) {
  674. t = flatbuffers::EndianScalar(t);
  675. memcpy(const_cast<uint8_t *>(dest), &t, byte_width);
  676. }
  677. return fits;
  678. }
  679. template<typename T>
  680. bool MutateF(const uint8_t *dest, T t, size_t byte_width,
  681. BitWidth value_width) {
  682. if (byte_width == sizeof(double))
  683. return Mutate(dest, static_cast<double>(t), byte_width, value_width);
  684. if (byte_width == sizeof(float))
  685. return Mutate(dest, static_cast<float>(t), byte_width, value_width);
  686. FLATBUFFERS_ASSERT(false);
  687. return false;
  688. }
  689. friend class Verifier;
  690. const uint8_t *data_;
  691. uint8_t parent_width_;
  692. uint8_t byte_width_;
  693. Type type_;
  694. };
  695. // Template specialization for As().
  696. template<> inline bool Reference::As<bool>() const { return AsBool(); }
  697. template<> inline int8_t Reference::As<int8_t>() const { return AsInt8(); }
  698. template<> inline int16_t Reference::As<int16_t>() const { return AsInt16(); }
  699. template<> inline int32_t Reference::As<int32_t>() const { return AsInt32(); }
  700. template<> inline int64_t Reference::As<int64_t>() const { return AsInt64(); }
  701. template<> inline uint8_t Reference::As<uint8_t>() const { return AsUInt8(); }
  702. template<> inline uint16_t Reference::As<uint16_t>() const {
  703. return AsUInt16();
  704. }
  705. template<> inline uint32_t Reference::As<uint32_t>() const {
  706. return AsUInt32();
  707. }
  708. template<> inline uint64_t Reference::As<uint64_t>() const {
  709. return AsUInt64();
  710. }
  711. template<> inline double Reference::As<double>() const { return AsDouble(); }
  712. template<> inline float Reference::As<float>() const { return AsFloat(); }
  713. template<> inline String Reference::As<String>() const { return AsString(); }
  714. template<> inline std::string Reference::As<std::string>() const {
  715. return AsString().str();
  716. }
  717. template<> inline Blob Reference::As<Blob>() const { return AsBlob(); }
  718. template<> inline Vector Reference::As<Vector>() const { return AsVector(); }
  719. template<> inline TypedVector Reference::As<TypedVector>() const {
  720. return AsTypedVector();
  721. }
  722. template<> inline FixedTypedVector Reference::As<FixedTypedVector>() const {
  723. return AsFixedTypedVector();
  724. }
  725. template<> inline Map Reference::As<Map>() const { return AsMap(); }
  726. inline uint8_t PackedType(BitWidth bit_width, Type type) {
  727. return static_cast<uint8_t>(bit_width | (type << 2));
  728. }
  729. inline uint8_t NullPackedType() { return PackedType(BIT_WIDTH_8, FBT_NULL); }
  730. // Vector accessors.
  731. // Note: if you try to access outside of bounds, you get a Null value back
  732. // instead. Normally this would be an assert, but since this is "dynamically
  733. // typed" data, you may not want that (someone sends you a 2d vector and you
  734. // wanted 3d).
  735. // The Null converts seamlessly into a default value for any other type.
  736. // TODO(wvo): Could introduce an #ifdef that makes this into an assert?
  737. inline Reference Vector::operator[](size_t i) const {
  738. auto len = size();
  739. if (i >= len) return Reference(nullptr, 1, NullPackedType());
  740. auto packed_type = (data_ + len * byte_width_)[i];
  741. auto elem = data_ + i * byte_width_;
  742. return Reference(elem, byte_width_, packed_type);
  743. }
  744. inline Reference TypedVector::operator[](size_t i) const {
  745. auto len = size();
  746. if (i >= len) return Reference(nullptr, 1, NullPackedType());
  747. auto elem = data_ + i * byte_width_;
  748. return Reference(elem, byte_width_, 1, type_);
  749. }
  750. inline Reference FixedTypedVector::operator[](size_t i) const {
  751. if (i >= len_) return Reference(nullptr, 1, NullPackedType());
  752. auto elem = data_ + i * byte_width_;
  753. return Reference(elem, byte_width_, 1, type_);
  754. }
  755. template<typename T> int KeyCompare(const void *key, const void *elem) {
  756. auto str_elem = reinterpret_cast<const char *>(
  757. Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
  758. auto skey = reinterpret_cast<const char *>(key);
  759. return strcmp(skey, str_elem);
  760. }
  761. inline Reference Map::operator[](const char *key) const {
  762. auto keys = Keys();
  763. // We can't pass keys.byte_width_ to the comparison function, so we have
  764. // to pick the right one ahead of time.
  765. int (*comp)(const void *, const void *) = nullptr;
  766. switch (keys.byte_width_) {
  767. case 1: comp = KeyCompare<uint8_t>; break;
  768. case 2: comp = KeyCompare<uint16_t>; break;
  769. case 4: comp = KeyCompare<uint32_t>; break;
  770. case 8: comp = KeyCompare<uint64_t>; break;
  771. default: FLATBUFFERS_ASSERT(false); return Reference();
  772. }
  773. auto res = std::bsearch(key, keys.data_, keys.size(), keys.byte_width_, comp);
  774. if (!res) return Reference(nullptr, 1, NullPackedType());
  775. auto i = (reinterpret_cast<uint8_t *>(res) - keys.data_) / keys.byte_width_;
  776. return (*static_cast<const Vector *>(this))[i];
  777. }
  778. inline Reference Map::operator[](const std::string &key) const {
  779. return (*this)[key.c_str()];
  780. }
  781. inline Reference GetRoot(const uint8_t *buffer, size_t size) {
  782. // See Finish() below for the serialization counterpart of this.
  783. // The root starts at the end of the buffer, so we parse backwards from there.
  784. auto end = buffer + size;
  785. auto byte_width = *--end;
  786. auto packed_type = *--end;
  787. end -= byte_width; // The root data item.
  788. return Reference(end, byte_width, packed_type);
  789. }
  790. inline Reference GetRoot(const std::vector<uint8_t> &buffer) {
  791. return GetRoot(buffer.data(), buffer.size());
  792. }
  793. // Flags that configure how the Builder behaves.
  794. // The "Share" flags determine if the Builder automatically tries to pool
  795. // this type. Pooling can reduce the size of serialized data if there are
  796. // multiple maps of the same kind, at the expense of slightly slower
  797. // serialization (the cost of lookups) and more memory use (std::set).
  798. // By default this is on for keys, but off for strings.
  799. // Turn keys off if you have e.g. only one map.
  800. // Turn strings on if you expect many non-unique string values.
  801. // Additionally, sharing key vectors can save space if you have maps with
  802. // identical field populations.
  803. enum BuilderFlag {
  804. BUILDER_FLAG_NONE = 0,
  805. BUILDER_FLAG_SHARE_KEYS = 1,
  806. BUILDER_FLAG_SHARE_STRINGS = 2,
  807. BUILDER_FLAG_SHARE_KEYS_AND_STRINGS = 3,
  808. BUILDER_FLAG_SHARE_KEY_VECTORS = 4,
  809. BUILDER_FLAG_SHARE_ALL = 7,
  810. };
  811. class Builder FLATBUFFERS_FINAL_CLASS {
  812. public:
  813. Builder(size_t initial_size = 256,
  814. BuilderFlag flags = BUILDER_FLAG_SHARE_KEYS)
  815. : buf_(initial_size),
  816. finished_(false),
  817. has_duplicate_keys_(false),
  818. flags_(flags),
  819. force_min_bit_width_(BIT_WIDTH_8),
  820. key_pool(KeyOffsetCompare(buf_)),
  821. string_pool(StringOffsetCompare(buf_)) {
  822. buf_.clear();
  823. }
  824. #ifdef FLATBUFFERS_DEFAULT_DECLARATION
  825. Builder(Builder &&) = default;
  826. Builder &operator=(Builder &&) = default;
  827. #endif
  828. /// @brief Get the serialized buffer (after you call `Finish()`).
  829. /// @return Returns a vector owned by this class.
  830. const std::vector<uint8_t> &GetBuffer() const {
  831. Finished();
  832. return buf_;
  833. }
  834. // Size of the buffer. Does not include unfinished values.
  835. size_t GetSize() const { return buf_.size(); }
  836. // Reset all state so we can re-use the buffer.
  837. void Clear() {
  838. buf_.clear();
  839. stack_.clear();
  840. finished_ = false;
  841. // flags_ remains as-is;
  842. force_min_bit_width_ = BIT_WIDTH_8;
  843. key_pool.clear();
  844. string_pool.clear();
  845. }
  846. // All value constructing functions below have two versions: one that
  847. // takes a key (for placement inside a map) and one that doesn't (for inside
  848. // vectors and elsewhere).
  849. void Null() { stack_.push_back(Value()); }
  850. void Null(const char *key) {
  851. Key(key);
  852. Null();
  853. }
  854. void Int(int64_t i) { stack_.push_back(Value(i, FBT_INT, WidthI(i))); }
  855. void Int(const char *key, int64_t i) {
  856. Key(key);
  857. Int(i);
  858. }
  859. void UInt(uint64_t u) { stack_.push_back(Value(u, FBT_UINT, WidthU(u))); }
  860. void UInt(const char *key, uint64_t u) {
  861. Key(key);
  862. UInt(u);
  863. }
  864. void Float(float f) { stack_.push_back(Value(f)); }
  865. void Float(const char *key, float f) {
  866. Key(key);
  867. Float(f);
  868. }
  869. void Double(double f) { stack_.push_back(Value(f)); }
  870. void Double(const char *key, double d) {
  871. Key(key);
  872. Double(d);
  873. }
  874. void Bool(bool b) { stack_.push_back(Value(b)); }
  875. void Bool(const char *key, bool b) {
  876. Key(key);
  877. Bool(b);
  878. }
  879. void IndirectInt(int64_t i) { PushIndirect(i, FBT_INDIRECT_INT, WidthI(i)); }
  880. void IndirectInt(const char *key, int64_t i) {
  881. Key(key);
  882. IndirectInt(i);
  883. }
  884. void IndirectUInt(uint64_t u) {
  885. PushIndirect(u, FBT_INDIRECT_UINT, WidthU(u));
  886. }
  887. void IndirectUInt(const char *key, uint64_t u) {
  888. Key(key);
  889. IndirectUInt(u);
  890. }
  891. void IndirectFloat(float f) {
  892. PushIndirect(f, FBT_INDIRECT_FLOAT, BIT_WIDTH_32);
  893. }
  894. void IndirectFloat(const char *key, float f) {
  895. Key(key);
  896. IndirectFloat(f);
  897. }
  898. void IndirectDouble(double f) {
  899. PushIndirect(f, FBT_INDIRECT_FLOAT, WidthF(f));
  900. }
  901. void IndirectDouble(const char *key, double d) {
  902. Key(key);
  903. IndirectDouble(d);
  904. }
  905. size_t Key(const char *str, size_t len) {
  906. auto sloc = buf_.size();
  907. WriteBytes(str, len + 1);
  908. if (flags_ & BUILDER_FLAG_SHARE_KEYS) {
  909. auto it = key_pool.find(sloc);
  910. if (it != key_pool.end()) {
  911. // Already in the buffer. Remove key we just serialized, and use
  912. // existing offset instead.
  913. buf_.resize(sloc);
  914. sloc = *it;
  915. } else {
  916. key_pool.insert(sloc);
  917. }
  918. }
  919. stack_.push_back(Value(static_cast<uint64_t>(sloc), FBT_KEY, BIT_WIDTH_8));
  920. return sloc;
  921. }
  922. size_t Key(const char *str) { return Key(str, strlen(str)); }
  923. size_t Key(const std::string &str) { return Key(str.c_str(), str.size()); }
  924. size_t String(const char *str, size_t len) {
  925. auto reset_to = buf_.size();
  926. auto sloc = CreateBlob(str, len, 1, FBT_STRING);
  927. if (flags_ & BUILDER_FLAG_SHARE_STRINGS) {
  928. StringOffset so(sloc, len);
  929. auto it = string_pool.find(so);
  930. if (it != string_pool.end()) {
  931. // Already in the buffer. Remove string we just serialized, and use
  932. // existing offset instead.
  933. buf_.resize(reset_to);
  934. sloc = it->first;
  935. stack_.back().u_ = sloc;
  936. } else {
  937. string_pool.insert(so);
  938. }
  939. }
  940. return sloc;
  941. }
  942. size_t String(const char *str) { return String(str, strlen(str)); }
  943. size_t String(const std::string &str) {
  944. return String(str.c_str(), str.size());
  945. }
  946. void String(const flexbuffers::String &str) {
  947. String(str.c_str(), str.length());
  948. }
  949. void String(const char *key, const char *str) {
  950. Key(key);
  951. String(str);
  952. }
  953. void String(const char *key, const std::string &str) {
  954. Key(key);
  955. String(str);
  956. }
  957. void String(const char *key, const flexbuffers::String &str) {
  958. Key(key);
  959. String(str);
  960. }
  961. size_t Blob(const void *data, size_t len) {
  962. return CreateBlob(data, len, 0, FBT_BLOB);
  963. }
  964. size_t Blob(const std::vector<uint8_t> &v) {
  965. return CreateBlob(v.data(), v.size(), 0, FBT_BLOB);
  966. }
  967. void Blob(const char *key, const void *data, size_t len) {
  968. Key(key);
  969. Blob(data, len);
  970. }
  971. void Blob(const char *key, const std::vector<uint8_t> &v) {
  972. Key(key);
  973. Blob(v);
  974. }
  975. // TODO(wvo): support all the FlexBuffer types (like flexbuffers::String),
  976. // e.g. Vector etc. Also in overloaded versions.
  977. // Also some FlatBuffers types?
  978. size_t StartVector() { return stack_.size(); }
  979. size_t StartVector(const char *key) {
  980. Key(key);
  981. return stack_.size();
  982. }
  983. size_t StartMap() { return stack_.size(); }
  984. size_t StartMap(const char *key) {
  985. Key(key);
  986. return stack_.size();
  987. }
  988. // TODO(wvo): allow this to specify an alignment greater than the natural
  989. // alignment.
  990. size_t EndVector(size_t start, bool typed, bool fixed) {
  991. auto vec = CreateVector(start, stack_.size() - start, 1, typed, fixed);
  992. // Remove temp elements and return vector.
  993. stack_.resize(start);
  994. stack_.push_back(vec);
  995. return static_cast<size_t>(vec.u_);
  996. }
  997. size_t EndMap(size_t start) {
  998. // We should have interleaved keys and values on the stack.
  999. // Make sure it is an even number:
  1000. auto len = stack_.size() - start;
  1001. FLATBUFFERS_ASSERT(!(len & 1));
  1002. len /= 2;
  1003. // Make sure keys are all strings:
  1004. for (auto key = start; key < stack_.size(); key += 2) {
  1005. FLATBUFFERS_ASSERT(stack_[key].type_ == FBT_KEY);
  1006. }
  1007. // Now sort values, so later we can do a binary search lookup.
  1008. // We want to sort 2 array elements at a time.
  1009. struct TwoValue {
  1010. Value key;
  1011. Value val;
  1012. };
  1013. // TODO(wvo): strict aliasing?
  1014. // TODO(wvo): allow the caller to indicate the data is already sorted
  1015. // for maximum efficiency? With an assert to check sortedness to make sure
  1016. // we're not breaking binary search.
  1017. // Or, we can track if the map is sorted as keys are added which would be
  1018. // be quite cheap (cheaper than checking it here), so we can skip this
  1019. // step automatically when appliccable, and encourage people to write in
  1020. // sorted fashion.
  1021. // std::sort is typically already a lot faster on sorted data though.
  1022. auto dict = reinterpret_cast<TwoValue *>(stack_.data() + start);
  1023. std::sort(
  1024. dict, dict + len, [&](const TwoValue &a, const TwoValue &b) -> bool {
  1025. auto as = reinterpret_cast<const char *>(buf_.data() + a.key.u_);
  1026. auto bs = reinterpret_cast<const char *>(buf_.data() + b.key.u_);
  1027. auto comp = strcmp(as, bs);
  1028. // We want to disallow duplicate keys, since this results in a
  1029. // map where values cannot be found.
  1030. // But we can't assert here (since we don't want to fail on
  1031. // random JSON input) or have an error mechanism.
  1032. // Instead, we set has_duplicate_keys_ in the builder to
  1033. // signal this.
  1034. // TODO: Have to check for pointer equality, as some sort
  1035. // implementation apparently call this function with the same
  1036. // element?? Why?
  1037. if (!comp && &a != &b) has_duplicate_keys_ = true;
  1038. return comp < 0;
  1039. });
  1040. // First create a vector out of all keys.
  1041. // TODO(wvo): if kBuilderFlagShareKeyVectors is true, see if we can share
  1042. // the first vector.
  1043. auto keys = CreateVector(start, len, 2, true, false);
  1044. auto vec = CreateVector(start + 1, len, 2, false, false, &keys);
  1045. // Remove temp elements and return map.
  1046. stack_.resize(start);
  1047. stack_.push_back(vec);
  1048. return static_cast<size_t>(vec.u_);
  1049. }
  1050. // Call this after EndMap to see if the map had any duplicate keys.
  1051. // Any map with such keys won't be able to retrieve all values.
  1052. bool HasDuplicateKeys() const { return has_duplicate_keys_; }
  1053. template<typename F> size_t Vector(F f) {
  1054. auto start = StartVector();
  1055. f();
  1056. return EndVector(start, false, false);
  1057. }
  1058. template<typename F, typename T> size_t Vector(F f, T &state) {
  1059. auto start = StartVector();
  1060. f(state);
  1061. return EndVector(start, false, false);
  1062. }
  1063. template<typename F> size_t Vector(const char *key, F f) {
  1064. auto start = StartVector(key);
  1065. f();
  1066. return EndVector(start, false, false);
  1067. }
  1068. template<typename F, typename T>
  1069. size_t Vector(const char *key, F f, T &state) {
  1070. auto start = StartVector(key);
  1071. f(state);
  1072. return EndVector(start, false, false);
  1073. }
  1074. template<typename T> void Vector(const T *elems, size_t len) {
  1075. if (flatbuffers::is_scalar<T>::value) {
  1076. // This path should be a lot quicker and use less space.
  1077. ScalarVector(elems, len, false);
  1078. } else {
  1079. auto start = StartVector();
  1080. for (size_t i = 0; i < len; i++) Add(elems[i]);
  1081. EndVector(start, false, false);
  1082. }
  1083. }
  1084. template<typename T>
  1085. void Vector(const char *key, const T *elems, size_t len) {
  1086. Key(key);
  1087. Vector(elems, len);
  1088. }
  1089. template<typename T> void Vector(const std::vector<T> &vec) {
  1090. Vector(vec.data(), vec.size());
  1091. }
  1092. template<typename F> size_t TypedVector(F f) {
  1093. auto start = StartVector();
  1094. f();
  1095. return EndVector(start, true, false);
  1096. }
  1097. template<typename F, typename T> size_t TypedVector(F f, T &state) {
  1098. auto start = StartVector();
  1099. f(state);
  1100. return EndVector(start, true, false);
  1101. }
  1102. template<typename F> size_t TypedVector(const char *key, F f) {
  1103. auto start = StartVector(key);
  1104. f();
  1105. return EndVector(start, true, false);
  1106. }
  1107. template<typename F, typename T>
  1108. size_t TypedVector(const char *key, F f, T &state) {
  1109. auto start = StartVector(key);
  1110. f(state);
  1111. return EndVector(start, true, false);
  1112. }
  1113. template<typename T> size_t FixedTypedVector(const T *elems, size_t len) {
  1114. // We only support a few fixed vector lengths. Anything bigger use a
  1115. // regular typed vector.
  1116. FLATBUFFERS_ASSERT(len >= 2 && len <= 4);
  1117. // And only scalar values.
  1118. static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
  1119. return ScalarVector(elems, len, true);
  1120. }
  1121. template<typename T>
  1122. size_t FixedTypedVector(const char *key, const T *elems, size_t len) {
  1123. Key(key);
  1124. return FixedTypedVector(elems, len);
  1125. }
  1126. template<typename F> size_t Map(F f) {
  1127. auto start = StartMap();
  1128. f();
  1129. return EndMap(start);
  1130. }
  1131. template<typename F, typename T> size_t Map(F f, T &state) {
  1132. auto start = StartMap();
  1133. f(state);
  1134. return EndMap(start);
  1135. }
  1136. template<typename F> size_t Map(const char *key, F f) {
  1137. auto start = StartMap(key);
  1138. f();
  1139. return EndMap(start);
  1140. }
  1141. template<typename F, typename T> size_t Map(const char *key, F f, T &state) {
  1142. auto start = StartMap(key);
  1143. f(state);
  1144. return EndMap(start);
  1145. }
  1146. template<typename T> void Map(const std::map<std::string, T> &map) {
  1147. auto start = StartMap();
  1148. for (auto it = map.begin(); it != map.end(); ++it)
  1149. Add(it->first.c_str(), it->second);
  1150. EndMap(start);
  1151. }
  1152. // If you wish to share a value explicitly (a value not shared automatically
  1153. // through one of the BUILDER_FLAG_SHARE_* flags) you can do so with these
  1154. // functions. Or if you wish to turn those flags off for performance reasons
  1155. // and still do some explicit sharing. For example:
  1156. // builder.IndirectDouble(M_PI);
  1157. // auto id = builder.LastValue(); // Remember where we stored it.
  1158. // .. more code goes here ..
  1159. // builder.ReuseValue(id); // Refers to same double by offset.
  1160. // LastValue works regardless of whether the value has a key or not.
  1161. // Works on any data type.
  1162. struct Value;
  1163. Value LastValue() { return stack_.back(); }
  1164. void ReuseValue(Value v) { stack_.push_back(v); }
  1165. void ReuseValue(const char *key, Value v) {
  1166. Key(key);
  1167. ReuseValue(v);
  1168. }
  1169. // Overloaded Add that tries to call the correct function above.
  1170. void Add(int8_t i) { Int(i); }
  1171. void Add(int16_t i) { Int(i); }
  1172. void Add(int32_t i) { Int(i); }
  1173. void Add(int64_t i) { Int(i); }
  1174. void Add(uint8_t u) { UInt(u); }
  1175. void Add(uint16_t u) { UInt(u); }
  1176. void Add(uint32_t u) { UInt(u); }
  1177. void Add(uint64_t u) { UInt(u); }
  1178. void Add(float f) { Float(f); }
  1179. void Add(double d) { Double(d); }
  1180. void Add(bool b) { Bool(b); }
  1181. void Add(const char *str) { String(str); }
  1182. void Add(const std::string &str) { String(str); }
  1183. void Add(const flexbuffers::String &str) { String(str); }
  1184. template<typename T> void Add(const std::vector<T> &vec) { Vector(vec); }
  1185. template<typename T> void Add(const char *key, const T &t) {
  1186. Key(key);
  1187. Add(t);
  1188. }
  1189. template<typename T> void Add(const std::map<std::string, T> &map) {
  1190. Map(map);
  1191. }
  1192. template<typename T> void operator+=(const T &t) { Add(t); }
  1193. // This function is useful in combination with the Mutate* functions above.
  1194. // It forces elements of vectors and maps to have a minimum size, such that
  1195. // they can later be updated without failing.
  1196. // Call with no arguments to reset.
  1197. void ForceMinimumBitWidth(BitWidth bw = BIT_WIDTH_8) {
  1198. force_min_bit_width_ = bw;
  1199. }
  1200. void Finish() {
  1201. // If you hit this assert, you likely have objects that were never included
  1202. // in a parent. You need to have exactly one root to finish a buffer.
  1203. // Check your Start/End calls are matched, and all objects are inside
  1204. // some other object.
  1205. FLATBUFFERS_ASSERT(stack_.size() == 1);
  1206. // Write root value.
  1207. auto byte_width = Align(stack_[0].ElemWidth(buf_.size(), 0));
  1208. WriteAny(stack_[0], byte_width);
  1209. // Write root type.
  1210. Write(stack_[0].StoredPackedType(), 1);
  1211. // Write root size. Normally determined by parent, but root has no parent :)
  1212. Write(byte_width, 1);
  1213. finished_ = true;
  1214. }
  1215. private:
  1216. void Finished() const {
  1217. // If you get this assert, you're attempting to get access a buffer
  1218. // which hasn't been finished yet. Be sure to call
  1219. // Builder::Finish with your root object.
  1220. FLATBUFFERS_ASSERT(finished_);
  1221. }
  1222. // Align to prepare for writing a scalar with a certain size.
  1223. uint8_t Align(BitWidth alignment) {
  1224. auto byte_width = 1U << alignment;
  1225. buf_.insert(buf_.end(), flatbuffers::PaddingBytes(buf_.size(), byte_width),
  1226. 0);
  1227. return static_cast<uint8_t>(byte_width);
  1228. }
  1229. void WriteBytes(const void *val, size_t size) {
  1230. buf_.insert(buf_.end(), reinterpret_cast<const uint8_t *>(val),
  1231. reinterpret_cast<const uint8_t *>(val) + size);
  1232. }
  1233. template<typename T> void Write(T val, size_t byte_width) {
  1234. FLATBUFFERS_ASSERT(sizeof(T) >= byte_width);
  1235. val = flatbuffers::EndianScalar(val);
  1236. WriteBytes(&val, byte_width);
  1237. }
  1238. void WriteDouble(double f, uint8_t byte_width) {
  1239. switch (byte_width) {
  1240. case 8: Write(f, byte_width); break;
  1241. case 4: Write(static_cast<float>(f), byte_width); break;
  1242. // case 2: Write(static_cast<half>(f), byte_width); break;
  1243. // case 1: Write(static_cast<quarter>(f), byte_width); break;
  1244. default: FLATBUFFERS_ASSERT(0);
  1245. }
  1246. }
  1247. void WriteOffset(uint64_t o, uint8_t byte_width) {
  1248. auto reloff = buf_.size() - o;
  1249. FLATBUFFERS_ASSERT(byte_width == 8 || reloff < 1ULL << (byte_width * 8));
  1250. Write(reloff, byte_width);
  1251. }
  1252. template<typename T> void PushIndirect(T val, Type type, BitWidth bit_width) {
  1253. auto byte_width = Align(bit_width);
  1254. auto iloc = buf_.size();
  1255. Write(val, byte_width);
  1256. stack_.push_back(Value(static_cast<uint64_t>(iloc), type, bit_width));
  1257. }
  1258. static BitWidth WidthB(size_t byte_width) {
  1259. switch (byte_width) {
  1260. case 1: return BIT_WIDTH_8;
  1261. case 2: return BIT_WIDTH_16;
  1262. case 4: return BIT_WIDTH_32;
  1263. case 8: return BIT_WIDTH_64;
  1264. default: FLATBUFFERS_ASSERT(false); return BIT_WIDTH_64;
  1265. }
  1266. }
  1267. template<typename T> static Type GetScalarType() {
  1268. static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
  1269. return flatbuffers::is_floating_point<T>::value ? FBT_FLOAT
  1270. : flatbuffers::is_same<T, bool>::value
  1271. ? FBT_BOOL
  1272. : (flatbuffers::is_unsigned<T>::value ? FBT_UINT : FBT_INT);
  1273. }
  1274. public:
  1275. // This was really intended to be private, except for LastValue/ReuseValue.
  1276. struct Value {
  1277. union {
  1278. int64_t i_;
  1279. uint64_t u_;
  1280. double f_;
  1281. };
  1282. Type type_;
  1283. // For scalars: of itself, for vector: of its elements, for string: length.
  1284. BitWidth min_bit_width_;
  1285. Value() : i_(0), type_(FBT_NULL), min_bit_width_(BIT_WIDTH_8) {}
  1286. Value(bool b)
  1287. : u_(static_cast<uint64_t>(b)),
  1288. type_(FBT_BOOL),
  1289. min_bit_width_(BIT_WIDTH_8) {}
  1290. Value(int64_t i, Type t, BitWidth bw)
  1291. : i_(i), type_(t), min_bit_width_(bw) {}
  1292. Value(uint64_t u, Type t, BitWidth bw)
  1293. : u_(u), type_(t), min_bit_width_(bw) {}
  1294. Value(float f)
  1295. : f_(static_cast<double>(f)),
  1296. type_(FBT_FLOAT),
  1297. min_bit_width_(BIT_WIDTH_32) {}
  1298. Value(double f) : f_(f), type_(FBT_FLOAT), min_bit_width_(WidthF(f)) {}
  1299. uint8_t StoredPackedType(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
  1300. return PackedType(StoredWidth(parent_bit_width_), type_);
  1301. }
  1302. BitWidth ElemWidth(size_t buf_size, size_t elem_index) const {
  1303. if (IsInline(type_)) {
  1304. return min_bit_width_;
  1305. } else {
  1306. // We have an absolute offset, but want to store a relative offset
  1307. // elem_index elements beyond the current buffer end. Since whether
  1308. // the relative offset fits in a certain byte_width depends on
  1309. // the size of the elements before it (and their alignment), we have
  1310. // to test for each size in turn.
  1311. for (size_t byte_width = 1;
  1312. byte_width <= sizeof(flatbuffers::largest_scalar_t);
  1313. byte_width *= 2) {
  1314. // Where are we going to write this offset?
  1315. auto offset_loc = buf_size +
  1316. flatbuffers::PaddingBytes(buf_size, byte_width) +
  1317. elem_index * byte_width;
  1318. // Compute relative offset.
  1319. auto offset = offset_loc - u_;
  1320. // Does it fit?
  1321. auto bit_width = WidthU(offset);
  1322. if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) ==
  1323. byte_width)
  1324. return bit_width;
  1325. }
  1326. FLATBUFFERS_ASSERT(false); // Must match one of the sizes above.
  1327. return BIT_WIDTH_64;
  1328. }
  1329. }
  1330. BitWidth StoredWidth(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
  1331. if (IsInline(type_)) {
  1332. return (std::max)(min_bit_width_, parent_bit_width_);
  1333. } else {
  1334. return min_bit_width_;
  1335. }
  1336. }
  1337. };
  1338. private:
  1339. void WriteAny(const Value &val, uint8_t byte_width) {
  1340. switch (val.type_) {
  1341. case FBT_NULL:
  1342. case FBT_INT: Write(val.i_, byte_width); break;
  1343. case FBT_BOOL:
  1344. case FBT_UINT: Write(val.u_, byte_width); break;
  1345. case FBT_FLOAT: WriteDouble(val.f_, byte_width); break;
  1346. default: WriteOffset(val.u_, byte_width); break;
  1347. }
  1348. }
  1349. size_t CreateBlob(const void *data, size_t len, size_t trailing, Type type) {
  1350. auto bit_width = WidthU(len);
  1351. auto byte_width = Align(bit_width);
  1352. Write<uint64_t>(len, byte_width);
  1353. auto sloc = buf_.size();
  1354. WriteBytes(data, len + trailing);
  1355. stack_.push_back(Value(static_cast<uint64_t>(sloc), type, bit_width));
  1356. return sloc;
  1357. }
  1358. template<typename T>
  1359. size_t ScalarVector(const T *elems, size_t len, bool fixed) {
  1360. auto vector_type = GetScalarType<T>();
  1361. auto byte_width = sizeof(T);
  1362. auto bit_width = WidthB(byte_width);
  1363. // If you get this assert, you're trying to write a vector with a size
  1364. // field that is bigger than the scalars you're trying to write (e.g. a
  1365. // byte vector > 255 elements). For such types, write a "blob" instead.
  1366. // TODO: instead of asserting, could write vector with larger elements
  1367. // instead, though that would be wasteful.
  1368. FLATBUFFERS_ASSERT(WidthU(len) <= bit_width);
  1369. Align(bit_width);
  1370. if (!fixed) Write<uint64_t>(len, byte_width);
  1371. auto vloc = buf_.size();
  1372. for (size_t i = 0; i < len; i++) Write(elems[i], byte_width);
  1373. stack_.push_back(Value(static_cast<uint64_t>(vloc),
  1374. ToTypedVector(vector_type, fixed ? len : 0),
  1375. bit_width));
  1376. return vloc;
  1377. }
  1378. Value CreateVector(size_t start, size_t vec_len, size_t step, bool typed,
  1379. bool fixed, const Value *keys = nullptr) {
  1380. FLATBUFFERS_ASSERT(
  1381. !fixed ||
  1382. typed); // typed=false, fixed=true combination is not supported.
  1383. // Figure out smallest bit width we can store this vector with.
  1384. auto bit_width = (std::max)(force_min_bit_width_, WidthU(vec_len));
  1385. auto prefix_elems = 1;
  1386. if (keys) {
  1387. // If this vector is part of a map, we will pre-fix an offset to the keys
  1388. // to this vector.
  1389. bit_width = (std::max)(bit_width, keys->ElemWidth(buf_.size(), 0));
  1390. prefix_elems += 2;
  1391. }
  1392. Type vector_type = FBT_KEY;
  1393. // Check bit widths and types for all elements.
  1394. for (size_t i = start; i < stack_.size(); i += step) {
  1395. auto elem_width =
  1396. stack_[i].ElemWidth(buf_.size(), i - start + prefix_elems);
  1397. bit_width = (std::max)(bit_width, elem_width);
  1398. if (typed) {
  1399. if (i == start) {
  1400. vector_type = stack_[i].type_;
  1401. } else {
  1402. // If you get this assert, you are writing a typed vector with
  1403. // elements that are not all the same type.
  1404. FLATBUFFERS_ASSERT(vector_type == stack_[i].type_);
  1405. }
  1406. }
  1407. }
  1408. // If you get this assert, your typed types are not one of:
  1409. // Int / UInt / Float / Key.
  1410. FLATBUFFERS_ASSERT(!typed || IsTypedVectorElementType(vector_type));
  1411. auto byte_width = Align(bit_width);
  1412. // Write vector. First the keys width/offset if available, and size.
  1413. if (keys) {
  1414. WriteOffset(keys->u_, byte_width);
  1415. Write<uint64_t>(1ULL << keys->min_bit_width_, byte_width);
  1416. }
  1417. if (!fixed) Write<uint64_t>(vec_len, byte_width);
  1418. // Then the actual data.
  1419. auto vloc = buf_.size();
  1420. for (size_t i = start; i < stack_.size(); i += step) {
  1421. WriteAny(stack_[i], byte_width);
  1422. }
  1423. // Then the types.
  1424. if (!typed) {
  1425. for (size_t i = start; i < stack_.size(); i += step) {
  1426. buf_.push_back(stack_[i].StoredPackedType(bit_width));
  1427. }
  1428. }
  1429. return Value(static_cast<uint64_t>(vloc),
  1430. keys ? FBT_MAP
  1431. : (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0)
  1432. : FBT_VECTOR),
  1433. bit_width);
  1434. }
  1435. // You shouldn't really be copying instances of this class.
  1436. Builder(const Builder &);
  1437. Builder &operator=(const Builder &);
  1438. std::vector<uint8_t> buf_;
  1439. std::vector<Value> stack_;
  1440. bool finished_;
  1441. bool has_duplicate_keys_;
  1442. BuilderFlag flags_;
  1443. BitWidth force_min_bit_width_;
  1444. struct KeyOffsetCompare {
  1445. explicit KeyOffsetCompare(const std::vector<uint8_t> &buf) : buf_(&buf) {}
  1446. bool operator()(size_t a, size_t b) const {
  1447. auto stra = reinterpret_cast<const char *>(buf_->data() + a);
  1448. auto strb = reinterpret_cast<const char *>(buf_->data() + b);
  1449. return strcmp(stra, strb) < 0;
  1450. }
  1451. const std::vector<uint8_t> *buf_;
  1452. };
  1453. typedef std::pair<size_t, size_t> StringOffset;
  1454. struct StringOffsetCompare {
  1455. explicit StringOffsetCompare(const std::vector<uint8_t> &buf)
  1456. : buf_(&buf) {}
  1457. bool operator()(const StringOffset &a, const StringOffset &b) const {
  1458. auto stra = buf_->data() + a.first;
  1459. auto strb = buf_->data() + b.first;
  1460. auto cr = memcmp(stra, strb, (std::min)(a.second, b.second) + 1);
  1461. return cr < 0 || (cr == 0 && a.second < b.second);
  1462. }
  1463. const std::vector<uint8_t> *buf_;
  1464. };
  1465. typedef std::set<size_t, KeyOffsetCompare> KeyOffsetMap;
  1466. typedef std::set<StringOffset, StringOffsetCompare> StringOffsetMap;
  1467. KeyOffsetMap key_pool;
  1468. StringOffsetMap string_pool;
  1469. friend class Verifier;
  1470. };
  1471. // Helper class to verify the integrity of a FlexBuffer
  1472. class Verifier FLATBUFFERS_FINAL_CLASS {
  1473. public:
  1474. Verifier(const uint8_t *buf, size_t buf_len,
  1475. // Supplying this vector likely results in faster verification
  1476. // of larger buffers with many shared keys/strings, but
  1477. // comes at the cost of using additional memory the same size of
  1478. // the buffer being verified, so it is by default off.
  1479. std::vector<uint8_t> *reuse_tracker = nullptr,
  1480. bool _check_alignment = true,
  1481. size_t max_depth = 64)
  1482. : buf_(buf),
  1483. size_(buf_len),
  1484. depth_(0),
  1485. max_depth_(max_depth),
  1486. num_vectors_(0),
  1487. max_vectors_(buf_len),
  1488. check_alignment_(_check_alignment),
  1489. reuse_tracker_(reuse_tracker) {
  1490. FLATBUFFERS_ASSERT(size_ < FLATBUFFERS_MAX_BUFFER_SIZE);
  1491. if (reuse_tracker_) {
  1492. reuse_tracker_->clear();
  1493. reuse_tracker_->resize(size_, PackedType(BIT_WIDTH_8, FBT_NULL));
  1494. }
  1495. }
  1496. private:
  1497. // Central location where any verification failures register.
  1498. bool Check(bool ok) const {
  1499. // clang-format off
  1500. #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE
  1501. FLATBUFFERS_ASSERT(ok);
  1502. #endif
  1503. // clang-format on
  1504. return ok;
  1505. }
  1506. // Verify any range within the buffer.
  1507. bool VerifyFrom(size_t elem, size_t elem_len) const {
  1508. return Check(elem_len < size_ && elem <= size_ - elem_len);
  1509. }
  1510. bool VerifyBefore(size_t elem, size_t elem_len) const {
  1511. return Check(elem_len <= elem);
  1512. }
  1513. bool VerifyFromPointer(const uint8_t *p, size_t len) {
  1514. auto o = static_cast<size_t>(p - buf_);
  1515. return VerifyFrom(o, len);
  1516. }
  1517. bool VerifyBeforePointer(const uint8_t *p, size_t len) {
  1518. auto o = static_cast<size_t>(p - buf_);
  1519. return VerifyBefore(o, len);
  1520. }
  1521. bool VerifyByteWidth(size_t width) {
  1522. return Check(width == 1 || width == 2 || width == 4 || width == 8);
  1523. }
  1524. bool VerifyType(int type) {
  1525. return Check(type >= 0 && type < FBT_MAX_TYPE);
  1526. }
  1527. bool VerifyOffset(uint64_t off, const uint8_t *p) {
  1528. return Check(off <= static_cast<uint64_t>(size_)) &&
  1529. off <= static_cast<uint64_t>(p - buf_);
  1530. }
  1531. bool VerifyAlignment(const uint8_t *p, size_t size) const {
  1532. auto o = static_cast<size_t>(p - buf_);
  1533. return Check((o & (size - 1)) == 0 || !check_alignment_);
  1534. }
  1535. // Macro, since we want to escape from parent function & use lazy args.
  1536. #define FLEX_CHECK_VERIFIED(P, PACKED_TYPE) \
  1537. if (reuse_tracker_) { \
  1538. auto packed_type = PACKED_TYPE; \
  1539. auto existing = (*reuse_tracker_)[P - buf_]; \
  1540. if (existing == packed_type) return true; \
  1541. /* Fail verification if already set with different type! */ \
  1542. if (!Check(existing == 0)) return false; \
  1543. (*reuse_tracker_)[P - buf_] = packed_type; \
  1544. }
  1545. bool VerifyVector(Reference r, const uint8_t *p, Type elem_type) {
  1546. // Any kind of nesting goes thru this function, so guard against that
  1547. // here, both with simple nesting checks, and the reuse tracker if on.
  1548. depth_++;
  1549. num_vectors_++;
  1550. if (!Check(depth_ <= max_depth_ && num_vectors_ <= max_vectors_))
  1551. return false;
  1552. auto size_byte_width = r.byte_width_;
  1553. FLEX_CHECK_VERIFIED(p, PackedType(Builder::WidthB(size_byte_width), r.type_));
  1554. if (!VerifyBeforePointer(p, size_byte_width))
  1555. return false;
  1556. auto sized = Sized(p, size_byte_width);
  1557. auto num_elems = sized.size();
  1558. auto elem_byte_width =
  1559. r.type_ == FBT_STRING || r.type_ == FBT_BLOB ? uint8_t(1) : r.byte_width_;
  1560. auto max_elems = SIZE_MAX / elem_byte_width;
  1561. if (!Check(num_elems < max_elems))
  1562. return false; // Protect against byte_size overflowing.
  1563. auto byte_size = num_elems * elem_byte_width;
  1564. if (!VerifyFromPointer(p, byte_size))
  1565. return false;
  1566. if (elem_type == FBT_NULL) {
  1567. // Verify type bytes after the vector.
  1568. if (!VerifyFromPointer(p + byte_size, num_elems)) return false;
  1569. auto v = Vector(p, size_byte_width);
  1570. for (size_t i = 0; i < num_elems; i++)
  1571. if (!VerifyRef(v[i])) return false;
  1572. } else if (elem_type == FBT_KEY) {
  1573. auto v = TypedVector(p, elem_byte_width, FBT_KEY);
  1574. for (size_t i = 0; i < num_elems; i++)
  1575. if (!VerifyRef(v[i])) return false;
  1576. } else {
  1577. FLATBUFFERS_ASSERT(IsInline(elem_type));
  1578. }
  1579. depth_--;
  1580. return true;
  1581. }
  1582. bool VerifyKeys(const uint8_t *p, uint8_t byte_width) {
  1583. // The vector part of the map has already been verified.
  1584. const size_t num_prefixed_fields = 3;
  1585. if (!VerifyBeforePointer(p, byte_width * num_prefixed_fields))
  1586. return false;
  1587. p -= byte_width * num_prefixed_fields;
  1588. auto off = ReadUInt64(p, byte_width);
  1589. if (!VerifyOffset(off, p))
  1590. return false;
  1591. auto key_byte_with =
  1592. static_cast<uint8_t>(ReadUInt64(p + byte_width, byte_width));
  1593. if (!VerifyByteWidth(key_byte_with))
  1594. return false;
  1595. return VerifyVector(Reference(p, byte_width, key_byte_with, FBT_VECTOR_KEY),
  1596. p - off, FBT_KEY);
  1597. }
  1598. bool VerifyKey(const uint8_t* p) {
  1599. FLEX_CHECK_VERIFIED(p, PackedType(BIT_WIDTH_8, FBT_KEY));
  1600. while (p < buf_ + size_)
  1601. if (*p++) return true;
  1602. return false;
  1603. }
  1604. #undef FLEX_CHECK_VERIFIED
  1605. bool VerifyTerminator(const String &s) {
  1606. return VerifyFromPointer(reinterpret_cast<const uint8_t *>(s.c_str()),
  1607. s.size() + 1);
  1608. }
  1609. bool VerifyRef(Reference r) {
  1610. // r.parent_width_ and r.data_ already verified.
  1611. if (!VerifyByteWidth(r.byte_width_) || !VerifyType(r.type_)) {
  1612. return false;
  1613. }
  1614. if (IsInline(r.type_)) {
  1615. // Inline scalars, don't require further verification.
  1616. return true;
  1617. }
  1618. // All remaining types are an offset.
  1619. auto off = ReadUInt64(r.data_, r.parent_width_);
  1620. if (!VerifyOffset(off, r.data_))
  1621. return false;
  1622. auto p = r.Indirect();
  1623. if (!VerifyAlignment(p, r.byte_width_))
  1624. return false;
  1625. switch (r.type_) {
  1626. case FBT_INDIRECT_INT:
  1627. case FBT_INDIRECT_UINT:
  1628. case FBT_INDIRECT_FLOAT:
  1629. return VerifyFromPointer(p, r.byte_width_);
  1630. case FBT_KEY:
  1631. return VerifyKey(p);
  1632. case FBT_MAP:
  1633. return VerifyVector(r, p, FBT_NULL) &&
  1634. VerifyKeys(p, r.byte_width_);
  1635. case FBT_VECTOR:
  1636. return VerifyVector(r, p, FBT_NULL);
  1637. case FBT_VECTOR_INT:
  1638. return VerifyVector(r, p, FBT_INT);
  1639. case FBT_VECTOR_BOOL:
  1640. case FBT_VECTOR_UINT:
  1641. return VerifyVector(r, p, FBT_UINT);
  1642. case FBT_VECTOR_FLOAT:
  1643. return VerifyVector(r, p, FBT_FLOAT);
  1644. case FBT_VECTOR_KEY:
  1645. return VerifyVector(r, p, FBT_KEY);
  1646. case FBT_VECTOR_STRING_DEPRECATED:
  1647. // Use of FBT_KEY here intentional, see elsewhere.
  1648. return VerifyVector(r, p, FBT_KEY);
  1649. case FBT_BLOB:
  1650. return VerifyVector(r, p, FBT_UINT);
  1651. case FBT_STRING:
  1652. return VerifyVector(r, p, FBT_UINT) &&
  1653. VerifyTerminator(String(p, r.byte_width_));
  1654. case FBT_VECTOR_INT2:
  1655. case FBT_VECTOR_UINT2:
  1656. case FBT_VECTOR_FLOAT2:
  1657. case FBT_VECTOR_INT3:
  1658. case FBT_VECTOR_UINT3:
  1659. case FBT_VECTOR_FLOAT3:
  1660. case FBT_VECTOR_INT4:
  1661. case FBT_VECTOR_UINT4:
  1662. case FBT_VECTOR_FLOAT4: {
  1663. uint8_t len = 0;
  1664. auto vtype = ToFixedTypedVectorElementType(r.type_, &len);
  1665. if (!VerifyType(vtype))
  1666. return false;
  1667. return VerifyFromPointer(p, r.byte_width_ * len);
  1668. }
  1669. default:
  1670. return false;
  1671. }
  1672. }
  1673. public:
  1674. bool VerifyBuffer() {
  1675. if (!Check(size_ >= 3)) return false;
  1676. auto end = buf_ + size_;
  1677. auto byte_width = *--end;
  1678. auto packed_type = *--end;
  1679. return VerifyByteWidth(byte_width) &&
  1680. Check(end - buf_ >= byte_width) &&
  1681. VerifyRef(Reference(end - byte_width, byte_width, packed_type));
  1682. }
  1683. private:
  1684. const uint8_t *buf_;
  1685. size_t size_;
  1686. size_t depth_;
  1687. const size_t max_depth_;
  1688. size_t num_vectors_;
  1689. const size_t max_vectors_;
  1690. bool check_alignment_;
  1691. std::vector<uint8_t> *reuse_tracker_;
  1692. };
  1693. // Utility function that contructs the Verifier for you, see above for parameters.
  1694. inline bool VerifyBuffer(const uint8_t *buf, size_t buf_len,
  1695. std::vector<uint8_t> *reuse_tracker = nullptr) {
  1696. Verifier verifier(buf, buf_len, reuse_tracker);
  1697. return verifier.VerifyBuffer();
  1698. }
  1699. #ifdef FLATBUFFERS_H_
  1700. // This is a verifier utility function that works together with the
  1701. // FlatBuffers verifier, which should only be present if flatbuffer.h
  1702. // has been included (which it typically is in generated code).
  1703. inline bool VerifyNestedFlexBuffer(const flatbuffers::Vector<uint8_t> *nv,
  1704. flatbuffers::Verifier &verifier) {
  1705. if (!nv) return true;
  1706. return verifier.Check(
  1707. flexbuffers::VerifyBuffer(nv->data(), nv->size(),
  1708. verifier.GetFlexReuseTracker()));
  1709. }
  1710. #endif
  1711. } // namespace flexbuffers
  1712. #if defined(_MSC_VER)
  1713. # pragma warning(pop)
  1714. #endif
  1715. #endif // FLATBUFFERS_FLEXBUFFERS_H_