~ruther/NosSmooth

ref: e0b8687280cdb96a7eefdf11c6a3df0de5f03d82 NosSmooth/Data/NosSmooth.Data.NOSFiles/Decryptors/DatDecryptor.cs -rw-r--r-- 2.1 KiB
e0b86872 — František Boháček feat(data): add .NOS file readers 3 years ago
                                                                                
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
//
//  DatDecryptor.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text;
using Remora.Results;

namespace NosSmooth.Data.NOSFiles.Decryptors;

/// <inheritdoc />
public class DatDecryptor : IDecryptor
{
    private readonly byte[] _cryptoArray;

    /// <summary>
    /// Initializes a new instance of the <see cref="DatDecryptor"/> class.
    /// </summary>
    public DatDecryptor()
    {
        _cryptoArray = new byte[] { 0x00, 0x20, 0x2D, 0x2E, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x0A, 0x00 };
    }

    /// <inheritdoc />
    public Result<byte[]> Decrypt(ReadOnlySpan<byte> data)
    {
        using var output = new MemoryStream();
        int i = 0;
        while (i < data.Length)
        {
            byte currentByte = data[i];
            i++;

            if (currentByte == 0xFF)
            {
                output.WriteByte(0xD);
                continue;
            }

            int validate = currentByte & 0x7F;
            if ((currentByte & 0x80) != 0)
            {
                for (; validate > 0 && i < data.Length; validate -= 2)
                {
                    currentByte = data[i];
                    i++;
                    byte firstByte = _cryptoArray[(currentByte & 0xF0) >> 4];
                    output.WriteByte(firstByte);

                    if (validate <= 1)
                    {
                        break;
                    }

                    byte secondByte = _cryptoArray[currentByte & 0x0F];
                    if (secondByte == 0)
                    {
                        break;
                    }
                    output.WriteByte(secondByte);
                }
            }
            else
            {
                for (; validate > 0 && i < data.Length; validate--)
                {
                    currentByte = data[i];
                    output.WriteByte((byte)(currentByte ^ 0x33));
                    i++;
                }
            }
        }

        return output.ToArray();
    }
}
Do not follow this link