summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/mii/mii_util.h
blob: 3534fa31d5bf381a6585e6b044f317d1ff063c0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <random>
#include <span>

#include "common/common_types.h"
#include "common/swap.h"
#include "common/uuid.h"
#include "core/hle/service/mii/mii_types.h"

namespace Service::Mii {
class MiiUtil {
public:
    static u16 CalculateCrc16(const void* data, std::size_t size) {
        s32 crc{};
        for (std::size_t i = 0; i < size; i++) {
            crc ^= static_cast<const u8*>(data)[i] << 8;
            for (std::size_t j = 0; j < 8; j++) {
                crc <<= 1;
                if ((crc & 0x10000) != 0) {
                    crc = (crc ^ 0x1021) & 0xFFFF;
                }
            }
        }
        return Common::swap16(static_cast<u16>(crc));
    }

    static u16 CalculateDeviceCrc16(const Common::UUID& uuid, std::size_t data_size) {
        constexpr u16 magic{0x1021};
        s32 crc{};

        for (std::size_t i = 0; i < uuid.uuid.size(); i++) {
            for (std::size_t j = 0; j < 8; j++) {
                crc <<= 1;
                if ((crc & 0x10000) != 0) {
                    crc = crc ^ magic;
                }
            }
            crc ^= uuid.uuid[i];
        }

        // As much as this looks wrong this is what N's does

        for (std::size_t i = 0; i < data_size * 8; i++) {
            crc <<= 1;
            if ((crc & 0x10000) != 0) {
                crc = crc ^ magic;
            }
        }

        return Common::swap16(static_cast<u16>(crc));
    }

    static Common::UUID MakeCreateId() {
        return Common::UUID::MakeRandomRFC4122V4();
    }

    static Common::UUID GetDeviceId() {
        // This should be nn::settings::detail::GetMiiAuthorId()
        return Common::UUID::MakeDefault();
    }

    template <typename T>
    static T GetRandomValue(T min, T max) {
        std::random_device device;
        std::mt19937 gen(device());
        std::uniform_int_distribution<u64> distribution(static_cast<u64>(min),
                                                        static_cast<u64>(max));
        return static_cast<T>(distribution(gen));
    }

    template <typename T>
    static T GetRandomValue(T max) {
        return GetRandomValue<T>({}, max);
    }

    static bool IsFontRegionValid(FontRegion font, std::span<const char16_t> text) {
        // TODO: This function needs to check against the font tables
        return true;
    }
};
} // namespace Service::Mii