-
Notifications
You must be signed in to change notification settings - Fork 83
/
xml.go
351 lines (312 loc) · 8.9 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package androidbinary
import (
"bytes"
"encoding/binary"
"encoding/xml"
"fmt"
"io"
"reflect"
)
// XMLFile is an XML file expressed in binary format.
type XMLFile struct {
stringPool *ResStringPool
notPrecessedNS map[ResStringPoolRef]ResStringPoolRef
namespaces xmlNamespaces
xmlBuffer bytes.Buffer
}
type InvalidReferenceError struct {
Ref ResStringPoolRef
}
func (e *InvalidReferenceError) Error() string {
return fmt.Sprintf("androidbinary: invalid reference: 0x%08X", e.Ref)
}
type (
xmlNamespaces struct {
l []namespaceVal
}
namespaceVal struct {
key ResStringPoolRef
value ResStringPoolRef
}
)
func (x *xmlNamespaces) add(key ResStringPoolRef, value ResStringPoolRef) {
x.l = append(x.l, namespaceVal{key: key, value: value})
}
func (x *xmlNamespaces) remove(key ResStringPoolRef) {
for i := len(x.l) - 1; i >= 0; i-- {
if x.l[i].key == key {
var newList = append(x.l[:i], x.l[i+1:]...)
x.l = newList
return
}
}
}
func (x *xmlNamespaces) get(key ResStringPoolRef) ResStringPoolRef {
for i := len(x.l) - 1; i >= 0; i-- {
if x.l[i].key == key {
return x.l[i].value
}
}
return ResStringPoolRef(0)
}
// ResXMLTreeNode is basic XML tree node.
type ResXMLTreeNode struct {
Header ResChunkHeader
LineNumber uint32
Comment ResStringPoolRef
}
// ResXMLTreeNamespaceExt is extended XML tree node for namespace start/end nodes.
type ResXMLTreeNamespaceExt struct {
Prefix ResStringPoolRef
URI ResStringPoolRef
}
// ResXMLTreeAttrExt is extended XML tree node for start tags -- includes attribute.
type ResXMLTreeAttrExt struct {
NS ResStringPoolRef
Name ResStringPoolRef
AttributeStart uint16
AttributeSize uint16
AttributeCount uint16
IDIndex uint16
ClassIndex uint16
StyleIndex uint16
}
// ResXMLTreeAttribute is an attribute of start tags.
type ResXMLTreeAttribute struct {
NS ResStringPoolRef
Name ResStringPoolRef
RawValue ResStringPoolRef
TypedValue ResValue
}
// ResXMLTreeEndElementExt is extended XML tree node for element start/end nodes.
type ResXMLTreeEndElementExt struct {
NS ResStringPoolRef
Name ResStringPoolRef
}
// NewXMLFile returns a new XMLFile.
func NewXMLFile(r io.ReaderAt) (*XMLFile, error) {
f := new(XMLFile)
sr := io.NewSectionReader(r, 0, 1<<63-1)
fmt.Fprintf(&f.xmlBuffer, xml.Header)
header := new(ResChunkHeader)
if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
return nil, err
}
offset := int64(header.HeaderSize)
for offset < int64(header.Size) {
chunkHeader, err := f.readChunk(r, offset)
if err != nil {
return nil, err
}
offset += int64(chunkHeader.Size)
}
return f, nil
}
// Reader returns a reader of XML file expressed in text format.
func (f *XMLFile) Reader() *bytes.Reader {
return bytes.NewReader(f.xmlBuffer.Bytes())
}
// Decode decodes XML file and stores the result in the value pointed to by v.
// To resolve the resource references, Decode also stores default TableFile and ResTableConfig in the value pointed to by v.
func (f *XMLFile) Decode(v interface{}, table *TableFile, config *ResTableConfig) error {
decoder := xml.NewDecoder(f.Reader())
if err := decoder.Decode(v); err != nil {
return err
}
inject(reflect.ValueOf(v), table, config)
return nil
}
func (f *XMLFile) readChunk(r io.ReaderAt, offset int64) (*ResChunkHeader, error) {
sr := io.NewSectionReader(r, offset, 1<<63-1-offset)
chunkHeader := &ResChunkHeader{}
if _, err := sr.Seek(0, io.SeekStart); err != nil {
return nil, err
}
if err := binary.Read(sr, binary.LittleEndian, chunkHeader); err != nil {
return nil, err
}
if chunkHeader.HeaderSize < uint16(binary.Size(chunkHeader)) {
return nil, fmt.Errorf("androidbinary: invalid chunk header size: %d", chunkHeader.HeaderSize)
}
if chunkHeader.Size < uint32(chunkHeader.HeaderSize) {
return nil, fmt.Errorf("androidbinary: invalid chunk size: %d", chunkHeader.Size)
}
var err error
if _, err := sr.Seek(0, io.SeekStart); err != nil {
return nil, err
}
switch chunkHeader.Type {
case ResStringPoolChunkType:
f.stringPool, err = readStringPool(sr)
case ResXMLStartNamespaceType:
err = f.readStartNamespace(sr)
case ResXMLEndNamespaceType:
err = f.readEndNamespace(sr)
case ResXMLStartElementType:
err = f.readStartElement(sr)
case ResXMLEndElementType:
err = f.readEndElement(sr)
}
if err != nil {
return nil, err
}
return chunkHeader, nil
}
// GetString returns a string referenced by ref.
// It panics if the pool doesn't contain ref.
func (f *XMLFile) GetString(ref ResStringPoolRef) string {
return f.stringPool.GetString(ref)
}
func (f *XMLFile) HasString(ref ResStringPoolRef) bool {
return f.stringPool.HasString(ref)
}
func (f *XMLFile) readStartNamespace(sr *io.SectionReader) error {
header := new(ResXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
return err
}
if _, err := sr.Seek(int64(header.Header.HeaderSize), io.SeekStart); err != nil {
return err
}
namespace := new(ResXMLTreeNamespaceExt)
if err := binary.Read(sr, binary.LittleEndian, namespace); err != nil {
return err
}
if f.notPrecessedNS == nil {
f.notPrecessedNS = make(map[ResStringPoolRef]ResStringPoolRef)
}
f.notPrecessedNS[namespace.URI] = namespace.Prefix
f.namespaces.add(namespace.URI, namespace.Prefix)
return nil
}
func (f *XMLFile) readEndNamespace(sr *io.SectionReader) error {
header := new(ResXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
return err
}
if _, err := sr.Seek(int64(header.Header.HeaderSize), io.SeekStart); err != nil {
return err
}
namespace := new(ResXMLTreeNamespaceExt)
if err := binary.Read(sr, binary.LittleEndian, namespace); err != nil {
return err
}
f.namespaces.remove(namespace.URI)
return nil
}
func (f *XMLFile) addNamespacePrefix(ns, name ResStringPoolRef) (string, error) {
if !f.HasString(name) {
return "", &InvalidReferenceError{Ref: name}
}
if ns != NilResStringPoolRef {
ref := f.namespaces.get(ns)
if ref == 0 {
return "", &InvalidReferenceError{Ref: ns}
}
if !f.HasString(ref) {
return "", &InvalidReferenceError{Ref: ref}
}
prefix := f.GetString(ref)
return fmt.Sprintf("%s:%s", prefix, f.GetString(name)), nil
}
return f.GetString(name), nil
}
func (f *XMLFile) readStartElement(sr *io.SectionReader) error {
header := new(ResXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
return err
}
if _, err := sr.Seek(int64(header.Header.HeaderSize), io.SeekStart); err != nil {
return err
}
ext := new(ResXMLTreeAttrExt)
if err := binary.Read(sr, binary.LittleEndian, ext); err != nil {
return nil
}
tag, err := f.addNamespacePrefix(ext.NS, ext.Name)
if err != nil {
return err
}
f.xmlBuffer.WriteString("<")
f.xmlBuffer.WriteString(tag)
// output XML namespaces
if f.notPrecessedNS != nil {
for uri, prefix := range f.notPrecessedNS {
if !f.HasString(uri) {
return &InvalidReferenceError{Ref: uri}
}
if !f.HasString(prefix) {
return &InvalidReferenceError{Ref: prefix}
}
fmt.Fprintf(&f.xmlBuffer, " xmlns:%s=\"", f.GetString(prefix))
xml.Escape(&f.xmlBuffer, []byte(f.GetString(uri)))
fmt.Fprint(&f.xmlBuffer, "\"")
}
f.notPrecessedNS = nil
}
// process attributes
offset := int64(ext.AttributeStart + header.Header.HeaderSize)
for i := 0; i < int(ext.AttributeCount); i++ {
if _, err := sr.Seek(offset, io.SeekStart); err != nil {
return err
}
attr := new(ResXMLTreeAttribute)
binary.Read(sr, binary.LittleEndian, attr)
var value string
if attr.RawValue != NilResStringPoolRef {
if !f.HasString(attr.RawValue) {
return &InvalidReferenceError{Ref: attr.RawValue}
}
value = f.GetString(attr.RawValue)
} else {
data := attr.TypedValue.Data
switch attr.TypedValue.DataType {
case TypeNull:
value = ""
case TypeReference:
value = fmt.Sprintf("@0x%08X", data)
case TypeIntDec:
value = fmt.Sprintf("%d", data)
case TypeIntHex:
value = fmt.Sprintf("0x%08X", data)
case TypeIntBoolean:
if data != 0 {
value = "true"
} else {
value = "false"
}
default:
value = fmt.Sprintf("@0x%08X", data)
}
}
name, err := f.addNamespacePrefix(attr.NS, attr.Name)
if err != nil {
return err
}
fmt.Fprintf(&f.xmlBuffer, " %s=\"", name)
xml.Escape(&f.xmlBuffer, []byte(value))
fmt.Fprint(&f.xmlBuffer, "\"")
offset += int64(ext.AttributeSize)
}
fmt.Fprint(&f.xmlBuffer, ">")
return nil
}
func (f *XMLFile) readEndElement(sr *io.SectionReader) error {
header := new(ResXMLTreeNode)
if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
return err
}
if _, err := sr.Seek(int64(header.Header.HeaderSize), io.SeekStart); err != nil {
return err
}
ext := new(ResXMLTreeEndElementExt)
if err := binary.Read(sr, binary.LittleEndian, ext); err != nil {
return err
}
tag, err := f.addNamespacePrefix(ext.NS, ext.Name)
if err != nil {
return err
}
fmt.Fprintf(&f.xmlBuffer, "</%s>", tag)
return nil
}