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