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/check_op.h"
9 #include "base/process/memory.h"
10 #include "base/sys_byteorder.h"
11
12 #include "third_party/zlib/google/compression_utils_portable.h"
13
14 namespace compression {
15
GzipCompress(base::span<const char> input,char * output_buffer,size_t output_buffer_size,size_t * compressed_size,void * (* malloc_fn)(size_t),void (* free_fn)(void *))16 bool GzipCompress(base::span<const char> input,
17 char* output_buffer,
18 size_t output_buffer_size,
19 size_t* compressed_size,
20 void* (*malloc_fn)(size_t),
21 void (*free_fn)(void*)) {
22 static_assert(sizeof(Bytef) == 1, "");
23
24 // uLongf can be larger than size_t.
25 uLongf compressed_size_long = static_cast<uLongf>(output_buffer_size);
26 if (zlib_internal::GzipCompressHelper(
27 bit_cast<Bytef*>(output_buffer), &compressed_size_long,
28 bit_cast<const Bytef*>(input.data()),
29 static_cast<uLongf>(input.size()), malloc_fn, free_fn) != Z_OK) {
30 return false;
31 }
32 // No overflow, as compressed_size_long <= output.size() which is a size_t.
33 *compressed_size = static_cast<size_t>(compressed_size_long);
34 return true;
35 }
36
GzipCompress(base::span<const char> input,std::string * output)37 bool GzipCompress(base::span<const char> input, std::string* output) {
38 return GzipCompress(base::as_bytes(input), output);
39 }
40
GzipCompress(base::span<const uint8_t> input,std::string * output)41 bool GzipCompress(base::span<const uint8_t> input, std::string* output) {
42 // Not using std::vector<> because allocation failures are recoverable,
43 // which is hidden by std::vector<>.
44 static_assert(sizeof(Bytef) == 1, "");
45 const uLongf input_size = static_cast<uLongf>(input.size());
46
47 uLongf compressed_data_size =
48 zlib_internal::GzipExpectedCompressedSize(input_size);
49
50 Bytef* compressed_data;
51 if (!base::UncheckedMalloc(compressed_data_size,
52 reinterpret_cast<void**>(&compressed_data))) {
53 return false;
54 }
55
56 if (zlib_internal::GzipCompressHelper(compressed_data, &compressed_data_size,
57 bit_cast<const Bytef*>(input.data()),
58 input_size, nullptr, nullptr) != Z_OK) {
59 free(compressed_data);
60 return false;
61 }
62
63 Bytef* resized_data =
64 reinterpret_cast<Bytef*>(realloc(compressed_data, compressed_data_size));
65 if (!resized_data) {
66 free(compressed_data);
67 return false;
68 }
69 output->assign(resized_data, resized_data + compressed_data_size);
70 DCHECK_EQ(input_size, GetUncompressedSize(*output));
71
72 free(resized_data);
73 return true;
74 }
75
GzipUncompress(const std::string & input,std::string * output)76 bool GzipUncompress(const std::string& input, std::string* output) {
77 std::string uncompressed_output;
78 uLongf uncompressed_size = static_cast<uLongf>(GetUncompressedSize(input));
79 if (uncompressed_size > uncompressed_output.max_size())
80 return false;
81
82 uncompressed_output.resize(uncompressed_size);
83 if (zlib_internal::GzipUncompressHelper(
84 bit_cast<Bytef*>(uncompressed_output.data()), &uncompressed_size,
85 bit_cast<const Bytef*>(input.data()),
86 static_cast<uLongf>(input.length())) == Z_OK) {
87 output->swap(uncompressed_output);
88 return true;
89 }
90 return false;
91 }
92
GzipUncompress(base::span<const char> input,base::span<const char> output)93 bool GzipUncompress(base::span<const char> input,
94 base::span<const char> output) {
95 return GzipUncompress(base::as_bytes(input), base::as_bytes(output));
96 }
97
GzipUncompress(base::span<const uint8_t> input,base::span<const uint8_t> output)98 bool GzipUncompress(base::span<const uint8_t> input,
99 base::span<const uint8_t> output) {
100 uLongf uncompressed_size = GetUncompressedSize(input);
101 if (uncompressed_size > output.size())
102 return false;
103 return zlib_internal::GzipUncompressHelper(
104 bit_cast<Bytef*>(output.data()), &uncompressed_size,
105 bit_cast<const Bytef*>(input.data()),
106 static_cast<uLongf>(input.size())) == Z_OK;
107 }
108
GzipUncompress(base::span<const char> input,std::string * output)109 bool GzipUncompress(base::span<const char> input, std::string* output) {
110 return GzipUncompress(base::as_bytes(input), output);
111 }
112
GzipUncompress(base::span<const uint8_t> input,std::string * output)113 bool GzipUncompress(base::span<const uint8_t> input, std::string* output) {
114 // Disallow in-place usage, i.e., |input| using |*output| as underlying data.
115 DCHECK_NE(reinterpret_cast<const char*>(input.data()), output->data());
116 uLongf uncompressed_size = GetUncompressedSize(input);
117 output->resize(uncompressed_size);
118 return zlib_internal::GzipUncompressHelper(
119 bit_cast<Bytef*>(output->data()), &uncompressed_size,
120 bit_cast<const Bytef*>(input.data()),
121 static_cast<uLongf>(input.size())) == Z_OK;
122 }
123
GetUncompressedSize(base::span<const char> compressed_data)124 uint32_t GetUncompressedSize(base::span<const char> compressed_data) {
125 return GetUncompressedSize(base::as_bytes(compressed_data));
126 }
127
GetUncompressedSize(base::span<const uint8_t> compressed_data)128 uint32_t GetUncompressedSize(base::span<const uint8_t> compressed_data) {
129 return zlib_internal::GetGzipUncompressedSize(
130 bit_cast<Bytef*>(compressed_data.data()), compressed_data.size());
131 }
132
133 } // namespace compression
134