This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml.go
293 lines (250 loc) · 7.38 KB
/
xml.go
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package apk
import (
"encoding/binary"
"encoding/xml"
"fmt"
"io"
"strings"
)
// XMLFile is a representation of an Android binary XML file.
type XMLFile struct {
stringPool map[resStringPoolRef]string
nsToPrefix map[resStringPoolRef]resStringPoolRef
namespaces map[resStringPoolRef]resStringPoolRef
xml strings.Builder
table *ResTable
cfg *ResTableConfig
}
// NewXMLFile creates a new XMLFile instance from a reader of Android binary XML. If t is provided,
// resource table references will be followed as necessary. If t is nil, references will be replaced
// with a hexadecimal resource ID in the XML text accessible by calling String().
func NewXMLFile(r io.ReaderAt, t *ResTable, cfg *ResTableConfig) (*XMLFile, error) {
f := new(XMLFile)
f.table = t
f.cfg = cfg
fmt.Fprintf(&f.xml, xml.Header)
header := new(resXMLTreeHeader)
sr := io.NewSectionReader(r, 0, maxReadBytes)
if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
return nil, err
}
if header.Header.Type != resXMLType {
return nil, ErrMalformedHeader
}
offset := int64(header.Header.HeaderSize)
for offset < int64(header.Header.Size) {
chunk := new(resChunkHeader)
if _, err := sr.Seek(offset, io.SeekStart); err != nil {
return nil, err
}
if err := binary.Read(sr, binary.LittleEndian, chunk); err != nil {
return nil, err
}
if _, err := sr.Seek(offset, io.SeekStart); err != nil {
return nil, err
}
var err error
switch chunk.Type {
case resStringPoolType:
f.stringPool, err = parseStringPool(io.NewSectionReader(
sr,
offset,
maxReadBytes-offset,
))
case resXMLResourceMapType:
case resXMLStartNamespaceType:
err = f.parseStartNamespace(sr)
case resXMLEndNamespaceType:
err = f.parseEndNamespace(sr)
case resXMLStartElementType:
err = f.parseXMLStartElement(sr)
case resXMLEndElementType:
err = f.parseXMLEndElement(sr)
case resXMLCDATAType:
err = f.parseCDATA(sr)
default:
return nil, ErrInvalidChunkType
}
if err != nil {
return nil, err
}
offset += int64(chunk.Size)
}
return f, nil
}
// String prints the XMLFile's raw XML as text. It does not resolve resource table references or
// perform any prettification
func (f *XMLFile) String() string {
return f.xml.String()
}
// parseStartNamespace parses a resXMLTreeNamespaceExt as a namespace start and updates the parsing
// state of f as necessary.
func (f *XMLFile) parseStartNamespace(sr *io.SectionReader) error {
node := new(resXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, node); err != nil {
return err
}
ns := new(resXMLTreeNamespaceExt)
if err := binary.Read(sr, binary.LittleEndian, ns); err != nil {
return err
}
if f.nsToPrefix == nil {
f.nsToPrefix = make(map[resStringPoolRef]resStringPoolRef)
}
if f.namespaces == nil {
f.namespaces = make(map[resStringPoolRef]resStringPoolRef)
}
f.nsToPrefix[ns.URI] = ns.Prefix
f.namespaces[ns.URI] = ns.Prefix
return nil
}
// parseEndNamespace parses a resXMLTreeNamespaceExt as a namespace end and updates the parsing
// state of f as necessary.
func (f *XMLFile) parseEndNamespace(sr *io.SectionReader) error {
node := new(resXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, node); err != nil {
return err
}
ns := new(resXMLTreeNamespaceExt)
if err := binary.Read(sr, binary.LittleEndian, ns); err != nil {
return err
}
delete(f.namespaces, ns.URI)
return nil
}
// parseXMLStartElement parses an XML start element along with its attributes and updates the
// parsing state of f as necessary.
func (f *XMLFile) parseXMLStartElement(sr *io.SectionReader) error {
node := new(resXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, node); err != nil {
return err
}
element := new(resXMLTreeAttrExt)
if err := binary.Read(sr, binary.LittleEndian, element); err != nil {
return err
}
fmt.Fprintf(&f.xml, "<%s", f.nsPrefix(element.NS, element.Name))
for uri, prefix := range f.nsToPrefix {
fmt.Fprintf(&f.xml, " xmlns:%s=\"", f.stringPool[prefix])
if err := xml.EscapeText(&f.xml, []byte(f.stringPool[uri])); err != nil {
return err
}
fmt.Fprintf(&f.xml, "\"")
}
f.nsToPrefix = nil
for i := 0; i < int(element.AttributeCount); i++ {
attr := new(resXMLTreeAttribute)
if err := binary.Read(sr, binary.LittleEndian, attr); err != nil {
return err
}
var value string
switch attr.TypedValue.DataType {
case typeNull:
value = ""
case typeReference:
if f.table != nil {
r, err := f.table.getResource(resID(attr.TypedValue.Data), f.cfg)
if err != nil {
return err
}
value = r
} else {
value = fmt.Sprintf("@0x%08X", attr.TypedValue.Data)
}
case typeString:
value = f.stringPool[attr.RawValue]
case typeFloat:
value = fmt.Sprintf("%f", float32(attr.TypedValue.Data))
case typeIntDec:
value = fmt.Sprintf("%d", attr.TypedValue.Data)
case typeIntHex:
value = fmt.Sprintf("0x%08X", attr.TypedValue.Data)
case typeIntBoolean:
if attr.TypedValue.Data != 0 {
value = "true"
} else {
value = "false"
}
}
fmt.Fprintf(&f.xml, " %s=\"", f.nsPrefix(attr.NS, attr.Name))
if err := xml.EscapeText(&f.xml, []byte(value)); err != nil {
return err
}
fmt.Fprintf(&f.xml, "\"")
}
fmt.Fprintf(&f.xml, ">")
return nil
}
// parseXMLEndElement parses an XML end element and updates the parsing state of f as necessary.
func (f *XMLFile) parseXMLEndElement(sr *io.SectionReader) error {
node := new(resXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, node); err != nil {
return err
}
element := new(resXMLTreeEndElementExt)
if err := binary.Read(sr, binary.LittleEndian, element); err != nil {
return err
}
fmt.Fprintf(&f.xml, "</%s>", f.nsPrefix(element.NS, element.Name))
return nil
}
// parseCDATA parses XML CDATA and updates the parsing state of f as necessary.
func (f *XMLFile) parseCDATA(sr *io.SectionReader) error {
node := new(resXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, node); err != nil {
return err
}
cdata := new(resXMLTreeCDATAExt)
if err := binary.Read(sr, binary.LittleEndian, cdata); err != nil {
return err
}
if err := xml.EscapeText(&f.xml, []byte(f.stringPool[cdata.Data])); err != nil {
return err
}
return nil
}
// nsPrefix takes a namespace and an XML attribute name as string pool references and returns the
// XML attribute prefixed with the namespace if the namespace string pool reference is not empty.
func (f *XMLFile) nsPrefix(ns resStringPoolRef, name resStringPoolRef) string {
if ns.Index == 0xFFFFFFFF {
return f.stringPool[name]
} else {
return fmt.Sprintf("%s:%s", f.stringPool[f.namespaces[ns]], f.stringPool[name])
}
}
type resXMLTreeHeader struct {
Header resChunkHeader
}
type resXMLTreeNode struct {
Header resChunkHeader
LineNumber uint32
Comment resStringPoolRef
}
type resXMLTreeCDATAExt struct {
Data resStringPoolRef
TypedData resValue
}
type resXMLTreeNamespaceExt struct {
Prefix resStringPoolRef
URI resStringPoolRef
}
type resXMLTreeEndElementExt struct {
NS resStringPoolRef
Name resStringPoolRef
}
type resXMLTreeAttrExt struct {
NS resStringPoolRef
Name resStringPoolRef
AttributeStart uint16
AttributeSize uint16
AttributeCount uint16
IDIndex uint16
ClassIndex uint16
StyleIndex uint16
}
type resXMLTreeAttribute struct {
NS resStringPoolRef
Name resStringPoolRef
RawValue resStringPoolRef
TypedValue resValue
}