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



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

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:



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

def classes
  @classes
end

#dataString (readonly)

Returns dex binary data.

Returns:

  • (String)

    dex binary data



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

def data
  @data
end

#field_idsObject (readonly)

Returns the value of attribute field_ids.



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

def field_ids
  @field_ids
end

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

Returns dex header information.

Returns:

  • (Dex::Header)

    dex header information



14
15
16
# File 'lib/android/dex.rb', line 14

def header
  @header
end

#method_idsObject (readonly)

Returns the value of attribute method_ids.



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

def method_ids
  @method_ids
end

#proto_idsObject (readonly)

Returns the value of attribute proto_ids.



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

def proto_ids
  @proto_ids
end

#stringsArray<String> (readonly)

strings in dex file.

Returns:

  • (Array<String>)

    the current value of strings



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

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



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

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:



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

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



26
27
28
29
# File 'lib/android/dex/utils.rb', line 26

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

Instance Method Details

#inspectObject



35
36
37
# File 'lib/android/dex.rb', line 35

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

#type_resolve(typeid) ⇒ Object



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

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