| // Get the size of the VMO |
| uint64_t content_size_prop = 0; |
| zx_status_t status = config_vmo.get_prop_content_size(&content_size_prop); |
| ZX_ASSERT_MSG(status == ZX_OK, "Could not get content size of config VMO"); |
| size_t vmo_content_size = static_cast<size_t>(content_size_prop); |
| |
| // Checksum length must be correct |
| uint16_t checksum_length = 0; |
| status = config_vmo.read(&checksum_length, 0, 2); |
| ZX_ASSERT_MSG(status == ZX_OK, "Could not read checksum length from config VMO"); |
| |
| // Verify Checksum |
| std::vector<uint8_t> checksum(checksum_length); |
| status = config_vmo.read(checksum.data(), 2, checksum_length); |
| ZX_ASSERT_MSG(status == ZX_OK, "Could not read checksum from config VMO"); |
| std::vector<uint8_t> expected_checksum { |
| {{#each expected_checksum}} |
| {{(hex_byte this)}}{{#unless @last}},{{/unless}} |
| {{/each}} |
| }; |
| ZX_ASSERT_MSG(checksum == expected_checksum, "Invalid checksum for config VMO"); |
| |
| // Read the FIDL struct into memory |
| // Skip the checksum length + checksum + FIDL persistent header |
| // Align the struct pointer to 8 bytes (as required by FIDL) |
| size_t header = 2 + checksum_length + 8; |
| size_t fidl_struct_size = vmo_content_size - header; |
| std::unique_ptr<uint8_t[]> fidl_struct(new uint8_t[fidl_struct_size]); |
| status = config_vmo.read(fidl_struct.get(), header, fidl_struct_size); |
| ZX_ASSERT_MSG(status == ZX_OK, "Could not read FIDL struct from config VMO"); |
| |
| // TODO(https://fxbug.dev/100147) use fidl::Unpersist(header_and_struct_bytes) instead |
| // Decode the FIDL struct |
| fidl::unstable::DecodedMessage<::{{fidl_cpp_namespace}}::wire::Config> decoded( |
| fidl::internal::WireFormatVersion::kV2, fidl_struct.get(), static_cast<uint32_t>(fidl_struct_size)); |
| ZX_ASSERT_MSG(decoded.ok(), "Could not decode Config FIDL structure"); |
| ::{{fidl_cpp_namespace}}::wire::Config* fidl_config = decoded.PrimaryObject(); |
| |
| // Convert the configuration into a new struct |
| Config c\{{ |
| {{#each fields}} |
| {{#if (is_string_vector type_)}} |
| .{{ident}} = from_vector_string_view(fidl_config->{{ident}}) |
| {{else}} |
| {{#if (is_vector type_)}} |
| .{{ident}} = from_vector_view(fidl_config->{{ident}}) |
| {{else}} |
| {{#if (is_string type_)}} |
| .{{ident}} = std::string(fidl_config->{{ident}}.get()) |
| {{else}} |
| .{{ident}} = fidl_config->{{ident}} |
| {{/if}} |
| {{/if}} |
| {{/if}} |
| {{~#unless @last}},{{/unless}} |
| {{~/each~}} |
| }}; |
| |
| return c; |