1
1
using System ;
2
2
using System . IO ;
3
+ using System . Xml ;
3
4
4
5
namespace ApplicationUtility ;
5
6
@@ -8,6 +9,7 @@ public class AndroidManifest : IAspect
8
9
public string Description { get ; }
9
10
10
11
AXMLParser ? binaryParser ;
12
+ XmlDocument ? xmlDoc ;
11
13
12
14
AndroidManifest ( AXMLParser binaryParser , string ? description )
13
15
{
@@ -35,6 +37,7 @@ public static IAspect LoadAspect (Stream stream, IAspectState state, string? des
35
37
36
38
public static IAspectState ProbeAspect ( Stream stream , string ? description )
37
39
{
40
+ Log . Debug ( $ "Checking if '{ description } ' is an Android binary XML document.") ;
38
41
try {
39
42
stream . Seek ( 0 , SeekOrigin . Begin ) ;
40
43
@@ -44,15 +47,48 @@ public static IAspectState ProbeAspect (Stream stream, string? description)
44
47
// We leave parsing of the data to `LoadAspect`, here we only detect the format
45
48
return new AndroidManifestAspectState ( binaryParser ) ;
46
49
} catch ( Exception ex ) {
47
- Log . Debug ( $ "Failed to instantiate AXML binary parser for '{ description } '", ex ) ;
50
+ Log . Debug ( $ "Failed to instantiate AXML binary parser for '{ description } '. Exception thrown: ", ex ) ;
48
51
}
49
52
50
- // TODO: detect plain XML
51
- throw new NotImplementedException ( ) ;
53
+ Log . Debug ( $ "Checking if '{ description } ' is an plain XML document.") ;
54
+ try {
55
+ return new AndroidManifestAspectState ( ParsePlainXML ( stream ) ) ;
56
+ } catch ( Exception ex ) {
57
+ Log . Debug ( $ "Failed to parse '{ description } ' as XML document. Exception thrown:", ex ) ;
58
+ }
59
+
60
+ // TODO: AndroidManifest.xml in AAB files is actually a protobuf data dump. Attempt to
61
+ // deserialize it here.
62
+ return new BasicAspectState ( success : false ) ;
52
63
}
53
64
54
65
void Read ( )
55
66
{
56
- throw new NotImplementedException ( ) ;
67
+ if ( binaryParser == null ) {
68
+ throw new NotImplementedException ( ) ;
69
+ }
70
+
71
+ xmlDoc = binaryParser . Parse ( ) ;
72
+ if ( xmlDoc == null || ! binaryParser . IsValid ) {
73
+ Log . Debug ( $ "AXML parser didn't render a valid document for '{ Description } '") ;
74
+ return ;
75
+ }
76
+ Log . Debug ( $ "'{ Description } ' loaded and parsed correctly.") ;
77
+ }
78
+
79
+ static XmlDocument ParsePlainXML ( Stream stream )
80
+ {
81
+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
82
+ var settings = new XmlReaderSettings {
83
+ IgnoreComments = true ,
84
+ IgnoreProcessingInstructions = true ,
85
+ IgnoreWhitespace = true ,
86
+ } ;
87
+
88
+ using var reader = XmlReader . Create ( stream , settings ) ;
89
+ var doc = new XmlDocument ( ) ;
90
+ doc . Load ( reader ) ;
91
+
92
+ return doc ;
57
93
}
58
94
}
0 commit comments