Skip to content

Commit ac8ee22

Browse files
committed
Android manifest almost finished (protobuf missing) + native bits
Beginnings of support for reading ELF binaries, needed for typemaps and friends
1 parent 20db77e commit ac8ee22

16 files changed

+1043
-11
lines changed

tools/apput/src/Android/ARSCHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ARSCHeader (Stream data, AndroidManifestChunkType? expectedType = null)
2828
}
2929

3030
// Data in AXML is little-endian, which is fortuitous as that's the only format BinaryReader understands.
31-
using BinaryReader reader = Utilities.GetReaderAndRewindStream (data);
31+
using BinaryReader reader = Utilities.GetReaderAndRewindStream (data, rewindStream: false);
3232

3333
// ushort: type
3434
// ushort: header_size

tools/apput/src/Android/AXMLParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public AXMLParser (Stream data)
108108
{
109109
valid = true;
110110

111-
XmlDocument ret = new XmlDocument ();
111+
var ret = new XmlDocument ();
112112
XmlDeclaration declaration = ret.CreateXmlDeclaration ("1.0", stringPool.IsUTF8 ? "UTF-8" : "UTF-16", null);
113113
ret.InsertBefore (declaration, ret.DocumentElement);
114114

tools/apput/src/Android/AndroidManifest.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Xml;
34

45
namespace ApplicationUtility;
56

@@ -8,6 +9,7 @@ public class AndroidManifest : IAspect
89
public string Description { get; }
910

1011
AXMLParser? binaryParser;
12+
XmlDocument? xmlDoc;
1113

1214
AndroidManifest (AXMLParser binaryParser, string? description)
1315
{
@@ -35,6 +37,7 @@ public static IAspect LoadAspect (Stream stream, IAspectState state, string? des
3537

3638
public static IAspectState ProbeAspect (Stream stream, string? description)
3739
{
40+
Log.Debug ($"Checking if '{description}' is an Android binary XML document.");
3841
try {
3942
stream.Seek (0, SeekOrigin.Begin);
4043

@@ -44,15 +47,48 @@ public static IAspectState ProbeAspect (Stream stream, string? description)
4447
// We leave parsing of the data to `LoadAspect`, here we only detect the format
4548
return new AndroidManifestAspectState (binaryParser);
4649
} 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);
4851
}
4952

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);
5263
}
5364

5465
void Read ()
5566
{
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;
5793
}
5894
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
using System.Xml;
2+
13
namespace ApplicationUtility;
24

35
class AndroidManifestAspectState : IAspectState
46
{
57
public bool Success => true;
68
public AXMLParser? BinaryParser { get; }
9+
public XmlDocument? Xml { get; }
710

811
public AndroidManifestAspectState (AXMLParser? binaryParser)
912
{
1013
BinaryParser = binaryParser;
1114
}
15+
16+
public AndroidManifestAspectState (XmlDocument? xmlDoc)
17+
{
18+
Xml = xmlDoc;
19+
}
1220
}

tools/apput/src/Common/Utilities.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ public static void CloseAndDeleteFile (FileStream stream, bool quiet = true)
3333
DeleteFile (path);
3434
}
3535

36-
public static BinaryReader GetReaderAndRewindStream (Stream stream)
36+
public static BinaryReader GetReaderAndRewindStream (Stream stream, bool rewindStream = false)
3737
{
38-
stream.Seek (0, SeekOrigin.Begin);
38+
if (rewindStream) {
39+
stream.Seek (0, SeekOrigin.Begin);
40+
}
41+
3942
return new BinaryReader (stream, Encoding.UTF8, leaveOpen: true);
4043
}
4144

@@ -44,4 +47,6 @@ public static BasicAspectState GetFailureAspectState (string message)
4447
Log.Debug (message);
4548
return new BasicAspectState (false);
4649
}
50+
51+
public static string ToStringOrNull<T> (T? reference) => reference == null ? "<NULL>" : reference.ToString () ?? "[unknown]";
4752
}

0 commit comments

Comments
 (0)