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/strings/string_piece.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::StringPiece 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::StringPiece input, std::string* output); 33 34 // Uncompresses the data in |input| using gzip, storing the result in |output|. 35 // |input| and |output| are allowed to be the same string (in-place operation). 36 // Returns true for success. 37 bool GzipUncompress(const std::string& input, std::string* output); 38 39 // Like the above method, but uses base::StringPiece to avoid allocations if 40 // needed. |output|'s size must be at least as large as the return value from 41 // GetUncompressedSize. 42 // Returns true for success. 43 bool GzipUncompress(base::StringPiece input, base::StringPiece output); 44 45 // Uncompresses the data in |input| using gzip, and writes the results to 46 // |output|, which must NOT be the underlying string of |input|, and is resized 47 // if necessary. 48 // Returns true for success. 49 bool GzipUncompress(base::StringPiece input, std::string* output); 50 51 // Returns the uncompressed size from GZIP-compressed |compressed_data|. 52 uint32_t GetUncompressedSize(base::StringPiece compressed_data); 53 54 } // namespace compression 55 56 #endif // THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_ 57