Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 | |
| 5 | #include "third_party/zlib/google/compression_utils.h" |
| 6 | |
| 7 | #include "base/bit_cast.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/process/memory.h" |
| 10 | #include "base/strings/string_piece.h" |
| 11 | #include "base/sys_byteorder.h" |
| 12 | |
| 13 | #include "third_party/zlib/google/compression_utils_portable.h" |
| 14 | |
Kaido Kert | f585e26 | 2020-06-08 11:42:28 -0700 | [diff] [blame] | 15 | #if defined(OS_STARBOARD) |
| 16 | #include "starboard/client_porting/poem/stdio_poem.h" |
| 17 | #endif |
| 18 | |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 19 | namespace compression { |
| 20 | |
| 21 | bool GzipCompress(base::StringPiece input, |
| 22 | char* output_buffer, |
| 23 | size_t output_buffer_size, |
| 24 | size_t* compressed_size, |
| 25 | void* (*malloc_fn)(size_t), |
| 26 | void (*free_fn)(void*)) { |
| 27 | static_assert(sizeof(Bytef) == 1, ""); |
| 28 | |
| 29 | // uLongf can be larger than size_t. |
| 30 | uLongf compressed_size_long = static_cast<uLongf>(output_buffer_size); |
| 31 | if (zlib_internal::GzipCompressHelper( |
| 32 | bit_cast<Bytef*>(output_buffer), &compressed_size_long, |
| 33 | bit_cast<const Bytef*>(input.data()), |
| 34 | static_cast<uLongf>(input.size()), malloc_fn, free_fn) != Z_OK) { |
| 35 | return false; |
| 36 | } |
| 37 | // No overflow, as compressed_size_long <= output.size() which is a size_t. |
| 38 | *compressed_size = static_cast<size_t>(compressed_size_long); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | bool GzipCompress(base::StringPiece input, std::string* output) { |
| 43 | // Not using std::vector<> because allocation failures are recoverable, |
| 44 | // which is hidden by std::vector<>. |
| 45 | static_assert(sizeof(Bytef) == 1, ""); |
| 46 | const uLongf input_size = static_cast<uLongf>(input.size()); |
| 47 | |
| 48 | uLongf compressed_data_size = |
| 49 | zlib_internal::GZipExpectedCompressedSize(input_size); |
| 50 | |
| 51 | Bytef* compressed_data; |
| 52 | if (!base::UncheckedMalloc(compressed_data_size, |
| 53 | reinterpret_cast<void**>(&compressed_data))) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | if (zlib_internal::GzipCompressHelper(compressed_data, &compressed_data_size, |
| 58 | bit_cast<const Bytef*>(input.data()), |
| 59 | input_size, nullptr, nullptr) != Z_OK) { |
| 60 | free(compressed_data); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | Bytef* resized_data = |
| 65 | reinterpret_cast<Bytef*>(realloc(compressed_data, compressed_data_size)); |
| 66 | if (!resized_data) { |
| 67 | free(compressed_data); |
| 68 | return false; |
| 69 | } |
| 70 | output->assign(resized_data, resized_data + compressed_data_size); |
| 71 | DCHECK_EQ(input_size, GetUncompressedSize(*output)); |
| 72 | |
| 73 | free(resized_data); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool GzipUncompress(const std::string& input, std::string* output) { |
| 78 | std::string uncompressed_output; |
| 79 | uLongf uncompressed_size = static_cast<uLongf>(GetUncompressedSize(input)); |
| 80 | if (uncompressed_size > uncompressed_output.max_size()) |
| 81 | return false; |
| 82 | |
| 83 | uncompressed_output.resize(uncompressed_size); |
| 84 | if (zlib_internal::GzipUncompressHelper( |
| 85 | bit_cast<Bytef*>(uncompressed_output.data()), &uncompressed_size, |
| 86 | bit_cast<const Bytef*>(input.data()), |
| 87 | static_cast<uLongf>(input.length())) == Z_OK) { |
| 88 | output->swap(uncompressed_output); |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | bool GzipUncompress(base::StringPiece input, base::StringPiece output) { |
| 95 | uLongf uncompressed_size = GetUncompressedSize(input); |
| 96 | if (uncompressed_size > output.size()) |
| 97 | return false; |
| 98 | return zlib_internal::GzipUncompressHelper( |
| 99 | bit_cast<Bytef*>(output.data()), &uncompressed_size, |
| 100 | bit_cast<const Bytef*>(input.data()), |
| 101 | static_cast<uLongf>(input.length())) == Z_OK; |
| 102 | } |
| 103 | |
| 104 | bool GzipUncompress(base::StringPiece input, std::string* output) { |
| 105 | // Disallow in-place usage, i.e., |input| using |*output| as underlying data. |
| 106 | DCHECK_NE(input.data(), output->data()); |
| 107 | uLongf uncompressed_size = GetUncompressedSize(input); |
| 108 | output->resize(uncompressed_size); |
| 109 | return zlib_internal::GzipUncompressHelper( |
| 110 | bit_cast<Bytef*>(output->data()), &uncompressed_size, |
| 111 | bit_cast<const Bytef*>(input.data()), |
| 112 | static_cast<uLongf>(input.length())) == Z_OK; |
| 113 | } |
| 114 | |
| 115 | uint32_t GetUncompressedSize(base::StringPiece compressed_data) { |
| 116 | // The uncompressed size is stored in the last 4 bytes of |input| in LE. |
| 117 | uint32_t size; |
| 118 | if (compressed_data.length() < sizeof(size)) |
| 119 | return 0; |
| 120 | memcpy(&size, |
| 121 | &compressed_data.data()[compressed_data.length() - sizeof(size)], |
| 122 | sizeof(size)); |
| 123 | return base::ByteSwapToLE32(size); |
| 124 | } |
| 125 | |
| 126 | } // namespace compression |