aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Ngc/Detail/SimilarFormTable.cs
blob: 7999e6ca136601295c493813e3f00fb70d08da23 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;

namespace Ryujinx.Horizon.Sdk.Ngc.Detail
{
    class SimilarFormTable
    {
        private int _similarTableStringLength;
        private int _canonicalTableStringLength;
        private int _count;
        private byte[][] _similarTable;
        private byte[][] _canonicalTable;

        public bool Import(ref BinaryReader reader)
        {
            if (!reader.Read(out _similarTableStringLength) ||
                !reader.Read(out _canonicalTableStringLength) ||
                !reader.Read(out _count))
            {
                return false;
            }

            _similarTable = new byte[_count][];
            _canonicalTable = new byte[_count][];

            if (_count < 1)
            {
                return true;
            }

            for (int tableIndex = 0; tableIndex < _count; tableIndex++)
            {
                if (reader.AllocateAndReadArray(ref _similarTable[tableIndex], _similarTableStringLength) != _similarTableStringLength ||
                    reader.AllocateAndReadArray(ref _canonicalTable[tableIndex], _canonicalTableStringLength) != _canonicalTableStringLength)
                {
                    return false;
                }
            }

            return true;
        }

        public ReadOnlySpan<byte> FindCanonicalString(ReadOnlySpan<byte> similarFormString)
        {
            int lowerBound = 0;
            int upperBound = _count;

            for (int charIndex = 0; charIndex < similarFormString.Length; charIndex++)
            {
                byte character = similarFormString[charIndex];

                int newLowerBound = GetLowerBound(character, charIndex, lowerBound - 1, upperBound - 1);
                if (newLowerBound < 0 || _similarTable[newLowerBound][charIndex] != character)
                {
                    return ReadOnlySpan<byte>.Empty;
                }

                int newUpperBound = GetUpperBound(character, charIndex, lowerBound - 1, upperBound - 1);
                if (newUpperBound < 0)
                {
                    newUpperBound = upperBound;
                }

                lowerBound = newLowerBound;
                upperBound = newUpperBound;
            }

            return _canonicalTable[lowerBound];
        }

        private int GetLowerBound(byte character, int charIndex, int left, int right)
        {
            while (right - left > 1)
            {
                int range = right + left;

                if (range < 0)
                {
                    range++;
                }

                int middle = range / 2;

                if (character <= _similarTable[middle][charIndex])
                {
                    right = middle;
                }
                else
                {
                    left = middle;
                }
            }

            if (_similarTable[right][charIndex] < character)
            {
                return -1;
            }

            return right;
        }

        private int GetUpperBound(byte character, int charIndex, int left, int right)
        {
            while (right - left > 1)
            {
                int range = right + left;

                if (range < 0)
                {
                    range++;
                }

                int middle = range / 2;

                if (_similarTable[middle][charIndex] <= character)
                {
                    left = middle;
                }
                else
                {
                    right = middle;
                }
            }

            if (_similarTable[right][charIndex] <= character)
            {
                return -1;
            }

            return right;
        }
    }
}