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