File: dex_parser.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (769 lines) | stat: -rwxr-xr-x 26,528 bytes parent folder | download | duplicates (10)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#!/usr/bin/env python3
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Utilities for optimistically parsing DEX files.

This file is not meant to provide a generic tool for analyzing DEX files.
A DexFile class that exposes access to several memory items in the DEX format
is provided, but it does not include error handling or validation.
"""

import abc
import argparse
import collections
import errno
import functools
import os
import re
import struct
import sys
import zipfile

import dalvik_bytecode
import stream_reader

# https://source.android.com/devices/tech/dalvik/dex-format#header-item
_DEX_HEADER_FMT = (
    ('magic', '8s'),
    ('checksum', 'I'),
    ('signature', '20s'),
    ('file_size', 'I'),
    ('header_size', 'I'),
    ('endian_tag', 'I'),
    ('link_size', 'I'),
    ('link_off', 'I'),
    ('map_off', 'I'),
    ('string_ids_size', 'I'),
    ('string_ids_off', 'I'),
    ('type_ids_size', 'I'),
    ('type_ids_off', 'I'),
    ('proto_ids_size', 'I'),
    ('proto_ids_off', 'I'),
    ('field_ids_size', 'I'),
    ('field_ids_off', 'I'),
    ('method_ids_size', 'I'),
    ('method_ids_off', 'I'),
    ('class_defs_size', 'I'),
    ('class_defs_off', 'I'),
    ('data_size', 'I'),
    ('data_off', 'I'),
)


class TypeCode:
  LOOKUP = {}

  def __init__(self, name, value):
    assert value not in TypeCode.LOOKUP
    self.name = name
    self.value = value
    TypeCode.LOOKUP[value] = self

# Full list of type codes:
# https://source.android.com/devices/tech/dalvik/dex-format#type-codes
_TYPE_HEADER_ITEM = TypeCode('HEADER', 0x0000)
_TYPE_STRING_ID_ITEM = TypeCode('STRING_ID', 0x0001)
_TYPE_TYPE_ID_ITEM = TypeCode('TYPE_ID', 0x0002)
_TYPE_PROTO_ID_ITEM = TypeCode('PROTO_ID', 0x0003)
_TYPE_FIELD_ID_ITEM = TypeCode('FIELD_ID', 0x0004)
_TYPE_METHOD_ID_ITEM = TypeCode('METHOD_ID', 0x0005)
_TYPE_CLASS_DEF_ITEM = TypeCode('CLASS_DEF', 0x0006)
_TYPE_CALL_SITE_ID_ITEM = TypeCode('CALL_SITE_ID', 0x0007)
_TYPE_METHOD_HANDLE_ITEM = TypeCode('METHOD_HANDLE', 0x0008)
_TYPE_MAP_LIST = TypeCode('MAP', 0x1000)
_TYPE_TYPE_LIST = TypeCode('TYPE', 0x1001)
_TYPE_ANNOTATION_SET_REF_LIST = TypeCode('ANNOTATION_SET_REF', 0x1002)
_TYPE_ANNOTATION_SET_ITEM = TypeCode('ANNOTATION_SET', 0x1003)
_TYPE_CLASS_DATA_ITEM = TypeCode('CLASS_DATA', 0x2000)
_TYPE_CODE_ITEM = TypeCode('CODE', 0x2001)
_TYPE_STRING_DATA_ITEM = TypeCode('STRING_DATA', 0x2002)
_TYPE_DEBUG_INFO_ITEM = TypeCode('DEBUG_INFO', 0x2003)
_TYPE_ANNOTATION_ITEM = TypeCode('ANNOTATION', 0x2004)
_TYPE_ENCODED_ARRAY_ITEM = TypeCode('ENCODED_ARRAY', 0x2005)
_TYPE_ANNOTATIONS_DIRECTORY_ITEM = TypeCode('ANNOTATIONS_DIRECTORY', 0x2006)
_TYPE_HIDDENAPI_CLASS_DATA_ITEM = TypeCode('HIDDENAPI_CLASS_DATA', 0xF000)

_CLASS_ACCESS_FLAGS = {
    0x1: 'public',
    0x2: 'private',
    0x4: 'protected',
    0x8: 'static',
    0x10: 'final',
    0x200: 'interface',
    0x400: 'abstract',
    0x1000: 'synthetic',
    0x2000: 'annotation',
    0x4000: 'enum',
}

DexHeader = collections.namedtuple('DexHeader',
                                   ','.join(t[0] for t in _DEX_HEADER_FMT))

# Simple memory items.
_StringDataItem = collections.namedtuple('StringDataItem',
                                         'utf16_size,data,byte_size')
_TypeIdItem = collections.namedtuple('TypeIdItem', 'descriptor_idx')
_ProtoIdItem = collections.namedtuple(
    'ProtoIdItem', 'shorty_idx,return_type_idx,parameters_off')
_FieldIdItem = collections.namedtuple('FieldIdItem',
                                      'class_idx,type_idx,name_idx')
_MethodIdItem = collections.namedtuple('MethodIdItem',
                                       'type_idx,proto_idx,name_idx')
_ClassDefItem = collections.namedtuple(
    'ClassDefItem',
    'class_idx,access_flags,superclass_idx,interfaces_off,source_file_idx,'
    'annotations_off,class_data_off,static_values_off')

_ClassDataItem = collections.namedtuple(
    'ClassDataItem',
    'offset,static_fields_size,instance_fields_size,direct_methods_size,'
    'virtual_methods_size,static_fields,instance_fields,direct_methods,'
    'virtual_methods')
_EncodedField = collections.namedtuple('EncodedField', 'field_idx,access_flags')
_EncodedMethod = collections.namedtuple('EncodedMethod',
                                        'method_idx,access_flags,code_off')

_TypeItem = collections.namedtuple('TypeItem', 'type_idx')
_CodeItem = collections.namedtuple(
    'CodeItem',
    'offset,registers_size,ins_size,outs_size,tries_size,debug_info_off,'
    'insns_size,insns,tries,handlers')
_TryItem = collections.namedtuple('TryItem',
                                  'start_addr,insn_count,handler_off')
_EncodedCatchHandlerList = collections.namedtuple('_EncodedCatchHandlerList',
                                                  'size,list')
_EncodedCatchHandler = collections.namedtuple('_EncodedCatchHandler',
                                              'size,handlers,catch_all_addr')
_EncodedTypeAddrPair = collections.namedtuple('_EncodedTypeAddrPair',
                                              'type_idx,addr')


class _DexReader(stream_reader.StreamReader):
  def NextDexHeader(self):
    header_fmt = '<' + ''.join(t[1] for t in _DEX_HEADER_FMT)
    return DexHeader._make(self.NextStruct(header_fmt))

  def NextClassDataItem(self):
    offset = self.Tell()
    static_fields_size = self.NextULeb128()
    instance_fields_size = self.NextULeb128()
    direct_methods_size = self.NextULeb128()
    virtual_methods_size = self.NextULeb128()
    static_fields = self.NextEncodedFieldList(static_fields_size)
    instance_fields = self.NextEncodedFieldList(instance_fields_size)
    direct_methods = self.NextEncodedMethodList(direct_methods_size)
    virtual_methods = self.NextEncodedMethodList(virtual_methods_size)
    return _ClassDataItem(offset, static_fields_size, instance_fields_size,
                          direct_methods_size, virtual_methods_size,
                          static_fields, instance_fields, direct_methods,
                          virtual_methods)

  def NextEncodedFieldList(self, count):
    ret = []
    field_idx = 0
    for _ in range(count):
      field_idx += self.NextULeb128()
      ret.append(_EncodedField(field_idx, self.NextULeb128()))
    return ret

  def NextEncodedMethodList(self, count):
    ret = []
    method_idx = 0
    for _ in range(count):
      method_idx += self.NextULeb128()
      ret.append(
          _EncodedMethod(method_idx, self.NextULeb128(), self.NextULeb128()))
    return ret

  def NextCodeItem(self):
    item_offset = self.Tell()
    registers_size = self.NextUShort()
    ins_size = self.NextUShort()
    outs_size = self.NextUShort()
    tries_size = self.NextUShort()
    debug_info_off = self.NextUInt()
    insns_size = self.NextUInt()
    insns = self.NextBytes(insns_size * 2)
    if tries_size > 0:
      self.AlignUpTo(4)
    tries = [self.NextTryItem() for _ in range(tries_size)]
    handlers = None
    if tries_size != 0:
      handlers = self.NextEncodedCatchHandlerList()
    self.AlignUpTo(4)
    return _CodeItem(item_offset, registers_size, ins_size, outs_size,
                     tries_size, debug_info_off, insns_size, insns, tries,
                     handlers)

  def NextTryItem(self):
    return _TryItem(self.NextUInt(), self.NextUShort(), self.NextUShort())

  def NextEncodedCatchHandlerList(self):
    size = self.NextULeb128()
    handler_list = [self.NextEncodedCatchHandler() for _ in range(size)]
    return _EncodedCatchHandlerList(size, handler_list)

  def NextEncodedCatchHandler(self):
    size = self.NextSLeb128()
    handlers = [self.NextEncodedTypeAddrPair() for _ in range(abs(size))]
    catch_all_addr = self.NextULeb128() if size <= 0 else None
    return _EncodedCatchHandler(size, handlers, catch_all_addr)

  def NextEncodedTypeAddrPair(self):
    return _EncodedTypeAddrPair(self.NextULeb128(), self.NextULeb128())


class _MemoryItemList:
  """Base class for repeated memory items."""

  def __init__(self,
               reader,
               offset,
               size,
               factory,
               alignment=None,
               first_item_offset=None):
    """Creates the item list using the specific item factory.

    Args:
      reader: _DexReader used for decoding the memory item.
      offset: Offset from start of the file to the item list, serving as the
        key for some item types.
      size: Number of memory items in the list.
      factory: Function to extract each memory item from a _DexReader.
      alignment: Optional integer specifying the alignment for the memory
        section represented by this list.
      first_item_offset: Optional, specifies a different offset to use for
        extracting memory items (default is to use offset).
    """
    self.offset = offset
    self.size = size
    if self.size > 0:
      reader.Seek(first_item_offset or offset)
      self._items = reader.NextList(size, factory)
    else:
      self._items = []
    if alignment:
      reader.AlignUpTo(alignment)

  def __iter__(self):
    return iter(self._items)

  def __getitem__(self, key):
    return self._items[key]

  def __len__(self):
    return len(self._items)

  def __repr__(self):
    item_type_part = ''
    if self.size != 0:
      item_type = type(self._items[0])
      item_type_part = ', item type={}'.format(item_type.__name__)

    return '{}(offset={:#x}, size={}{})'.format(
        type(self).__name__, self.offset, self.size, item_type_part)


class _StringItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    reader.Seek(offset)
    string_data_item_offsets = iter([reader.NextUInt() for _ in range(size)])

    def factory(x):
      start_pos = next(string_data_item_offsets)
      x.Seek(start_pos)
      string = x.NextString()
      byte_size = x.Tell() - start_pos
      return _StringDataItem(len(string), string, byte_size)

    super().__init__(reader, offset, size, factory)


class _TypeIdItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    factory = lambda x: _TypeIdItem(x.NextUInt())
    super().__init__(reader, offset, size, factory)


class _ProtoIdItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    factory = lambda x: _ProtoIdItem(x.NextUInt(), x.NextUInt(), x.NextUInt())
    super().__init__(reader, offset, size, factory)


class _FieldIdItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    factory = (
        lambda x: _FieldIdItem(x.NextUShort(), x.NextUShort(), x.NextUInt()))
    super().__init__(reader, offset, size, factory)


class _MethodIdItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    factory = (
        lambda x: _MethodIdItem(x.NextUShort(), x.NextUShort(), x.NextUInt()))
    super().__init__(reader, offset, size, factory)


class _ClassDefItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    reader.Seek(offset)

    def factory(x):
      return _ClassDefItem(*(x.NextUInt()
                             for _ in range(len(_ClassDefItem._fields))))

    super().__init__(reader, offset, size, factory)


class _ClassDataItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    reader.Seek(offset)
    super().__init__(reader, offset, size, lambda x: x.NextClassDataItem())


class _TypeListItem(_MemoryItemList):
  def __init__(self, reader):
    offset = reader.Tell()
    size = reader.NextUInt()
    factory = lambda x: _TypeItem(x.NextUShort())
    # This is necessary because we need to extract the size of the type list
    # (in other cases the list size is provided in the header).
    first_item_offset = reader.Tell()
    super().__init__(reader,
                     offset,
                     size,
                     factory,
                     alignment=4,
                     first_item_offset=first_item_offset)


class _TypeListItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    super().__init__(reader, offset, size, _TypeListItem)


class _CodeItemList(_MemoryItemList):
  def __init__(self, reader, offset, size):
    reader.Seek(offset)
    super().__init__(reader, offset, size, lambda x: x.NextCodeItem())


class _DexMapItem:
  def __init__(self, reader):
    if reader:
      self.type = reader.NextUShort()
      reader.NextUShort()  # Unused.
      self.size = reader.NextUInt()
      self.offset = reader.NextUInt()
    else:
      self.type = None
      self.size = 0
      self.offset = 0

  def GetName(self):
    type_code = TypeCode.LOOKUP.get(self.type)
    return type_code.name if type_code else ('(Type code %04x)' % self.type)

  def __repr__(self):
    return '_DexMapItem(type={}, size={}, offset={:#x})'.format(
        self.type, self.size, self.offset)


class _DexMapList:
  def __init__(self, reader, offset):
    self._map = {}
    reader.Seek(offset)
    self._size = reader.NextUInt()
    for _ in range(self._size):
      item = _DexMapItem(reader)
      self._map[item.type] = item

  def get(self, key):
    return self._map.get(key)

  def GetMapItemsSortedByOffset(self):
    return sorted(self._map.values(), key=lambda x: x.offset)

  def __repr__(self):
    return '_DexMapList(size={}, items={})'.format(self._size, self._map)


class DexFile:
  """Represents a single DEX file.

  Parses and exposes access to DEX file structure and contents, as described
  at https://source.android.com/devices/tech/dalvik/dex-format

  Fields:
    reader: _DexReader object used to decode DEX file contents.
    header: DexHeader for this DEX file.
    map_list: _DexMapList object containing list of DEX file contents.
    string_data_item_list: _StringItemList containing _StringDataItems that are
      referenced by index in other sections.
    type_id_item_list: _TypeIdItemList containing _TypeIdItems.
    proto_id_item_list: _ProtoIdItemList containing _ProtoIdItems.
    field_id_item_list: _FieldIdItemList containing _FieldIdItems.
    method_id_item_list: _MethodIdItemList containing _MethodIdItems.
    type_list_item_list: _TypeListItemList containing _TypeListItems.
      _TypeListItems are referenced by their offsets from other DEX items.
    class_def_item_list: _ClassDefItemList containing _ClassDefItems.
    class_data_item_list: _ClassDataItemList containing _ClassDataItems.
    code_item_list: _CodeItemList containing _CodeItems.
  """

  def __init__(self, data, is_eager=False):
    """Decodes dex file memory sections.

    Args:
      data: bytearray containing the contents of a DEX file.
      is_eager: Whether to parse |data| fully (slower) instead of lazy parsing.
    """
    self.size = len(data)
    self.reader = _DexReader(data)
    self.header = self.reader.NextDexHeader()
    self.map_list = _DexMapList(self.reader, self.header.map_off)

    # Data that require nontrivial processing are lazily initialized by default,
    # with support for eager initialization if desired.
    if is_eager:
      _ = self.string_data_item_list
      _ = self.type_id_item_list
      _ = self.proto_id_item_list
      _ = self.field_id_item_list
      _ = self.method_id_item_list
      _ = self.class_def_item_list
      _ = self.type_list_item_list
      _ = self.class_data_item_list
      _ = self.code_item_list
      _ = self._type_lists_by_offset
      _ = self._class_data_item_by_offset
      _ = self._code_item_by_offset

  @property
  @functools.lru_cache
  def string_data_item_list(self):
    return _StringItemList(self.reader, self.header.string_ids_off,
                           self.header.string_ids_size)

  @property
  @functools.lru_cache
  def type_id_item_list(self):
    return _TypeIdItemList(self.reader, self.header.type_ids_off,
                           self.header.type_ids_size)

  @property
  @functools.lru_cache
  def proto_id_item_list(self):
    return _ProtoIdItemList(self.reader, self.header.proto_ids_off,
                            self.header.proto_ids_size)

  @property
  @functools.lru_cache
  def field_id_item_list(self):
    return _FieldIdItemList(self.reader, self.header.field_ids_off,
                            self.header.field_ids_size)

  @property
  @functools.lru_cache
  def method_id_item_list(self):
    return _MethodIdItemList(self.reader, self.header.method_ids_off,
                             self.header.method_ids_size)

  @property
  @functools.lru_cache
  def class_def_item_list(self):
    return _ClassDefItemList(self.reader, self.header.class_defs_off,
                             self.header.class_defs_size)

  @property
  @functools.lru_cache
  def type_list_item_list(self):
    item = self.map_list.get(_TYPE_TYPE_LIST.value) or _DexMapItem(None)
    return _TypeListItemList(self.reader, item.offset, item.size)

  @property
  @functools.lru_cache
  def class_data_item_list(self):
    item = self.map_list.get(_TYPE_CLASS_DATA_ITEM.value) or _DexMapItem(None)
    return _ClassDataItemList(self.reader, item.offset, item.size)

  @property
  @functools.lru_cache
  def code_item_list(self):
    item = self.map_list.get(_TYPE_CODE_ITEM.value) or _DexMapItem(None)
    return _CodeItemList(self.reader, item.offset, item.size)

  @property
  @functools.lru_cache
  def _type_lists_by_offset(self):
    return {
        type_list.offset: type_list
        for type_list in self.type_list_item_list
    }

  @property
  @functools.lru_cache
  def _class_data_item_by_offset(self):
    return {
        class_data_item.offset: class_data_item
        for class_data_item in self.class_data_item_list
    }

  @property
  @functools.lru_cache
  def _code_item_by_offset(self):
    return {code_item.offset: code_item for code_item in self.code_item_list}

  def ComputeMapItemSizes(self):
    """Returns map item offsets and sizes.

    Returns: A list of dicts containing offsets and sizes of each DEX map item
      (i.e., DEX section). Fields:
      * name: Simplified item type code name (FOO = TYPE_FOO_ITEM).
      * offset: Byte offset of map item.
      * size: Number of elements in map item.
      * byte_size: Number of bytes spanned by map item, estimated by subtracting
        element offsets (and file length for the last map item). Therefore this
        also includes alignment paddings.
    """
    map_items = self.map_list.GetMapItemsSortedByOffset()
    n = len(map_items)
    ret = []
    for i in range(n):
      end = self.size if i + 1 >= n else map_items[i + 1].offset
      item = map_items[i]
      ret.append({
          'name': item.GetName(),
          'offset': item.offset,
          'size': item.size,
          'byte_size': end - item.offset
      })
    return ret

  def GetString(self, string_idx):
    string_data_item = self.string_data_item_list[string_idx]
    return string_data_item.data

  def GetTypeString(self, type_idx):
    type_id_item = self.type_id_item_list[type_idx]
    return self.GetString(type_id_item.descriptor_idx)

  def GetTypeListStringsByOffset(self, offset):
    if not offset:
      return ()
    type_list = self._type_lists_by_offset[offset]
    return tuple(self.GetTypeString(item.type_idx) for item in type_list)

  def GetClassDataItemByOffset(self, offset):
    return self._class_data_item_by_offset.get(offset)

  def GetCodeItemByOffset(self, offset):
    return self._code_item_by_offset.get(offset)

  def IterAllStringIdsUsedByCodeItem(self, code_item):
    if not code_item:
      return
    for bytecode in dalvik_bytecode.Split(code_item.insns):
      if bytecode[0] in (0x1a, 0x1b):
        # 1a 21c  const-string vAA, string@BBBB
        # 1b 31c  const-string/jumbo vAA, string@BBBBBBBB
        fmt = '<H' if bytecode[0] == 0x1a else '<L'
        yield struct.unpack_from(fmt, bytecode, 2)[0]

  @staticmethod
  def ResolveClassAccessFlags(access_flags):
    return tuple(flag_string
                 for flag, flag_string in _CLASS_ACCESS_FLAGS.items()
                 if flag & access_flags)

  def IterMethodSignatureParts(self):
    """Yields the string components of DEX methods in a DEX file.

    Yields:
      Tuples that look like:
        (class name, return type, method name, (parameter type, ...)).
    """
    for method_id_item in self.method_id_item_list:
      class_name = self.GetTypeString(method_id_item.type_idx)
      method_name = self.GetString(method_id_item.name_idx)
      proto_id_item = self.proto_id_item_list[method_id_item.proto_idx]
      return_type_name = self.GetTypeString(proto_id_item.return_type_idx)
      parameter_types = self.GetTypeListStringsByOffset(
          proto_id_item.parameters_off)
      yield (class_name, return_type_name, method_name, parameter_types)

  def __repr__(self):
    items = [
        self.header,
        self.map_list,
        self.string_data_item_list,
        self.type_id_item_list,
        self.proto_id_item_list,
        self.field_id_item_list,
        self.method_id_item_list,
        self.type_list_item_list,
        self.class_def_item_list,
        self.class_data_item_list,
        self.code_item_list,
    ]
    return '\n'.join(str(item) for item in items)


class _DumpCommand:
  def __init__(self, dexfile):
    self._dexfile = dexfile

  @abc.abstractmethod
  def Run(self):
    pass


class _DumpSummary(_DumpCommand):
  def Run(self):
    print(self._dexfile)


class _DumpStrings(_DumpCommand):
  def Run(self):
    for (i, string_data_item) in enumerate(self._dexfile.string_data_item_list):
      # Some strings are likely to be non-ascii (vs. methods/classes).
      s = string_data_item.data
      rep_str = (repr(s) if s.isprintable() else s.encode(
          'utf-8', errors='surrogatepass'))
      print('string(%08X): %s' % (i, rep_str))


class _DumpMap(_DumpCommand):
  def Run(self):
    for item_info in self._dexfile.ComputeMapItemSizes():
      print(item_info)


class _DumpFields(_DumpCommand):
  def Run(self):
    dexfile = self._dexfile
    for (i, field_id_item) in enumerate(dexfile.field_id_item_list):
      class_name = dexfile.GetTypeString(field_id_item.class_idx)
      type_name = dexfile.GetTypeString(field_id_item.type_idx)
      name = dexfile.GetString(field_id_item.name_idx)
      print('field(%08x): (class=%s, name=%s, type=%s)' %
            (i, class_name, name, type_name))


class _DumpMethods(_DumpCommand):
  def Run(self):
    for (i, parts) in enumerate(self._dexfile.IterMethodSignatureParts()):
      class_name, return_type_name, method_name, parameter_types = parts
      print('method(%08x): (class=%s, name=%s, return_type=%s, params=%s)' %
            (i, class_name, method_name, return_type_name, parameter_types))


class _DumpClasses(_DumpCommand):
  def Run(self):
    dexfile = self._dexfile
    fmt = ('class(%08x): (name=%s, superclass=%s, interfaces=%s, ' +
           'access_flags=%s)')
    for (i, class_item) in enumerate(dexfile.class_def_item_list):
      name = dexfile.GetTypeString(class_item.class_idx)
      superclass_name = dexfile.GetTypeString(class_item.superclass_idx)
      interfaces = dexfile.GetTypeListStringsByOffset(class_item.interfaces_off)
      access_flags = DexFile.ResolveClassAccessFlags(class_item.access_flags)
      print(fmt % (i, name, superclass_name, interfaces, access_flags))


class _DumpCodes(_DumpCommand):
  def Run(self):
    dexfile = self._dexfile
    total_insns_bytes = 0
    total_insns_count = 0
    total_code_bytes = 0
    total_tries_count = 0
    insns_popu = [0] * 256
    attrs = [
        'registers_size', 'ins_size', 'outs_size', 'tries_size', 'insns_size'
    ]
    for i, code_item in enumerate(dexfile.code_item_list):
      total_insns_bytes += len(code_item.insns)
      total_tries_count += code_item.tries_size
      insns_count = 0
      actual_insns_bytes = 0
      for bytecode in dalvik_bytecode.Split(code_item.insns):
        insns_popu[bytecode[0]] += 1
        insns_count += 1
        actual_insns_bytes += len(bytecode)
      total_insns_count += insns_count
      total_code_bytes += actual_insns_bytes
      details = ', '.join('{}={}'.format(a, getattr(code_item, a))
                          for a in attrs if getattr(code_item, a) > 0)
      print('code(%08x) o:%08X: (%s)' % (i, code_item.offset, details))
    total_data_bytes = total_insns_bytes - total_code_bytes

    print('\n*** Summary ***')
    print('Code item count:         %d' % len(dexfile.code_item_list))
    print('Total instruction count: %d' % total_insns_count)
    print('Total instruction bytes: %d' % total_insns_bytes)
    print('  Code: %d' % total_code_bytes)
    print('  Data: %d' % total_data_bytes)
    print('Total tries count:       %d' % total_tries_count)
    print('Total tries bytes:       %d' % (total_tries_count * (4 + 2 + 2)))


def _DumpDexItems(dexfile_data, name, item, is_eager):
  dexfile = DexFile(data=bytearray(dexfile_data), is_eager=is_eager)
  print('dex_parser: Dumping {} for {}'.format(item, name))
  cmds = {
      'summary': _DumpSummary,
      'map': _DumpMap,
      'strings': _DumpStrings,
      'fields': _DumpFields,
      'methods': _DumpMethods,
      'classes': _DumpClasses,
      'codes': _DumpCodes,
  }
  try:
    cmds[item](dexfile).Run()
  except IOError as e:
    if e.errno == errno.EPIPE:
      # Assume we're piping to "less", do nothing.
      pass


def main():
  parser = argparse.ArgumentParser(description='Dump DEX contents to stdout.')
  parser.add_argument('input',
                      help='Input (.dex, .jar, .zip, .aab, .apk) file path.')
  parser.add_argument('item',
                      choices=('summary', 'map', 'strings', 'fields', 'methods',
                               'classes', 'codes'),
                      help='Item to dump',
                      nargs='?',
                      default='summary')
  parser.add_argument('--eager', action='store_true')
  args = parser.parse_args()

  if args.item == 'summary':
    args.eager = True

  if os.path.splitext(args.input)[1] in ('.apk', '.jar', '.zip', '.aab'):
    with zipfile.ZipFile(args.input) as z:
      dex_file_paths = [
          f for f in z.namelist() if re.match(r'.*classes[0-9]*\.dex$', f)
      ]
      if not dex_file_paths:
        print('Error: {} does not contain any classes.dex files'.format(
            args.input))
        sys.exit(1)

      for path in dex_file_paths:
        _DumpDexItems(z.read(path), path, args.item, args.eager)

  else:
    with open(args.input, 'rb') as f:
      _DumpDexItems(f.read(), args.input, args.item, args.eager)


if __name__ == '__main__':
  main()