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 BLOCKS_DIFF_H
17#define BLOCKS_DIFF_H
18
19#include <iostream>
20#include <vector>
21#include "bzip2_adapter.h"
22#include "diffpatch.h"
23#include "pkg_manager.h"
24#include "securec.h"
25
26namespace UpdatePatch {
27class BlocksPatch {
28public:
29    BlocksPatch() = delete;
30    explicit BlocksPatch(const PatchBuffer &patchInfo) : patchInfo_(patchInfo) {}
31    virtual ~BlocksPatch() {}
32
33    int32_t ApplyPatch();
34protected:
35    int32_t ReadControlData(ControlData &ctrlData);
36
37    virtual int32_t ReadHeader(int64_t &controlDataSize, int64_t &diffDataSize, int64_t &newSize);
38    virtual int32_t RestoreDiffData(const ControlData &ctrlData) = 0;
39    virtual int32_t RestoreExtraData(const ControlData &ctrlData) = 0;
40
41    PatchBuffer patchInfo_ { nullptr };
42    int64_t newSize_ = { 0 };
43    int64_t oldOffset_ { 0 };
44    int64_t newOffset_ { 0 };
45
46    std::unique_ptr<BZip2ReadAdapter> controlDataReader_ { nullptr };
47    std::unique_ptr<BZip2ReadAdapter> diffDataReader_ { nullptr };
48    std::unique_ptr<BZip2ReadAdapter> extraDataReader_ { nullptr };
49};
50
51class BlocksBufferPatch : public BlocksPatch {
52public:
53    BlocksBufferPatch(const PatchBuffer &patchInfo, const BlockBuffer &oldInfo, std::vector<uint8_t> &newData)
54        : BlocksPatch(patchInfo), oldInfo_(oldInfo), newData_(newData) {}
55    ~BlocksBufferPatch() override {}
56
57private:
58    int32_t ReadHeader(int64_t &controlDataSize, int64_t &diffDataSize, int64_t &newSize) override;
59    int32_t RestoreDiffData(const ControlData &ctrlData) override;
60    int32_t RestoreExtraData(const ControlData &ctrlData) override;
61
62    BlockBuffer oldInfo_ { nullptr, 0 };
63    std::vector<uint8_t>  &newData_;
64};
65
66class BlocksStreamPatch : public BlocksPatch {
67public:
68    BlocksStreamPatch(const PatchBuffer &patchInfo,
69        Hpackage::PkgManager::StreamPtr stream, UpdatePatchWriterPtr writer)
70        : BlocksPatch(patchInfo), stream_(stream), writer_(writer) {}
71    ~BlocksStreamPatch() override {}
72private:
73    int32_t RestoreDiffData(const ControlData &ctrlData) override;
74    int32_t RestoreExtraData(const ControlData &ctrlData) override;
75
76    Hpackage::PkgManager::StreamPtr stream_ { nullptr };
77    UpdatePatchWriterPtr writer_ { nullptr };
78};
79} // namespace UpdatePatch
80#endif // BLOCKS_DIFF_H