1fc0b0055Sopenharmony_ci/*
2fc0b0055Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3fc0b0055Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc0b0055Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc0b0055Sopenharmony_ci * You may obtain a copy of the License at
6fc0b0055Sopenharmony_ci *
7fc0b0055Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fc0b0055Sopenharmony_ci *
9fc0b0055Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc0b0055Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc0b0055Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc0b0055Sopenharmony_ci * See the License for the specific language governing permissions and
13fc0b0055Sopenharmony_ci * limitations under the License.
14fc0b0055Sopenharmony_ci */
15fc0b0055Sopenharmony_ci
16fc0b0055Sopenharmony_ci#ifndef ACCESSTOKEN_FUZZDATA_TEMPLATE_H
17fc0b0055Sopenharmony_ci#define ACCESSTOKEN_FUZZDATA_TEMPLATE_H
18fc0b0055Sopenharmony_ci
19fc0b0055Sopenharmony_ci#include <cstdio>
20fc0b0055Sopenharmony_ci#include <string>
21fc0b0055Sopenharmony_ci
22fc0b0055Sopenharmony_ci#include "securec.h"
23fc0b0055Sopenharmony_ci
24fc0b0055Sopenharmony_cinamespace OHOS {
25fc0b0055Sopenharmony_cinamespace Security {
26fc0b0055Sopenharmony_cinamespace AccessToken {
27fc0b0055Sopenharmony_cinamespace {
28fc0b0055Sopenharmony_cistatic constexpr uint32_t BOOL_MODULO_NUM = 2;
29fc0b0055Sopenharmony_ci}
30fc0b0055Sopenharmony_ciclass AccessTokenFuzzData {
31fc0b0055Sopenharmony_cipublic:
32fc0b0055Sopenharmony_ci    explicit AccessTokenFuzzData(const uint8_t *data, const size_t size)
33fc0b0055Sopenharmony_ci        : data_(data), size_(size), pos_(0) {}
34fc0b0055Sopenharmony_ci
35fc0b0055Sopenharmony_ci    template <class T> T GetData()
36fc0b0055Sopenharmony_ci    {
37fc0b0055Sopenharmony_ci        T object{};
38fc0b0055Sopenharmony_ci        size_t objectSize = sizeof(object);
39fc0b0055Sopenharmony_ci        if (data_ == nullptr || objectSize > size_ - pos_) {
40fc0b0055Sopenharmony_ci            return object;
41fc0b0055Sopenharmony_ci        }
42fc0b0055Sopenharmony_ci        errno_t ret = memcpy_s(&object, objectSize, data_ + pos_, objectSize);
43fc0b0055Sopenharmony_ci        if (ret != EOK) {
44fc0b0055Sopenharmony_ci            return {};
45fc0b0055Sopenharmony_ci        }
46fc0b0055Sopenharmony_ci        pos_ += objectSize;
47fc0b0055Sopenharmony_ci        return object;
48fc0b0055Sopenharmony_ci    }
49fc0b0055Sopenharmony_ci
50fc0b0055Sopenharmony_ci    std::string GenerateRandomString()
51fc0b0055Sopenharmony_ci    {
52fc0b0055Sopenharmony_ci        uint8_t strlen = GetData<uint8_t>();
53fc0b0055Sopenharmony_ci
54fc0b0055Sopenharmony_ci        char cstr[strlen + 1];
55fc0b0055Sopenharmony_ci        cstr[strlen] = '\0';
56fc0b0055Sopenharmony_ci
57fc0b0055Sopenharmony_ci        for (uint8_t i = 0; i < strlen; i++) {
58fc0b0055Sopenharmony_ci            char tmp = GetData<char>();
59fc0b0055Sopenharmony_ci            if (tmp == '\0') {
60fc0b0055Sopenharmony_ci                tmp = '1';
61fc0b0055Sopenharmony_ci            }
62fc0b0055Sopenharmony_ci            cstr[i] = tmp;
63fc0b0055Sopenharmony_ci        }
64fc0b0055Sopenharmony_ci        std::string str(cstr);
65fc0b0055Sopenharmony_ci        return str;
66fc0b0055Sopenharmony_ci    }
67fc0b0055Sopenharmony_ci
68fc0b0055Sopenharmony_ci    template <class T> T GenerateRandomEnmu(T enmuMax)
69fc0b0055Sopenharmony_ci    {
70fc0b0055Sopenharmony_ci        T enmuData = static_cast<T>(GetData<uint32_t>() % (static_cast<uint32_t>(enmuMax) + 1));
71fc0b0055Sopenharmony_ci        return enmuData;
72fc0b0055Sopenharmony_ci    }
73fc0b0055Sopenharmony_ci
74fc0b0055Sopenharmony_ci    bool GenerateRandomBool()
75fc0b0055Sopenharmony_ci    {
76fc0b0055Sopenharmony_ci        return (GetData<uint32_t>() % BOOL_MODULO_NUM) == 0;
77fc0b0055Sopenharmony_ci    }
78fc0b0055Sopenharmony_ci
79fc0b0055Sopenharmony_ciprivate:
80fc0b0055Sopenharmony_ci    const uint8_t *data_;
81fc0b0055Sopenharmony_ci    const size_t size_;
82fc0b0055Sopenharmony_ci    size_t pos_;
83fc0b0055Sopenharmony_ci};
84fc0b0055Sopenharmony_ci} // namespace AccessToken
85fc0b0055Sopenharmony_ci} // namespace Security
86fc0b0055Sopenharmony_ci} // namespace OHOS
87fc0b0055Sopenharmony_ci
88fc0b0055Sopenharmony_ci#endif // ACCESSTOKEN_FUZZDATA_TEMPLATE_H
89