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 BZIP2_ADAPTER_H
17fb299fa2Sopenharmony_ci#define BZIP2_ADAPTER_H
18fb299fa2Sopenharmony_ci#include <vector>
19fb299fa2Sopenharmony_ci#include "bzlib.h"
20fb299fa2Sopenharmony_ci#include "deflate_adapter.h"
21fb299fa2Sopenharmony_ci#include "diffpatch.h"
22fb299fa2Sopenharmony_ci#include "pkg_manager.h"
23fb299fa2Sopenharmony_ci#include "securec.h"
24fb299fa2Sopenharmony_ci
25fb299fa2Sopenharmony_cinamespace UpdatePatch {
26fb299fa2Sopenharmony_ciclass BZip2Adapter : public DeflateAdapter {
27fb299fa2Sopenharmony_cipublic:
28fb299fa2Sopenharmony_ci    BZip2Adapter() : DeflateAdapter() {}
29fb299fa2Sopenharmony_ci    ~BZip2Adapter() override {}
30fb299fa2Sopenharmony_ci
31fb299fa2Sopenharmony_ci    int32_t Open() override;
32fb299fa2Sopenharmony_ci    int32_t Close() override;
33fb299fa2Sopenharmony_ciprotected:
34fb299fa2Sopenharmony_ci    static constexpr int32_t BLOCK_SIZE_BEST = 9;
35fb299fa2Sopenharmony_ci    bz_stream stream_ {};
36fb299fa2Sopenharmony_ci};
37fb299fa2Sopenharmony_ci
38fb299fa2Sopenharmony_ciclass BZipBuffer2Adapter : public BZip2Adapter {
39fb299fa2Sopenharmony_cipublic:
40fb299fa2Sopenharmony_ci    BZipBuffer2Adapter(std::vector<uint8_t> &buffer, size_t offset)
41fb299fa2Sopenharmony_ci        : BZip2Adapter(), offset_(offset), buffer_(buffer) {}
42fb299fa2Sopenharmony_ci    ~BZipBuffer2Adapter() override {}
43fb299fa2Sopenharmony_ci
44fb299fa2Sopenharmony_ci    int32_t WriteData(const BlockBuffer &srcData) override;
45fb299fa2Sopenharmony_ci    int32_t FlushData(size_t &dataSize) override;
46fb299fa2Sopenharmony_ciprivate:
47fb299fa2Sopenharmony_ci    size_t offset_ { 0 };
48fb299fa2Sopenharmony_ci    size_t dataSize_ { 0 };
49fb299fa2Sopenharmony_ci    std::vector<uint8_t> &buffer_;
50fb299fa2Sopenharmony_ci};
51fb299fa2Sopenharmony_ci
52fb299fa2Sopenharmony_ciclass BZip2StreamAdapter : public BZip2Adapter {
53fb299fa2Sopenharmony_cipublic:
54fb299fa2Sopenharmony_ci    explicit BZip2StreamAdapter(std::fstream &stream) : BZip2Adapter(), outStream_(stream) {}
55fb299fa2Sopenharmony_ci    ~BZip2StreamAdapter() override {}
56fb299fa2Sopenharmony_ci
57fb299fa2Sopenharmony_ci    int32_t Open() override;
58fb299fa2Sopenharmony_ci    int32_t WriteData(const BlockBuffer &srcData) override;
59fb299fa2Sopenharmony_ci    int32_t FlushData(size_t &dataSize) override;
60fb299fa2Sopenharmony_ciprivate:
61fb299fa2Sopenharmony_ci    std::vector<char> buffer_ { 0 };
62fb299fa2Sopenharmony_ci    size_t dataSize_ { 0 };
63fb299fa2Sopenharmony_ci    std::fstream &outStream_;
64fb299fa2Sopenharmony_ci};
65fb299fa2Sopenharmony_ci
66fb299fa2Sopenharmony_ciclass BZip2ReadAdapter {
67fb299fa2Sopenharmony_cipublic:
68fb299fa2Sopenharmony_ci    BZip2ReadAdapter(size_t offset, size_t length) : offset_(offset), dataLength_(length) {}
69fb299fa2Sopenharmony_ci    virtual ~BZip2ReadAdapter() {}
70fb299fa2Sopenharmony_ci
71fb299fa2Sopenharmony_ci    virtual int32_t Open() = 0;
72fb299fa2Sopenharmony_ci    virtual int32_t Close()
73fb299fa2Sopenharmony_ci    {
74fb299fa2Sopenharmony_ci        return 0;
75fb299fa2Sopenharmony_ci    };
76fb299fa2Sopenharmony_ci    virtual int32_t ReadData(BlockBuffer &info) = 0;
77fb299fa2Sopenharmony_ciprotected:
78fb299fa2Sopenharmony_ci    bool init_ { false };
79fb299fa2Sopenharmony_ci    size_t offset_ { 0 };
80fb299fa2Sopenharmony_ci    size_t dataLength_ = { 0 };
81fb299fa2Sopenharmony_ci    bz_stream stream_ {};
82fb299fa2Sopenharmony_ci};
83fb299fa2Sopenharmony_ci
84fb299fa2Sopenharmony_ciclass BZip2BufferReadAdapter : public BZip2ReadAdapter {
85fb299fa2Sopenharmony_cipublic:
86fb299fa2Sopenharmony_ci    BZip2BufferReadAdapter(size_t offset, size_t length, const BlockBuffer &info)
87fb299fa2Sopenharmony_ci        : BZip2ReadAdapter(offset, length), buffer_(info) {}
88fb299fa2Sopenharmony_ci    ~BZip2BufferReadAdapter() override {}
89fb299fa2Sopenharmony_ci
90fb299fa2Sopenharmony_ci    int32_t Open() override;
91fb299fa2Sopenharmony_ci    int32_t Close() override;
92fb299fa2Sopenharmony_ci
93fb299fa2Sopenharmony_ci    int32_t ReadData(BlockBuffer &info) override;
94fb299fa2Sopenharmony_ciprivate:
95fb299fa2Sopenharmony_ci    BlockBuffer buffer_ {};
96fb299fa2Sopenharmony_ci};
97fb299fa2Sopenharmony_ci} // namespace UpdatePatch
98fb299fa2Sopenharmony_ci#endif // BZIP2_ADAPTER_H