-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathAssemblyStoreExplorer.cs
More file actions
133 lines (106 loc) · 4.81 KB
/
AssemblyStoreExplorer.cs
File metadata and controls
133 lines (106 loc) · 4.81 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
/*
* Adapted from https://github.com/dotnet/android/blob/5ebcb1dd1503648391e3c0548200495f634d90c6/tools/assembly-store-reader-mk2/AssemblyStore/AssemblyStoreExplorer.cs
* Original code licensed under the MIT License (https://github.com/dotnet/android/blob/5ebcb1dd1503648391e3c0548200495f634d90c6/LICENSE.TXT)
*/
namespace Sentry.Android.AssemblyReader.V2;
internal class AssemblyStoreExplorer
{
private readonly AssemblyStoreReader _reader;
public AndroidTargetArch? TargetArch { get; }
public IList<AssemblyStoreItem>? Assemblies { get; }
public IDictionary<string, AssemblyStoreItem>? AssembliesByName { get; }
public bool Is64Bit { get; }
private AssemblyStoreExplorer(Stream storeStream, string path, DebugLogger? logger)
{
var storeReader = AssemblyStoreReader.Create(storeStream, path, logger);
if (storeReader == null)
{
storeStream.Dispose();
throw new NotSupportedException($"Format of assembly store '{path}' is unsupported");
}
_reader = storeReader;
TargetArch = _reader.TargetArch;
Assemblies = _reader.Assemblies;
Is64Bit = _reader.Is64Bit;
var dict = new Dictionary<string, AssemblyStoreItem>(StringComparer.Ordinal);
if (Assemblies is not null)
{
foreach (var item in Assemblies)
{
logger?.Invoke("Assembly {0} indexed from AssemblyStore {1}", item.Name, path);
dict.Add(item.Name, item);
}
}
AssembliesByName = dict.AsReadOnly();
}
private AssemblyStoreExplorer(FileInfo storeInfo, DebugLogger? logger)
: this(storeInfo.OpenRead(), storeInfo.FullName, logger)
{ }
public static (IList<AssemblyStoreExplorer>? explorers, string? errorMessage) Open(string inputFile, DebugLogger? logger)
{
var (format, info) = Utils.DetectFileFormat(inputFile);
if (info == null)
{
return (null, $"File '{inputFile}' does not exist.");
}
switch (format)
{
case FileFormat.Unknown:
return (null, $"File '{inputFile}' has an unknown format.");
case FileFormat.Zip:
return (null, $"File '{inputFile}' is a ZIP archive, but not an Android one.");
case FileFormat.AssemblyStore:
case FileFormat.ELF:
return (new List<AssemblyStoreExplorer> { new AssemblyStoreExplorer(info, logger) }, null);
case FileFormat.Aab:
return OpenAab(info, logger);
case FileFormat.AabBase:
return OpenAabBase(info, logger);
case FileFormat.Apk:
return OpenApk(info, logger);
default:
return (null, $"File '{inputFile}' has an unsupported format '{format}'");
}
}
private static (IList<AssemblyStoreExplorer>? explorers, string? errorMessage) OpenAab(FileInfo fi, DebugLogger? logger)
=> OpenCommon(fi, [StoreReaderV2.AabPaths, StoreReader_V1.AabPaths], logger);
private static (IList<AssemblyStoreExplorer>? explorers, string? errorMessage) OpenAabBase(FileInfo fi, DebugLogger? logger)
=> OpenCommon(fi, [StoreReaderV2.AabBasePaths, StoreReader_V1.AabBasePaths], logger);
private static (IList<AssemblyStoreExplorer>? explorers, string? errorMessage) OpenApk(FileInfo fi, DebugLogger? logger)
=> OpenCommon(fi, [StoreReaderV2.ApkPaths, StoreReader_V1.ApkPaths], logger);
private static (IList<AssemblyStoreExplorer>? explorers, string? errorMessage) OpenCommon(FileInfo fi, List<IList<string>> pathLists, DebugLogger? logger)
{
using var zip = ZipFile.OpenRead(fi.FullName);
foreach (var paths in pathLists)
{
var (explorers, errorMessage, pathsFound) = TryLoad(fi, zip, paths, logger);
if (pathsFound)
{
return (explorers, errorMessage);
}
}
return (null, "Unable to find any blob entries");
}
private static (IList<AssemblyStoreExplorer>? explorers, string? errorMessage, bool pathsFound) TryLoad(FileInfo fi, ZipArchive zip, IList<string> paths, DebugLogger? logger)
{
var ret = new List<AssemblyStoreExplorer>();
foreach (var path in paths)
{
if (zip.GetEntry(path) is not { } entry)
{
continue;
}
var stream = entry.Extract();
ret.Add(new AssemblyStoreExplorer(stream, $"{fi.FullName}!{path}", logger));
}
if (ret.Count == 0)
{
return (null, null, false);
}
return (ret, null, true);
}
public MemoryStream? ReadImageData(AssemblyStoreItem item, bool uncompressIfNeeded = false)
{
return _reader.ReadEntryImageData(item, uncompressIfNeeded);
}
}