1*890232f2SAndroid Build Coastguard Worker# Annotated Flatbuffer Binary 2*890232f2SAndroid Build Coastguard Worker 3*890232f2SAndroid Build Coastguard WorkerThis directory demonstrates the ability of flatc to annotate binary flatbuffers 4*890232f2SAndroid Build Coastguard Workerwith helpful annotations. The resulting annotated flatbuffer binary (afb) 5*890232f2SAndroid Build Coastguard Workercontains all the binary data with line-by-line annotations. 6*890232f2SAndroid Build Coastguard Worker 7*890232f2SAndroid Build Coastguard Worker## Usage 8*890232f2SAndroid Build Coastguard Worker 9*890232f2SAndroid Build Coastguard WorkerGiven a `schema` in either plain-text (.fbs) or already compiled to a binary 10*890232f2SAndroid Build Coastguard Workerschema (.bfbs) and `binary` file(s) that was created by the `schema`. 11*890232f2SAndroid Build Coastguard Worker 12*890232f2SAndroid Build Coastguard Worker```sh 13*890232f2SAndroid Build Coastguard Workerflatc --annotate {schema_file} -- {binary_file}... 14*890232f2SAndroid Build Coastguard Worker``` 15*890232f2SAndroid Build Coastguard Worker 16*890232f2SAndroid Build Coastguard Worker### Example 17*890232f2SAndroid Build Coastguard Worker 18*890232f2SAndroid Build Coastguard WorkerThe following command should produce `annotated_binary.afb` in this directory: 19*890232f2SAndroid Build Coastguard Worker 20*890232f2SAndroid Build Coastguard Worker```sh 21*890232f2SAndroid Build Coastguard Workercd tests\annotated_binary 22*890232f2SAndroid Build Coastguard Worker..\..\flatc --annotate annotated_binary.fbs -- annotated_binary.bin 23*890232f2SAndroid Build Coastguard Worker``` 24*890232f2SAndroid Build Coastguard Worker 25*890232f2SAndroid Build Coastguard WorkerThe `annotated_binary.bin` is the flatbufer binary of the data contained within 26*890232f2SAndroid Build Coastguard Worker `annotated_binary.json`, which was made by the following command: 27*890232f2SAndroid Build Coastguard Worker 28*890232f2SAndroid Build Coastguard Worker```sh 29*890232f2SAndroid Build Coastguard Worker..\..\flatc -b annotated_binary.fbs annotated_binary.json 30*890232f2SAndroid Build Coastguard Worker``` 31*890232f2SAndroid Build Coastguard Worker 32*890232f2SAndroid Build Coastguard Worker## Text Format 33*890232f2SAndroid Build Coastguard Worker 34*890232f2SAndroid Build Coastguard WorkerCurrently there is a built-in text-based format for outputting the annotations. 35*890232f2SAndroid Build Coastguard WorkerThe `annotated_binary.afb` is an example of the text format of a binary 36*890232f2SAndroid Build Coastguard Worker`annotated_binary.bin` and the `annotated_binary.fbs` (or 37*890232f2SAndroid Build Coastguard Worker`annotated_binary.bfbs`) schema. 38*890232f2SAndroid Build Coastguard Worker 39*890232f2SAndroid Build Coastguard WorkerThe file is ordered in increasing the offsets from the beginning of the binary. 40*890232f2SAndroid Build Coastguard WorkerThe offset is the 1st column, expressed in hexadecimal format (e.g. `+0x003c`). 41*890232f2SAndroid Build Coastguard Worker 42*890232f2SAndroid Build Coastguard Worker### Binary Sections 43*890232f2SAndroid Build Coastguard Worker 44*890232f2SAndroid Build Coastguard WorkerBinary sections are comprised of contigious [binary regions](#binary-regions) 45*890232f2SAndroid Build Coastguard Workerthat are logically grouped together. For example, a binary section may be a 46*890232f2SAndroid Build Coastguard Workersingle instance of a flatbuffer `Table` or its `vtable`. The sections may be 47*890232f2SAndroid Build Coastguard Workerlabelled with the name of the associated type, as defined in the input schema. 48*890232f2SAndroid Build Coastguard Worker 49*890232f2SAndroid Build Coastguard WorkerExample of a `vtable` Binary Section that is associated with the user-defined 50*890232f2SAndroid Build Coastguard Worker`AnnotateBinary.Bar` table. 51*890232f2SAndroid Build Coastguard Worker 52*890232f2SAndroid Build Coastguard Worker``` 53*890232f2SAndroid Build Coastguard Workervtable (AnnotatedBinary.Bar): 54*890232f2SAndroid Build Coastguard Worker +0x00A0 | 08 00 | uint16_t | 0x0008 (8) | size of this vtable 55*890232f2SAndroid Build Coastguard Worker +0x00A2 | 13 00 | uint16_t | 0x0013 (19) | size of referring table 56*890232f2SAndroid Build Coastguard Worker +0x00A4 | 08 00 | VOffset16 | 0x0008 (8) | offset to field `a` (id: 0) 57*890232f2SAndroid Build Coastguard Worker +0x00A6 | 04 00 | VOffset16 | 0x0004 (4) | offset to field `b` (id: 1) 58*890232f2SAndroid Build Coastguard Worker``` 59*890232f2SAndroid Build Coastguard Worker 60*890232f2SAndroid Build Coastguard Worker### Binary Regions 61*890232f2SAndroid Build Coastguard Worker 62*890232f2SAndroid Build Coastguard WorkerBinary regions are contigious bytes regions that are grouped together to form 63*890232f2SAndroid Build Coastguard Workersome sort of value, e.g. a `scalar` or an array of scalars. A binary region may 64*890232f2SAndroid Build Coastguard Workerbe split up over multiple text lines, if the size of the region is large. 65*890232f2SAndroid Build Coastguard Worker 66*890232f2SAndroid Build Coastguard WorkerLooking at an example binary region: 67*890232f2SAndroid Build Coastguard Worker 68*890232f2SAndroid Build Coastguard Worker``` 69*890232f2SAndroid Build Coastguard Workervtable (AnnotatedBinary.Bar): 70*890232f2SAndroid Build Coastguard Worker +0x00A0 | 08 00 | uint16_t | 0x0008 (8) | size of this vtable 71*890232f2SAndroid Build Coastguard Worker``` 72*890232f2SAndroid Build Coastguard Worker 73*890232f2SAndroid Build Coastguard WorkerThe first column (`+0x00A0`) is the offset to this region from the beginning of 74*890232f2SAndroid Build Coastguard Workerthe buffer. 75*890232f2SAndroid Build Coastguard Worker 76*890232f2SAndroid Build Coastguard WorkerThe second column are the raw bytes (hexadecimal) that make up this 77*890232f2SAndroid Build Coastguard Workerregion. These are expressed in the little-endian format that flatbuffers uses 78*890232f2SAndroid Build Coastguard Workerfor the wire format. 79*890232f2SAndroid Build Coastguard Worker 80*890232f2SAndroid Build Coastguard WorkerThe third column is the type to interpret the bytes as. Some types are special 81*890232f2SAndroid Build Coastguard Workerto flatbuffer internals (e.g. `SOffet32`, `Offset32`, and `VOffset16`) which are 82*890232f2SAndroid Build Coastguard Workerused by flatbuffers to point to various offsetes. The other types are specified 83*890232f2SAndroid Build Coastguard Workeras C++-like types which are the standard fix-width scalars. For the above 84*890232f2SAndroid Build Coastguard Workerexample, the type is `uint16_t` which is a 16-bit unsigned integer type. 85*890232f2SAndroid Build Coastguard Worker 86*890232f2SAndroid Build Coastguard WorkerThe fourth column shows the raw bytes as a compacted, big-endian value. The raw 87*890232f2SAndroid Build Coastguard Workerbytes are duplicated in this fashion since it is more intutive to read the data 88*890232f2SAndroid Build Coastguard Workerin the big-endian format (e.g., `0x0008`). This value is followed by the decimal 89*890232f2SAndroid Build Coastguard Workerrepresentation of the value (e.g., `(8)`). (For strings, the raw string value 90*890232f2SAndroid Build Coastguard Workeris shown instead). 91*890232f2SAndroid Build Coastguard Worker 92*890232f2SAndroid Build Coastguard WorkerThe fifth column is a textual comment on what the value is. As much metadata as 93*890232f2SAndroid Build Coastguard Workerknown is provided. 94*890232f2SAndroid Build Coastguard Worker 95*890232f2SAndroid Build Coastguard Worker#### Offsets 96*890232f2SAndroid Build Coastguard Worker 97*890232f2SAndroid Build Coastguard WorkerIf the type in the 3rd column is of an absolute offset (`SOffet32` or 98*890232f2SAndroid Build Coastguard Worker`Offset32`), the fourth column also shows an `Loc: +0x025A` value which shows 99*890232f2SAndroid Build Coastguard Workerwhere in the binary this region is pointing to. These values are absolute from 100*890232f2SAndroid Build Coastguard Workerthe beginning of the file, their calculation from the raw value in the 4th 101*890232f2SAndroid Build Coastguard Workercolumn depends on the context. 102