summaryrefslogtreecommitdiff
path: root/plugins/python/androperms/parser.py
blob: 1939bbe30cb2e7b89ae613b544474a54827b5b8d (plain)
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
#!/usr/bin/python -u
# -*- coding: utf-8 -*-


from defs import *
from stack import NamespaceStack
from string import StringBlock

from struct import unpack


class AXMLParser():


    def __init__(self, reader):

        self._reader = reader

        magic = reader.readInt()
        if magic != CHUNK_AXML_FILE:
            raise Exception('Bad Magic Number (0x%08lx)!' % magic)

        # chunkSize
        reader.skipInt()

        self._strings = StringBlock(reader)
        self._namespaces = NamespaceStack()
        self._operational = True

        self.resetEventInfo()


    def resetEventInfo(self):

        self._event = -1
        self._line_number = -1
        self._name = -1
        self._namespace_uri = -1
        self._attributes = None
        self._id_attrib = -1
        self._class_attrib = -1
        self._style_attrib = -1

        self._decreaseDepth = False



    def next(self):

        self.doNext()
        return self._event


    def doNext(self):





        event = self._event


        while True:

            if self._decreaseDepth:
                self._decreaseDepth = False
                self._namespaces.decDepth()

            # Fake END_DOCUMENT event.
            if event == END_TAG and self._namespaces.getDepth() == 1 and self._namespaces.count() == 0:
                self._event = END_DOCUMENT
                break

            if event == START_DOCUMENT:
                # Fake event, see CHUNK_XML_START_TAG handler.
                chunk = CHUNK_XML_START_TAG

            else:
                chunk = self._reader.readInt()

            if chunk == CHUNK_RESOURCEIDS:

                size = self._reader.readInt()
                if size < 8 or size % 4 != 0:
                    raise Exception('Invalid resource ids size (%d).' % size)

                self._resource_ids = self._reader.readIntArray(size / 4 - 2)

                continue

            if chunk < CHUNK_XML_FIRST or chunk > CHUNK_XML_LAST:
                raise Exception('Invalid chunk type 0x%08lx.' % chunk)

            # Fake START_DOCUMENT event.
            if chunk == CHUNK_XML_START_TAG and event == -1:
                self._event = START_DOCUMENT
                break

            # Common header.
            self._reader.skipInt() # chunkSize
            self._line_number = self._reader.readInt()
            self._reader.skipInt() # 0xffffffff

            if chunk == CHUNK_XML_START_NAMESPACE or chunk == CHUNK_XML_END_NAMESPACE:

                if chunk == CHUNK_XML_START_NAMESPACE:

                    prefix = self._reader.readInt()
                    uri = self._reader.readInt()
                    self._namespaces.push(prefix, uri)

                else:

                    self._reader.skipInt() # prefix
                    self._reader.skipInt() # uri
                    self._namespaces.pop()

                continue

            elif chunk == CHUNK_XML_START_TAG:

                self._namespace_uri = self._reader.readInt()
                self._name = self._reader.readInt()
                self._reader.skipInt() # flags ?

                attribs_count = self._reader.readInt()
                self._id_attrib = (attribs_count >> 16) - 1
                attribs_count &= 0xffff

                self._class_attrib = self._reader.readInt()
                self._style_attrib = (self._class_attrib >> 16) - 1
                self._class_attrib = (self._class_attrib & 0xffff) - 1

                self._attributes = self._reader.readIntArray(attribs_count * ATTRIBUTE_LENGHT)

                for i in range(ATTRIBUTE_IX_VALUE_TYPE, len(self._attributes), ATTRIBUTE_LENGHT):
                    self._attributes[i] >>= 24

                self._namespaces.incDepth()
                self._event = START_TAG

                break

            elif chunk == CHUNK_XML_END_TAG:

                self._namespaceUri = self._reader.readInt()
                self._name = self._reader.readInt()

                self._event = END_TAG
                self._decreaseDepth = True

                break

            elif chunk == CHUNK_XML_TEXT:

                self._name = self._reader.readInt()
                self._reader.skipInt() # ???
                self._reader.skipInt() # ???

                self._event=TEXT

                break				

            else:
                raise Exception('Unknown chunck (0x%08lx)' % chunk)


    ### ESPACES ###

    def getNamespacePrefix(self, index):

        name = self._namespaces.getPrefix(index)

        if name == -1:
            return ''

        else:
            return self._strings.getRaw(name)


    def getNamespaceUri(self, index):

        name = self._namespaces.getUri(index)

        if name == -1:
            return ''

        else:
            return self._strings.getRaw(name)


    ### NAMES ###

    def getTagPrefix(self):
        """Provide the prefix linked to START_TAG or END_TAG."""

        name = self._namespaces.findPrefix(self._namespace_uri)

        if name == -1:
            return ''

        else:
            return self._strings.getRaw(name) + ':'


    def getTagName(self):
        """Provide the name linked to START_TAG or END_TAG."""

        if self._name == -1 or (self._event != START_TAG and self._event != END_TAG):
            raise Exception('Invalid tag name.')

        return self._strings.getRaw(self._name)


    def getText(self):
        """Provide the content linked to TEXT."""

        if self._name == -1 or self._event != START_TEXT:
            raise Exception('Invalid text content.')

        return self._strings.getRaw(self._name)


    ### ATRIBUTES ###

    def countAttributes(self):
        """Count the properties of the current tag."""

        if self._event != START_TAG:
            raise Exception('Invalid event.')

        return len(self._attributes) / ATTRIBUTE_LENGHT


    def getAttribPrefix(self, index):
        """Get the prefix of a given attribute."""

        index *= ATTRIBUTE_LENGHT

        if index >= len(self._attributes):
            raise Exception('Bad attribute index.')

        uri = self._attributes[index + ATTRIBUTE_IX_NAMESPACE_URI]
        name = self._namespaces.findPrefix(uri)

        if name == -1:
            return ''

        else:
            return self._strings.getRaw(name) + ':'


    def getAttribName(self, index):
        """Get the name of a given attribute."""

        index *= ATTRIBUTE_LENGHT

        if index >= len(self._attributes):
            raise Exception('Bad attribute index.')

        name = self._attributes[index + ATTRIBUTE_IX_NAME]

        if name == -1:
            return '???'

        else:
            return self._strings.getRaw(name)


    def getAttribValue(self, index):
        """Get the value of a given attribute."""

        index *= ATTRIBUTE_LENGHT

        if index >= len(self._attributes):
            raise Exception('Bad attribute index.')

        vtype = self._attributes[index + ATTRIBUTE_IX_VALUE_TYPE]
        vdata = self._attributes[index + ATTRIBUTE_IX_VALUE_DATA]

        if vtype == TYPE_NULL:
            return '???'

        elif vtype == TYPE_REFERENCE:
            return '@%s%08X' % (self.getPackage(vdata), vdata)

        elif vtype == TYPE_ATTRIBUTE:
            return '?%s%08X' % (self.getPackage(vdata), vdata)

        if vtype == TYPE_STRING:
            vdata = self._attributes[index + ATTRIBUTE_IX_VALUE_STRING]
            return self._strings.getRaw(vdata)

        elif vtype == TYPE_FLOAT:
            return '%f' % unpack('=f', pack('=L', vdata))[0] 

        elif vtype == TYPE_DIMENSION:
            return '%f%s' % (self.complexToFloat(vdata), DIMENSION_UNITS[vdata & COMPLEX_UNIT_MASK])

        elif vtype == TYPE_FRACTION:
            return '%f%s' % (self.complexToFloat(vdata), FRACTION_UNITS[vdata & COMPLEX_UNIT_MASK])

        elif vtype == TYPE_INT_HEX:
            return '0x%08x' % vdata

        elif vtype == TYPE_INT_BOOLEAN:
            if vdata == 0:
                return 'false'
            else:
                return 'true'

        elif vtype >= TYPE_FIRST_COLOR_INT and vtype <= TYPE_LAST_COLOR_INT:
            return '#%08x' % vdata

        elif vtype >= TYPE_FIRST_INT and vtype <= TYPE_LAST_INT:
            return str(vdata)

        return "<0x%x, 0x%02x>" % (vdata, vtype)


    def complexToFloat(self, xcomplex):
        return (float)(xcomplex & 0xffffff00) * RADIX_MULTS[(xcomplex >> 4) & 3];

    def getPackage(self, id):
        if id >> 24 == 1:
            return "android:"
        else:
            return ""