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 IMAGE_PATCH_H
17fb299fa2Sopenharmony_ci#define IMAGE_PATCH_H
18fb299fa2Sopenharmony_ci
19fb299fa2Sopenharmony_ci#include <sys/types.h>
20fb299fa2Sopenharmony_ci#include "deflate_adapter.h"
21fb299fa2Sopenharmony_ci#include "diffpatch.h"
22fb299fa2Sopenharmony_ci#include "openssl/sha.h"
23fb299fa2Sopenharmony_ci#include "package/pkg_manager.h"
24fb299fa2Sopenharmony_ci#include "securec.h"
25fb299fa2Sopenharmony_ci
26fb299fa2Sopenharmony_cinamespace UpdatePatch {
27fb299fa2Sopenharmony_ciclass ImagePatch {
28fb299fa2Sopenharmony_cipublic:
29fb299fa2Sopenharmony_ci    explicit ImagePatch(UpdatePatchWriterPtr writer) : writer_(writer) {}
30fb299fa2Sopenharmony_ci    virtual ~ImagePatch() = default;
31fb299fa2Sopenharmony_ci
32fb299fa2Sopenharmony_ci    virtual int32_t ApplyImagePatch(const PatchParam &param, size_t &startOffset) = 0;
33fb299fa2Sopenharmony_ci
34fb299fa2Sopenharmony_ci    template<typename T>
35fb299fa2Sopenharmony_ci    static T ReadLE(const uint8_t *address)
36fb299fa2Sopenharmony_ci    {
37fb299fa2Sopenharmony_ci        T result;
38fb299fa2Sopenharmony_ci        errno_t ret = memcpy_s(&result, sizeof(result), address, sizeof(T));
39fb299fa2Sopenharmony_ci        if (ret != EOK) {
40fb299fa2Sopenharmony_ci            // only warning no need to return invalid value
41fb299fa2Sopenharmony_ci            PATCH_LOGE("Failed to memcpy");
42fb299fa2Sopenharmony_ci        }
43fb299fa2Sopenharmony_ci        return result;
44fb299fa2Sopenharmony_ci    }
45fb299fa2Sopenharmony_ciprotected:
46fb299fa2Sopenharmony_ci    UpdatePatchWriterPtr writer_ {nullptr};
47fb299fa2Sopenharmony_ci};
48fb299fa2Sopenharmony_ci
49fb299fa2Sopenharmony_ciclass RowImagePatch : public ImagePatch {
50fb299fa2Sopenharmony_cipublic:
51fb299fa2Sopenharmony_ci    explicit RowImagePatch(UpdatePatchWriterPtr writer) : ImagePatch(writer) {}
52fb299fa2Sopenharmony_ci    ~RowImagePatch() override {}
53fb299fa2Sopenharmony_ci
54fb299fa2Sopenharmony_ci    int32_t ApplyImagePatch(const PatchParam &param, size_t &startOffset) override;
55fb299fa2Sopenharmony_ci};
56fb299fa2Sopenharmony_ci
57fb299fa2Sopenharmony_ciclass NormalImagePatch : public ImagePatch {
58fb299fa2Sopenharmony_cipublic:
59fb299fa2Sopenharmony_ci    explicit NormalImagePatch(UpdatePatchWriterPtr writer) : ImagePatch(writer) {}
60fb299fa2Sopenharmony_ci    ~NormalImagePatch() override {}
61fb299fa2Sopenharmony_ci
62fb299fa2Sopenharmony_ci    int32_t ApplyImagePatch(const PatchParam &param, size_t &startOffset)  override;
63fb299fa2Sopenharmony_ci};
64fb299fa2Sopenharmony_ci
65fb299fa2Sopenharmony_ciclass CompressedImagePatch : public ImagePatch {
66fb299fa2Sopenharmony_cipublic:
67fb299fa2Sopenharmony_ci    CompressedImagePatch(UpdatePatchWriterPtr writer, const std::vector<uint8_t> &bonusData)
68fb299fa2Sopenharmony_ci        : ImagePatch(writer), bonusData_(bonusData) {}
69fb299fa2Sopenharmony_ci    ~CompressedImagePatch() override {}
70fb299fa2Sopenharmony_ci
71fb299fa2Sopenharmony_ci    int32_t ApplyImagePatch(const PatchParam &param, size_t &startOffset) override;
72fb299fa2Sopenharmony_ciprotected:
73fb299fa2Sopenharmony_ci    virtual int32_t ReadHeader(const PatchParam &param, PatchHeader &header, size_t &offset) = 0;
74fb299fa2Sopenharmony_ci    virtual std::unique_ptr<Hpackage::FileInfo> GetFileInfo() const = 0;
75fb299fa2Sopenharmony_ci    int32_t StartReadHeader(const PatchParam &param, PatchHeader &header, size_t &offset);
76fb299fa2Sopenharmony_ci    int32_t DecompressData(Hpackage::PkgManager::PkgManagerPtr &pkgManager, Hpackage::PkgBuffer buffer,
77fb299fa2Sopenharmony_ci        Hpackage::PkgManager::StreamPtr &stream, bool memory, size_t expandedLen) const;
78fb299fa2Sopenharmony_ci
79fb299fa2Sopenharmony_ci    std::vector<uint8_t> bonusData_ {};
80fb299fa2Sopenharmony_ci};
81fb299fa2Sopenharmony_ci
82fb299fa2Sopenharmony_ciclass ZipImagePatch : public CompressedImagePatch {
83fb299fa2Sopenharmony_cipublic:
84fb299fa2Sopenharmony_ci    ZipImagePatch(UpdatePatchWriterPtr writer, const std::vector<uint8_t> &bonusData)
85fb299fa2Sopenharmony_ci        : CompressedImagePatch(writer, bonusData) {}
86fb299fa2Sopenharmony_ci    ~ZipImagePatch() override {}
87fb299fa2Sopenharmony_ci
88fb299fa2Sopenharmony_ciprotected:
89fb299fa2Sopenharmony_ci    int32_t ReadHeader(const PatchParam &param, PatchHeader &header, size_t &offset) override;
90fb299fa2Sopenharmony_ci    std::unique_ptr<Hpackage::FileInfo> GetFileInfo() const override;
91fb299fa2Sopenharmony_ci
92fb299fa2Sopenharmony_ci    int32_t method_ {0};
93fb299fa2Sopenharmony_ci    int32_t level_ {0};
94fb299fa2Sopenharmony_ci    int32_t windowBits_ {0};
95fb299fa2Sopenharmony_ci    int32_t memLevel_ {0};
96fb299fa2Sopenharmony_ci    int32_t strategy_ {0};
97fb299fa2Sopenharmony_ci};
98fb299fa2Sopenharmony_ci
99fb299fa2Sopenharmony_ciclass Lz4ImagePatch : public CompressedImagePatch {
100fb299fa2Sopenharmony_cipublic:
101fb299fa2Sopenharmony_ci    Lz4ImagePatch(UpdatePatchWriterPtr writer, const std::vector<uint8_t> &bonusData)
102fb299fa2Sopenharmony_ci        : CompressedImagePatch(writer, bonusData) {}
103fb299fa2Sopenharmony_ci    ~Lz4ImagePatch() override {}
104fb299fa2Sopenharmony_ci
105fb299fa2Sopenharmony_ciprotected:
106fb299fa2Sopenharmony_ci    int32_t ReadHeader(const PatchParam &param, PatchHeader &header, size_t &offset) override;
107fb299fa2Sopenharmony_ci    std::unique_ptr<Hpackage::FileInfo> GetFileInfo() const override;
108fb299fa2Sopenharmony_ci
109fb299fa2Sopenharmony_ci    int32_t compressionLevel_ {0};
110fb299fa2Sopenharmony_ci    int32_t blockIndependence_ {0};
111fb299fa2Sopenharmony_ci    int32_t contentChecksumFlag_ {0};
112fb299fa2Sopenharmony_ci    int32_t blockSizeID_ {0};
113fb299fa2Sopenharmony_ci    int32_t method_ {0};
114fb299fa2Sopenharmony_ci    int32_t autoFlush_ {1};
115fb299fa2Sopenharmony_ci};
116fb299fa2Sopenharmony_ci
117fb299fa2Sopenharmony_ciclass CompressedFileRestore : public UpdatePatchWriter {
118fb299fa2Sopenharmony_cipublic:
119fb299fa2Sopenharmony_ci    CompressedFileRestore(Hpackage::PkgManager::FileInfoPtr fileInfo, UpdatePatchWriterPtr writer)
120fb299fa2Sopenharmony_ci        : UpdatePatchWriter(), fileInfo_(fileInfo), writer_(writer) {}
121fb299fa2Sopenharmony_ci    ~CompressedFileRestore() override {}
122fb299fa2Sopenharmony_ci
123fb299fa2Sopenharmony_ci    int32_t Init() override;
124fb299fa2Sopenharmony_ci    int32_t Write(size_t start, const BlockBuffer &buffer, size_t len) override;
125fb299fa2Sopenharmony_ci    int32_t Finish() override
126fb299fa2Sopenharmony_ci    {
127fb299fa2Sopenharmony_ci        return 0;
128fb299fa2Sopenharmony_ci    }
129fb299fa2Sopenharmony_ci    int32_t CompressData(size_t &originalSize, size_t &compressSize);
130fb299fa2Sopenharmony_ciprivate:
131fb299fa2Sopenharmony_ci    std::vector<uint8_t> data_ {};
132fb299fa2Sopenharmony_ci    size_t dataSize_ {0};
133fb299fa2Sopenharmony_ci    Hpackage::PkgManager::FileInfoPtr fileInfo_ { nullptr };
134fb299fa2Sopenharmony_ci    UpdatePatchWriterPtr writer_ { nullptr };
135fb299fa2Sopenharmony_ci    std::unique_ptr<DeflateAdapter> deflateAdapter_ { nullptr };
136fb299fa2Sopenharmony_ci    SHA256_CTX sha256Ctx_ {};
137fb299fa2Sopenharmony_ci};
138fb299fa2Sopenharmony_ci} // namespace UpdatePatch
139fb299fa2Sopenharmony_ci#endif  // IMAGE_PATCH_H
140