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