xref: /third_party/mindspore/test/fuzztest/data.h (revision be168c0d)
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 OHOS_MINDSPORE_TEST_FUZZTEST_DATA_H
17#define OHOS_MINDSPORE_TEST_FUZZTEST_DATA_H
18
19#include "securec.h"
20#include "../utils/log.h"
21
22class Data {
23public:
24    Data(const uint8_t *data, size_t size) {
25        dataFuzz = data;
26        dataSize = size;
27    }
28
29    template<class T> T GetData() {
30        T object {};
31        size_t objectSize = sizeof(object);
32        if (dataFuzz == nullptr || objectSize > dataSize - dataPos) {
33            LOGE("Date is not enough");
34            return {};
35        }
36        if (memcpy_s(&object, objectSize, dataFuzz + dataPos, objectSize) != EOK) {
37            LOGE("memcpy_s failed.");
38            return {};
39        }
40        dataPos = dataPos + objectSize;
41        return object;
42    }
43
44    const uint8_t* GetNowData() const {
45        return dataFuzz + dataPos;
46    }
47
48    size_t GetNowDataSize() const {
49        return dataSize - dataPos;
50    }
51
52private:
53    const uint8_t* dataFuzz {nullptr};
54    size_t dataSize {0};
55    size_t dataPos {0};
56};
57
58#endif //OHOS_MINDSPORE_TEST_FUZZTEST_DATA_H
59