-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathUtils.cs
72 lines (64 loc) · 1.76 KB
/
Utils.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using Syroot.BinaryData;
using Syroot.BinaryData.Memory;
namespace PDTools.STStruct;
public static class Utils
{
public static uint Read7BitUInt32(this ref SpanReader sr)
{
uint value = sr.ReadByte();
if ((value & 0x80) != 0)
{
uint mask = 0x80;
do
{
value = ((value - mask) << 8) + sr.ReadByte();
mask <<= 7;
} while ((value & mask) != 0);
}
return value;
}
public static void EncodeAndAdvance(this BinaryStream bs, uint value)
{
uint mask = 0x80;
Span<byte> buffer = [];
if (value <= 0x7F)
{
bs.WriteByte((byte)value);
return;
}
else if (value <= 0x3FFF)
{
Span<byte> tempBuf = BitConverter.GetBytes(value).AsSpan();
tempBuf.Reverse();
buffer = tempBuf.Slice(2, 2);
}
else if (value <= 0x1FFFFF)
{
Span<byte> tempBuf = BitConverter.GetBytes(value).AsSpan();
tempBuf.Reverse();
buffer = tempBuf.Slice(1, 3);
}
else if (value <= 0xFFFFFFF)
{
buffer = BitConverter.GetBytes(value);
buffer.Reverse();
}
else if (value <= 0xFFFFFFFF)
{
buffer = BitConverter.GetBytes(value);
buffer.Reverse();
buffer = new byte[] { 0, buffer[0], buffer[1], buffer[2], buffer[3] };
}
else
throw new Exception("????");
for (int i = 1; i < buffer.Length; i++)
{
buffer[0] += (byte)mask;
mask >>= 1;
}
bs.Write(buffer.ToArray());
}
}