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 LZ4_ADAPTER_H 17 #define LZ4_ADAPTER_H 18 #include <iostream> 19 #include <vector> 20 #include "deflate_adapter.h" 21 #include "diffpatch.h" 22 #include "lz4.h" 23 #include "lz4frame.h" 24 #include "lz4hc.h" 25 #include "pkg_manager.h" 26 #include "securec.h" 27 28 namespace UpdatePatch { 29 #define LZ4_BLOCK_SIZE(blockId) (1 << (8 + (2 * (blockId)))) 30 31 class Lz4Adapter : public DeflateAdapter { 32 public: 33 static constexpr uint32_t LZ4B_BLOCK_SIZE = 1 << 22; // (4M) 34 static constexpr uint32_t LZ4B_MAGIC_NUMBER = 0x184C2102; 35 static constexpr uint32_t LZ4B_REVERSED_LEN = 4; 36 Lz4Adapter(UpdatePatchWriterPtr outStream, size_t offset, const Hpackage::PkgManager::FileInfoPtr fileInfo); ~Lz4Adapter()37 ~Lz4Adapter() override {} 38 protected: 39 virtual int32_t CompressData(const BlockBuffer &srcData) = 0; 40 std::vector<uint8_t> buffer_ {}; 41 UpdatePatchWriterPtr outStream_ { nullptr }; 42 43 size_t offset_; 44 int32_t compressionLevel_ { 0 }; 45 int32_t blockIndependence_ { 0 }; 46 int32_t contentChecksumFlag_ { 0 }; 47 uint32_t blockSizeID_ { 0 }; 48 int32_t autoFlush_ {1}; 49 }; 50 51 class Lz4FrameAdapter : public Lz4Adapter { 52 public: Lz4FrameAdapter(UpdatePatchWriterPtr outStream,size_t offset,const Hpackage::PkgManager::FileInfoPtr fileInfo)53 Lz4FrameAdapter(UpdatePatchWriterPtr outStream, size_t offset, 54 const Hpackage::PkgManager::FileInfoPtr fileInfo) : Lz4Adapter(outStream, offset, fileInfo) {} ~Lz4FrameAdapter()55 ~Lz4FrameAdapter() override 56 { 57 Close(); 58 } 59 60 int32_t Open() override; 61 int32_t Close() override; 62 int32_t WriteData(const BlockBuffer &srcData) override; 63 int32_t FlushData(size_t &offset) override; 64 protected: 65 int32_t CompressData(const BlockBuffer &srcData) override; 66 size_t currDataSize_ = 0; 67 std::vector<uint8_t> inData_ {}; 68 private: 69 LZ4F_compressionContext_t compressionContext_ {}; 70 }; 71 72 class Lz4BlockAdapter : public Lz4FrameAdapter { 73 public: Lz4BlockAdapter(UpdatePatchWriterPtr outStream,size_t offset,const Hpackage::PkgManager::FileInfoPtr fileInfo)74 Lz4BlockAdapter(UpdatePatchWriterPtr outStream, size_t offset, 75 const Hpackage::PkgManager::FileInfoPtr fileInfo) : Lz4FrameAdapter(outStream, offset, fileInfo) {} ~Lz4BlockAdapter()76 ~Lz4BlockAdapter() override 77 { 78 Close(); 79 } 80 81 int32_t Open() override; 82 int32_t Close() override; 83 int32_t FlushData(size_t &offset) override; 84 private: 85 int32_t CompressData(const BlockBuffer &srcData) override; 86 }; 87 } // namespace UpdatePatch 88 #endif // LZ4_ADAPTER_H 89