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_FILE_H 16 #define PKG_FILE_H 17 18 #include <map> 19 #include "pkg_algorithm.h" 20 #include "pkg_manager.h" 21 #include "pkg_info_utils.h" 22 #include "pkg_utils.h" 23 24 namespace Hpackage { 25 class PkgFileImpl : public PkgFile { 26 public: PkgFileImpl(PkgManager::PkgManagerPtr manager,PkgStreamPtr stream,PkgType type)27 PkgFileImpl(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgType type) 28 : type_(type), pkgStream_(stream), pkgManager_(manager) {} 29 30 virtual ~PkgFileImpl(); 31 AddEntry(const FileInfoPtr file,const PkgStreamPtr input)32 int32_t AddEntry(const FileInfoPtr file, const PkgStreamPtr input) override 33 { 34 UNUSED(file); 35 UNUSED(input); 36 return PKG_SUCCESS; 37 } 38 SavePackage(size_t & signOffset)39 int32_t SavePackage(size_t &signOffset) override 40 { 41 UNUSED(signOffset); 42 return PKG_SUCCESS; 43 } 44 45 int32_t ExtractFile(const PkgEntryPtr node, const PkgStreamPtr output) override; 46 47 int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verifier = nullptr) override 48 { 49 UNUSED(fileNames); 50 UNUSED(verifier); 51 return PKG_SUCCESS; 52 } 53 ParseComponents(std::vector<std::string> & fileNames)54 int32_t ParseComponents(std::vector<std::string> &fileNames) override 55 { 56 UNUSED(fileNames); 57 return PKG_SUCCESS; 58 } 59 60 PkgEntryPtr FindPkgEntry(const std::string &fileName) override; 61 GetPkgStream()62 PkgStreamPtr GetPkgStream() const override 63 { 64 return pkgStream_; 65 } 66 GetPkgInfo()67 const PkgInfo *GetPkgInfo() const override 68 { 69 return nullptr; 70 } 71 GetPkgType()72 PkgType GetPkgType() const override 73 { 74 return type_; 75 } 76 ClearPkgStream()77 void ClearPkgStream() override 78 { 79 pkgStream_ = nullptr; 80 } 81 82 static int32_t ConvertBufferToString(std::string &fileName, const PkgBuffer &buffer); 83 84 static int32_t ConvertStringToBuffer(const std::string &fileName, const PkgBuffer &buffer, size_t &realLen); 85 86 void AddSignData(uint8_t digestMethod, size_t currOffset, size_t &signOffset); 87 88 protected: 89 PkgEntryPtr AddPkgEntry(const std::string& fileName); 90 bool CheckState(std::vector<uint32_t> states, uint32_t state); 91 92 protected: 93 enum { 94 PKG_FILE_STATE_IDLE = 0, 95 PKG_FILE_STATE_WORKING, // 打包数据的状态 96 PKG_FILE_STATE_CLOSE 97 }; 98 99 PkgType type_ {}; 100 PkgStreamPtr pkgStream_ = nullptr; 101 PkgManager::PkgManagerPtr pkgManager_ = nullptr; 102 uint32_t nodeId_ = 0; 103 std::map<uint32_t, PkgEntryPtr> pkgEntryMapId_ {}; 104 std::multimap<std::string, PkgEntryPtr, std::greater<std::string>> pkgEntryMapFileName_ {}; 105 uint32_t state_ = PKG_FILE_STATE_IDLE; 106 }; 107 } // namespace Hpackage 108 #endif 109