1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#ifndef BLOCKS_DIFF_H
17fb299fa2Sopenharmony_ci#define BLOCKS_DIFF_H
18fb299fa2Sopenharmony_ci
19fb299fa2Sopenharmony_ci#include <iostream>
20fb299fa2Sopenharmony_ci#include <vector>
21fb299fa2Sopenharmony_ci#include "bzip2_adapter.h"
22fb299fa2Sopenharmony_ci#include "diffpatch.h"
23fb299fa2Sopenharmony_ci#include "pkg_manager.h"
24fb299fa2Sopenharmony_ci#include "securec.h"
25fb299fa2Sopenharmony_ci#include "update_diff.h"
26fb299fa2Sopenharmony_ci
27fb299fa2Sopenharmony_cinamespace UpdatePatch {
28fb299fa2Sopenharmony_citemplate<class DataType>
29fb299fa2Sopenharmony_ciclass SuffixArray {
30fb299fa2Sopenharmony_cipublic:
31fb299fa2Sopenharmony_ci    SuffixArray() = default;
32fb299fa2Sopenharmony_ci    ~SuffixArray() {}
33fb299fa2Sopenharmony_ci
34fb299fa2Sopenharmony_ci    void Init(const BlockBuffer &oldInfo);
35fb299fa2Sopenharmony_ci    int64_t Search(const BlockBuffer &newInfo,
36fb299fa2Sopenharmony_ci        const BlockBuffer &oldInfo, int64_t start, int64_t end, int64_t &pos) const;
37fb299fa2Sopenharmony_ciprivate:
38fb299fa2Sopenharmony_ci    void InitBuckets(const BlockBuffer &oldInfo,
39fb299fa2Sopenharmony_ci        std::vector<DataType> &buckets, std::vector<DataType> &suffixArrayTemp);
40fb299fa2Sopenharmony_ci    void Split(std::vector<DataType> &suffixArrayTemp, DataType start, DataType len, DataType h);
41fb299fa2Sopenharmony_ci    void SplitForLess(std::vector<DataType> &suffixArrayTemp, DataType start, DataType len, DataType h);
42fb299fa2Sopenharmony_ci    int64_t MatchLength(const BlockBuffer &oldBuffer, const BlockBuffer &newBuffer) const;
43fb299fa2Sopenharmony_ci
44fb299fa2Sopenharmony_ci    std::vector<DataType> suffixArray_;
45fb299fa2Sopenharmony_ci};
46fb299fa2Sopenharmony_ci
47fb299fa2Sopenharmony_ciclass BlocksDiff {
48fb299fa2Sopenharmony_cipublic:
49fb299fa2Sopenharmony_ci    BlocksDiff() = default;
50fb299fa2Sopenharmony_ci    virtual ~BlocksDiff() {}
51fb299fa2Sopenharmony_ci
52fb299fa2Sopenharmony_ci    static int32_t MakePatch(const std::string &oldFileName,
53fb299fa2Sopenharmony_ci        const std::string &newFileName, const std::string &patchFileName);
54fb299fa2Sopenharmony_ci    static int32_t MakePatch(const BlockBuffer &newInfo,
55fb299fa2Sopenharmony_ci        const BlockBuffer &oldInfo, std::vector<uint8_t> &patchData, size_t offset, size_t &patchSize);
56fb299fa2Sopenharmony_ci    static int32_t MakePatch(const BlockBuffer &newInfo,
57fb299fa2Sopenharmony_ci        const BlockBuffer &oldInfo, std::fstream &patchFile, size_t &patchSize);
58fb299fa2Sopenharmony_ci
59fb299fa2Sopenharmony_ci    int32_t MakePatch(const BlockBuffer &newInfo, const BlockBuffer &oldInfo, size_t &patchSize);
60fb299fa2Sopenharmony_ci
61fb299fa2Sopenharmony_ciprivate:
62fb299fa2Sopenharmony_ci    virtual std::unique_ptr<DeflateAdapter> CreateBZip2Adapter(size_t patchOffset) = 0;
63fb299fa2Sopenharmony_ci    virtual int32_t WritePatchHeader(int64_t controlSize,
64fb299fa2Sopenharmony_ci        int64_t diffDataSize, int64_t newSize, size_t &headerLen) = 0;
65fb299fa2Sopenharmony_ci
66fb299fa2Sopenharmony_ci    int32_t GetCtrlDatas(const BlockBuffer &newInfo,
67fb299fa2Sopenharmony_ci        const BlockBuffer &oldInfo, std::vector<ControlData> &controlDatas);
68fb299fa2Sopenharmony_ci    int32_t WritePatchData(const std::vector<ControlData> &controlDatas,
69fb299fa2Sopenharmony_ci        const BlockBuffer &newInfo, size_t &patchSize);
70fb299fa2Sopenharmony_ci    int32_t WriteControlData(const std::vector<ControlData> controlDatas, size_t &patchSize);
71fb299fa2Sopenharmony_ci    int32_t WriteDiffData(const std::vector<ControlData> controlDatas, size_t &patchSize);
72fb299fa2Sopenharmony_ci    int32_t WriteExtraData(const std::vector<ControlData> controlDatas, size_t &patchSize);
73fb299fa2Sopenharmony_ci
74fb299fa2Sopenharmony_ci    void ComputeOldScore(const BlockBuffer &newInfo,
75fb299fa2Sopenharmony_ci        const BlockBuffer &oldInfo, int64_t &oldScore, int64_t &matchLen);
76fb299fa2Sopenharmony_ci    void ComputeLength(const BlockBuffer &newInfo,
77fb299fa2Sopenharmony_ci        const BlockBuffer &oldInfo, int64_t &lengthFront, int64_t &lengthBack);
78fb299fa2Sopenharmony_ci
79fb299fa2Sopenharmony_ci    std::unique_ptr<SuffixArray<int32_t>> suffixArray_ {nullptr};
80fb299fa2Sopenharmony_ci    std::vector<ControlData> controls_ {};
81fb299fa2Sopenharmony_ci    int64_t matchPos_ { 0 };
82fb299fa2Sopenharmony_ci    int64_t currentOffset_ { 0 };
83fb299fa2Sopenharmony_ci    int64_t lastOffset_ { 0 };
84fb299fa2Sopenharmony_ci    int64_t lastScan_ { 0 };
85fb299fa2Sopenharmony_ci    int64_t lastPos_ { 0 };
86fb299fa2Sopenharmony_ci};
87fb299fa2Sopenharmony_ci
88fb299fa2Sopenharmony_ciclass BlocksStreamDiff : public BlocksDiff {
89fb299fa2Sopenharmony_cipublic:
90fb299fa2Sopenharmony_ci    BlocksStreamDiff(std::fstream &stream, size_t offset) : BlocksDiff(), stream_(stream), offset_(offset) {}
91fb299fa2Sopenharmony_ci    ~BlocksStreamDiff() override {}
92fb299fa2Sopenharmony_ci
93fb299fa2Sopenharmony_ciprivate:
94fb299fa2Sopenharmony_ci    std::unique_ptr<DeflateAdapter> CreateBZip2Adapter(size_t patchOffset) override;
95fb299fa2Sopenharmony_ci    int32_t WritePatchHeader(int64_t controlSize,
96fb299fa2Sopenharmony_ci        int64_t diffDataSize, int64_t newSize, size_t &headerLen) override;
97fb299fa2Sopenharmony_ci    std::fstream &stream_;
98fb299fa2Sopenharmony_ci    size_t offset_ { 0 };
99fb299fa2Sopenharmony_ci};
100fb299fa2Sopenharmony_ci
101fb299fa2Sopenharmony_ciclass BlocksBufferDiff : public BlocksDiff {
102fb299fa2Sopenharmony_cipublic:
103fb299fa2Sopenharmony_ci    BlocksBufferDiff(std::vector<uint8_t> &patchData, size_t offset)
104fb299fa2Sopenharmony_ci        : BlocksDiff(), patchData_(patchData), offset_(offset) {}
105fb299fa2Sopenharmony_ci    ~BlocksBufferDiff() override {}
106fb299fa2Sopenharmony_ci
107fb299fa2Sopenharmony_ciprivate:
108fb299fa2Sopenharmony_ci    std::unique_ptr<DeflateAdapter> CreateBZip2Adapter(size_t patchOffset) override;
109fb299fa2Sopenharmony_ci    int32_t WritePatchHeader(int64_t controlSize,
110fb299fa2Sopenharmony_ci        int64_t diffDataSize, int64_t newSize, size_t &headerLen) override;
111fb299fa2Sopenharmony_ci    std::vector<uint8_t> &patchData_;
112fb299fa2Sopenharmony_ci    size_t offset_ { 0 };
113fb299fa2Sopenharmony_ci};
114fb299fa2Sopenharmony_ci} // namespace UpdatePatch
115fb299fa2Sopenharmony_ci#endif // BLOCKS_DIFF_H