gzipper.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/data_decoder/gzipper.h"
  5. #include "base/bit_cast.h"
  6. #include "base/containers/span.h"
  7. #include "base/strings/string_piece_forward.h"
  8. #include "mojo/public/cpp/base/big_buffer.h"
  9. #include "third_party/zlib/google/compression_utils.h"
  10. #include "third_party/zlib/google/compression_utils_portable.h"
  11. namespace data_decoder {
  12. namespace {
  13. mojo_base::BigBuffer StringToBuffer(const std::string& string) {
  14. return base::as_bytes(base::make_span(string));
  15. }
  16. } // namespace
  17. Gzipper::Gzipper() = default;
  18. Gzipper::~Gzipper() = default;
  19. void Gzipper::Deflate(mojo_base::BigBuffer data, DeflateCallback callback) {
  20. uLongf compressed_data_size = compressBound(data.size());
  21. std::vector<uint8_t> compressed_data(compressed_data_size);
  22. if (zlib_internal::CompressHelper(
  23. zlib_internal::ZRAW, compressed_data.data(), &compressed_data_size,
  24. base::bit_cast<const Bytef*>(data.data()), data.size(),
  25. Z_DEFAULT_COMPRESSION,
  26. /*malloc_fn=*/nullptr, /*free_fn=*/nullptr) != Z_OK) {
  27. std::move(callback).Run(absl::nullopt);
  28. return;
  29. }
  30. compressed_data.resize(compressed_data_size);
  31. std::move(callback).Run(std::move(compressed_data));
  32. }
  33. void Gzipper::Inflate(mojo_base::BigBuffer data,
  34. uint64_t max_uncompressed_size,
  35. InflateCallback callback) {
  36. uLongf uncompressed_size = static_cast<uLongf>(max_uncompressed_size);
  37. std::vector<uint8_t> output(max_uncompressed_size);
  38. if (zlib_internal::UncompressHelper(zlib_internal::ZRAW, output.data(),
  39. &uncompressed_size, data.data(),
  40. data.size()) != Z_OK) {
  41. std::move(callback).Run(absl::nullopt);
  42. return;
  43. }
  44. output.resize(uncompressed_size);
  45. std::move(callback).Run(std::move(output));
  46. }
  47. void Gzipper::Compress(mojo_base::BigBuffer data, CompressCallback callback) {
  48. // mojo_base::BigBuffer does not support resizing the backing storage. Output
  49. // the result into a std::string and copy its contents into a BigBuffer.
  50. std::string output;
  51. if (!compression::GzipCompress(data, &output)) {
  52. std::move(callback).Run(absl::nullopt);
  53. return;
  54. }
  55. std::move(callback).Run(StringToBuffer(output));
  56. }
  57. void Gzipper::Uncompress(mojo_base::BigBuffer compressed_data,
  58. UncompressCallback callback) {
  59. mojo_base::BigBuffer output(
  60. compression::GetUncompressedSize(compressed_data));
  61. if (!compression::GzipUncompress(compressed_data, output)) {
  62. std::move(callback).Run(absl::nullopt);
  63. return;
  64. }
  65. std::move(callback).Run(std::move(output));
  66. }
  67. } // namespace data_decoder