# This file is part of Metasm, the Ruby assembly manipulation suite # Copyright (C) 2007 Yoann GUILLOT # # Licence is LGPL, see LICENCE in the top-level directory # # this exemple illustrates the use of the cparser/preprocessor #factorize functionnality: # it generates code that references to the functions imported by a windows executable, and # factorizes the windows headers through them # usage: factorize-imports.rb [...] # require 'metasm' include Metasm ddk = ARGV.delete('--ddk') ? 1 : 0 pe = PE.decode_file_header(ARGV.shift) pe.decode_imports funcnames = pe.imports.map { |id| id.imports.map { |i| i.name } }.flatten.compact.uniq.sort raise 'need a path to the headers' if not visualstudiopath = ARGV.shift ARGV.each { |n| if n[0] == ?- funcnames.delete n[1..-1] else funcnames |= [n] end } src = < #include #else #define WIN32_LEAN_AND_MEAN #include #include #endif void *fnptr[] = { #{funcnames.map { |f| '&'+f }.join(', ')} }; EOS puts src if $DEBUG puts Ia32.new.new_cparser.factorize(src)