Class: SmaliInput

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/dex-oracle/smali_input.rb

Constant Summary collapse

DEX_MAGIC =
[0x64, 0x65, 0x78].freeze
PK_ZIP_MAGIC =
[0x50, 0x4b, 0x3].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, logger, #logger, logger=

Constructor Details

#initialize(input) ⇒ SmaliInput

Returns a new instance of SmaliInput.



13
14
15
# File 'lib/dex-oracle/smali_input.rb', line 13

def initialize(input)
  prepare(input)
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



6
7
8
# File 'lib/dex-oracle/smali_input.rb', line 6

def dir
  @dir
end

#out_apkObject (readonly)

Returns the value of attribute out_apk.



6
7
8
# File 'lib/dex-oracle/smali_input.rb', line 6

def out_apk
  @out_apk
end

#out_dexObject (readonly)

Returns the value of attribute out_dex.



6
7
8
# File 'lib/dex-oracle/smali_input.rb', line 6

def out_dex
  @out_dex
end

#temp_dexObject (readonly)

Returns the value of attribute temp_dex.



6
7
8
# File 'lib/dex-oracle/smali_input.rb', line 6

def temp_dex
  @temp_dex
end

#temp_dirObject (readonly)

Returns the value of attribute temp_dir.



6
7
8
# File 'lib/dex-oracle/smali_input.rb', line 6

def temp_dir
  @temp_dir
end

Class Method Details

.compile(dir, out_dex = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/dex-oracle/smali_input.rb', line 24

def self.compile(dir, out_dex = nil)
  raise 'Smali could not be found on the path.' if Utility.which('smali').nil?
  out_dex = Tempfile.new(%w(oracle .dex)) if out_dex.nil?
  logger.info("Compiling DEX #{out_dex.path} ...")
  exit_code = SmaliInput.exec("smali #{dir} -o #{out_dex.path}")
  # Remember kids, if you make a CLI, exit with non-zero status for failures
  raise 'Crap, smali compilation failed.' if $CHILD_STATUS.exitstatus != 0
  out_dex
end

.exec(cmd) ⇒ Object



43
44
45
# File 'lib/dex-oracle/smali_input.rb', line 43

def self.exec(cmd)
  `#{cmd}`
end

.extract_dex(apk, out_dex) ⇒ Object



39
40
41
# File 'lib/dex-oracle/smali_input.rb', line 39

def self.extract_dex(apk, out_dex)
  Utility.extract_file(apk, 'classes.dex', out_dex)
end

.update_apk(dir, out_apk) ⇒ Object



34
35
36
37
# File 'lib/dex-oracle/smali_input.rb', line 34

def self.update_apk(dir, out_apk)
  out_dex = compile(dir)
  Utility.update_zip(out_apk, 'classes.dex', out_dex)
end

Instance Method Details

#finishObject



17
18
19
20
21
22
# File 'lib/dex-oracle/smali_input.rb', line 17

def finish
  SmaliInput.update_apk(dir, @out_apk) if @out_apk
  SmaliInput.compile(dir, @out_dex) if @out_dex && !@out_apk
  FileUtils.rm_rf(@dir) if @temp_dir
  FileUtils.rm_rf(@out_dex) if @temp_dex
end