wgtmac commented on code in PR #624:
URL: https://github.com/apache/iceberg-cpp/pull/624#discussion_r3287383998


##########
src/iceberg/puffin/puffin_reader.cc:
##########
@@ -0,0 +1,204 @@
+/*
+ * 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 <cstdint>
+#include <cstring>
+#include <string_view>
+
+#include "iceberg/file_io.h"
+#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 in a buffer 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 {}, got [{:#04x}, {:#04x}, "
+        "{:#04x}, {:#04x}]",
+        offset, begin[0], begin[1], begin[2], begin[3]);
+  }
+  return {};
+}
+
+// Validate that no unknown flag bits are set.
+Status CheckUnknownFlags(std::span<const uint8_t, 4> flags) {
+  constexpr uint8_t kKnownBitsMask = 0x01;
+  if ((flags[0] & ~kKnownBitsMask) != 0 || flags[1] != 0 || flags[2] != 0 ||
+      flags[3] != 0) {
+    return Invalid(
+        "Invalid file: unknown footer flags set [{:#04x}, {:#04x}, {:#04x}, 
{:#04x}]",
+        flags[0], flags[1], flags[2], flags[3]);
+  }
+  return {};
+}
+
+}  // namespace
+
+PuffinReader::PuffinReader(std::span<const std::byte> data)
+    : data_(data), file_size_(static_cast<int64_t>(data.size())) {}
+
+PuffinReader::PuffinReader(std::unique_ptr<InputFile> input_file)
+    : input_file_(std::move(input_file)) {}
+
+PuffinReader::~PuffinReader() = default;
+
+Result<std::vector<std::byte>> PuffinReader::ReadBytes(int64_t offset, int64_t 
length) {
+  if (IsFileMode()) {
+    if (!stream_) {
+      ICEBERG_ASSIGN_OR_RAISE(stream_, input_file_->Open());
+    }
+    std::vector<std::byte> buf(length);
+    ICEBERG_RETURN_UNEXPECTED(stream_->ReadFully(offset, buf));
+    return buf;
+  }
+  // Memory mode
+  if (offset < 0 || length < 0 || offset > file_size_ || length > file_size_ - 
offset) {
+    return Invalid("Read out of bounds: offset {} + length {} exceeds file 
size {}",
+                   offset, length, file_size_);
+  }
+  return std::vector<std::byte>(data_.data() + offset, data_.data() + offset + 
length);
+}
+
+Result<FileMetadata> PuffinReader::ReadFileMetadata() {
+  // Get file size
+  if (IsFileMode()) {
+    ICEBERG_ASSIGN_OR_RAISE(file_size_, input_file_->Size());

Review Comment:
   Please fail on unknown footer flags here, matching Java. Ignoring extra bits 
can make newer or corrupted Puffin files look valid.



##########
src/iceberg/puffin/puffin_reader.h:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+/// \file iceberg/puffin/puffin_reader.h
+/// Puffin file reader.
+
+#include <cstddef>
+#include <cstdint>
+#include <memory>
+#include <span>
+#include <utility>
+#include <vector>
+
+#include "iceberg/iceberg_data_export.h"
+#include "iceberg/puffin/file_metadata.h"
+#include "iceberg/result.h"
+
+namespace iceberg {
+class InputFile;
+class SeekableInputStream;
+}  // namespace iceberg
+
+namespace iceberg::puffin {
+
+/// \brief Reader for Puffin files.
+///
+/// Supports two modes:
+/// - Memory mode: parses from an in-memory buffer.

Review Comment:
   Can we avoid making the public Puffin API in-memory only? Java and the C++ 
IO layer are file/stream based, which fits object stores and range reads better.



-- 
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]

Reply via email to