• 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     void UpdateBlocks(const BlockBuffer &orgNewBuffer, const Hpackage::FileInfo *newFileInfo,
77         const BlockBuffer &orgOldBuffer, const Hpackage::FileInfo *oldFileInfo,
78         std::pair<std::vector<uint8_t>, std::vector<uint8_t>> &buffer);
79     int32_t CompressData(Hpackage::PkgManager::FileInfoPtr info,
80         const BlockBuffer &buffer, std::vector<uint8_t> &outData, size_t &outSize) const;
81     int32_t type_;
82 };
83 
84 class ZipImageDiff : public CompressedImageDiff {
85 public:
ZipImageDiff(size_t limit,UpdateDiff::ImageParserPtr newParser,UpdateDiff::ImageParserPtr oldParser)86     ZipImageDiff(size_t limit, UpdateDiff::ImageParserPtr newParser, UpdateDiff::ImageParserPtr oldParser)
87         : CompressedImageDiff(limit, newParser, oldParser, BLOCK_DEFLATE) {}
~ZipImageDiff()88     ~ZipImageDiff() override {}
89 
90     int32_t WriteHeader(std::ofstream &patchFile,
91         std::fstream &blockPatchFile, size_t &dataOffset, ImageBlock &block) const override;
92 
93 protected:
94     int32_t TestAndSetConfig(const BlockBuffer &buffer, const std::string &fileName) override;
95 
96     int32_t method_ = 0;
97     int32_t level_ = 0;
98     int32_t windowBits_ = 0;
99     int32_t memLevel_ = 0;
100     int32_t strategy_ = 0;
101 };
102 
103 class Lz4ImageDiff : public CompressedImageDiff {
104 public:
Lz4ImageDiff(size_t limit,UpdateDiff::ImageParserPtr newParser,UpdateDiff::ImageParserPtr oldParser)105     Lz4ImageDiff(size_t limit, UpdateDiff::ImageParserPtr newParser, UpdateDiff::ImageParserPtr oldParser)
106         : CompressedImageDiff(limit, newParser, oldParser, BLOCK_LZ4) {}
~Lz4ImageDiff()107     ~Lz4ImageDiff() override {}
108 
109     int32_t WriteHeader(std::ofstream &patchFile,
110         std::fstream &blockPatchFile, size_t &dataOffset, ImageBlock &block) const override;
111 
112 protected:
113     int32_t TestAndSetConfig(const BlockBuffer &buffer, const std::string &fileName) override;
114 
115 private:
116     int32_t method_ {0};
117     int32_t compressionLevel_ {0};
118     int32_t blockIndependence_ {0};
119     int32_t contentChecksumFlag_ {0};
120     int32_t blockSizeID_ {0};
121     int32_t autoFlush_ {1};
122 };
123 
124 class GZipImageDiff : public ZipImageDiff {
125 public:
GZipImageDiff(size_t limit,UpdateDiff::ImageParserPtr newParser,UpdateDiff::ImageParserPtr oldParser)126     GZipImageDiff(size_t limit, UpdateDiff::ImageParserPtr newParser, UpdateDiff::ImageParserPtr oldParser)
127         : ZipImageDiff(limit, newParser, oldParser) {}
~GZipImageDiff()128     ~GZipImageDiff() override {}
129 };
130 } // namespace UpdatePatch
131 #endif // IMGAE_DIFF_H