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