1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BZIP2_ADAPTER_H 17 #define BZIP2_ADAPTER_H 18 #include <vector> 19 #include "bzlib.h" 20 #include "deflate_adapter.h" 21 #include "diffpatch.h" 22 #include "pkg_manager.h" 23 #include "securec.h" 24 25 namespace UpdatePatch { 26 class BZip2Adapter : public DeflateAdapter { 27 public: BZip2Adapter()28 BZip2Adapter() : DeflateAdapter() {} ~BZip2Adapter()29 ~BZip2Adapter() override {} 30 31 int32_t Open() override; 32 int32_t Close() override; 33 protected: 34 static constexpr int32_t BLOCK_SIZE_BEST = 9; 35 bz_stream stream_ {}; 36 }; 37 38 class BZipBuffer2Adapter : public BZip2Adapter { 39 public: BZipBuffer2Adapter(std::vector<uint8_t> & buffer,size_t offset)40 BZipBuffer2Adapter(std::vector<uint8_t> &buffer, size_t offset) 41 : BZip2Adapter(), offset_(offset), buffer_(buffer) {} ~BZipBuffer2Adapter()42 ~BZipBuffer2Adapter() override {} 43 44 int32_t WriteData(const BlockBuffer &srcData) override; 45 int32_t FlushData(size_t &dataSize) override; 46 private: 47 size_t offset_ { 0 }; 48 size_t dataSize_ { 0 }; 49 std::vector<uint8_t> &buffer_; 50 }; 51 52 class BZip2StreamAdapter : public BZip2Adapter { 53 public: BZip2StreamAdapter(std::fstream & stream)54 explicit BZip2StreamAdapter(std::fstream &stream) : BZip2Adapter(), outStream_(stream) {} ~BZip2StreamAdapter()55 ~BZip2StreamAdapter() override {} 56 57 int32_t Open() override; 58 int32_t WriteData(const BlockBuffer &srcData) override; 59 int32_t FlushData(size_t &dataSize) override; 60 private: 61 std::vector<char> buffer_ { 0 }; 62 size_t dataSize_ { 0 }; 63 std::fstream &outStream_; 64 }; 65 66 class BZip2ReadAdapter { 67 public: BZip2ReadAdapter(size_t offset,size_t length)68 BZip2ReadAdapter(size_t offset, size_t length) : offset_(offset), dataLength_(length) {} ~BZip2ReadAdapter()69 virtual ~BZip2ReadAdapter() {} 70 71 virtual int32_t Open() = 0; Close()72 virtual int32_t Close() 73 { 74 return 0; 75 }; 76 virtual int32_t ReadData(BlockBuffer &info) = 0; 77 protected: 78 bool init_ { false }; 79 size_t offset_ { 0 }; 80 size_t dataLength_ = { 0 }; 81 bz_stream stream_ {}; 82 }; 83 84 class BZip2BufferReadAdapter : public BZip2ReadAdapter { 85 public: BZip2BufferReadAdapter(size_t offset,size_t length,const BlockBuffer & info)86 BZip2BufferReadAdapter(size_t offset, size_t length, const BlockBuffer &info) 87 : BZip2ReadAdapter(offset, length), buffer_(info) {} ~BZip2BufferReadAdapter()88 ~BZip2BufferReadAdapter() override {} 89 90 int32_t Open() override; 91 int32_t Close() override; 92 93 int32_t ReadData(BlockBuffer &info) override; 94 private: 95 BlockBuffer buffer_ {}; 96 }; 97 } // namespace UpdatePatch 98 #endif // BZIP2_ADAPTER_H