From b561f6746546b99caf21b58e431e07c8a6ec47fa Mon Sep 17 00:00:00 2001 From: Tyler Young Date: Thu, 29 Jul 2021 10:39:58 -0400 Subject: [PATCH 1/4] add FLATCC_OFFSET_SIZE to CMakeLists.txt --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a201691..70979abd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -294,6 +294,11 @@ if (FLATCC_PORTABLE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_PORTABLE") endif() + +if (DEFINED FLATCC_OFFSET_SIZE) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_OFFSET_SIZE=${FLATCC_OFFSET_SIZE}") +endif() + if (CLANG_VERSION) message(STATUS "CLANG_VERSION: ${CLANG_VERSION}") endif() From 6b72542956a7ec83e4a97a96b98d86f310336588 Mon Sep 17 00:00:00 2001 From: Tyler Young Date: Thu, 29 Jul 2021 15:28:46 -0400 Subject: [PATCH 2/4] support variable offset sizes for real --- CMakeLists.txt | 32 ++++++- include/flatcc/flatcc_identifier.h | 63 ++++++++++++-- include/flatcc/flatcc_json_parser.h | 6 +- include/flatcc/flatcc_json_printer.h | 8 +- include/flatcc/flatcc_types.h | 85 ++++++++++++++----- include/flatcc/flatcc_verifier.h | 10 +-- .../reflection/flatbuffers_common_reader.h | 10 +-- .../flatcc/reflection/reflection_verifier.h | 18 ++-- src/compiler/codegen_c.h | 37 ++++++++ src/compiler/codegen_c_builder.c | 11 ++- src/compiler/codegen_c_json_parser.c | 9 +- src/compiler/codegen_c_json_printer.c | 15 ++-- src/compiler/codegen_c_reader.c | 30 +++---- src/compiler/codegen_c_verifier.c | 4 +- src/compiler/parser.c | 17 +++- src/runtime/builder.c | 6 +- src/runtime/json_parser.c | 14 +-- src/runtime/json_printer.c | 10 +-- src/runtime/verifier.c | 10 +-- test/monster_test/monster_test.fbs | 2 +- 20 files changed, 287 insertions(+), 110 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70979abd..fd53643a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,23 @@ option (FLATCC_ALLOW_WERROR "allow -Werror to be configured" ON) # try using this option. option (FLATCC_IGNORE_CONST_COND "silence const condition warnings" OFF) +# Enforces parsing of identifier size per the spec of 4 characters. +# FLATBUFFERS_STRICT_IDENTIFIER_SIZE has no effect if the identifier +# size is 4. +# Note: Encoded identifiers will still be padded when FLATCC_OFFSET_SIZE +# is greater than 4 and truncated when offset size is less than 4. +option (FLATBUFFERS_STRICT_IDENTIFIER_SIZE + "enforce identifier size being offset size" OFF) + +# Relaxes the size specification for file identifier sizes to allow them +# to be padded if shorter or truncated if longer. +# The restriction prohibits interpreting schema oriented at other offset +# sizes, so this is implied when FLATCC_OFFSET_SIZE is not 4. +# This option expects FLATBUFFERS_STRICT_IDENTIFIER_SIZE to be OFF. +option (FLATBUFFERS_RELAXED_IDENTIFIER_SIZE + "identifier will be padded if too short or truncated if too long" + OFF) + if (FLATCC_RTONLY) set(FLATCC_TEST off) endif() @@ -179,7 +196,7 @@ if (CMAKE_C_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra") # Fix broken C++ alignas - either will do set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPORTABLE_PATCH_CPLUSPLUS_STDALIGN") + #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPORTABLE_PATCH_CPLUSPLUS_STDALIGN") if (FLATCC_ALLOW_WERROR) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") endif() @@ -219,7 +236,7 @@ elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU") # structs, but these are valid as zero-paddded, not zero terminated. # # -Wno-format-overflow: - # GCC 9 warns on mistakenly assumed NULL string when + # GCC 9 warns on mistakenly assumed NULL string when # printing from a required FlatBuffer string field. # message(STATUS "Disabling GNU C compiler warnings: -Wstringop-truncation -Wno-format-overflow") @@ -294,11 +311,20 @@ if (FLATCC_PORTABLE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_PORTABLE") endif() - if (DEFINED FLATCC_OFFSET_SIZE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_OFFSET_SIZE=${FLATCC_OFFSET_SIZE}") endif() +if (DEFINED FLATCC_VOFFSET_SIZE) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_VOFFSET_SIZE=${FLATCC_VOFFSET_SIZE}") +endif() + +if (DEFINED FLATCC_OFFSET_SIZE AND NOT FLATCC_OFFSET_SIZE EQUAL 4 AND NOT DEFINED FLATBUFFERS_STRICT_IDENTIFIER_SIZE) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATBUFFERS_RELAXED_IDENTIFIER_SIZE") +elseif(DEFINED FLATBUFFERS_RELAXED_IDENTIFIER_SIZE) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATBUFFERS_RELAXED_IDENTIFIER_SIZE") +endif() + if (CLANG_VERSION) message(STATUS "CLANG_VERSION: ${CLANG_VERSION}") endif() diff --git a/include/flatcc/flatcc_identifier.h b/include/flatcc/flatcc_identifier.h index 825f0fda..e331d4a1 100644 --- a/include/flatcc/flatcc_identifier.h +++ b/include/flatcc/flatcc_identifier.h @@ -66,10 +66,24 @@ static inline void flatbuffers_identifier_from_type_hash(flatbuffers_thash_t typ out_identifier[0] = (char)(type_hash & 0xff); type_hash >>= 8; out_identifier[1] = (char)(type_hash & 0xff); + +#if FLATBUFFERS_THASH_WIDTH > 16 type_hash >>= 8; out_identifier[2] = (char)(type_hash & 0xff); type_hash >>= 8; out_identifier[3] = (char)(type_hash & 0xff); +#endif + +#if FLATBUFFERS_THASH_WIDTH > 32 + type_hash >>= 8; + out_identifier[4] = (char)(type_hash & 0xff); + type_hash >>= 8; + out_identifier[5] = (char)(type_hash & 0xff); + type_hash >>= 8; + out_identifier[6] = (char)(type_hash & 0xff); + type_hash >>= 8; + out_identifier[7] = (char)(type_hash & 0xff); +#endif } /* Native integer encoding of file identifier. */ @@ -77,8 +91,16 @@ static inline flatbuffers_thash_t flatbuffers_type_hash_from_identifier(const fl { uint8_t *p = (uint8_t *)identifier; - return identifier ? - (uint32_t)p[0] + (((uint32_t)p[1]) << 8) + (((uint32_t)p[2]) << 16) + (((uint32_t)p[3]) << 24) : 0; + return !identifier ? 0 : + +#if FLATBUFFERS_THASH_WIDTH == 16 + (uint16_t)p[0] + (((uint16_t)p[1]) << 8); +#elif FLATBUFFERS_THASH_WIDTH == 32 + (uint32_t)p[0] + (((uint32_t)p[1]) << 8) + (((uint32_t)p[2]) << 16) + (((uint32_t)p[3]) << 24); +#elif FLATBUFFERS_THASH_WIDTH == 64 + (uint64_t)p[0] + (((uint64_t)p[1]) << 8) + (((uint64_t)p[2]) << 16) + (((uint64_t)p[3]) << 24) + + (((uint64_t)p[4]) << 32) + (((uint64_t)p[5]) << 40) + (((uint64_t)p[6]) << 48) + (((uint64_t)p[7]) << 56); +#endif } /* @@ -91,14 +113,31 @@ static inline flatbuffers_thash_t flatbuffers_type_hash_from_string(const char * flatbuffers_thash_t h = 0; const uint8_t *p = (const uint8_t *)identifier; + if (!p) return 0; + if (!p[0]) return h; h += ((flatbuffers_thash_t)p[0]); if (!p[1]) return h; h += ((flatbuffers_thash_t)p[1]) << 8; + +#if FLATBUFFERS_THASH_WIDTH > 16 if (!p[2]) return h; h += ((flatbuffers_thash_t)p[2]) << 16; - /* No need to test for termination here. */ + if (!p[3]) return h; h += ((flatbuffers_thash_t)p[3]) << 24; +#endif + +#if FLATBUFFERS_THASH_WIDTH > 32 + if (!p[4]) return h; + h += ((flatbuffers_thash_t)p[4]) << 32; + if (!p[5]) return h; + h += ((flatbuffers_thash_t)p[5]) << 40; + if (!p[6]) return h; + h += ((flatbuffers_thash_t)p[6]) << 48; + if (!p[7]) return h; + h += ((flatbuffers_thash_t)p[7]) << 56; +#endif + return h; } @@ -125,21 +164,31 @@ static inline void flatbuffers_identifier_from_name(const char *name, flatbuffer * additional information and just complicates matters. Furthermore, the * unmodified type hash has the benefit that it can seed a child namespace. */ -static inline uint32_t flatbuffers_disperse_type_hash(flatbuffers_thash_t type_hash) +static inline flatbuffers_thash_t flatbuffers_disperse_type_hash(flatbuffers_thash_t type_hash) { + flatbuffers_thash_t x = type_hash; + +#if FLATBUFFERS_THASH_WIDTH == 32 /* http://stackoverflow.com/a/12996028 */ - uint32_t x = type_hash; x = ((x >> 16) ^ x) * UINT32_C(0x45d9f3b); x = ((x >> 16) ^ x) * UINT32_C(0x45d9f3b); x = ((x >> 16) ^ x); +#elif FLATBUFFERS_THASH_WIDTH == 64 + /* http://stackoverflow.com/a/12996028 */ + + x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); + x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); + x = x ^ (x >> 31); +#endif + return x; } /* We have hardcoded assumptions about identifier size. */ -static_assert(sizeof(flatbuffers_fid_t) == 4, "unexpected file identifier size"); -static_assert(sizeof(flatbuffers_thash_t) == 4, "unexpected type hash size"); +//static_assert(sizeof(flatbuffers_fid_t) == 4, "unexpected file identifier size"); +//static_assert(sizeof(flatbuffers_thash_t) == 4, "unexpected type hash size"); #ifdef __cplusplus } diff --git a/include/flatcc/flatcc_json_parser.h b/include/flatcc/flatcc_json_parser.h index 1907fc7f..f3f751ee 100644 --- a/include/flatcc/flatcc_json_parser.h +++ b/include/flatcc/flatcc_json_parser.h @@ -168,7 +168,7 @@ static inline const char *flatcc_json_parser_string_end(flatcc_json_parser_t *ct * and raise errors according to overflow/underflow runtime flags. Zero * and truncate as needed. A trailing zero is not inserted if the input * is at least the same length as the char array. - * + * * Runtime flags: `skip_array_overflow`, `pad_array_underflow`. */ const char *flatcc_json_parser_char_array(flatcc_json_parser_t *ctx, @@ -875,7 +875,7 @@ const char *flatcc_json_parser_union_type_vector(flatcc_json_parser_t *ctx, * `flags` default to 0. See also `flatcc_json_parser_flags`. */ int flatcc_json_parser_table_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, - const char *buf, size_t bufsiz, int flags, const char *fid, + const char *buf, size_t bufsiz, int flags, const flatbuffers_fid_t fid, flatcc_json_parser_table_f *parser); /* @@ -883,7 +883,7 @@ int flatcc_json_parser_table_as_root(flatcc_builder_t *B, flatcc_json_parser_t * * root. */ int flatcc_json_parser_struct_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, - const char *buf, size_t bufsiz, int flags, const char *fid, + const char *buf, size_t bufsiz, int flags, const flatbuffers_fid_t fid, flatcc_json_parser_struct_f *parser); #include "flatcc/portable/pdiagnostic_pop.h" diff --git a/include/flatcc/flatcc_json_printer.h b/include/flatcc/flatcc_json_printer.h index 0ce49c14..28a89de3 100644 --- a/include/flatcc/flatcc_json_printer.h +++ b/include/flatcc/flatcc_json_printer.h @@ -679,11 +679,11 @@ void flatcc_json_printer_uint8_vector_base64_field(flatcc_json_printer_t *ctx, * require aligned memory addresses (as always for flatbuffers). */ int flatcc_json_printer_table_as_root(flatcc_json_printer_t *ctx, - const void *buf, size_t bufsiz, const char *fid, + const void *buf, size_t bufsiz, const flatbuffers_fid_t fid, flatcc_json_printer_table_f *pf); int flatcc_json_printer_struct_as_root(flatcc_json_printer_t *ctx, - const void *buf, size_t bufsiz, const char *fid, + const void *buf, size_t bufsiz, const flatbuffers_fid_t fid, flatcc_json_printer_struct_f *pf); /* @@ -756,13 +756,13 @@ void flatcc_json_printer_union_vector_field(flatcc_json_printer_t *ctx, void flatcc_json_printer_struct_as_nested_root(flatcc_json_printer_t *ctx, flatcc_json_printer_table_descriptor_t *td, int id, const char *name, size_t len, - const char *fid, + const flatbuffers_fid_t fid, flatcc_json_printer_struct_f *pf); void flatcc_json_printer_table_as_nested_root(flatcc_json_printer_t *ctx, flatcc_json_printer_table_descriptor_t *td, int id, const char *name, size_t len, - const char *fid, + const flatbuffers_fid_t fid, flatcc_json_printer_table_f pf); void flatcc_json_printer_union_field(flatcc_json_printer_t *ctx, diff --git a/include/flatcc/flatcc_types.h b/include/flatcc/flatcc_types.h index 69605d2d..12e69157 100644 --- a/include/flatcc/flatcc_types.h +++ b/include/flatcc/flatcc_types.h @@ -11,6 +11,8 @@ extern "C" { #include #endif +#include "../../config/config.h" + /* * This should match generated type declaratios in * `flatbuffers_common_reader.h` (might have different name prefix). @@ -42,29 +44,11 @@ extern "C" { #define flatbuffers_utype_t_defined #define flatbuffers_bool_t_defined #define flatbuffers_thash_t_defined -#define flatbuffers_fid_t_defined - -/* uoffset_t is also used for vector and string headers. */ -#define FLATBUFFERS_UOFFSET_MAX UINT32_MAX -#define FLATBUFFERS_SOFFSET_MAX INT32_MAX -#define FLATBUFFERS_SOFFSET_MIN INT32_MIN -#define FLATBUFFERS_VOFFSET_MAX UINT16_MAX -#define FLATBUFFERS_UTYPE_MAX UINT8_MAX -/* Well - the max of the underlying type. */ -#define FLATBUFFERS_BOOL_MAX UINT8_MAX -#define FLATBUFFERS_THASH_MAX UINT32_MAX #define FLATBUFFERS_ID_MAX (FLATBUFFERS_VOFFSET_MAX / sizeof(flatbuffers_voffset_t) - 3) /* Vectors of empty structs can yield div by zero, so we must guard against this. */ #define FLATBUFFERS_COUNT_MAX(elem_size) (FLATBUFFERS_UOFFSET_MAX/((elem_size) == 0 ? 1 : (elem_size))) -#define FLATBUFFERS_UOFFSET_WIDTH 32 -#define FLATBUFFERS_COUNT_WIDTH 32 -#define FLATBUFFERS_SOFFSET_WIDTH 32 -#define FLATBUFFERS_VOFFSET_WIDTH 16 -#define FLATBUFFERS_UTYPE_WIDTH 8 -#define FLATBUFFERS_BOOL_WIDTH 8 -#define FLATBUFFERS_THASH_WIDTH 32 #define FLATBUFFERS_TRUE 1 #define FLATBUFFERS_FALSE 0 @@ -72,22 +56,83 @@ extern "C" { #define FLATBUFFERS_PROTOCOL_IS_LE 1 #define FLATBUFFERS_PROTOCOL_IS_BE 0 +/* uoffset_t is also used for vector and string headers. */ +#if FLATCC_OFFSET_SIZE == 4 typedef uint32_t flatbuffers_uoffset_t; typedef int32_t flatbuffers_soffset_t; +typedef uint32_t flatbuffers_thash_t; +#define FLATBUFFERS_UOFFSET_MAX UINT32_MAX +#define FLATBUFFERS_SOFFSET_MAX INT32_MAX +#define FLATBUFFERS_SOFFSET_MIN INT32_MIN +#define FLATBUFFERS_THASH_MAX UINT32_MAX +#define FLATBUFFERS_UOFFSET_WIDTH 32 +#define FLATBUFFERS_SOFFSET_WIDTH 32 +#define FLATBUFFERS_THASH_WIDTH 32 +#elif FLATCC_OFFSET_SIZE == 8 +typedef uint64_t flatbuffers_uoffset_t; +typedef int64_t flatbuffers_soffset_t; +typedef uint64_t flatbuffers_thash_t; +#define FLATBUFFERS_UOFFSET_MAX UINT64_MAX +#define FLATBUFFERS_SOFFSET_MAX INT64_MAX +#define FLATBUFFERS_SOFFSET_MIN INT64_MIN +#define FLATBUFFERS_THASH_MAX UINT64_MAX +#define FLATBUFFERS_UOFFSET_WIDTH 64 +#define FLATBUFFERS_SOFFSET_WIDTH 64 +#define FLATBUFFERS_THASH_WIDTH 64 +#elif FLATCC_OFFSET_SIZE == 2 +typedef uint16_t flatbuffers_uoffset_t; +typedef int16_t flatbuffers_soffset_t; +typedef uint16_t flatbuffers_thash_t; +#define FLATBUFFERS_UOFFSET_MAX UINT16_MAX +#define FLATBUFFERS_SOFFSET_MAX INT16_MAX +#define FLATBUFFERS_SOFFSET_MIN INT16_MIN +#define FLATBUFFERS_THASH_MAX UINT16_MAX +#define FLATBUFFERS_UOFFSET_WIDTH 16 +#define FLATBUFFERS_SOFFSET_WIDTH 16 +#define FLATBUFFERS_THASH_WIDTH 16 +#else +#error FLATCC_OFFSET_SIZE must be defined. +#endif + +#if FLATCC_VOFFSET_SIZE == 2 typedef uint16_t flatbuffers_voffset_t; +#define FLATBUFFERS_VOFFSET_MAX UINT16_MAX +#define FLATBUFFERS_VOFFSET_WIDTH 16 +#elif FLATCC_VOFFSET_SIZE == 8 +typedef uint64_t flatbuffers_voffset_t; +#define FLATBUFFERS_VOFFSET_MAX UINT64_MAX +#define FLATBUFFERS_VOFFSET_WIDTH 64 +#elif FLATCC_VOFFSET_SIZE == 4 +typedef uint32_t flatbuffers_voffset_t; +#define FLATBUFFERS_VOFFSET_MAX UINT32_MAX +#define FLATBUFFERS_VOFFSET_WIDTH 32 +#else +#error FLATCC_VOFFSET_SIZE must be defined. +#endif + +#define FLATBUFFERS_UTYPE_MAX UINT8_MAX +#define FLATBUFFERS_UTYPE_WIDTH 8 typedef uint8_t flatbuffers_utype_t; + +/* Well - the max of the underlying type. */ +#define FLATBUFFERS_BOOL_MAX UINT8_MAX +#define FLATBUFFERS_BOOL_WIDTH 8 typedef uint8_t flatbuffers_bool_t; -typedef uint32_t flatbuffers_thash_t; + /* Public facing type operations. */ typedef flatbuffers_utype_t flatbuffers_union_type_t; static const flatbuffers_bool_t flatbuffers_true = FLATBUFFERS_TRUE; static const flatbuffers_bool_t flatbuffers_false = FLATBUFFERS_FALSE; -#define FLATBUFFERS_IDENTIFIER_SIZE (FLATBUFFERS_THASH_WIDTH / 8) +#ifndef flatbuffers_fid_t_defined +#define FLATBUFFERS_IDENTIFIER_SIZE FLATCC_OFFSET_SIZE typedef char flatbuffers_fid_t[FLATBUFFERS_IDENTIFIER_SIZE]; +#define flatbuffers_fid_t_defined +#endif + #endif /* flatbuffers_types_defined */ #ifdef __cplusplus diff --git a/include/flatcc/flatcc_verifier.h b/include/flatcc/flatcc_verifier.h index 7e0d2966..17b42bb4 100644 --- a/include/flatcc/flatcc_verifier.h +++ b/include/flatcc/flatcc_verifier.h @@ -164,13 +164,13 @@ typedef int flatcc_union_verifier_f(flatcc_union_verifier_descriptor_t *ud); * require aligned memory addresses. The buffer pointers alignment is * not significant to internal verification of the buffer. */ -int flatcc_verify_struct_as_root(const void *buf, size_t bufsiz, const char *fid, +int flatcc_verify_struct_as_root(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid, size_t size, uint16_t align); int flatcc_verify_struct_as_typed_root(const void *buf, size_t bufsiz, flatbuffers_thash_t thash, size_t size, uint16_t align); -int flatcc_verify_table_as_root(const void *buf, size_t bufsiz, const char *fid, +int flatcc_verify_table_as_root(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid, flatcc_table_verifier_f *root_tvf); int flatcc_verify_table_as_typed_root(const void *buf, size_t bufsiz, flatbuffers_thash_t thash, @@ -179,7 +179,7 @@ int flatcc_verify_table_as_typed_root(const void *buf, size_t bufsiz, flatbuffer * The buffer header is verified by any of the `_as_root` verifiers, but * this function may be used as a quick sanity check. */ -int flatcc_verify_buffer_header(const void *buf, size_t bufsiz, const char *fid); +int flatcc_verify_buffer_header(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid); int flatcc_verify_typed_buffer_header(const void *buf, size_t bufsiz, flatbuffers_thash_t type_hash); @@ -204,10 +204,10 @@ int flatcc_verify_table_vector_field(flatcc_table_verifier_descriptor_t *td, flatbuffers_voffset_t id, int required, flatcc_table_verifier_f tvf); /* Table verifiers pass 0 as fid. */ int flatcc_verify_struct_as_nested_root(flatcc_table_verifier_descriptor_t *td, - flatbuffers_voffset_t id, int required, const char *fid, + flatbuffers_voffset_t id, int required, const flatbuffers_fid_t fid, size_t size, uint16_t align); int flatcc_verify_table_as_nested_root(flatcc_table_verifier_descriptor_t *td, - flatbuffers_voffset_t id, int required, const char *fid, + flatbuffers_voffset_t id, int required, const flatbuffers_fid_t fid, uint16_t align, flatcc_table_verifier_f tvf); /* diff --git a/include/flatcc/reflection/flatbuffers_common_reader.h b/include/flatcc/reflection/flatbuffers_common_reader.h index 67971e9a..9335078c 100644 --- a/include/flatcc/reflection/flatbuffers_common_reader.h +++ b/include/flatcc/reflection/flatbuffers_common_reader.h @@ -529,7 +529,7 @@ __flatbuffers_define_scan_by_scalar_field(N, NK, T) static inline T N ## _ ## NK ## _get(N ## _struct_t t__tmp) { return t__tmp ? &(t__tmp->NK) : 0; }\ static inline T N ## _ ## NK (N ## _struct_t t__tmp) { return t__tmp ? &(t__tmp->NK) : 0; } /* If fid is null, the function returns true without testing as buffer is not expected to have any id. */ -static inline int flatbuffers_has_identifier(const void *buffer, const char *fid) +static inline int flatbuffers_has_identifier(const void *buffer, const flatbuffers_fid_t fid) { flatbuffers_thash_t id, id2 = 0; if (fid == 0) { return 1; }; id2 = flatbuffers_type_hash_from_string(fid); id = __flatbuffers_thash_read_from_pe(((flatbuffers_uoffset_t *)buffer) + 1); @@ -554,20 +554,20 @@ static inline void *flatbuffers_read_size_prefix(void *b, size_t *size_out) ((T ## _ ## K ## t)(((uint8_t *)buffer) +\ __flatbuffers_uoffset_read_from_pe(buffer)))) #define __flatbuffers_nested_buffer_as_root(C, N, T, K)\ -static inline T ## _ ## K ## t C ## _ ## N ## _as_root_with_identifier(C ## _ ## table_t t__tmp, const char *fid__tmp)\ +static inline T ## _ ## K ## t C ## _ ## N ## _as_root_with_identifier(C ## _ ## table_t t__tmp, const flatbuffers_fid_t fid__tmp)\ { const uint8_t *buffer__tmp = C ## _ ## N(t__tmp); return __flatbuffers_read_root(T, K, buffer__tmp, fid__tmp); }\ static inline T ## _ ## K ## t C ## _ ## N ## _as_typed_root(C ## _ ## table_t t__tmp)\ { const uint8_t *buffer__tmp = C ## _ ## N(t__tmp); return __flatbuffers_read_root(T, K, buffer__tmp, C ## _ ## type_identifier); }\ static inline T ## _ ## K ## t C ## _ ## N ## _as_root(C ## _ ## table_t t__tmp)\ -{ const char *fid__tmp = T ## _file_identifier;\ +{ const flatbuffers_fid_t fid__tmp = T ## _file_identifier;\ const uint8_t *buffer__tmp = C ## _ ## N(t__tmp); return __flatbuffers_read_root(T, K, buffer__tmp, fid__tmp); } #define __flatbuffers_buffer_as_root(N, K)\ -static inline N ## _ ## K ## t N ## _as_root_with_identifier(const void *buffer__tmp, const char *fid__tmp)\ +static inline N ## _ ## K ## t N ## _as_root_with_identifier(const void *buffer__tmp, const flatbuffers_fid_t fid__tmp)\ { return __flatbuffers_read_root(N, K, buffer__tmp, fid__tmp); }\ static inline N ## _ ## K ## t N ## _as_root_with_type_hash(const void *buffer__tmp, flatbuffers_thash_t thash__tmp)\ { return __flatbuffers_read_typed_root(N, K, buffer__tmp, thash__tmp); }\ static inline N ## _ ## K ## t N ## _as_root(const void *buffer__tmp)\ -{ const char *fid__tmp = N ## _file_identifier;\ +{ const flatbuffers_fid_t fid__tmp = N ## _file_identifier;\ return __flatbuffers_read_root(N, K, buffer__tmp, fid__tmp); }\ static inline N ## _ ## K ## t N ## _as_typed_root(const void *buffer__tmp)\ { return __flatbuffers_read_typed_root(N, K, buffer__tmp, N ## _type_hash); } diff --git a/include/flatcc/reflection/reflection_verifier.h b/include/flatcc/reflection/reflection_verifier.h index 7d04f34d..b13bd733 100644 --- a/include/flatcc/reflection/reflection_verifier.h +++ b/include/flatcc/reflection/reflection_verifier.h @@ -39,7 +39,7 @@ static inline int reflection_Type_verify_as_typed_root(const void *buf, size_t b return flatcc_verify_table_as_root(buf, bufsiz, reflection_Type_type_identifier, &reflection_Type_verify_table); } -static inline int reflection_Type_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_Type_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_Type_verify_table); } @@ -67,7 +67,7 @@ static inline int reflection_KeyValue_verify_as_typed_root(const void *buf, size return flatcc_verify_table_as_root(buf, bufsiz, reflection_KeyValue_type_identifier, &reflection_KeyValue_verify_table); } -static inline int reflection_KeyValue_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_KeyValue_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_KeyValue_verify_table); } @@ -98,7 +98,7 @@ static inline int reflection_EnumVal_verify_as_typed_root(const void *buf, size_ return flatcc_verify_table_as_root(buf, bufsiz, reflection_EnumVal_type_identifier, &reflection_EnumVal_verify_table); } -static inline int reflection_EnumVal_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_EnumVal_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_EnumVal_verify_table); } @@ -130,7 +130,7 @@ static inline int reflection_Enum_verify_as_typed_root(const void *buf, size_t b return flatcc_verify_table_as_root(buf, bufsiz, reflection_Enum_type_identifier, &reflection_Enum_verify_table); } -static inline int reflection_Enum_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_Enum_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_Enum_verify_table); } @@ -168,7 +168,7 @@ static inline int reflection_Field_verify_as_typed_root(const void *buf, size_t return flatcc_verify_table_as_root(buf, bufsiz, reflection_Field_type_identifier, &reflection_Field_verify_table); } -static inline int reflection_Field_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_Field_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_Field_verify_table); } @@ -201,7 +201,7 @@ static inline int reflection_Object_verify_as_typed_root(const void *buf, size_t return flatcc_verify_table_as_root(buf, bufsiz, reflection_Object_type_identifier, &reflection_Object_verify_table); } -static inline int reflection_Object_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_Object_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_Object_verify_table); } @@ -232,7 +232,7 @@ static inline int reflection_RPCCall_verify_as_typed_root(const void *buf, size_ return flatcc_verify_table_as_root(buf, bufsiz, reflection_RPCCall_type_identifier, &reflection_RPCCall_verify_table); } -static inline int reflection_RPCCall_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_RPCCall_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_RPCCall_verify_table); } @@ -262,7 +262,7 @@ static inline int reflection_Service_verify_as_typed_root(const void *buf, size_ return flatcc_verify_table_as_root(buf, bufsiz, reflection_Service_type_identifier, &reflection_Service_verify_table); } -static inline int reflection_Service_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_Service_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_Service_verify_table); } @@ -294,7 +294,7 @@ static inline int reflection_Schema_verify_as_typed_root(const void *buf, size_t return flatcc_verify_table_as_root(buf, bufsiz, reflection_Schema_type_identifier, &reflection_Schema_verify_table); } -static inline int reflection_Schema_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) +static inline int reflection_Schema_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { return flatcc_verify_table_as_root(buf, bufsiz, fid, &reflection_Schema_verify_table); } diff --git a/src/compiler/codegen_c.h b/src/compiler/codegen_c.h index 6eba54aa..35bfeeec 100644 --- a/src/compiler/codegen_c.h +++ b/src/compiler/codegen_c.h @@ -7,6 +7,7 @@ #include "symbols.h" #include "parser.h" #include "codegen.h" +#include "flatcc/flatcc_types.h" /* -DFLATCC_PORTABLE may help if inttypes.h is missing. */ #ifndef PRId64 @@ -19,6 +20,10 @@ #define gen_panic(context, msg) fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg), assert(0), exit(-1) #endif +#ifndef STRINGIZE +#define STRINGIZE_(x) #x +#define STRINGIZE(x) STRINGIZE_(x) +#endif static inline void token_name(fb_token_t *t, int *n, const char **s) { *n = (int)t->len; @@ -339,6 +344,38 @@ static inline int gen_prologue(fb_output_t *out) if (out->opts->cgen_pragmas) { fprintf(out->fp, "#include \"flatcc/flatcc_prologue.h\"\n"); } + + fputs( + "#ifndef FLATCC_OFFSET_SIZE\n" + "#define FLATCC_OFFSET_SIZE " STRINGIZE(FLATCC_OFFSET_SIZE) "\n" + "#endif\n", + out->fp); + + fputs( + "#ifndef FLATCC_VOFFSET_SIZE\n" + "#define FLATCC_VOFFSET_SIZE " STRINGIZE(FLATCC_VOFFSET_SIZE) "\n" + "#endif\n", + out->fp); + + fputs( + "#ifndef FLATBUFFERS_THASH_WIDTH\n" + "#define FLATBUFFERS_THASH_WIDTH " STRINGIZE(FLATBUFFERS_THASH_WIDTH) "\n" + "#endif\n", + out->fp); + + fputs( + "#ifndef FLATBUFFERS_IDENTIFIER_SIZE\n" + "#define FLATBUFFERS_IDENTIFIER_SIZE " STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) "\n" + "#endif\n", + out->fp); + + fputs( + "#ifndef flatbuffers_fid_t_defined\n" + "typedef char flatbuffers_fid_t[" STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) "];\n" + "#define flatbuffers_fid_t_defined\n" + "#endif\n", + out->fp); + return 0; } diff --git a/src/compiler/codegen_c_builder.c b/src/compiler/codegen_c_builder.c index ffa105db..c38435a4 100644 --- a/src/compiler/codegen_c_builder.c +++ b/src/compiler/codegen_c_builder.c @@ -884,15 +884,18 @@ static int gen_builder_pretext(fb_output_t *out) if (out->S->file_identifier.type == vt_string) { fprintf(out->fp, "#undef %sidentifier\n" - "#define %sidentifier \"%.*s\"\n", + "const flatbuffers_fid_t s_%sidentifier = \"%.*s\";\n" + "#define %sidentifier s_%sidentifier\n", nsc, - nsc, out->S->file_identifier.s.len, out->S->file_identifier.s.s); + nsc, out->S->file_identifier.s.len, out->S->file_identifier.s.s, + nsc, nsc); } else { fprintf(out->fp, "#ifndef %sidentifier\n" - "#define %sidentifier 0\n" + "const flatbuffers_fid_t s_%sidentifier = \"\";\n" + "#define %sidentifier s_%sidentifier\n" "#endif\n", - nsc, nsc); + nsc, nsc, nsc, nsc); } if (out->S->file_extension.type == vt_string) { fprintf(out->fp, diff --git a/src/compiler/codegen_c_json_parser.c b/src/compiler/codegen_c_json_parser.c index 29ebc85d..4f9a91a3 100644 --- a/src/compiler/codegen_c_json_parser.c +++ b/src/compiler/codegen_c_json_parser.c @@ -1424,7 +1424,7 @@ static int gen_struct_parser(fb_output_t *out, fb_compound_type_t *ct) println(out, "return flatcc_json_parser_set_error(ctx, buf, end, flatcc_json_parser_error_runtime);"); unindent(); println(out, "}"); println(out, ""); - println(out, "static inline int %s_parse_json_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, const char *buf, size_t bufsiz, int flags, const char *fid)", snt.text); + println(out, "static inline int %s_parse_json_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, const char *buf, size_t bufsiz, int flags, const flatbuffers_fid_t fid)", snt.text); println(out, "{"); indent(); println(out, "return flatcc_json_parser_struct_as_root(B, ctx, buf, bufsiz, flags, fid, %s_parse_json_struct);", snt.text); @@ -1527,7 +1527,7 @@ static int gen_table_parser(fb_output_t *out, fb_compound_type_t *ct) println(out, "return flatcc_json_parser_set_error(ctx, buf, end, flatcc_json_parser_error_runtime);"); unindent(); println(out, "}"); println(out, ""); - println(out, "static inline int %s_parse_json_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, const char *buf, size_t bufsiz, int flags, const char *fid)", snt.text); + println(out, "static inline int %s_parse_json_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, const char *buf, size_t bufsiz, int flags, const flatbuffers_fid_t fid)", snt.text); println(out, "{"); indent(); println(out, "return flatcc_json_parser_table_as_root(B, ctx, buf, bufsiz, flags, fid, %s_parse_json_table);", snt.text); @@ -1664,14 +1664,15 @@ static int gen_root_table_parser(fb_output_t *out, fb_compound_type_t *ct) println(out, "const char *buf, size_t bufsiz, int flags)"); unindent(); unindent(); println(out, "{"); indent(); + println(out, "const flatbuffers_fid_t identifier = \"%.*s\";", + out->S->file_identifier.s.len, out->S->file_identifier.s.s); println(out, "flatcc_json_parser_t parser;"); println(out, "flatcc_builder_ref_t root;"); println(out, ""); println(out, "ctx = ctx ? ctx : &parser;"); println(out, "flatcc_json_parser_init(ctx, B, buf, buf + bufsiz, flags);"); if (out->S->file_identifier.type == vt_string) { - println(out, "if (flatcc_builder_start_buffer(B, \"%.*s\", 0, 0)) return -1;", - out->S->file_identifier.s.len, out->S->file_identifier.s.s); + println(out, "if (flatcc_builder_start_buffer(B, identifier, 0, 0)) return -1;"); } else { println(out, "if (flatcc_builder_start_buffer(B, 0, 0, 0)) return -1;"); } diff --git a/src/compiler/codegen_c_json_printer.c b/src/compiler/codegen_c_json_printer.c index 2bdaba63..ce334b86 100644 --- a/src/compiler/codegen_c_json_printer.c +++ b/src/compiler/codegen_c_json_printer.c @@ -360,7 +360,7 @@ static int gen_json_printer_struct(fb_output_t *out, fb_compound_type_t *ct) } fprintf(out->fp, "}\n\n"); fprintf(out->fp, - "static inline int %s_print_json_as_root(flatcc_json_printer_t *ctx, const void *buf, size_t bufsiz, const char *fid)\n" + "static inline int %s_print_json_as_root(flatcc_json_printer_t *ctx, const void *buf, size_t bufsiz, const char fid[FLATBUFFERS_IDENTIFIER_SIZE])\n" "{\n return flatcc_json_printer_struct_as_root(ctx, buf, bufsiz, fid, %s_print_json_struct);\n}\n\n", snt.text, snt.text); return 0; @@ -544,7 +544,7 @@ static int gen_json_printer_table(fb_output_t *out, fb_compound_type_t *ct) } fprintf(out->fp, "\n}\n\n"); fprintf(out->fp, - "static inline int %s_print_json_as_root(flatcc_json_printer_t *ctx, const void *buf, size_t bufsiz, const char *fid)\n" + "static inline int %s_print_json_as_root(flatcc_json_printer_t *ctx, const void *buf, size_t bufsiz, const char fid[FLATBUFFERS_IDENTIFIER_SIZE])\n" "{\n return flatcc_json_printer_table_as_root(ctx, buf, bufsiz, fid, %s_print_json_table);\n}\n\n", snt.text, snt.text); done: @@ -680,7 +680,11 @@ static int gen_root_type_printer(fb_output_t *out, fb_compound_type_t *ct) out->S->basename); fprintf(out->fp, "{\n" - " flatcc_json_printer_t printer;\n" + " flatcc_json_printer_t printer;\n"); + fprintf(out->fp, + " const flatbuffers_fid_t identifier = \"%.*s\";\n", + out->S->file_identifier.s.len, out->S->file_identifier.s.s); + fprintf(out->fp, "\n" " if (ctx == 0) {\n" " ctx = &printer;\n" @@ -690,11 +694,10 @@ static int gen_root_type_printer(fb_output_t *out, fb_compound_type_t *ct) snt.text); if (out->S->file_identifier.type == vt_string) { fprintf(out->fp, - "\"%.*s\");\n", - out->S->file_identifier.s.len, out->S->file_identifier.s.s); + "identifier);\n"); } else { fprintf(out->fp, - "0);"); + "0);\n"); } fprintf(out->fp, "}\n\n"); diff --git a/src/compiler/codegen_c_reader.c b/src/compiler/codegen_c_reader.c index 67f8055b..d694faea 100644 --- a/src/compiler/codegen_c_reader.c +++ b/src/compiler/codegen_c_reader.c @@ -62,23 +62,25 @@ static void print_type_identifier(fb_output_t *out, fb_compound_type_t *ct) file_identifier = out->S->file_identifier.s.s; file_identifier_len = out->S->file_identifier.s.len; } else { - quote = ""; - file_identifier = "0"; - file_identifier_len = 1; + quote = "\""; + file_identifier = ""; + file_identifier_len = 0; } fprintf(out->fp, "#ifndef %s_file_identifier\n" - "#define %s_file_identifier %s%.*s%s\n" + "const flatbuffers_fid_t s_%s_file_identifier = %s%.*s%s;\n" + "#define %s_file_identifier s_%s_file_identifier\n" "#endif\n", - name, name, quote, file_identifier_len, file_identifier, quote); + name, name, quote, file_identifier_len, file_identifier, quote, name, name); if (!conflict) { /* For backwards compatibility. */ fprintf(out->fp, "/* deprecated, use %s_file_identifier */\n" "#ifndef %s_identifier\n" - "#define %s_identifier %s%.*s%s\n" + "const flatbuffers_fid_t s_%s_identifier = %s%.*s%s;\n" + "#define %s_identifier s_%s_identifier\n" "#endif\n", - name, name, name, quote, file_identifier_len, file_identifier, quote); + name, name, name, quote, file_identifier_len, file_identifier, quote, name, name); } fprintf(out->fp, "#define %s_type_hash ((%sthash_t)0x%lx)\n", @@ -900,7 +902,7 @@ static void gen_helpers(fb_output_t *out) } fprintf(out->fp, "/* If fid is null, the function returns true without testing as buffer is not expected to have any id. */\n" - "static inline int %shas_identifier(const void *buffer, const char *fid)\n" + "static inline int %shas_identifier(const void *buffer, const flatbuffers_fid_t fid)\n" "{ %sthash_t id, id2 = 0; if (fid == 0) { return 1; };\n" " id2 = %stype_hash_from_string(fid);\n" " id = __%sthash_read_from_pe(((%suoffset_t *)buffer) + 1);\n" @@ -916,7 +918,7 @@ static void gen_helpers(fb_output_t *out) "{ if (size_out) { *size_out = (size_t)__%suoffset_read_from_pe(b); }\n" " return (uint8_t *)b + sizeof(%suoffset_t); }\n", nsc, nsc, nsc); fprintf(out->fp, - "/* Null file identifier accepts anything, otherwise fid should be 4 characters. */\n" + "/* Null file identifier accepts anything, otherwise fid should be " STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) " characters. */\n" "#define __%sread_root(T, K, buffer, fid)\\\n" " ((!buffer || !%shas_identifier(buffer, fid)) ? 0 :\\\n" " ((T ## _ ## K ## t)(((uint8_t *)buffer) +\\\n" @@ -928,23 +930,21 @@ static void gen_helpers(fb_output_t *out) nsc, nsc, nsc, nsc, nsc, nsc); fprintf(out->fp, "#define __%snested_buffer_as_root(C, N, T, K)\\\n" - "static inline T ## _ ## K ## t C ## _ ## N ## _as_root_with_identifier(C ## _ ## table_t t__tmp, const char *fid__tmp)\\\n" + "static inline T ## _ ## K ## t C ## _ ## N ## _as_root_with_identifier(C ## _ ## table_t t__tmp, const flatbuffers_fid_t fid__tmp)\\\n" "{ const uint8_t *buffer__tmp = C ## _ ## N(t__tmp); return __%sread_root(T, K, buffer__tmp, fid__tmp); }\\\n" "static inline T ## _ ## K ## t C ## _ ## N ## _as_typed_root(C ## _ ## table_t t__tmp)\\\n" "{ const uint8_t *buffer__tmp = C ## _ ## N(t__tmp); return __%sread_root(T, K, buffer__tmp, C ## _ ## type_identifier); }\\\n" "static inline T ## _ ## K ## t C ## _ ## N ## _as_root(C ## _ ## table_t t__tmp)\\\n" - "{ const char *fid__tmp = T ## _file_identifier;\\\n" - " const uint8_t *buffer__tmp = C ## _ ## N(t__tmp); return __%sread_root(T, K, buffer__tmp, fid__tmp); }\n", + "{ const uint8_t *buffer__tmp = C ## _ ## N(t__tmp); return __%sread_root(T, K, buffer__tmp, T ## _file_identifier); }\n", nsc, nsc, nsc, nsc); fprintf(out->fp, "#define __%sbuffer_as_root(N, K)\\\n" - "static inline N ## _ ## K ## t N ## _as_root_with_identifier(const void *buffer__tmp, const char *fid__tmp)\\\n" + "static inline N ## _ ## K ## t N ## _as_root_with_identifier(const void *buffer__tmp, const flatbuffers_fid_t fid__tmp)\\\n" "{ return __%sread_root(N, K, buffer__tmp, fid__tmp); }\\\n" "static inline N ## _ ## K ## t N ## _as_root_with_type_hash(const void *buffer__tmp, %sthash_t thash__tmp)\\\n" "{ return __%sread_typed_root(N, K, buffer__tmp, thash__tmp); }\\\n" "static inline N ## _ ## K ## t N ## _as_root(const void *buffer__tmp)\\\n" - "{ const char *fid__tmp = N ## _file_identifier;\\\n" - " return __%sread_root(N, K, buffer__tmp, fid__tmp); }\\\n" + "{ return __%sread_root(N, K, buffer__tmp, N ## _file_identifier); }\\\n" "static inline N ## _ ## K ## t N ## _as_typed_root(const void *buffer__tmp)\\\n" "{ return __%sread_typed_root(N, K, buffer__tmp, N ## _type_hash); }\n" "#define __%sstruct_as_root(N) __%sbuffer_as_root(N, struct_)\n" diff --git a/src/compiler/codegen_c_verifier.c b/src/compiler/codegen_c_verifier.c index 9b1a0486..80d14d6d 100644 --- a/src/compiler/codegen_c_verifier.c +++ b/src/compiler/codegen_c_verifier.c @@ -220,7 +220,7 @@ static int gen_table_verifier(fb_output_t *out, fb_compound_type_t *ct) "{\n return flatcc_verify_table_as_root(buf, bufsiz, %s_type_identifier, &%s_verify_table);\n}\n\n", snt.text, snt.text, snt.text); fprintf(out->fp, - "static inline int %s_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid)\n" + "static inline int %s_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid)\n" "{\n return flatcc_verify_table_as_root(buf, bufsiz, fid, &%s_verify_table);\n}\n\n", snt.text, snt.text); fprintf(out->fp, @@ -250,7 +250,7 @@ static int gen_struct_verifier(fb_output_t *out, fb_compound_type_t *ct) "{\n return flatcc_verify_struct_as_typed_root(buf, bufsiz, thash, %"PRIu64", %"PRIu16");\n}\n\n", snt.text, out->nsc, ct->size, ct->align); fprintf(out->fp, - "static inline int %s_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid)\n" + "static inline int %s_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid)\n" "{\n return flatcc_verify_struct_as_root(buf, bufsiz, fid, %"PRIu64", %"PRIu16");\n}\n\n", snt.text, ct->size, ct->align); return 0; diff --git a/src/compiler/parser.c b/src/compiler/parser.c index 250d6600..fcf4c5d9 100644 --- a/src/compiler/parser.c +++ b/src/compiler/parser.c @@ -18,6 +18,12 @@ #include "pstrutil.h" #include "flatcc/portable/pattributes.h" /* fallthrough */ #include "flatcc/portable/pparseint.h" +#include "flatcc/flatcc_types.h" + +#ifndef STRINGIZE +#define STRINGIZE_(x) #x +#define STRINGIZE(x) STRINGIZE_(x) +#endif void fb_default_error_out(void *err_ctx, const char *buf, size_t len) { @@ -1154,10 +1160,17 @@ static void parse_file_identifier(fb_parser_t *P, fb_value_t *v) } t = P->token; parse_string_literal(P, v); - if (v->s.s && v->s.len != 4) { +#ifndef FLATBUFFERS_RELAXED_IDENTIFIER_SIZE + if (v->s.s && v->s.len != FLATBUFFERS_IDENTIFIER_SIZE) { + v->type = vt_invalid; + error_tok(P, t, "file_identifier must be " STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) " characters"); + } +#else + if (v->s.s && v->s.len < 1) { v->type = vt_invalid; - error_tok(P, t, "file_identifier must be 4 characters"); + error_tok(P, t, "file_identifier must be at least 1 character or not specified"); } +#endif match(P, ';', "file_identifier expected ';'"); return; fail: diff --git a/src/runtime/builder.c b/src/runtime/builder.c index 9f54d884..c2a0d6d1 100644 --- a/src/runtime/builder.c +++ b/src/runtime/builder.c @@ -743,7 +743,7 @@ flatcc_builder_ref_t flatcc_builder_embed_buffer(flatcc_builder_t *B, } flatcc_builder_ref_t flatcc_builder_create_buffer(flatcc_builder_t *B, - const char identifier[identifier_size], uint16_t block_align, + const flatbuffers_fid_t identifier, uint16_t block_align, flatcc_builder_ref_t object_ref, uint16_t align, int flags) { flatcc_builder_ref_t buffer_ref; @@ -808,7 +808,7 @@ flatcc_builder_ref_t flatcc_builder_create_struct(flatcc_builder_t *B, const voi } int flatcc_builder_start_buffer(flatcc_builder_t *B, - const char identifier[identifier_size], uint16_t block_align, int flags) + const flatbuffers_fid_t identifier, uint16_t block_align, int flags) { /* * This saves the parent `min_align` in the align field since we @@ -1908,7 +1908,7 @@ void flatcc_builder_set_vtable_cache_limit(flatcc_builder_t *B, size_t size) B->vb_flush_limit = size; } -void flatcc_builder_set_identifier(flatcc_builder_t *B, const char identifier[identifier_size]) +void flatcc_builder_set_identifier(flatcc_builder_t *B, const flatbuffers_fid_t identifier) { set_identifier(identifier); } diff --git a/src/runtime/json_parser.c b/src/runtime/json_parser.c index 313bd808..29bf1a91 100644 --- a/src/runtime/json_parser.c +++ b/src/runtime/json_parser.c @@ -259,7 +259,7 @@ static inline int decode_utf16_surrogate_pair(uint32_t high, uint32_t low, char } -/* +/* * UTF-8 code points can have up to 4 bytes but JSON can only * encode up to 3 bytes via the \uXXXX syntax. * To handle the range U+10000..U+10FFFF two UTF-16 surrogate @@ -329,7 +329,7 @@ const char *flatcc_json_parser_string_escape(flatcc_json_parser_t *ctx, const ch return flatcc_json_parser_set_error(ctx, buf, end, flatcc_json_parser_error_invalid_escape); }; /* If a high UTF-16 surrogate half pair was detected */ - if (u >= 0xd800 && u <= 0xdbff && + if (u >= 0xd800 && u <= 0xdbff && /* and there is space for a matching low half pair */ end - buf >= 12 && /* and there is a second escape following immediately */ @@ -344,7 +344,7 @@ const char *flatcc_json_parser_string_escape(flatcc_json_parser_t *ctx, const ch return flatcc_json_parser_set_error(ctx, buf, end, flatcc_json_parser_error_invalid_escape); } return buf + 12; - /* + /* * Otherwise decode unmatched surrogate pairs as is any * other UTF-8. Some systems might depend on these surviving. * Leave ignored errors for the next parse step. @@ -855,7 +855,7 @@ const char *flatcc_json_parser_char_array(flatcc_json_parser_t *ctx, if (k > n) { if (!(ctx->flags & flatcc_json_parser_f_skip_array_overflow)) { return flatcc_json_parser_set_error(ctx, buf, end, flatcc_json_parser_error_array_overflow); - } + } k = n; /* Might truncate UTF-8. */ } memcpy(s, mark, k); @@ -869,7 +869,7 @@ const char *flatcc_json_parser_char_array(flatcc_json_parser_t *ctx, if (k > n) { if (!(ctx->flags & flatcc_json_parser_f_skip_array_overflow)) { return flatcc_json_parser_set_error(ctx, buf, end, flatcc_json_parser_error_array_overflow); - } + } k = n; /* Might truncate UTF-8. */ } memcpy(s, mark, k); @@ -1258,7 +1258,7 @@ const char *flatcc_json_parser_union_type_vector(flatcc_json_parser_t *ctx, } int flatcc_json_parser_table_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, - const char *buf, size_t bufsiz, int flags, const char *fid, + const char *buf, size_t bufsiz, int flags, const flatbuffers_fid_t fid, flatcc_json_parser_table_f *parser) { flatcc_json_parser_t _ctx; @@ -1278,7 +1278,7 @@ int flatcc_json_parser_table_as_root(flatcc_builder_t *B, flatcc_json_parser_t * } int flatcc_json_parser_struct_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx, - const char *buf, size_t bufsiz, int flags, const char *fid, + const char *buf, size_t bufsiz, int flags, const flatbuffers_fid_t fid, flatcc_json_parser_table_f *parser) { flatcc_json_parser_t _ctx; diff --git a/src/runtime/json_printer.c b/src/runtime/json_printer.c index 4ebe1c1d..7326b3da 100644 --- a/src/runtime/json_printer.c +++ b/src/runtime/json_printer.c @@ -1245,7 +1245,7 @@ void flatcc_json_printer_struct_field(flatcc_json_printer_t *ctx, * check. */ static int accept_header(flatcc_json_printer_t * ctx, - const void *buf, size_t bufsiz, const char *fid) + const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { flatbuffers_thash_t id, id2 = 0; @@ -1267,7 +1267,7 @@ static int accept_header(flatcc_json_printer_t * ctx, } int flatcc_json_printer_struct_as_root(flatcc_json_printer_t *ctx, - const void *buf, size_t bufsiz, const char *fid, + const void *buf, size_t bufsiz, const flatbuffers_fid_t fid, flatcc_json_printer_struct_f *pf) { if (!accept_header(ctx, buf, bufsiz, fid)) { @@ -1281,7 +1281,7 @@ int flatcc_json_printer_struct_as_root(flatcc_json_printer_t *ctx, } int flatcc_json_printer_table_as_root(flatcc_json_printer_t *ctx, - const void *buf, size_t bufsiz, const char *fid, flatcc_json_printer_table_f *pf) + const void *buf, size_t bufsiz, const flatbuffers_fid_t fid, flatcc_json_printer_table_f *pf) { if (!accept_header(ctx, buf, bufsiz, fid)) { return -1; @@ -1294,7 +1294,7 @@ int flatcc_json_printer_table_as_root(flatcc_json_printer_t *ctx, void flatcc_json_printer_struct_as_nested_root(flatcc_json_printer_t *ctx, flatcc_json_printer_table_descriptor_t *td, int id, const char *name, size_t len, - const char *fid, + const flatbuffers_fid_t fid, flatcc_json_printer_struct_f *pf) { const uoffset_t *buf; @@ -1320,7 +1320,7 @@ void flatcc_json_printer_struct_as_nested_root(flatcc_json_printer_t *ctx, void flatcc_json_printer_table_as_nested_root(flatcc_json_printer_t *ctx, flatcc_json_printer_table_descriptor_t *td, int id, const char *name, size_t len, - const char *fid, + const flatbuffers_fid_t fid, flatcc_json_printer_table_f pf) { const uoffset_t *buf; diff --git a/src/runtime/verifier.c b/src/runtime/verifier.c index 1831fcf6..3000af95 100644 --- a/src/runtime/verifier.c +++ b/src/runtime/verifier.c @@ -448,7 +448,7 @@ int flatcc_verify_union_string(flatcc_union_verifier_descriptor_t *ud) return verify_string(ud->buf, ud->end, ud->base, ud->offset); } -int flatcc_verify_buffer_header(const void *buf, size_t bufsiz, const char *fid) +int flatcc_verify_buffer_header(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid) { thash_t id, id2; @@ -492,7 +492,7 @@ int flatcc_verify_typed_buffer_header(const void *buf, size_t bufsiz, flatbuffer return flatcc_verify_ok; } -int flatcc_verify_struct_as_root(const void *buf, size_t bufsiz, const char *fid, size_t size, uint16_t align) +int flatcc_verify_struct_as_root(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid, size_t size, uint16_t align) { check_result(flatcc_verify_buffer_header(buf, bufsiz, fid)); return verify_struct((uoffset_t)bufsiz, 0, read_uoffset(buf, 0), (uoffset_t)size, align); @@ -504,7 +504,7 @@ int flatcc_verify_struct_as_typed_root(const void *buf, size_t bufsiz, flatbuffe return verify_struct((uoffset_t)bufsiz, 0, read_uoffset(buf, 0), (uoffset_t)size, align); } -int flatcc_verify_table_as_root(const void *buf, size_t bufsiz, const char *fid, flatcc_table_verifier_f *tvf) +int flatcc_verify_table_as_root(const void *buf, size_t bufsiz, const flatbuffers_fid_t fid, flatcc_table_verifier_f *tvf) { check_result(flatcc_verify_buffer_header(buf, (uoffset_t)bufsiz, fid)); return verify_table(buf, (uoffset_t)bufsiz, 0, read_uoffset(buf, 0), FLATCC_VERIFIER_MAX_LEVELS, tvf); @@ -517,7 +517,7 @@ int flatcc_verify_table_as_typed_root(const void *buf, size_t bufsiz, flatbuffer } int flatcc_verify_struct_as_nested_root(flatcc_table_verifier_descriptor_t *td, - voffset_t id, int required, const char *fid, size_t size, uint16_t align) + voffset_t id, int required, const flatbuffers_fid_t fid, size_t size, uint16_t align) { const uoffset_t *buf; uoffset_t bufsiz; @@ -533,7 +533,7 @@ int flatcc_verify_struct_as_nested_root(flatcc_table_verifier_descriptor_t *td, } int flatcc_verify_table_as_nested_root(flatcc_table_verifier_descriptor_t *td, - voffset_t id, int required, const char *fid, + voffset_t id, int required, const flatbuffers_fid_t fid, uint16_t align, flatcc_table_verifier_f tvf) { const uoffset_t *buf; diff --git a/test/monster_test/monster_test.fbs b/test/monster_test/monster_test.fbs index f0f78aaa..a63eb5e2 100755 --- a/test/monster_test/monster_test.fbs +++ b/test/monster_test/monster_test.fbs @@ -241,7 +241,7 @@ table TestBase64 // even if not listed first. A table with a single key field behaves the // same as a table with a single primary_key field, so use key for // compatiblity in that case. -// +// // attribute "primary_key"; // table MultipleKeys From 9144c94e3a39639585d5c5edb9df4788fc50164d Mon Sep 17 00:00:00 2001 From: Tyler Young Date: Thu, 12 Aug 2021 14:29:36 -0400 Subject: [PATCH 3/4] modified gen_prologue to not plug in custom definitions in the common header --- src/compiler/codegen_c.h | 53 +++++++++++---------------- src/compiler/codegen_c_builder.c | 4 +- src/compiler/codegen_c_json_parser.c | 2 +- src/compiler/codegen_c_json_printer.c | 2 +- src/compiler/codegen_c_reader.c | 4 +- src/compiler/codegen_c_verifier.c | 2 +- 6 files changed, 29 insertions(+), 38 deletions(-) diff --git a/src/compiler/codegen_c.h b/src/compiler/codegen_c.h index 35bfeeec..927e1283 100644 --- a/src/compiler/codegen_c.h +++ b/src/compiler/codegen_c.h @@ -7,6 +7,7 @@ #include "symbols.h" #include "parser.h" #include "codegen.h" +#include "flatcc/portable/pstdbool.h" #include "flatcc/flatcc_types.h" /* -DFLATCC_PORTABLE may help if inttypes.h is missing. */ @@ -339,42 +340,32 @@ static inline const char *scalar_suffix(fb_scalar_type_t scalar_type) } /* See also: https://github.com/philsquared/Catch/issues/376 */ -static inline int gen_prologue(fb_output_t *out) +static inline int gen_prologue(fb_output_t *out, bool is_common) { if (out->opts->cgen_pragmas) { fprintf(out->fp, "#include \"flatcc/flatcc_prologue.h\"\n"); } - fputs( - "#ifndef FLATCC_OFFSET_SIZE\n" - "#define FLATCC_OFFSET_SIZE " STRINGIZE(FLATCC_OFFSET_SIZE) "\n" - "#endif\n", - out->fp); - - fputs( - "#ifndef FLATCC_VOFFSET_SIZE\n" - "#define FLATCC_VOFFSET_SIZE " STRINGIZE(FLATCC_VOFFSET_SIZE) "\n" - "#endif\n", - out->fp); - - fputs( - "#ifndef FLATBUFFERS_THASH_WIDTH\n" - "#define FLATBUFFERS_THASH_WIDTH " STRINGIZE(FLATBUFFERS_THASH_WIDTH) "\n" - "#endif\n", - out->fp); - - fputs( - "#ifndef FLATBUFFERS_IDENTIFIER_SIZE\n" - "#define FLATBUFFERS_IDENTIFIER_SIZE " STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) "\n" - "#endif\n", - out->fp); - - fputs( - "#ifndef flatbuffers_fid_t_defined\n" - "typedef char flatbuffers_fid_t[" STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) "];\n" - "#define flatbuffers_fid_t_defined\n" - "#endif\n", - out->fp); + if (!is_common) { + fputs( + "#ifndef FLATCC_OFFSET_SIZE\n" + "#define FLATCC_OFFSET_SIZE " STRINGIZE(FLATCC_OFFSET_SIZE) "\n" + "#endif\n" + "#ifndef FLATCC_VOFFSET_SIZE\n" + "#define FLATCC_VOFFSET_SIZE " STRINGIZE(FLATCC_VOFFSET_SIZE) "\n" + "#endif\n" + "#ifndef FLATBUFFERS_THASH_WIDTH\n" + "#define FLATBUFFERS_THASH_WIDTH " STRINGIZE(FLATBUFFERS_THASH_WIDTH) "\n" + "#endif\n" + "#ifndef FLATBUFFERS_IDENTIFIER_SIZE\n" + "#define FLATBUFFERS_IDENTIFIER_SIZE " STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) "\n" + "#endif\n" + "#ifndef flatbuffers_fid_t_defined\n" + "typedef char flatbuffers_fid_t[" STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) "];\n" + "#define flatbuffers_fid_t_defined\n" + "#endif\n", + out->fp); + } return 0; } diff --git a/src/compiler/codegen_c_builder.c b/src/compiler/codegen_c_builder.c index c38435a4..0ce2743a 100644 --- a/src/compiler/codegen_c_builder.c +++ b/src/compiler/codegen_c_builder.c @@ -11,7 +11,7 @@ int fb_gen_common_c_builder_header(fb_output_t *out) fprintf(out->fp, "#define %s_COMMON_BUILDER_H\n", nscup); fprintf(out->fp, "\n/* " FLATCC_GENERATED_BY " */\n\n"); fprintf(out->fp, "/* Common FlatBuffers build functionality for C. */\n\n"); - gen_prologue(out); + gen_prologue(out, true); fprintf(out->fp, "#ifndef FLATBUILDER_H\n"); fprintf(out->fp, "#include \"flatcc/flatcc_builder.h\"\n"); @@ -875,7 +875,7 @@ static int gen_builder_pretext(fb_output_t *out) fb_gen_c_includes(out, "_builder.h", "_BUILDER_H"); - gen_prologue(out); + gen_prologue(out, false); /* * Even if defined in the reader header, we must redefine it here diff --git a/src/compiler/codegen_c_json_parser.c b/src/compiler/codegen_c_json_parser.c index 4f9a91a3..e082c4e4 100644 --- a/src/compiler/codegen_c_json_parser.c +++ b/src/compiler/codegen_c_json_parser.c @@ -56,7 +56,7 @@ static int gen_json_parser_pretext(fb_output_t *out) println(out, ""); println(out, "#include \"flatcc/flatcc_json_parser.h\""); fb_gen_c_includes(out, "_json_parser.h", "_JSON_PARSER_H"); - gen_prologue(out); + gen_prologue(out, false); println(out, ""); return 0; } diff --git a/src/compiler/codegen_c_json_printer.c b/src/compiler/codegen_c_json_printer.c index ce334b86..f75c3238 100644 --- a/src/compiler/codegen_c_json_printer.c +++ b/src/compiler/codegen_c_json_printer.c @@ -18,7 +18,7 @@ static int gen_json_printer_pretext(fb_output_t *out) fprintf(out->fp, "\n/* " FLATCC_GENERATED_BY " */\n\n"); fprintf(out->fp, "#include \"flatcc/flatcc_json_printer.h\"\n"); fb_gen_c_includes(out, "_json_printer.h", "_JSON_PRINTER_H"); - gen_prologue(out); + gen_prologue(out, false); fprintf(out->fp, "\n"); return 0; } diff --git a/src/compiler/codegen_c_reader.c b/src/compiler/codegen_c_reader.c index d694faea..6bf04e8e 100644 --- a/src/compiler/codegen_c_reader.c +++ b/src/compiler/codegen_c_reader.c @@ -970,7 +970,7 @@ int fb_gen_common_c_header(fb_output_t *out) " * but find operations are supported on pre-sorted vectors.\n" " */\n"); } - gen_prologue(out); + gen_prologue(out, true); gen_helpers(out); gen_epilogue(out); fprintf(out->fp, @@ -1028,7 +1028,7 @@ static void gen_pretext(fb_output_t *out) "#include \n" "#endif\n"); } - gen_prologue(out); + gen_prologue(out, false); if (out->S->file_identifier.type == vt_string) { fprintf(out->fp, "#undef %sidentifier\n" diff --git a/src/compiler/codegen_c_verifier.c b/src/compiler/codegen_c_verifier.c index 80d14d6d..89474024 100644 --- a/src/compiler/codegen_c_verifier.c +++ b/src/compiler/codegen_c_verifier.c @@ -21,7 +21,7 @@ static int gen_verifier_pretext(fb_output_t *out) fprintf(out->fp, "#endif\n"); fprintf(out->fp, "#include \"flatcc/flatcc_verifier.h\"\n"); fb_gen_c_includes(out, "_verifier.h", "_VERIFIER_H"); - gen_prologue(out); + gen_prologue(out, false); fprintf(out->fp, "\n"); return 0; } From 8cc37ac293f39e80a269f7db3631fa656514e2e5 Mon Sep 17 00:00:00 2001 From: Tyler Young Date: Thu, 12 Aug 2021 14:52:20 -0400 Subject: [PATCH 4/4] replace FLATBUFFERS_ option prefix with FLATCC_ for IDENTIFIER_SIZE options use struct size assertions when FLATCC_RELAXED_IDENTIFIER_SIZE is not defined --- CMakeLists.txt | 16 ++++++++-------- include/flatcc/flatcc_identifier.h | 8 +++++--- src/compiler/parser.c | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd53643a..f3958ea2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,19 +104,19 @@ option (FLATCC_ALLOW_WERROR "allow -Werror to be configured" ON) option (FLATCC_IGNORE_CONST_COND "silence const condition warnings" OFF) # Enforces parsing of identifier size per the spec of 4 characters. -# FLATBUFFERS_STRICT_IDENTIFIER_SIZE has no effect if the identifier +# FLATCC_STRICT_IDENTIFIER_SIZE has no effect if the identifier # size is 4. # Note: Encoded identifiers will still be padded when FLATCC_OFFSET_SIZE # is greater than 4 and truncated when offset size is less than 4. -option (FLATBUFFERS_STRICT_IDENTIFIER_SIZE +option (FLATCC_STRICT_IDENTIFIER_SIZE "enforce identifier size being offset size" OFF) # Relaxes the size specification for file identifier sizes to allow them # to be padded if shorter or truncated if longer. # The restriction prohibits interpreting schema oriented at other offset # sizes, so this is implied when FLATCC_OFFSET_SIZE is not 4. -# This option expects FLATBUFFERS_STRICT_IDENTIFIER_SIZE to be OFF. -option (FLATBUFFERS_RELAXED_IDENTIFIER_SIZE +# This option expects FLATCC_STRICT_IDENTIFIER_SIZE to be OFF. +option (FLATCC_RELAXED_IDENTIFIER_SIZE "identifier will be padded if too short or truncated if too long" OFF) @@ -319,10 +319,10 @@ if (DEFINED FLATCC_VOFFSET_SIZE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_VOFFSET_SIZE=${FLATCC_VOFFSET_SIZE}") endif() -if (DEFINED FLATCC_OFFSET_SIZE AND NOT FLATCC_OFFSET_SIZE EQUAL 4 AND NOT DEFINED FLATBUFFERS_STRICT_IDENTIFIER_SIZE) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATBUFFERS_RELAXED_IDENTIFIER_SIZE") -elseif(DEFINED FLATBUFFERS_RELAXED_IDENTIFIER_SIZE) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATBUFFERS_RELAXED_IDENTIFIER_SIZE") +if (DEFINED FLATCC_OFFSET_SIZE AND NOT FLATCC_OFFSET_SIZE EQUAL 4 AND NOT DEFINED FLATCC_STRICT_IDENTIFIER_SIZE) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_RELAXED_IDENTIFIER_SIZE") +elseif(DEFINED FLATCC_RELAXED_IDENTIFIER_SIZE) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_RELAXED_IDENTIFIER_SIZE") endif() if (CLANG_VERSION) diff --git a/include/flatcc/flatcc_identifier.h b/include/flatcc/flatcc_identifier.h index e331d4a1..5b9361aa 100644 --- a/include/flatcc/flatcc_identifier.h +++ b/include/flatcc/flatcc_identifier.h @@ -186,9 +186,11 @@ static inline flatbuffers_thash_t flatbuffers_disperse_type_hash(flatbuffers_tha } -/* We have hardcoded assumptions about identifier size. */ -//static_assert(sizeof(flatbuffers_fid_t) == 4, "unexpected file identifier size"); -//static_assert(sizeof(flatbuffers_thash_t) == 4, "unexpected type hash size"); +/* We have hardcoded assumptions about identifier size when not relaxed. */ +#ifndef FLATCC_RELAXED_IDENTIFIER_SIZE +static_assert(sizeof(flatbuffers_fid_t) == FLATBUFFERS_IDENTIFIER_SIZE, "unexpected file identifier size"); +static_assert(sizeof(flatbuffers_thash_t) == FLATBUFFERS_THASH_WIDTH/8, "unexpected type hash size"); +#endif #ifdef __cplusplus } diff --git a/src/compiler/parser.c b/src/compiler/parser.c index fcf4c5d9..fecdf631 100644 --- a/src/compiler/parser.c +++ b/src/compiler/parser.c @@ -1160,7 +1160,7 @@ static void parse_file_identifier(fb_parser_t *P, fb_value_t *v) } t = P->token; parse_string_literal(P, v); -#ifndef FLATBUFFERS_RELAXED_IDENTIFIER_SIZE +#ifndef FLATCC_RELAXED_IDENTIFIER_SIZE if (v->s.s && v->s.len != FLATBUFFERS_IDENTIFIER_SIZE) { v->type = vt_invalid; error_tok(P, t, "file_identifier must be " STRINGIZE(FLATBUFFERS_IDENTIFIER_SIZE) " characters");