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 16 #ifndef UPDATE_DIFF_H 17 #define UPDATE_DIFF_H 18 19 #include <memory> 20 #include <vector> 21 #include "diffpatch.h" 22 #include "package/package.h" 23 #include "package/pkg_manager.h" 24 25 namespace UpdatePatch { 26 class ImageParser { 27 public: 28 ImageParser() = default; 29 ~ImageParser(); 30 31 int32_t Parse(const std::string &packageName); 32 int32_t Extract(const std::string &fileName, std::vector<uint8_t> &buffer); GetType()33 PkgPackType GetType() const 34 { 35 return type_; 36 } GetFileIds()37 const std::vector<std::string> &GetFileIds() const 38 { 39 return fileIds_; 40 } 41 int32_t GetPkgBuffer(BlockBuffer &buffer) const; 42 const Hpackage::FileInfo *GetFileInfo(const std::string &fileName) const; 43 private: 44 PkgPackType type_ = PKG_PACK_TYPE_NONE; 45 MemMapInfo memMap_ {}; 46 std::vector<std::string> fileIds_; 47 Hpackage::PkgManager::StreamPtr stream_ { nullptr }; 48 Hpackage::PkgManager::PkgManagerPtr pkgManager_ { nullptr }; 49 }; 50 51 class UpdateDiff { 52 public: 53 using ImageParserPtr = ImageParser *; UpdateDiff(size_t limit,bool blockDiff)54 UpdateDiff(size_t limit, bool blockDiff) : limit_(limit * IGMDIFF_LIMIT_UNIT), blockDiff_(blockDiff) {} ~UpdateDiff()55 ~UpdateDiff() {} 56 57 int32_t MakePatch(const std::string &oldFileName, const std::string &newFileName, const std::string &patchFileName); 58 59 static int32_t DiffImage(size_t limit, const std::string &oldFileName, 60 const std::string &newFileName, const std::string &patchFileName); 61 62 static int32_t DiffBlock(const std::string &oldFileName, 63 const std::string &newFileName, const std::string &patchFileName); 64 65 private: 66 size_t limit_ { 0 }; 67 bool blockDiff_ { true }; 68 std::unique_ptr<ImageParser> newParser_ { nullptr }; 69 std::unique_ptr<ImageParser> oldParser_ { nullptr }; 70 }; 71 } // namespace UpdatePatch 72 #endif // UPDATE_DIFF_H