From 3c3742a54a47ccfbf2fb82d641def01a493b64b7 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Fri, 16 Jun 2017 12:30:36 -0700 Subject: [PATCH] Adding a method to get the file identifier from a flatbuffer. Change-Id: Ie28fd1f0b463aac23647d38921600f15c8b7c10a --- include/flatbuffers/flatbuffers.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 4ca829547..1f45668c8 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -1516,9 +1516,21 @@ template const T *GetTemporaryPointer(FlatBufferBuilder &fbb, return GetMutableTemporaryPointer(fbb, offset); } + +/// @brief Get a pointer to the the file_identifier section of the buffer. +/// @return Returns a const char pointer to the start of the file_identifier +/// characters in the buffer. The returned char * has length +/// 'flatbuffers::FlatBufferBuilder::kFileIdentifierLength'. +/// This function is UNDEFINED for FlatBuffers whose schema does not include +/// a file_identifier (likely points at padding or the start of a the root +/// vtable). +inline const char *GetBufferIdentifier(const void *buf) { + return reinterpret_cast(buf) + sizeof(uoffset_t); +} + // Helper to see if the identifier in a buffer has the expected value. inline bool BufferHasIdentifier(const void *buf, const char *identifier) { - return strncmp(reinterpret_cast(buf) + sizeof(uoffset_t), + return strncmp(GetBufferIdentifier(buf), identifier, FlatBufferBuilder::kFileIdentifierLength) == 0; }