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_H 16 #define PKG_ALGORITHM_H 17 18 #include "pkg_algo_digest.h" 19 #include "pkg_algo_sign.h" 20 #include "pkg_stream.h" 21 22 namespace Hpackage { 23 struct PkgContextOffset { 24 size_t srcOffset; 25 size_t destOffset; PkgContextOffsetPkgContextOffset26 PkgContextOffset(size_t srcOffset, size_t destOffset) 27 { 28 this->srcOffset = srcOffset; 29 this->destOffset = destOffset; 30 } 31 }; 32 33 struct PkgContextSize { 34 size_t packedSize = 0; 35 size_t unpackedSize = 0; PkgContextSizePkgContextSize36 PkgContextSize(size_t packedSize, size_t unpackedSize) 37 { 38 this->packedSize = packedSize; 39 this->unpackedSize = unpackedSize; 40 } 41 }; 42 43 struct PkgAlgorithmContext { 44 size_t srcOffset; 45 size_t destOffset; 46 size_t packedSize = 0; 47 size_t unpackedSize = 0; 48 uint32_t crc = 0; 49 uint8_t digestMethod = 0; 50 uint8_t digest[DIGEST_MAX_LEN] = {}; 51 PkgAlgorithmContextPkgAlgorithmContext52 PkgAlgorithmContext(PkgContextOffset offset, PkgContextSize size, uint32_t crc, uint8_t digestMethod) 53 { 54 this->srcOffset = offset.srcOffset; 55 this->destOffset = offset.destOffset; 56 this->packedSize = size.packedSize; 57 this->unpackedSize = size.unpackedSize; 58 this->crc = crc; 59 this->digestMethod = digestMethod; 60 } 61 }; 62 63 class PkgAlgorithm { 64 public: 65 using PkgAlgorithmPtr = std::shared_ptr<PkgAlgorithm>; 66 using VerifyFunction = std::function<int32_t(PkgBuffer &buffer, size_t len, size_t offset)>; 67 PkgAlgorithm()68 PkgAlgorithm() {} 69 ~PkgAlgorithm()70 virtual ~PkgAlgorithm() {} 71 72 virtual int32_t Pack(const PkgStreamPtr inStream, const PkgStreamPtr outStream, 73 PkgAlgorithmContext &context); 74 virtual int32_t Unpack(const PkgStreamPtr inStream, const PkgStreamPtr outStream, 75 PkgAlgorithmContext &context); 76 UpdateFileInfo(PkgManager::FileInfoPtr info)77 virtual void UpdateFileInfo(PkgManager::FileInfoPtr info) const 78 { 79 (void)info; 80 } 81 82 int32_t UnpackWithVerify(const PkgStreamPtr inStream, const PkgStreamPtr outStream, 83 PkgAlgorithmContext &context, VerifyFunction verifier = nullptr); 84 protected: 85 int32_t FinalDigest(DigestAlgorithm::DigestAlgorithmPtr algorithm, 86 PkgAlgorithmContext &context, bool check) const; 87 int32_t ReadData(const PkgStreamPtr inStream, 88 size_t offset, PkgBuffer &buffer, size_t &remainSize, size_t &readLen) const; 89 }; 90 91 class PkgAlgorithmFactory { 92 public: 93 static PkgAlgorithm::PkgAlgorithmPtr GetAlgorithm(const PkgManager::FileInfoPtr config = nullptr); 94 95 static DigestAlgorithm::DigestAlgorithmPtr GetDigestAlgorithm(uint8_t type); 96 97 static SignAlgorithm::SignAlgorithmPtr GetSignAlgorithm(const std::string &path, uint8_t signMethod, 98 uint8_t type); 99 100 static SignAlgorithm::SignAlgorithmPtr GetVerifyAlgorithm(const std::string &path, uint8_t type); 101 }; 102 } // namespace Hpackage 103 #endif 104