-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
BamlReader.cs
272 lines (258 loc) · 8.56 KB
/
BamlReader.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright (c) 2015 Ki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace ILSpy.BamlDecompiler.Baml
{
internal class BamlBinaryReader : BinaryReader
{
public BamlBinaryReader(Stream stream)
: base(stream)
{
}
public int ReadEncodedInt() => Read7BitEncodedInt();
}
internal class BamlReader
{
const string MSBAML_SIG = "MSBAML";
internal static bool IsBamlHeader(Stream str)
{
var pos = str.Position;
try
{
var rdr = new BinaryReader(str, Encoding.Unicode);
int len = (int)(rdr.ReadUInt32() >> 1);
if (len != MSBAML_SIG.Length)
return false;
var sig = new string(rdr.ReadChars(len));
return sig == MSBAML_SIG;
}
finally
{
str.Position = pos;
}
}
static string ReadSignature(Stream str)
{
var rdr = new BinaryReader(str, Encoding.Unicode);
uint len = rdr.ReadUInt32();
var sig = new string(rdr.ReadChars((int)(len >> 1)));
rdr.ReadBytes((int)(((len + 3) & ~3) - len));
return sig;
}
public static BamlDocument ReadDocument(Stream str, CancellationToken token)
{
var ret = new BamlDocument();
var reader = new BamlBinaryReader(str);
ret.Signature = ReadSignature(str);
if (ret.Signature != MSBAML_SIG)
throw new NotSupportedException();
ret.ReaderVersion = new BamlDocument.BamlVersion { Major = reader.ReadUInt16(), Minor = reader.ReadUInt16() };
ret.UpdaterVersion = new BamlDocument.BamlVersion { Major = reader.ReadUInt16(), Minor = reader.ReadUInt16() };
ret.WriterVersion = new BamlDocument.BamlVersion { Major = reader.ReadUInt16(), Minor = reader.ReadUInt16() };
if (ret.ReaderVersion.Major != 0 || ret.ReaderVersion.Minor != 0x60 ||
ret.UpdaterVersion.Major != 0 || ret.UpdaterVersion.Minor != 0x60 ||
ret.WriterVersion.Major != 0 || ret.WriterVersion.Minor != 0x60)
throw new NotSupportedException();
var recs = new Dictionary<long, BamlRecord>();
while (str.Position < str.Length)
{
token.ThrowIfCancellationRequested();
long pos = str.Position;
var type = (BamlRecordType)reader.ReadByte();
BamlRecord rec = null;
switch (type)
{
case BamlRecordType.AssemblyInfo:
rec = new AssemblyInfoRecord();
break;
case BamlRecordType.AttributeInfo:
rec = new AttributeInfoRecord();
break;
case BamlRecordType.ConstructorParametersStart:
rec = new ConstructorParametersStartRecord();
break;
case BamlRecordType.ConstructorParametersEnd:
rec = new ConstructorParametersEndRecord();
break;
case BamlRecordType.ConstructorParameterType:
rec = new ConstructorParameterTypeRecord();
break;
case BamlRecordType.ConnectionId:
rec = new ConnectionIdRecord();
break;
case BamlRecordType.ContentProperty:
rec = new ContentPropertyRecord();
break;
case BamlRecordType.DefAttribute:
rec = new DefAttributeRecord();
break;
case BamlRecordType.DefAttributeKeyString:
rec = new DefAttributeKeyStringRecord();
break;
case BamlRecordType.DefAttributeKeyType:
rec = new DefAttributeKeyTypeRecord();
break;
case BamlRecordType.DeferableContentStart:
rec = new DeferableContentStartRecord();
break;
case BamlRecordType.DocumentEnd:
rec = new DocumentEndRecord();
break;
case BamlRecordType.DocumentStart:
rec = new DocumentStartRecord();
break;
case BamlRecordType.ElementEnd:
rec = new ElementEndRecord();
break;
case BamlRecordType.ElementStart:
rec = new ElementStartRecord();
break;
case BamlRecordType.KeyElementEnd:
rec = new KeyElementEndRecord();
break;
case BamlRecordType.KeyElementStart:
rec = new KeyElementStartRecord();
break;
case BamlRecordType.LineNumberAndPosition:
rec = new LineNumberAndPositionRecord();
break;
case BamlRecordType.LinePosition:
rec = new LinePositionRecord();
break;
case BamlRecordType.LiteralContent:
rec = new LiteralContentRecord();
break;
case BamlRecordType.NamedElementStart:
rec = new NamedElementStartRecord();
break;
case BamlRecordType.OptimizedStaticResource:
rec = new OptimizedStaticResourceRecord();
break;
case BamlRecordType.PIMapping:
rec = new PIMappingRecord();
break;
case BamlRecordType.PresentationOptionsAttribute:
rec = new PresentationOptionsAttributeRecord();
break;
case BamlRecordType.Property:
rec = new PropertyRecord();
break;
case BamlRecordType.PropertyArrayEnd:
rec = new PropertyArrayEndRecord();
break;
case BamlRecordType.PropertyArrayStart:
rec = new PropertyArrayStartRecord();
break;
case BamlRecordType.PropertyComplexEnd:
rec = new PropertyComplexEndRecord();
break;
case BamlRecordType.PropertyComplexStart:
rec = new PropertyComplexStartRecord();
break;
case BamlRecordType.PropertyCustom:
rec = new PropertyCustomRecord();
break;
case BamlRecordType.PropertyDictionaryEnd:
rec = new PropertyDictionaryEndRecord();
break;
case BamlRecordType.PropertyDictionaryStart:
rec = new PropertyDictionaryStartRecord();
break;
case BamlRecordType.PropertyListEnd:
rec = new PropertyListEndRecord();
break;
case BamlRecordType.PropertyListStart:
rec = new PropertyListStartRecord();
break;
case BamlRecordType.PropertyStringReference:
rec = new PropertyStringReferenceRecord();
break;
case BamlRecordType.PropertyTypeReference:
rec = new PropertyTypeReferenceRecord();
break;
case BamlRecordType.PropertyWithConverter:
rec = new PropertyWithConverterRecord();
break;
case BamlRecordType.PropertyWithExtension:
rec = new PropertyWithExtensionRecord();
break;
case BamlRecordType.PropertyWithStaticResourceId:
rec = new PropertyWithStaticResourceIdRecord();
break;
case BamlRecordType.RoutedEvent:
rec = new RoutedEventRecord();
break;
case BamlRecordType.StaticResourceEnd:
rec = new StaticResourceEndRecord();
break;
case BamlRecordType.StaticResourceId:
rec = new StaticResourceIdRecord();
break;
case BamlRecordType.StaticResourceStart:
rec = new StaticResourceStartRecord();
break;
case BamlRecordType.StringInfo:
rec = new StringInfoRecord();
break;
case BamlRecordType.Text:
rec = new TextRecord();
break;
case BamlRecordType.TextWithConverter:
rec = new TextWithConverterRecord();
break;
case BamlRecordType.TextWithId:
rec = new TextWithIdRecord();
break;
case BamlRecordType.TypeInfo:
rec = new TypeInfoRecord();
break;
case BamlRecordType.TypeSerializerInfo:
rec = new TypeSerializerInfoRecord();
break;
case BamlRecordType.XmlnsProperty:
rec = new XmlnsPropertyRecord();
break;
case BamlRecordType.XmlAttribute:
case BamlRecordType.ProcessingInstruction:
case BamlRecordType.LastRecordType:
case BamlRecordType.EndAttributes:
case BamlRecordType.DefTag:
case BamlRecordType.ClrEvent:
case BamlRecordType.Comment:
default:
throw new NotSupportedException();
}
rec.Position = pos;
rec.Read(reader);
ret.Add(rec);
recs.Add(pos, rec);
}
for (int i = 0; i < ret.Count; i++)
{
if (ret[i] is IBamlDeferRecord defer)
defer.ReadDefer(ret, i, _ => recs[_]);
}
return ret;
}
}
}