• 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 IMGAE_DIFF_H
17 #define IMGAE_DIFF_H
18 #include <iostream>
19 #include <memory>
20 #include <vector>
21 #include "blocks_diff.h"
22 #include "update_diff.h"
23 
24 namespace UpdatePatch {
25 struct ImageBlock {
26     int32_t type;
27     PatchBuffer newInfo;
28     PatchBuffer oldInfo;
29     size_t srcOriginalLength;
30     size_t destOriginalLength;
31     std::vector<uint8_t> patchData;
32     std::vector<uint8_t> srcOriginalData;
33     std::vector<uint8_t> destOriginalData;
34 };
35 
36 class ImageDiff {
37 public:
38     ImageDiff() = delete;
ImageDiff(size_t limit,UpdateDiff::ImageParserPtr newParser,UpdateDiff::ImageParserPtr oldParser)39     ImageDiff(size_t limit, UpdateDiff::ImageParserPtr newParser, UpdateDiff::ImageParserPtr oldParser)
40         : limit_(limit), newParser_(newParser), oldParser_(oldParser)  {}
~ImageDiff()41     virtual ~ImageDiff()
42     {
43         updateBlocks_.clear();
44     }
45 
46     virtual int32_t MakePatch(const std::string &patchName);
47     virtual int32_t WriteHeader(std::ofstream &patchFile,
48         std::fstream &blockPatchFile, size_t &dataOffset, ImageBlock &block) const;
49 
50 protected:
51     int32_t SplitImage(const PatchBuffer &oldInfo, const PatchBuffer &newInfo);
52     int32_t DiffImage(const std::string &patchName);
53     int32_t MakeBlockPatch(ImageBlock &block, std::fstream &blockPatchFile,
54         const BlockBuffer &newInfo, const BlockBuffer &oldInfo, size_t &patchSize) const;
55     int32_t WritePatch(std::ofstream &patchFile, std::fstream &blockPatchFile);
56 
57     size_t limit_;
58     std::vector<ImageBlock> updateBlocks_ {};
59     UpdateDiff::ImageParserPtr newParser_ {nullptr};
60     UpdateDiff::ImageParserPtr oldParser_ {nullptr};
61     bool usePatchFile_ { false };
62 };
63 
64 class CompressedImageDiff : public ImageDiff {
65 public:
CompressedImageDiff(size_t limit,UpdateDiff::ImageParserPtr newParser,UpdateDiff::ImageParserPtr oldParser,int32_t type)66     CompressedImageDiff(size_t limit,
67         UpdateDiff::ImageParserPtr newParser, UpdateDiff::ImageParserPtr oldParser, int32_t type)
68         : ImageDiff(limit, newParser, oldParser), type_(type) {}
~CompressedImageDiff()69     ~CompressedImageDiff() override {}
70 
71     int32_t MakePatch(const std::string &patchName) override;
72 
73 protected:
74     virtual int32_t TestAndSetConfig(const BlockBuffer &buffer, const std::string &fileName) = 0;
75     int32_t DiffFile(const std::string &fileName, size_t &oldOffset, size_t &newOffset);
76     int32_t CompressData(Hpackage::PkgManager::FileInfoPtr info,
77         const BlockBuffer &buffer, std::vector<uint8_t> &outData, size_t &outSize) const;
78     int32_t type_;
79 };
80 
81 class ZipImageDiff : public CompressedImageDiff {
82 public:
ZipImageDiff(size_t limit,UpdateDiff::ImageParserPtr newParser,UpdateDiff::ImageParserPtr oldParser)83     ZipImageDiff(size_t limit, UpdateDiff::ImageParserPtr newParser, UpdateDiff::ImageParserPtr oldParser)
84         : CompressedImageDiff(limit, newParser, oldParser, BLOCK_DEFLATE) {}
~ZipImageDiff()85     ~ZipImageDiff() override {}
86 
87     int32_t WriteHeader(std::ofstream &patchFile,
88         std::fstream &blockPatchFile, size_t &dataOffset, ImageBlock &block) const override;
89 
90 protected:
91     int32_t TestAndSetConfig(const BlockBuffer &buffer, const std::string &fileName) override;
92 
93     int32_t method_ = 0;
94     int32_t level_ = 0;
95     int32_t windowBits_ = 0;
96     int32_t memLevel_ = 0;
97     int32_t strategy_ = 0;
98 };
99 
100 class Lz4ImageDiff : public CompressedImageDiff {
101 public:
Lz4ImageDiff(size_t limit,UpdateDiff::ImageParserPtr newParser,UpdateDiff::ImageParserPtr oldParser)102     Lz4ImageDiff(size_t limit, UpdateDiff::ImageParserPtr newParser, UpdateDiff::ImageParserPtr oldParser)
103         : CompressedImageDiff(limit, newParser, oldParser, BLOCK_LZ4) {}
~Lz4ImageDiff()104     ~Lz4ImageDiff() override {}
105 
106     int32_t WriteHeader(std::ofstream &patchFile,
107         std::fstream &blockPatchFile, size_t &dataOffset, ImageBlock &block) const override;
108 
109 protected:
110     int32_t TestAndSetConfig(const BlockBuffer &buffer, const std::string &fileName) override;
111 
112 private:
113     int32_t method_ {0};
114     int32_t compressionLevel_ {0};
115     int32_t blockIndependence_ {0};
116     int32_t contentChecksumFlag_ {0};
117     int32_t blockSizeID_ {0};
118     int32_t autoFlush_ {1};
119 };
120 
121 class GZipImageDiff : public ZipImageDiff {
122 public:
GZipImageDiff(size_t limit,UpdateDiff::ImageParserPtr newParser,UpdateDiff::ImageParserPtr oldParser)123     GZipImageDiff(size_t limit, UpdateDiff::ImageParserPtr newParser, UpdateDiff::ImageParserPtr oldParser)
124         : ZipImageDiff(limit, newParser, oldParser) {}
~GZipImageDiff()125     ~GZipImageDiff() override {}
126 };
127 } // namespace UpdatePatch
128 #endif // IMGAE_DIFF_H