1/*
2 * Copyright (c) 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 TEST_FUZZTEST_COMMON_DATA_GENERATE_H
17#define TEST_FUZZTEST_COMMON_DATA_GENERATE_H
18
19#include <string>
20#include <securec.h>
21#include "ibuffer_consumer_listener.h"
22
23namespace g_fuzzCommon {
24constexpr size_t STR_LEN = 10;
25const uint8_t* g_data = nullptr;
26size_t g_size = 0;
27size_t g_pos;
28
29
30/*
31* describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
32* tips: only support basic type
33*/
34template<class T>
35T GetData()
36{
37    T object {};
38    size_t objectSize = sizeof(object);
39    if (g_data == nullptr || objectSize > g_size - g_pos) {
40        return object;
41    }
42    auto ret = memcpy_s(&object, objectSize, g_data + g_pos, objectSize);
43    if (ret != EOK) {
44        return {};
45    }
46    g_pos += objectSize;
47    return object;
48}
49
50/*
51* get a string from g_data
52*/
53[[maybe_unused]] std::string GetStringFromData(int strlen)
54{
55    char cstr[strlen];
56    cstr[strlen - 1] = '\0';
57    for (int i = 0; i < strlen - 1; i++) {
58        cstr[i] = GetData<char>();
59    }
60    std::string str(cstr);
61    return str;
62}
63}
64namespace OHOS {
65class BufferConsumerListener : public IBufferConsumerListener {
66public:
67    void OnBufferAvailable() override
68    {
69    }
70};
71}
72#endif