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 #ifndef THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_
6 #define THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_
7 
8 #include <string>
9 
10 #include "base/containers/span.h"
11 
12 namespace compression {
13 
14 // Compresses the data in |input| using gzip, storing the result in
15 // |output_buffer|, of size |output_buffer_size|. If the buffer is large enough
16 // and compression succeeds, |compressed_size| points to the compressed data
17 // size after the call.
18 // |malloc_fn| and |free_fn| are pointers to malloc() and free()-like functions,
19 // or nullptr to use the standard ones.
20 // Returns true for success.
21 bool GzipCompress(base::span<const char> 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 
28 // Compresses the data in |input| using gzip, storing the result in |output|.
29 // |input| and |output| are allowed to point to the same string (in-place
30 // operation).
31 // Returns true for success.
32 bool GzipCompress(base::span<const char> input, std::string* output);
33 
34 // Like the above method, but using uint8_t instead.
35 bool GzipCompress(base::span<const uint8_t> input, std::string* output);
36 
37 // Uncompresses the data in |input| using gzip, storing the result in |output|.
38 // |input| and |output| are allowed to be the same string (in-place operation).
39 // Returns true for success.
40 bool GzipUncompress(const std::string& input, std::string* output);
41 
42 // Like the above method, but uses base::span to avoid allocations if
43 // needed. |output|'s size must be at least as large as the return value from
44 // GetUncompressedSize.
45 // Returns true for success.
46 bool GzipUncompress(base::span<const char> input,
47                     base::span<const char> output);
48 
49 // Like the above method, but using uint8_t instead.
50 bool GzipUncompress(base::span<const uint8_t> input,
51                     base::span<const uint8_t> output);
52 
53 // Uncompresses the data in |input| using gzip, and writes the results to
54 // |output|, which must NOT be the underlying string of |input|, and is resized
55 // if necessary.
56 // Returns true for success.
57 bool GzipUncompress(base::span<const char> input, std::string* output);
58 
59 // Like the above method, but using uint8_t instead.
60 bool GzipUncompress(base::span<const uint8_t> input, std::string* output);
61 
62 // Returns the uncompressed size from GZIP-compressed |compressed_data|.
63 uint32_t GetUncompressedSize(base::span<const char> compressed_data);
64 
65 // Like the above method, but using uint8_t instead.
66 uint32_t GetUncompressedSize(base::span<const uint8_t> compressed_data);
67 
68 }  // namespace compression
69 
70 #endif  // THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_
71