xref: /aosp_15_r20/system/core/fs_mgr/libsnapshot/include/libsnapshot/cow_compress.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1 //
2 // Copyright (C) 2023 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 
19 #include <memory>
20 #include <vector>
21 
22 #include "libsnapshot/cow_format.h"
23 
24 namespace android {
25 namespace snapshot {
26 
27 class ICompressor {
28   public:
ICompressor(const int32_t compression_level,const uint32_t block_size)29     explicit ICompressor(const int32_t compression_level, const uint32_t block_size)
30         : compression_level_(compression_level), block_size_(block_size) {}
31 
~ICompressor()32     virtual ~ICompressor() {}
33     // Factory methods for compression methods.
34     static std::unique_ptr<ICompressor> Gz(const int32_t compression_level,
35                                            const uint32_t block_size);
36     static std::unique_ptr<ICompressor> Brotli(const int32_t compression_level,
37                                                const uint32_t block_size);
38     static std::unique_ptr<ICompressor> Lz4(const int32_t compression_level,
39                                             const uint32_t block_size);
40     static std::unique_ptr<ICompressor> Zstd(const int32_t compression_level,
41                                              const uint32_t block_size);
42 
43     static std::unique_ptr<ICompressor> Create(CowCompression compression,
44                                                const uint32_t block_size);
45 
GetCompressionLevel()46     int32_t GetCompressionLevel() const { return compression_level_; }
GetBlockSize()47     uint32_t GetBlockSize() const { return block_size_; }
48     [[nodiscard]] virtual std::vector<uint8_t> Compress(const void* data, size_t length) const = 0;
49 
50   private:
51     const int32_t compression_level_;
52     const uint32_t block_size_;
53 };
54 }  // namespace snapshot
55 }  // namespace android
56