• 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_MANAGER_IMPL_H
16 #define PKG_MANAGER_IMPL_H
17 
18 #include <functional>
19 #include <map>
20 #include <memory>
21 #include "pkg_lz4file.h"
22 #include "pkg_manager.h"
23 #include "pkg_pkgfile.h"
24 #include "pkg_stream.h"
25 #include "pkg_utils.h"
26 
27 namespace hpackage {
28 class PkgManagerImpl : public PkgManager {
29 public:
PkgManagerImpl()30     PkgManagerImpl() {};
31 
32     ~PkgManagerImpl() override;
33 
34     int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
35         std::vector<std::pair<std::string, ZipFileInfo>> &files) override;
36 
37     int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
38         std::vector<std::pair<std::string, ComponentInfo>> &files) override;
39 
40     int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
41         std::vector<std::pair<std::string, Lz4FileInfo>> &files) override;
42 
43     int32_t CreatePackage(const std::string &path, PkgInfoPtr header,
44         std::vector<std::pair<std::string, ComponentInfo>> &files, size_t &offset, std::string &hashValue) override;
45 
46     int32_t VerifyPackage(const std::string &packagePath, const std::string &keyPath, const std::string &version,
47         const PkgBuffer &digest, VerifyCallback cb) override;
48 
49     int32_t LoadPackage(const std::string &packagePath, const std::string &keyPath,
50         std::vector<std::string> &fileIds) override;
51 
52     int32_t ExtractFile(const std::string &path, PkgManager::StreamPtr output) override;
53 
54     int32_t CreatePkgStream(StreamPtr &stream, const std::string &, size_t, int32_t) override;
55 
56     int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName,
57         PkgStream::ExtractFileProcessor processor, const void *context) override;
58 
59     void ClosePkgStream(StreamPtr &stream) override;
60 
61     const FileInfo *GetFileInfo(const std::string &path) override;
62 
63     const PkgInfo *GetPackageInfo(const std::string &packagePath) override;
64 
65     PkgEntryPtr GetPkgEntry(const std::string &path);
66 
67     int32_t DecompressBuffer(FileInfoPtr info, const PkgBuffer &buffer, StreamPtr stream) const override;
68 
69     int32_t CompressBuffer(FileInfoPtr info, const PkgBuffer &buffer, StreamPtr stream) const override;
70 
71     int32_t LoadPackageWithoutUnPack(const std::string &packagePath, std::vector<std::string> &fileIds) override;
72 
73     int32_t ParsePackage(StreamPtr stream, std::vector<std::string> &fileIds, int32_t type) override;
74 
75     int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName, const PkgBuffer &buffer) override;
76 
SetPkgDecodeProgress(PkgDecodeProgress decodeProgress)77     void SetPkgDecodeProgress(PkgDecodeProgress decodeProgress) override
78     {
79         decodeProgress_ = decodeProgress;
80     }
81 
82     void PostDecodeProgress(int type, size_t writeDataLen, const void *context) override;
83 private:
84     PkgFilePtr CreatePackage(PkgStreamPtr stream, PkgFile::PkgType type, PkgInfoPtr header = nullptr);
85 
86     template<class T>
87     PkgFilePtr CreatePackage(const std::string &path,
88         PkgInfoPtr header, std::vector<std::pair<std::string, T>> &files, size_t &offset);
89 
90     int32_t ExtraAndLoadPackage(const std::string &path, const std::string &name, PkgFile::PkgType,
91         std::vector<std::string> &fileIds);
92     int32_t LoadPackage(const std::string &path, std::vector<std::string> &fileNames, PkgFile::PkgType type);
93 
94     int32_t LoadPackageWithStream(const std::string &path, std::vector<std::string> &fileNames,
95         PkgFile::PkgType type, PkgStreamPtr stream);
96 
97     PkgFile::PkgType GetPkgTypeByName(const std::string &path);
98 
99     int32_t Sign(PkgStreamPtr stream, size_t offset, const PkgInfoPtr &info);
100 
101     int32_t Verify(uint8_t digestMethod, const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature);
102 
103     int32_t GenerateFileDigest(PkgStreamPtr stream,
104         uint8_t digestMethod, uint8_t flags, std::vector<std::vector<uint8_t>> &digestInfo, size_t hashBufferLen = 0);
105 
106     void ClearPkgFile();
107 
108     int32_t SetSignVerifyKeyName(const std::string &keyName);
109 
110     int32_t CreatePkgStream(PkgStreamPtr &stream, const std::string &, size_t, int32_t);
111 
112     int32_t CreatePkgStream(PkgStreamPtr &stream, const std::string &fileName,
113         PkgStream::ExtractFileProcessor processor, const void *context);
114 
115     void ClosePkgStream(PkgStreamPtr &stream);
116 private:
117     bool unzipToFile_ {true};
118     std::vector<PkgFilePtr> pkgFiles_ {};
119     std::map<std::string, PkgStreamPtr> pkgStreams_ {};
120     std::string signVerifyKeyName_ {};
121     PkgDecodeProgress decodeProgress_ { nullptr };
122 };
123 } // namespace hpackage
124 #endif // PKG_MANAGER_IMPL_H
125