shangxinli commented on code in PR #991: URL: https://github.com/apache/iceberg-cpp/pull/991#discussion_r4284230736
########## src/iceberg/encryption/standard_key_metadata.h: ########## @@ -0,0 +1,68 @@ +/* + * 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/encryption/standard_key_metadata.h +/// Key metadata of the standard Iceberg encryption scheme. + +#include <cstdint> +#include <optional> +#include <span> +#include <vector> + +#include "iceberg/iceberg_export.h" +#include "iceberg/result.h" + +namespace iceberg { + +/// \brief Per-file key metadata of the standard encryption scheme. +/// +/// Stored in the `key_metadata` field of data files, delete files and manifest files, +/// and (encrypted by a key encryption key) in the table metadata `encryption-keys` for +/// manifest lists. +/// +/// The serialized form is compatible with Java's StandardKeyMetadata: a one-byte schema +/// version (1) followed by an Avro binary encoded record with the fields +/// `encryption_key: bytes`, `aad_prefix: union{null, bytes}` and +/// `file_length: union{null, long}`. +struct ICEBERG_EXPORT StandardKeyMetadata { Review Comment: Rebased on main and committed the regenerated cpp20_compatibility_internal.h. ########## src/iceberg/encryption/standard_key_metadata.cc: ########## @@ -0,0 +1,144 @@ +/* + * 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/encryption/standard_key_metadata.h" + +#include <utility> + +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace { + +void WriteLong(int64_t value, std::vector<uint8_t>& out) { + // Avro zig-zag varint encoding + auto n = (static_cast<uint64_t>(value) << 1) ^ static_cast<uint64_t>(value >> 63); + while (n & ~0x7FULL) { + out.push_back(static_cast<uint8_t>((n & 0x7F) | 0x80)); + n >>= 7; + } + out.push_back(static_cast<uint8_t>(n)); +} + +void WriteBytes(std::span<const uint8_t> bytes, std::vector<uint8_t>& out) { + WriteLong(static_cast<int64_t>(bytes.size()), out); + out.insert(out.end(), bytes.begin(), bytes.end()); +} + +class Decoder { + public: + explicit Decoder(std::span<const uint8_t> buffer) : buffer_(buffer) {} + + Result<int64_t> ReadLong() { + uint64_t n = 0; + int shift = 0; + while (true) { + if (pos_ >= buffer_.size()) { + return Invalid("Truncated key metadata"); + } + if (shift > 63) { + return Invalid("Invalid varint in key metadata"); + } + uint8_t b = buffer_[pos_++]; + n |= static_cast<uint64_t>(b & 0x7F) << shift; + if ((b & 0x80) == 0) { + break; + } + shift += 7; + } + return static_cast<int64_t>((n >> 1) ^ -(n & 1)); + } + + Result<std::vector<uint8_t>> ReadBytes() { + ICEBERG_ASSIGN_OR_RAISE(auto length, ReadLong()); + if (length < 0 || std::cmp_greater(length, buffer_.size() - pos_)) { + return Invalid("Invalid bytes length in key metadata: {}", length); + } + std::vector<uint8_t> bytes(buffer_.begin() + pos_, buffer_.begin() + pos_ + length); + pos_ += length; + return bytes; + } + + /// \brief Read the branch index of a ["null", T] union; true if T is present. + Result<bool> ReadOptionalBranch() { + ICEBERG_ASSIGN_OR_RAISE(auto branch, ReadLong()); Review Comment: You are right - union indices now go through an int-limited varint read, with `80 80 80 80 80 00` added as a regression case. -- 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]
