1 // 2 // Copyright (C) 2020 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 <libsnapshot/cow_reader.h> 20 21 namespace android { 22 namespace snapshot { 23 24 class IByteStream { 25 public: ~IByteStream()26 virtual ~IByteStream() {} 27 28 // Read up to |length| bytes, storing the number of bytes read in the out- 29 // parameter. If the end of the stream is reached, 0 is returned. On error, 30 // -1 is returned. errno is NOT set. 31 virtual ssize_t Read(void* buffer, size_t length) = 0; 32 33 // Size of the stream. 34 virtual size_t Size() const = 0; 35 36 // Helper for Read(). Read the entire stream into |buffer|, up to |length| 37 // bytes. 38 ssize_t ReadFully(void* buffer, size_t length); 39 }; 40 41 class IDecompressor { 42 public: ~IDecompressor()43 virtual ~IDecompressor() {} 44 45 // Factory methods for decompression methods. 46 static std::unique_ptr<IDecompressor> Uncompressed(); 47 static std::unique_ptr<IDecompressor> Gz(); 48 static std::unique_ptr<IDecompressor> Brotli(); 49 static std::unique_ptr<IDecompressor> Lz4(); 50 static std::unique_ptr<IDecompressor> Zstd(); 51 52 static std::unique_ptr<IDecompressor> FromString(std::string_view compressor); 53 54 // Decompress at most |buffer_size| bytes, ignoring the first |ignore_bytes| 55 // of the decoded stream. |buffer_size| must be at least one byte. 56 // |decompressed_size| is the expected total size if the entire stream were 57 // decompressed. 58 // 59 // Returns the number of bytes written to |buffer|, or -1 on error. errno 60 // is NOT set. 61 virtual ssize_t Decompress(void* buffer, size_t buffer_size, size_t decompressed_size, 62 size_t ignore_bytes = 0) = 0; 63 set_stream(IByteStream * stream)64 void set_stream(IByteStream* stream) { stream_ = stream; } 65 66 protected: 67 IByteStream* stream_ = nullptr; 68 }; 69 70 } // namespace snapshot 71 } // namespace android 72