1/*
2 * Copyright (c) 2024-2024 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 SIGNATRUETOOLS_RANDOM_ACCESS_FILE_INPUT_H
17#define SIGNATRUETOOLS_RANDOM_ACCESS_FILE_INPUT_H
18
19#include "file_data_source.h"
20#include "random_access_file.h"
21#include "signature_tools_log.h"
22#include "zip_data_input.h"
23
24namespace OHOS {
25namespace SignatureTools {
26
27class RandomAccessFileInput : public ZipDataInput {
28public:
29    RandomAccessFileInput(RandomAccessFile& file);
30
31    /**
32     * Random Access File Zip Data Input
33     *
34     * @param file zip file
35     * @param offset offset
36     * @param size size
37     */
38    RandomAccessFileInput(RandomAccessFile& file, int64_t offset, int64_t size);
39
40    ~RandomAccessFileInput()
41    {
42    }
43
44    int64_t Size() override;
45
46    bool CopyTo(int64_t offset, int size, ByteBuffer& buffer) override;
47
48    ByteBuffer CreateByteBuffer(int64_t offset, int size) override;
49
50    DataSource* Slice(int64_t offset, int64_t size) override;
51
52private:
53    static constexpr int MAX_READ_BLOCK_SIZE = 1024 * 1024;
54
55    bool CheckBoundValid(int64_t offset, int64_t size, int64_t sourceSize);
56
57    RandomAccessFile& file;
58
59    const int64_t startIndex;
60
61    const int64_t size;
62};
63} // namespace SignatureTools
64} // namespace OHOS
65#endif // SIGNATRUETOOLS_RANDOM_ACCESS_FILE_INPUT_H