| // 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. | |
| // | |
| #include "formats/zip/zlib_util.h" | |
| #include <cstddef> | |
| #include <string> | |
| #include "absl/status/status.h" | |
| #include "absl/status/statusor.h" | |
| #include "absl/strings/string_view.h" | |
| #include "riegeli/base/maker.h" | |
| #include "riegeli/bytes/string_reader.h" | |
| #include "riegeli/bytes/string_writer.h" | |
| #include "riegeli/zlib/zlib_reader.h" | |
| #include "riegeli/zlib/zlib_writer.h" | |
| namespace credentio { | |
| absl::StatusOr<std::string> Compress(absl::string_view data) { | |
| std::string compressed_data; | |
| riegeli::ZlibWriter<riegeli::StringWriter<>> writer( | |
| riegeli::Maker(&compressed_data), | |
| riegeli::ZlibWriterBase::Options() | |
| .set_header(riegeli::ZlibWriterBase::Header::kRaw) | |
| .set_compression_level(1)); | |
| if (!writer.Write(data) || !writer.Close()) { | |
| return absl::InternalError("Failed to compress data."); | |
| } | |
| return compressed_data; | |
| } | |
| absl::StatusOr<std::string> Uncompress(absl::string_view data, | |
| size_t max_uncompressed_size) { | |
| riegeli::ZlibReader<riegeli::StringReader<>> reader( | |
| riegeli::Maker(data), riegeli::ZlibReaderBase::Options().set_header( | |
| riegeli::ZlibReaderBase::Header::kRaw)); | |
| std::string uncompressed_data; | |
| reader.Read(max_uncompressed_size, uncompressed_data); | |
| if (reader.Pull() || !reader.Close()) { | |
| return absl::InternalError("Failed to uncompress data."); | |
| } | |
| return uncompressed_data; | |
| } | |
| } // namespace credentio |