xref: /aosp_15_r20/system/extras/simpleperf/ZstdUtil.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #include <memory>
19 #include <string_view>
20 
21 namespace simpleperf {
22 
23 class Compressor {
24  public:
25   virtual ~Compressor();
26 
27   virtual bool AddInputData(const char* data, size_t size) = 0;
28   virtual bool FlushOutputData() = 0;
29   virtual std::string_view GetOutputData() = 0;
30   virtual void ConsumeOutputData(size_t size) = 0;
31 
TotalInputSize()32   uint64_t TotalInputSize() const { return total_input_size_; }
TotalOutputSize()33   uint64_t TotalOutputSize() const { return total_output_size_; }
34 
35  protected:
36   uint64_t total_input_size_ = 0;
37   uint64_t total_output_size_ = 0;
38 };
39 
40 class Decompressor {
41  public:
42   virtual ~Decompressor();
43 
44   virtual bool AddInputData(const char* data, size_t size) = 0;
45   virtual std::string_view GetOutputData() = 0;
46   virtual void ConsumeOutputData(size_t size) = 0;
47 
HasOutputData()48   bool HasOutputData() { return !GetOutputData().empty(); }
49 };
50 
51 std::unique_ptr<Compressor> CreateZstdCompressor(size_t compression_level = 3);
52 std::unique_ptr<Decompressor> CreateZstdDecompressor();
53 
54 bool ZstdCompress(const char* input_data, size_t input_size, std::string& output_data);
55 bool ZstdDecompress(const char* input_data, size_t input_size, std::string& output_data);
56 
57 }  // namespace simpleperf
58