Search code examples
c++flatbuffers

Access field value raw bytes from a constructed Flatbuffer


Say I have following Flatbuffer definition compiled to C++.

table ParentObj { // this is the root table
    timestamp:uint64;
    child:ChildObj
}

table ChildObj {
    ...some fields...
}

I build a ParentObj (which includes the ChildObj) with Flatbuffer builder and send the final bytes over the network to another party. Is there a way the receiver can access the raw bytes making up the ChildObj inside the received buffer? I can access individual fields in the child obj via the Flatbuffer generated C++ code interface. But can I get the buffer offset and length of the bytes making up the entire ChildObj object? I need this to generate a cryptographic signature of the ChildObj bytes.


Solution

  • No, because ChildObj is not necessarily contiguous in the buffer. It refers to a vtable (which may or may not be shared), and any sub-string/vector/table is an offset that may point to a non-adjacent part of the buffer.

    Typically, you use nested_flatbuffer to store children that need to be treated as their own isolated buffer further down the line: child:[ubyte] (nested_flatbuffer: ChildObj).

    However, getting a cryptographic signature from this is still a bad idea, since depending on how it was serialized (which implementation) the bytes may differ subtly, due to difference in alignment and ordering of fields/objects. To reliably get a hash out of this, you'd need to only has the actual data bytes, and in a fixed field order.