zhaoxuan1994 commented on code in PR #624: URL: https://github.com/apache/iceberg-cpp/pull/624#discussion_r3266958290
########## src/iceberg/puffin/puffin_reader.cc: ########## @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "iceberg/puffin/puffin_reader.h" + +#include <algorithm> +#include <array> +#include <cstring> +#include <string_view> + +#include "iceberg/puffin/json_serde_internal.h" +#include "iceberg/puffin/puffin_format.h" +#include "iceberg/util/endian.h" +#include "iceberg/util/macros.h" + +namespace iceberg::puffin { + +namespace { + +// Validate magic bytes at the given offset. +Status CheckMagic(std::span<const std::byte> data, int64_t offset) { + if (offset < 0 || + offset + PuffinFormat::kMagicLength > static_cast<int64_t>(data.size())) { + return Invalid("Invalid file: cannot read magic at offset {}", offset); + } + auto* begin = reinterpret_cast<const uint8_t*>(data.data() + offset); + if (!std::equal(PuffinFormat::kMagicV1.begin(), PuffinFormat::kMagicV1.end(), begin)) { + return Invalid("Invalid file: expected magic at offset {}", offset); + } + return {}; +} + +} // namespace + +PuffinReader::PuffinReader(std::span<const std::byte> data) : data_(data) {} + +Result<FileMetadata> PuffinReader::ReadFileMetadata() { + auto file_size = static_cast<int64_t>(data_.size()); + + if (file_size < PuffinFormat::kFooterStructLength) { + return Invalid("Invalid file: file length {} is less than minimal footer size {}", + file_size, PuffinFormat::kFooterStructLength); + } + + // Read footer struct from end of file + auto footer_struct_offset = file_size - PuffinFormat::kFooterStructLength; + + // Validate footer end magic + ICEBERG_RETURN_UNEXPECTED( + CheckMagic(data_, footer_struct_offset + PuffinFormat::kFooterStructMagicOffset)); + + // Read payload size from footer struct + auto payload_size = ReadLittleEndian<int32_t>( + data_.data() + footer_struct_offset + PuffinFormat::kFooterStructPayloadSizeOffset); + + if (payload_size < 0) { + return Invalid("Invalid file: negative payload size {}", payload_size); + } + + // Calculate total footer size and validate + int64_t footer_size = PuffinFormat::kFooterStartMagicLength + + static_cast<int64_t>(payload_size) + + PuffinFormat::kFooterStructLength; + auto footer_offset = file_size - footer_size; + if (footer_offset < 0) { + return Invalid("Invalid file: footer size {} exceeds file size {}", footer_size, + file_size); + } + + // Validate footer start magic + ICEBERG_RETURN_UNEXPECTED(CheckMagic(data_, footer_offset)); + + // Check flags for footer compression + std::array<uint8_t, 4> flags{}; + std::memcpy( + flags.data(), + data_.data() + footer_struct_offset + PuffinFormat::kFooterStructFlagsOffset, 4); + + PuffinCompressionCodec footer_compression = PuffinCompressionCodec::kNone; + if (IsFlagSet(flags, PuffinFlag::kFooterPayloadCompressed)) { + footer_compression = PuffinFormat::kDefaultFooterCompressionCodec; + } + + // Extract footer payload + auto payload_offset = footer_offset + PuffinFormat::kFooterStartMagicLength; + std::span<const std::byte> payload_span(data_.data() + payload_offset, payload_size); Review Comment: Yeah I thought about this — span is basically a pointer+size pair like string_view, so const on the member itself doesn't buy us much. The span is set once in the constructor and never reassigned. I'll leave it as-is to stay consistent with how the rest of the project uses span/string_view members. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
