| // 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_ZIP_EMBEDDER_H_ | |
| #define THIRD_PARTY_CREDENTIO_FORMATS_ZIP_EMBEDDER_H_ | |
| #include <cstdint> | |
| #include "absl/base/nullability.h" | |
| #include "absl/status/status.h" | |
| #include "absl/status/statusor.h" | |
| #include "absl/strings/string_view.h" | |
| #include "absl/types/span.h" | |
| #include "formats/zip/reader.h" | |
| #include "riegeli/base/types.h" | |
| #include "riegeli/bytes/reader.h" | |
| #include "riegeli/bytes/writer.h" | |
| namespace credentio { | |
| // A utility class to embed a C2PA manifest store into a ZIP archive. | |
| class ZipEmbedder { | |
| public: | |
| struct CopyAndAllocateResult { | |
| // The offset of the manifest store in the output. | |
| riegeli::Position manifest_store_offset; | |
| // The offset of the manifest store's central directory header in the | |
| // output. | |
| riegeli::Position manifest_store_central_directory_header_offset; | |
| // The offset of the central directory in the output. | |
| riegeli::Position central_directory_offset; | |
| }; | |
| explicit ZipEmbedder() = default; | |
| // Copies the file entries from the source to the destination, and allocates | |
| // space for the manifest store. Returns a `CopyAndAllocateResult` containing | |
| // the offsets of the manifest store and its central directory header in the | |
| // output. | |
| absl::StatusOr<CopyAndAllocateResult> CopyAndAllocate( | |
| riegeli::Reader* absl_nonnull source, | |
| absl::Span<const ZipReader::FileEntry> file_entries, | |
| int64_t manifest_store_size, | |
| riegeli::Writer* absl_nonnull destination) const; | |
| // Embeds the manifest store into the ZIP archive at the offsets specified in | |
| // `copy_and_allocate_result`. | |
| absl::Status Embed(absl::string_view manifest_store, | |
| const CopyAndAllocateResult& copy_and_allocate_result, | |
| riegeli::Writer* absl_nonnull destination) const; | |
| }; | |
| } // namespace credentio | |
| #endif // THIRD_PARTY_CREDENTIO_FORMATS_ZIP_EMBEDDER_H_ |