1/*
2 * Copyright (C) 2023 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 TEST_FUZZTEST_GETDATA_H
17#define TEST_FUZZTEST_GETDATA_H
18
19#include <cstdint>
20
21#include "../../common/log.h"
22#include "securec.h"
23
24namespace OHOS {
25namespace NeuralNetworkRuntime {
26class Data {
27public:
28    Data(const uint8_t* data, size_t size)
29    {
30        dataFuzz = data;
31        dataSize = size;
32    }
33
34    template<class T> T GetData()
35    {
36        T object {};
37        size_t objectSize = sizeof(object);
38        if (dataFuzz == nullptr || objectSize > dataSize - dataPos) {
39            LOGE("[GetData]Data is not enough.");
40            return {};
41        }
42        if (memcpy_s(&object, objectSize, dataFuzz + dataPos, objectSize) != EOK) {
43            LOGE("[GetData]Memcpy_s failed.");
44            return {};
45        }
46        dataPos = dataPos + objectSize;
47        return object;
48    }
49
50    const uint8_t* GetNowData() const
51    {
52        return dataFuzz + dataPos;
53    }
54
55    size_t GetNowDataSize() const
56    {
57        return dataSize - dataPos;
58    }
59
60    const uint8_t* GetSpecificData(size_t startPos, size_t& size) const
61    {
62        if ((startPos + size) > dataSize) {
63            size = dataSize - startPos;
64        }
65        return dataFuzz + startPos;
66    }
67
68private:
69    const uint8_t* dataFuzz {nullptr};
70    size_t dataSize {0};
71    size_t dataPos {0};
72};
73} // namespace NeuralNetworkRuntime
74} // namespace OHOS
75
76#endif