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