-
Notifications
You must be signed in to change notification settings - Fork 0
/
AAPFile.cs
173 lines (156 loc) · 5.05 KB
/
AAPFile.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System.Buffers.Binary;
using System.IO.Compression;
using AssEmbly.Resources.Localization;
namespace AssEmbly
{
[Flags]
public enum AAPFeatures : ulong
{
None = 0,
#if V1_CALL_STACK_COMPAT
V1CallStack = 0b1,
#endif
#if EXTENSION_SET_SIGNED
ExtensionSigned = 0b10,
#endif
#if EXTENSION_SET_FLOATING_POINT
ExtensionFloat = 0b100,
#endif
#if EXTENSION_SET_EXTENDED_BASE
ExtensionExtendedBase = 0b1000,
#endif
#if GZIP_COMPRESSION
GZipCompressed = 0b10000,
#endif
#if EXTENSION_SET_EXTERNAL_ASM
ExtensionExternalAssembly = 0b100000,
#endif
#if EXTENSION_SET_HEAP_ALLOCATE
ExtensionMemoryAllocation = 0b1000000,
#endif
#if EXTENSION_SET_FILE_SYSTEM
ExtensionFileSystem = 0b10000000,
#endif
#if EXTENSION_SET_TERMINAL
ExtensionTerminal = 0b100000000,
#endif
#if DISPLACEMENT
PointerDisplacement = 0b1000000000,
#endif
All = None
#if V1_CALL_STACK_COMPAT
| V1CallStack
#endif
#if EXTENSION_SET_SIGNED
| ExtensionSigned
#endif
#if EXTENSION_SET_FLOATING_POINT
| ExtensionFloat
#endif
#if EXTENSION_SET_EXTENDED_BASE
| ExtensionExtendedBase
#endif
#if GZIP_COMPRESSION
| GZipCompressed
#endif
#if EXTENSION_SET_EXTERNAL_ASM
| ExtensionExternalAssembly
#endif
#if EXTENSION_SET_HEAP_ALLOCATE
| ExtensionMemoryAllocation
#endif
#if EXTENSION_SET_FILE_SYSTEM
| ExtensionFileSystem
#endif
#if EXTENSION_SET_TERMINAL
| ExtensionTerminal
#endif
#if DISPLACEMENT
| PointerDisplacement
#endif
, Incompatible = ~All
}
public class AAPFile
{
public const int HeaderSize = 36;
// "AssEmbly"
public static readonly byte[] MagicBytes = { 65, 115, 115, 69, 109, 98, 108, 121 };
// Header
public Version LanguageVersion { get; }
public AAPFeatures Features { get; }
public ulong EntryPoint { get; }
public byte[] Program { get; }
public AAPFile(Version languageVersion, AAPFeatures features, ulong entryPoint, byte[] program)
{
LanguageVersion = languageVersion;
Features = features;
EntryPoint = entryPoint;
Program = program;
}
public AAPFile(byte[] executable)
{
if (executable.Length < HeaderSize)
{
throw new AAPFormatException(Strings.AAP_Error_Invalid_Not_Enough_Bytes);
}
if (!executable[..8].SequenceEqual(MagicBytes))
{
throw new AAPFormatException(Strings.AAP_Error_Invalid_Bad_Header);
}
ReadOnlySpan<byte> byteSpan = new(executable);
int major = BinaryPrimitives.ReadInt32LittleEndian(byteSpan[8..]);
int minor = BinaryPrimitives.ReadInt32LittleEndian(byteSpan[12..]);
int build = BinaryPrimitives.ReadInt32LittleEndian(byteSpan[16..]);
LanguageVersion = new Version(major, minor, build);
Features = (AAPFeatures)BinaryPrimitives.ReadUInt64LittleEndian(byteSpan[20..]);
EntryPoint = BinaryPrimitives.ReadUInt64LittleEndian(byteSpan[28..]);
#if GZIP_COMPRESSION
if (Features.HasFlag(AAPFeatures.GZipCompressed))
{
using MemoryStream compressedProgram = new(executable[36..]);
using GZipStream decompressor = new(compressedProgram, CompressionMode.Decompress);
using MemoryStream decompressedProgram = new();
decompressor.CopyTo(decompressedProgram);
Program = decompressedProgram.ToArray();
}
else
{
#endif
Program = executable[36..];
#if GZIP_COMPRESSION
}
#endif
}
public byte[] GetBytes()
{
byte[] programBytes;
#if GZIP_COMPRESSION
if (Features.HasFlag(AAPFeatures.GZipCompressed))
{
using MemoryStream compressedProgram = new();
using (GZipStream compressor = new(compressedProgram, CompressionLevel.SmallestSize, true))
{
compressor.Write(Program);
}
programBytes = compressedProgram.ToArray();
}
else
{
#endif
programBytes = Program;
#if GZIP_COMPRESSION
}
#endif
byte[] bytes = new byte[HeaderSize + programBytes.Length];
Span<byte> byteSpan = bytes.AsSpan();
MagicBytes.CopyTo(bytes, 0);
BinaryPrimitives.WriteInt32LittleEndian(byteSpan[8..], LanguageVersion.Major);
BinaryPrimitives.WriteInt32LittleEndian(byteSpan[12..], LanguageVersion.Minor);
BinaryPrimitives.WriteInt32LittleEndian(byteSpan[16..], LanguageVersion.Build);
BinaryPrimitives.WriteUInt64LittleEndian(byteSpan[20..], (ulong)Features);
BinaryPrimitives.WriteUInt64LittleEndian(byteSpan[28..], EntryPoint);
programBytes.CopyTo(bytes, HeaderSize);
return bytes;
}
}
}