• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
PkgAlgorithm()67     PkgAlgorithm() {}
68 
~PkgAlgorithm()69     virtual ~PkgAlgorithm() {}
70 
71     virtual int32_t Pack(const PkgStreamPtr inStream, const PkgStreamPtr outStream,
72         PkgAlgorithmContext &context);
73     virtual int32_t Unpack(const PkgStreamPtr inStream, const PkgStreamPtr outStream,
74         PkgAlgorithmContext &context);
75 
UpdateFileInfo(PkgManager::FileInfoPtr info)76     virtual void UpdateFileInfo(PkgManager::FileInfoPtr info) const
77     {
78         (void)info;
79     }
80 protected:
81     int32_t FinalDigest(DigestAlgorithm::DigestAlgorithmPtr algorithm,
82         PkgAlgorithmContext &context, bool check) const;
83     int32_t ReadData(const PkgStreamPtr inStream,
84         size_t offset, PkgBuffer &buffer, size_t &remainSize, size_t &readLen) const;
85 };
86 
87 class PkgAlgorithmFactory {
88 public:
89     static PkgAlgorithm::PkgAlgorithmPtr GetAlgorithm(const PkgManager::FileInfoPtr config = nullptr);
90 
91     static DigestAlgorithm::DigestAlgorithmPtr GetDigestAlgorithm(uint8_t type);
92 
93     static SignAlgorithm::SignAlgorithmPtr GetSignAlgorithm(const std::string &path, uint8_t signMethod,
94         uint8_t type);
95 
96     static SignAlgorithm::SignAlgorithmPtr GetVerifyAlgorithm(const std::string &path, uint8_t type);
97 };
98 } // namespace Hpackage
99 #endif
100