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_GRAPHICS_EFFECT_GET_OBJECT_H 17 #define TEST_FUZZTEST_GRAPHICS_EFFECT_GET_OBJECT_H 18 19 #include <cstdint> 20 #include <string> 21 22 #include "securec.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 namespace GETest { 27 28 namespace { 29 const int32_t STR_MAX_LEN = 1024; 30 } 31 32 static const uint8_t* g_data = nullptr; 33 static size_t g_size = 0; 34 static size_t g_pos = 0; 35 36 /* 37 * describe: get plain old data from outside untrusted data(g_data) which size is according to sizeof(T) 38 * tips: only support basic type 39 */ 40 template<class T> GetPlainData()41T GetPlainData() 42 { 43 T object {}; 44 size_t objectSize = sizeof(object); 45 if (g_data == nullptr || objectSize > g_size - g_pos) { 46 return object; 47 } 48 errno_t ret = memcpy_s(&object, objectSize, g_data + g_pos, objectSize); 49 if (ret != EOK) { 50 return {}; 51 } 52 g_pos += objectSize; 53 return object; 54 } 55 56 /* 57 * get a string from g_data 58 */ GetStringFromData(int strlen)59std::string GetStringFromData(int strlen) 60 { 61 if (strlen <= 0) { 62 return "fuzz"; 63 } 64 if (strlen > STR_MAX_LEN) { 65 strlen = STR_MAX_LEN; 66 } 67 char cstr[strlen]; 68 cstr[strlen - 1] = '\0'; 69 for (int i = 0; i < strlen - 1; i++) { 70 char tmp = GetPlainData<char>(); 71 if (tmp == '\0') { 72 tmp = '1'; 73 } 74 cstr[i] = tmp; 75 } 76 std::string str(cstr); 77 return str; 78 } 79 80 } // namespace GETest 81 } // namespace Rosen 82 } // namespace OHOS 83 84 #endif // GET_OBJECT_H 85