1be168c0dSopenharmony_ci/*
2be168c0dSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3be168c0dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4be168c0dSopenharmony_ci * you may not use this file except in compliance with the License.
5be168c0dSopenharmony_ci * You may obtain a copy of the License at
6be168c0dSopenharmony_ci *
7be168c0dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8be168c0dSopenharmony_ci *
9be168c0dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10be168c0dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11be168c0dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12be168c0dSopenharmony_ci * See the License for the specific language governing permissions and
13be168c0dSopenharmony_ci * limitations under the License.
14be168c0dSopenharmony_ci */
15be168c0dSopenharmony_ci
16be168c0dSopenharmony_ci#ifndef OHOS_MINDSPORE_TEST_FUZZTEST_DATA_H
17be168c0dSopenharmony_ci#define OHOS_MINDSPORE_TEST_FUZZTEST_DATA_H
18be168c0dSopenharmony_ci
19be168c0dSopenharmony_ci#include "securec.h"
20be168c0dSopenharmony_ci#include "../utils/log.h"
21be168c0dSopenharmony_ci
22be168c0dSopenharmony_ciclass Data {
23be168c0dSopenharmony_cipublic:
24be168c0dSopenharmony_ci    Data(const uint8_t *data, size_t size) {
25be168c0dSopenharmony_ci        dataFuzz = data;
26be168c0dSopenharmony_ci        dataSize = size;
27be168c0dSopenharmony_ci    }
28be168c0dSopenharmony_ci
29be168c0dSopenharmony_ci    template<class T> T GetData() {
30be168c0dSopenharmony_ci        T object {};
31be168c0dSopenharmony_ci        size_t objectSize = sizeof(object);
32be168c0dSopenharmony_ci        if (dataFuzz == nullptr || objectSize > dataSize - dataPos) {
33be168c0dSopenharmony_ci            LOGE("Date is not enough");
34be168c0dSopenharmony_ci            return {};
35be168c0dSopenharmony_ci        }
36be168c0dSopenharmony_ci        if (memcpy_s(&object, objectSize, dataFuzz + dataPos, objectSize) != EOK) {
37be168c0dSopenharmony_ci            LOGE("memcpy_s failed.");
38be168c0dSopenharmony_ci            return {};
39be168c0dSopenharmony_ci        }
40be168c0dSopenharmony_ci        dataPos = dataPos + objectSize;
41be168c0dSopenharmony_ci        return object;
42be168c0dSopenharmony_ci    }
43be168c0dSopenharmony_ci
44be168c0dSopenharmony_ci    const uint8_t* GetNowData() const {
45be168c0dSopenharmony_ci        return dataFuzz + dataPos;
46be168c0dSopenharmony_ci    }
47be168c0dSopenharmony_ci
48be168c0dSopenharmony_ci    size_t GetNowDataSize() const {
49be168c0dSopenharmony_ci        return dataSize - dataPos;
50be168c0dSopenharmony_ci    }
51be168c0dSopenharmony_ci
52be168c0dSopenharmony_ciprivate:
53be168c0dSopenharmony_ci    const uint8_t* dataFuzz {nullptr};
54be168c0dSopenharmony_ci    size_t dataSize {0};
55be168c0dSopenharmony_ci    size_t dataPos {0};
56be168c0dSopenharmony_ci};
57be168c0dSopenharmony_ci
58be168c0dSopenharmony_ci#endif //OHOS_MINDSPORE_TEST_FUZZTEST_DATA_H
59