1// Copyright 2014 The Chromium Authors
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/check_op.h"
8#include "base/process/memory.h"
9#include "base/sys_byteorder.h"
10
11#include "third_party/zlib/google/compression_utils_portable.h"
12
13namespace compression {
14
15bool GzipCompress(base::span<const char> input,
16                  char* output_buffer,
17                  size_t output_buffer_size,
18                  size_t* compressed_size,
19                  void* (*malloc_fn)(size_t),
20                  void (*free_fn)(void*)) {
21  static_assert(sizeof(Bytef) == 1, "");
22
23  // uLongf can be larger than size_t.
24  uLongf compressed_size_long = static_cast<uLongf>(output_buffer_size);
25  if (zlib_internal::GzipCompressHelper(
26          reinterpret_cast<Bytef*>(output_buffer), &compressed_size_long,
27          reinterpret_cast<const Bytef*>(input.data()),
28          static_cast<uLongf>(input.size()), malloc_fn, free_fn) != Z_OK) {
29    return false;
30  }
31  // No overflow, as compressed_size_long <= output.size() which is a size_t.
32  *compressed_size = static_cast<size_t>(compressed_size_long);
33  return true;
34}
35
36bool GzipCompress(base::span<const char> input, std::string* output) {
37  return GzipCompress(base::as_bytes(input), output);
38}
39
40bool GzipCompress(base::span<const uint8_t> input, std::string* output) {
41  // Not using std::vector<> because allocation failures are recoverable,
42  // which is hidden by std::vector<>.
43  static_assert(sizeof(Bytef) == 1, "");
44  const uLongf input_size = static_cast<uLongf>(input.size());
45
46  uLongf compressed_data_size =
47      zlib_internal::GzipExpectedCompressedSize(input_size);
48
49  Bytef* compressed_data;
50  if (!base::UncheckedMalloc(compressed_data_size,
51                             reinterpret_cast<void**>(&compressed_data))) {
52    return false;
53  }
54
55  if (zlib_internal::GzipCompressHelper(
56          compressed_data, &compressed_data_size,
57          reinterpret_cast<const Bytef*>(input.data()), input_size, nullptr,
58          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
76bool GzipUncompress(const std::string& input, std::string* output) {
77  std::string uncompressed_output;
78  uLongf uncompressed_size = static_cast<uLongf>(GetUncompressedSize(input));
79  if (size_t{uncompressed_size} > uncompressed_output.max_size())
80    return false;
81
82  uncompressed_output.resize(uncompressed_size);
83  if (zlib_internal::GzipUncompressHelper(
84          reinterpret_cast<Bytef*>(uncompressed_output.data()),
85          &uncompressed_size, reinterpret_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
93bool 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
98bool 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             reinterpret_cast<Bytef*>(const_cast<uint8_t*>(output.data())),
105             &uncompressed_size, reinterpret_cast<const Bytef*>(input.data()),
106             static_cast<uLongf>(input.size())) == Z_OK;
107}
108
109bool GzipUncompress(base::span<const char> input, std::string* output) {
110  return GzipUncompress(base::as_bytes(input), output);
111}
112
113bool 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             reinterpret_cast<Bytef*>(output->data()), &uncompressed_size,
120             reinterpret_cast<const Bytef*>(input.data()),
121             static_cast<uLongf>(input.size())) == Z_OK;
122}
123
124uint32_t GetUncompressedSize(base::span<const char> compressed_data) {
125  return GetUncompressedSize(base::as_bytes(compressed_data));
126}
127
128uint32_t GetUncompressedSize(base::span<const uint8_t> compressed_data) {
129  return zlib_internal::GetGzipUncompressedSize(
130      reinterpret_cast<const Bytef*>(compressed_data.data()),
131      compressed_data.size());
132}
133
134}  // namespace compression
135