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 CODE_SIGNATURE_FUZZ_COMMON
17 #define CODE_SIGNATURE_FUZZ_COMMON
18 #include <vector>
19 #include <string>
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace CodeSign {
25 class SignInfoRandomGenerator {
26 public:
SignInfoRandomGenerator(const uint8_t * data,const size_t size)27 SignInfoRandomGenerator(const uint8_t *data, const size_t size) : data_(data), dataLenth_(size) {}
GetData()28 template <class T> T GetData()
29 {
30 T object{};
31 size_t objectSize = sizeof(object);
32 if (data_ == nullptr || objectSize > dataLenth_ - basePos) {
33 basePos = 0; // reset read point
34 return object; // return empty obj
35 }
36 errno_t ret = memcpy_s(&object, objectSize, data_ + basePos, objectSize);
37 if (ret != EOK) {
38 return {};
39 }
40 basePos += objectSize;
41 return object;
42 }
43
44 void GenerateFilePath(std::string &str);
45 void GenerateString(std::string &str);
46 private:
47 const uint8_t *data_;
48 const size_t dataLenth_;
49 size_t basePos = 0;
50 };
51 }
52 }
53 }
54 #endif