| // Protocol Buffers - Google's data interchange format | |
| // Copyright 2023 Google LLC. All rights reserved. | |
| // | |
| // Use of this source code is governed by a BSD-style | |
| // license that can be found in the LICENSE file or at | |
| // https://developers.google.com/open-source/licenses/bsd | |
| #include "upb/mini_descriptor/link.h" | |
| #include <stddef.h> | |
| #include <stdint.h> | |
| #include <string.h> | |
| #include "upb/base/descriptor_constants.h" | |
| #include "upb/mini_table/enum.h" | |
| #include "upb/mini_table/field.h" | |
| #include "upb/mini_table/internal/field.h" | |
| #include "upb/mini_table/internal/message.h" | |
| #include "upb/mini_table/internal/sub.h" | |
| #include "upb/mini_table/message.h" | |
| #include "upb/mini_table/sub.h" | |
| // Our awkward dance for including fasttable only when it is enabled. | |
| #include "upb/port/def.inc" | |
| #if UPB_FASTTABLE | |
| #define UPB_INCLUDE_FAST_DECODE | |
| #endif | |
| #include "upb/port/undef.inc" | |
| #ifdef UPB_INCLUDE_FAST_DECODE | |
| #include "upb/wire/decode_fast/data.h" | |
| #include "upb/wire/decode_fast/field_parsers.h" | |
| #include "upb/wire/decode_fast/function_array.h" | |
| #include "upb/wire/decode_fast/select.h" | |
| #endif | |
| #undef UPB_INCLUDE_FAST_DECODE | |
| // Must be last. | |
| #include "upb/port/def.inc" | |
| bool upb_MiniTable_SetSubMessage(upb_MiniTable* table, | |
| upb_MiniTableField* field, | |
| const upb_MiniTable* sub) { | |
| UPB_ASSERT((uintptr_t)table->UPB_PRIVATE(fields) <= (uintptr_t)field && | |
| (uintptr_t)field < (uintptr_t)(table->UPB_PRIVATE(fields) + | |
| table->UPB_PRIVATE(field_count))); | |
| UPB_ASSERT(sub); | |
| const bool sub_is_map = sub->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry; | |
| const bool table_is_map = table->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry; | |
| switch (field->UPB_PRIVATE(descriptortype)) { | |
| case kUpb_FieldType_Message: | |
| if (sub_is_map) { | |
| if (UPB_UNLIKELY(table_is_map)) return false; | |
| // A map field on the parent table must be repeated, and cannot be in a | |
| // oneof or an extension. | |
| UPB_ASSERT(!upb_MiniTableField_IsMap(field)); | |
| if (UPB_UNLIKELY(!upb_MiniTableField_IsArray(field) || | |
| upb_MiniTableField_IsInOneof(field) || | |
| upb_MiniTableField_IsExtension(field))) { | |
| return false; | |
| } | |
| field->UPB_PRIVATE(mode) = | |
| (field->UPB_PRIVATE(mode) & ~kUpb_FieldMode_Mask) | | |
| kUpb_FieldMode_Map; | |
| #if UPB_FASTTABLE | |
| // Update fasttable entry with specialized map decoder function pointer | |
| // and metadata. | |
| int size = table->UPB_PRIVATE(table_mask) == 0xff | |
| ? 0 | |
| : ((table->UPB_PRIVATE(table_mask) >> 3) + 1); | |
| for (int i = 0; i < size; i++) { | |
| _upb_FastTable_Entry* entry = &table->UPB_PRIVATE(fasttable)[i]; | |
| uint32_t field_number = | |
| upb_DecodeFastData_GetFieldNumber(entry->field_data); | |
| if (field_number == upb_MiniTableField_Number(field)) { | |
| uint16_t expected_tag = | |
| upb_DecodeFastData_GetExpectedTag(entry->field_data); | |
| uint64_t subofs = upb_DecodeFastData_GetSubofs(entry->field_data); | |
| upb_DecodeFast_TableEntry fast_entry; | |
| if (upb_DecodeFast_TryFillMapEntry(field, sub, expected_tag, subofs, | |
| &fast_entry)) { | |
| entry->field_parser = | |
| upb_DecodeFast_GetFunctionPointer(fast_entry.function_idx); | |
| entry->field_data = fast_entry.function_data; | |
| } else { | |
| entry->field_parser = &_upb_FastDecoder_DecodeGeneric; | |
| entry->field_data = 0; | |
| } | |
| } | |
| } | |
| #endif | |
| } else if (table_is_map) { | |
| // If table is a map entry, only field 2 (value) can be a submessage. | |
| if (UPB_UNLIKELY(upb_MiniTableField_Number(field) != 2)) { | |
| return false; | |
| } | |
| } | |
| break; | |
| case kUpb_FieldType_Group: | |
| if (UPB_UNLIKELY(sub_is_map || table_is_map)) return false; | |
| break; | |
| default: | |
| return false; | |
| } | |
| upb_MiniTableSubInternal* table_sub = | |
| UPB_PRIVATE(_upb_MiniTableField_Sub)(field); | |
| UPB_ASSERT(upb_MiniTable_GetSubMessageTable(field) == NULL); | |
| table_sub->UPB_PRIVATE(submsg) = sub; | |
| return true; | |
| } | |
| bool upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTableField* field, | |
| const upb_MiniTableEnum* sub) { | |
| UPB_ASSERT((uintptr_t)table->UPB_PRIVATE(fields) <= (uintptr_t)field && | |
| (uintptr_t)field < (uintptr_t)(table->UPB_PRIVATE(fields) + | |
| table->UPB_PRIVATE(field_count))); | |
| UPB_ASSERT(sub); | |
| if (field->UPB_PRIVATE(descriptortype) != kUpb_FieldType_Enum) { | |
| return false; | |
| } | |
| if (table->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry) { | |
| if (UPB_UNLIKELY(upb_MiniTableField_Number(field) != 2)) { | |
| return false; | |
| } | |
| if (!upb_MiniTableEnum_CheckValue(sub, 0)) { | |
| // An enum used in a map must include 0 as a value. This matches a check | |
| // performed in protoc ("Enum value in map must define 0 as the first | |
| // value"). Protoc should ensure that we never get here. | |
| // | |
| // This ends up being important if we receive wire messages where a map | |
| // entry omits the value (and thus defaults to 0). | |
| return false; | |
| } | |
| } | |
| upb_MiniTableSubInternal* table_sub = | |
| UPB_PRIVATE(_upb_MiniTableField_Sub)(field); | |
| UPB_ASSERT(upb_MiniTable_GetSubEnumTable(field) == NULL); | |
| *table_sub = upb_MiniTableSub_FromEnum(sub); | |
| return true; | |
| } | |
| uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* m, | |
| const upb_MiniTableField** subs) { | |
| uint32_t msg_count = 0; | |
| uint32_t enum_count = 0; | |
| for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) { | |
| const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i); | |
| if (upb_MiniTableField_CType(f) == kUpb_CType_Message) { | |
| *subs = f; | |
| ++subs; | |
| msg_count++; | |
| } | |
| } | |
| for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) { | |
| const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i); | |
| if (upb_MiniTableField_IsClosedEnum(f)) { | |
| *subs = f; | |
| ++subs; | |
| enum_count++; | |
| } | |
| } | |
| return (msg_count << 16) | enum_count; | |
| } | |
| // The list of sub_tables and sub_enums must exactly match the number and order | |
| // of sub-message fields and sub-enum fields given by upb_MiniTable_GetSubList() | |
| // above. | |
| bool upb_MiniTable_Link(upb_MiniTable* m, const upb_MiniTable** sub_tables, | |
| size_t sub_table_count, | |
| const upb_MiniTableEnum** sub_enums, | |
| size_t sub_enum_count) { | |
| uint32_t msg_count = 0; | |
| uint32_t enum_count = 0; | |
| for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) { | |
| upb_MiniTableField* f = | |
| (upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i); | |
| if (upb_MiniTableField_CType(f) == kUpb_CType_Message) { | |
| if (msg_count >= sub_table_count) return false; | |
| const upb_MiniTable* sub = sub_tables[msg_count++]; | |
| if (sub && !upb_MiniTable_SetSubMessage(m, f, sub)) return false; | |
| } | |
| } | |
| for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) { | |
| upb_MiniTableField* f = | |
| (upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i); | |
| if (upb_MiniTableField_IsClosedEnum(f)) { | |
| if (enum_count >= sub_enum_count) return false; | |
| const upb_MiniTableEnum* sub = sub_enums[enum_count++]; | |
| if (sub && !upb_MiniTable_SetSubEnum(m, f, sub)) return false; | |
| } | |
| } | |
| return (msg_count == sub_table_count) && (enum_count == sub_enum_count); | |
| } |