-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathSymUnmanagedExtensions.Document.cs
More file actions
151 lines (127 loc) · 5.02 KB
/
SymUnmanagedExtensions.Document.cs
File metadata and controls
151 lines (127 loc) · 5.02 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the License.txt file in the project root for more information.
using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
namespace Microsoft.DiaSymReader
{
using static InteropUtilities;
partial class SymUnmanagedExtensions
{
public static string GetName(this ISymUnmanagedDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
return BufferToString(GetItems(document,
(ISymUnmanagedDocument a, int b, out int c, char[] d) => a.GetUrl(b, out c, d))!);
}
public static byte[] GetChecksum(this ISymUnmanagedDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
return NullToEmpty(GetItems(document,
(ISymUnmanagedDocument a, int b, out int c, byte[] d) => a.GetChecksum(b, out c, d)));
}
public static Guid GetLanguage(this ISymUnmanagedDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
Guid result = default(Guid);
ThrowExceptionForHR(document.GetLanguage(ref result));
return result;
}
public static Guid GetLanguageVendor(this ISymUnmanagedDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
Guid result = default(Guid);
ThrowExceptionForHR(document.GetLanguageVendor(ref result));
return result;
}
public static Guid GetDocumentType(this ISymUnmanagedDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
Guid result = default(Guid);
ThrowExceptionForHR(document.GetDocumentType(ref result));
return result;
}
public static Guid GetHashAlgorithm(this ISymUnmanagedDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
Guid result = default(Guid);
ThrowExceptionForHR(document.GetChecksumAlgorithmId(ref result));
return result;
}
/// <summary>
/// Returns the embedded source for specified document.
/// </summary>
/// <param name="document">The document to read source of.</param>
/// <returns>
/// default(<see cref="ArraySegment{T}"/>) if the document doesn't have embedded source,
/// otherwise byte array segment containing the source.
/// </returns>
public static ArraySegment<byte> GetEmbeddedSource(this ISymUnmanagedDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
Marshal.ThrowExceptionForHR(document.GetSourceLength(out int length));
if (length == 0)
{
return default(ArraySegment<byte>);
}
if (length < sizeof(int))
{
throw new InvalidDataException();
}
var blob = new byte[length];
Marshal.ThrowExceptionForHR(document.GetSourceRange(0, 0, int.MaxValue, int.MaxValue, length, out int bytesRead, blob));
if (bytesRead < sizeof(int) || bytesRead > blob.Length)
{
throw new InvalidDataException();
}
int uncompressedLength = blob[0] + (blob[1] << 8) + (blob[2] << 16) + (blob[3] << 24);
if (uncompressedLength == 0)
{
return new ArraySegment<byte>(blob, sizeof(int), bytesRead - sizeof(int));
}
var uncompressedBytes = new byte[uncompressedLength];
var compressed = new MemoryStream(blob, sizeof(int), bytesRead - sizeof(int));
using (var decompressor = new DeflateStream(compressed, CompressionMode.Decompress))
{
int position = 0;
while (true)
{
int bytesDecompressed = decompressor.Read(uncompressedBytes, position, uncompressedBytes.Length - position);
if (bytesDecompressed == 0)
{
break;
}
position += bytesDecompressed;
}
if (position != uncompressedBytes.Length || decompressor.ReadByte() != -1)
{
throw new InvalidDataException();
}
return new ArraySegment<byte>(uncompressedBytes);
}
}
}
}