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