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