1 /*
2 * Copyright (c) 2022 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 #include "fuzz_common_base.h"
17
18 extern "C" {
19 static constexpr uint32_t U32_AT_SIZE = 4;
20 static constexpr uint32_t MAX_MEMORY_SIZE = 4 * 1024 * 1024;
21
GetU32Size()22 uint32_t GetU32Size()
23 {
24 return U32_AT_SIZE;
25 }
26
GetU32Data(const char * ptr)27 uint32_t GetU32Data(const char* ptr)
28 {
29 // convert fuzz input data to an integer
30 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
31 }
32
ParseData(const uint8_t * data,size_t size)33 char* ParseData(const uint8_t* data, size_t size)
34 {
35 if (data == nullptr) {
36 return nullptr;
37 }
38
39 if (size > MAX_MEMORY_SIZE) {
40 return nullptr;
41 }
42
43 char* ch = (char *)malloc(size + 1);
44 if (ch == nullptr) {
45 return nullptr;
46 }
47
48 (void)memset_s(ch, size + 1, 0x00, size + 1);
49 if (memcpy_s(ch, size, data, size) != EOK) {
50 free(ch);
51 ch = nullptr;
52 return nullptr;
53 }
54
55 return ch;
56 }
57 }