Class: Android::Dex

Inherits:
Object
  • Object
show all
Defined in:
lib/android/dex.rb,
lib/android/dex/info.rb,
lib/android/dex/utils.rb,
lib/android/dex/dex_object.rb,
lib/android/dex/access_flag.rb

Overview

parsed dex object

Defined Under Namespace

Classes: AccessFlag, ClassAccessFlag, ClassInfo, DexObject, FieldInfo, MethodAccessFlag, MethodInfo

Constant Summary collapse

TYPE_DESCRIPTOR =
{ 
  'V' => 'void',
  'Z' => 'boolean',
  'B' => 'byte',
  'S' => 'short',
  'C' => 'short',
  'I' => 'int',
  'J' => 'long',
  'F' => 'float',
  'D' => 'double'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Dex

Returns a new instance of Dex.

Parameters:

  • data (String)

    dex binary data



22
23
24
25
26
27
# File 'lib/android/dex.rb', line 22

def initialize(data)
  @data = data
  @data.force_encoding(Encoding::ASCII_8BIT)
  @classes = []
  parse()
end

Instance Attribute Details

#classesArray<Dex::ClassInfo> (readonly)

Returns array of class information.

Returns:



18
19
20
# File 'lib/android/dex.rb', line 18

def classes
  @classes
end

#dataString (readonly)

Returns dex binary data.

Returns:

  • (String)

    dex binary data



16
17
18
# File 'lib/android/dex.rb', line 16

def data
  @data
end

#field_idsObject (readonly)

Returns the value of attribute field_ids.



20
21
22
# File 'lib/android/dex.rb', line 20

def field_ids
  @field_ids
end

#headerDex::Header (readonly) Also known as: h

Returns dex header information.

Returns:

  • (Dex::Header)

    dex header information



12
13
14
# File 'lib/android/dex.rb', line 12

def header
  @header
end

#method_idsObject (readonly)

Returns the value of attribute method_ids.



20
21
22
# File 'lib/android/dex.rb', line 20

def method_ids
  @method_ids
end

#proto_idsObject (readonly)

Returns the value of attribute proto_ids.



20
21
22
# File 'lib/android/dex.rb', line 20

def proto_ids
  @proto_ids
end

#stringsArray<String> (readonly)

strings in dex file.

Returns:

  • (Array<String>)

    the current value of strings



10
11
12
# File 'lib/android/dex.rb', line 10

def strings
  @strings
end

Class Method Details

.sleb128(data, offset = 0) ⇒ Integer

parse sleb128(signed integer) data

Parameters:

  • data (String)

    target byte data

  • offset (Integer) (defaults to: 0)

Returns:

  • (Integer, Integer)

    parsed value and parsed byte length



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/android/dex/utils.rb', line 32

def sleb128(data, offset=0)
  result  = 0
  shift = 0
  d = data[offset...data.size]
  (0..4).each do |i|
    byte  = d.getbyte(i)
    result  |=((byte & 0x7F) << shift)
    return (0 == (byte & 0x40) ? result : result - (1 << (shift+7))), i+1 if ((byte & 0x80) == 0)
    shift += 7
  end
end

.uleb128(data, offset = 0) ⇒ Integer

parse uleb128(unsigned integer) data

Parameters:

  • data (String)

    target byte data

  • offset (Integer) (defaults to: 0)

Returns:

  • (Integer, Integer)

    parsed value and parsed byte length

See Also:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/android/dex/utils.rb', line 9

def uleb128(data, offset=0)
  result = 0
  shift = 0
  d = data[offset...data.size]
  (0..4).each do |i|
    byte = d.getbyte(i)
    result |= ((byte & 0x7f) << shift)
    return result, i+1 if ((byte & 0x80) == 0)
    shift += 7
  end
end

.uleb128p1(data, offset = 0) ⇒ Integer

parse uleb128 + 1 data

Parameters:

  • data (String)

    target byte data

  • offset (Integer) (defaults to: 0)

Returns:

  • (Integer, Integer)

    parsed value and parsed byte length



24
25
26
27
# File 'lib/android/dex/utils.rb', line 24

def uleb128p1(data, offset=0)
  ret, len = self.uleb128(data, offset)
  return (ret - 1), len
end

Instance Method Details

#inspectObject



33
34
35
# File 'lib/android/dex.rb', line 33

def inspect
  "<Android::Dex @classes => #{@classes.size}, datasize => #{@data.size}>"
end

#type_resolve(typeid) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/android/dex.rb', line 52

def type_resolve(typeid)
  type = strings[@type_ids[typeid]]
  if type.start_with? '['
    type = type[1..type.size]
    return TYPE_DESCRIPTOR.fetch(type, type) + "[]" # TODO: recursive
  else
    return TYPE_DESCRIPTOR.fetch(type, type)
  end
end