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 #ifndef PKG_ALGORITHM_LZ4_H 16 #define PKG_ALGORITHM_LZ4_H 17 18 #include "lz4.h" 19 #include "lz4frame.h" 20 #include "lz4hc.h" 21 #include "pkg_algorithm.h" 22 #include "pkg_stream.h" 23 #include "pkg_utils.h" 24 25 namespace Hpackage { 26 class PkgAlgorithmLz4 : public PkgAlgorithm { 27 public: 28 static const uint32_t LZ4S_MAGIC_NUMBER = 0x184D2204; 29 static const uint32_t LZ4S_SKIPPABLE0 = 0x184D2A50; 30 static const uint32_t LZ4S_SKIPPABLE_MASK = 0xFFFFFFF0; 31 static const uint32_t LZ4B_MAGIC_NUMBER = 0x184C2102; 32 static const uint32_t LZ4B_BLOCK_SIZE = 1 << 22; // (4M) 33 static const uint32_t LZ4S_HEADER_LEN = 20; 34 35 explicit PkgAlgorithmLz4(const Lz4FileInfo &config); 36 ~PkgAlgorithmLz4()37 ~PkgAlgorithmLz4() override {} 38 39 int32_t Pack(const PkgStreamPtr inStream, const PkgStreamPtr outStream, 40 PkgAlgorithmContext &context) override; 41 42 int32_t Unpack(const PkgStreamPtr inStream, 43 const PkgStreamPtr outStream, PkgAlgorithmContext &context) override; 44 45 void UpdateFileInfo(PkgManager::FileInfoPtr info) const override; 46 47 protected: GetBlockSizeFromBlockId(int32_t id)48 int32_t GetBlockSizeFromBlockId(int32_t id) const 49 { 50 return (1 << (8 + (2 * id))); // 8,2 : Get block size from block ID 51 } 52 int32_t GetPackParam(LZ4F_compressionContext_t &ctx, LZ4F_preferences_t &preferences, 53 size_t &inBuffSize, size_t &outBuffSize) const; 54 int32_t GetUnpackParam(LZ4F_decompressionContext_t &ctx, 55 const PkgStreamPtr inStream, size_t &nextToRead, size_t &srcOffset); 56 57 int32_t AdpLz4Compress(const uint8_t *src, uint8_t *dest, uint32_t srcSize, uint32_t dstCapacity) const; 58 59 int32_t AdpLz4Decompress(const uint8_t *src, uint8_t *dest, uint32_t srcSize, uint32_t dstCapacity) const; 60 61 protected: 62 int8_t compressionLevel_ {0}; 63 int8_t blockIndependence_ {0}; 64 int8_t contentChecksumFlag_ {0}; 65 int8_t blockSizeID_ {0}; 66 int8_t autoFlush_ {1}; 67 }; 68 69 class PkgAlgorithmBlockLz4 : public PkgAlgorithmLz4 { 70 public: 71 static const uint32_t LZ4B_REVERSED_LEN = 4; PkgAlgorithmBlockLz4(const Lz4FileInfo & config)72 explicit PkgAlgorithmBlockLz4(const Lz4FileInfo &config) : PkgAlgorithmLz4(config) {} 73 ~PkgAlgorithmBlockLz4()74 ~PkgAlgorithmBlockLz4() override {} 75 76 int32_t Pack(const PkgStreamPtr inStream, 77 const PkgStreamPtr outStream, PkgAlgorithmContext &) override; 78 79 int32_t Unpack(const PkgStreamPtr inStream, const PkgStreamPtr outStream, 80 PkgAlgorithmContext &) override; 81 }; 82 } // namespace Hpackage 83 #endif 84