// A small FlatBuffers builder. // // HyperHDR's image input speaks the hyperion.ng FlatBuffers schema. The // upstream client generates code with flatcc and carries it as a submodule; // that is a lot of build machinery for four message shapes, so this // implements the builder algorithm directly. It follows the same back-to-front // construction as the reference implementations, so buffers it produces are // byte-comparable with flatc-generated output (minus vtable deduplication, // which is an optimisation, not a format requirement). #pragma once #include #include #include #define FB_MAX_FIELDS 8 typedef struct { uint8_t* bytes; // full allocation; live data is bytes[head..cap) size_t cap; size_t head; size_t minalign; uint16_t vtable[FB_MAX_FIELDS]; int vtable_count; size_t object_end; // fb_offset() captured at fb_start_table bool nested; bool failed; } fb_t; bool fb_init(fb_t* b, size_t initial_capacity); void fb_free(fb_t* b); // Offsets are distances from the end of the buffer, matching the reference // builders. Zero means "absent". uint32_t fb_create_uint8_vector(fb_t* b, const uint8_t* data, size_t count); uint32_t fb_create_string(fb_t* b, const char* s); void fb_start_table(fb_t* b, int num_fields); void fb_add_offset(fb_t* b, int slot, uint32_t offset); void fb_add_int32(fb_t* b, int slot, int32_t value, int32_t default_value); void fb_add_uint8(fb_t* b, int slot, uint8_t value, uint8_t default_value); uint32_t fb_end_table(fb_t* b); void fb_finish(fb_t* b, uint32_t root); // Valid until the next mutation or fb_free. const uint8_t* fb_data(const fb_t* b, size_t* len); bool fb_ok(const fb_t* b);