| // Copyright 2026 Google LLC | |
| // | |
| // Licensed 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 | |
| // | |
| // https://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. | |
| // | |
| #ifndef THIRD_PARTY_CREDENTIO_FORMATS_PNG_HASHER_H_ | |
| #define THIRD_PARTY_CREDENTIO_FORMATS_PNG_HASHER_H_ | |
| #include <cstdint> | |
| #include <optional> | |
| #include <vector> | |
| #include "absl/functional/function_ref.h" | |
| #include "absl/status/statusor.h" | |
| #include "crypto/algorithms.h" | |
| #include "formats/byte_range.h" | |
| #include "formats/png/reader.h" | |
| #include "proto/data_hash_assertion.pb.h" | |
| #include "riegeli/bytes/reader.h" | |
| namespace credentio { | |
| struct DigestPngResult { | |
| DataHashAssertion assertion; | |
| int64_t insertion_offset; | |
| std::optional<int64_t> existing_c2pa_length; | |
| }; | |
| // Determines byte ranges within a given PNG chunk to be excluded from hashing. | |
| // Returned ByteRange offsets must be absolute file offsets within | |
| // [chunk.offset, chunk.offset + chunk.length). | |
| using PngExclusionProcessor = | |
| absl::FunctionRef<absl::StatusOr<std::vector<ByteRange>>( | |
| riegeli::Reader& reader, const PngChunk& chunk)>; | |
| // Digests the given PNG image and returns DigestPngResult that can be used to | |
| // generate a C2PA manifest store. It is assumed that a C2PA manifest store will | |
| // be inserted immediately before the first IDAT chunk or already appears | |
| // somewhere before the first IDAT chunk. | |
| // | |
| // If `exclusion_processor` is provided, it is invoked for chunks to identify | |
| // byte ranges to exclude from hashing. | |
| absl::StatusOr<DigestPngResult> DigestPng( | |
| riegeli::Reader& input, HashAlgorithm alg, | |
| std::optional<PngExclusionProcessor> exclusion_processor = std::nullopt); | |
| } // namespace credentio | |
| #endif // THIRD_PARTY_CREDENTIO_FORMATS_PNG_HASHER_H_ |