node/lib/metasm/metasm/decompile.rb
# rename local variables from subfunc arg names
rename local variables from subfunc arg names
rename local variables from subfunc arg names
def rename_variables(scope)
		funcs = []
		cntrs = []
		cmpi = []
		walk_ce(scope) { |ce|
			funcs << ce if ce.op == :funcall
			cntrs << (ce.lexpr || ce.rexpr) if ce.op == :'++'
			cmpi << ce.lexpr if [:<, :>, :<=, :>=, :==, :'!='].include? ce.op and ce.rexpr.kind_of? C::CExpression and ce.rexpr.rexpr.kind_of? ::Integer
		}
		rename = lambda { |var, name|
			var = var.rexpr if var.kind_of? C::CExpression and not var.op
			next if not var.kind_of? C::Variable or not scope.symbol[var.name] or not name
			next if (var.name !~ /^(var|arg)_/ and not var.storage == :register) or not scope.symbol[var.name] or name =~ /^(var|arg)_/
			s = scope.symbol_ancestors
			n = name
			i = 0
			n = name + "#{i+=1}" while s[n]
			scope.symbol[n] = scope.symbol.delete(var.name)
			var.name = n
		}
		funcs.each { |ce|
			next if not ce.lexpr.kind_of? C::Variable or not ce.lexpr.type.kind_of? C::Function
			ce.rexpr.to_a.zip(ce.lexpr.type.args.to_a).each { |a, fa| rename[a, fa.name] if fa }
		}
		funcs.each { |ce|
			next if not ce.lexpr.kind_of? C::Variable or not ce.lexpr.type.kind_of? C::Function
			ce.rexpr.to_a.zip(ce.lexpr.type.args.to_a).each { |a, fa|
				next if not a.kind_of? C::CExpression or a.op != :& or a.lexpr
				next if not fa or not fa.name
				rename[a.rexpr, fa.name.sub(/^l?p/, '')]
			}
		}
		(cntrs & cmpi).each { |v| rename[v, 'cntr'] }
	end
def rename_variables ( scope ) funcs = [ ] cntrs = [ ] cmpi = [ ] walk_ce ( scope ) { | ce | funcs << ce if ce . op == :funcall cntrs << ( ce . lexpr || ce . rexpr ) if ce . op == :' ++ ' cmpi << ce . lexpr if [ :< , :> , :<= , :>= , :== , :' != ' ] . include? ce . op and ce . rexpr . kind_of? C :: CExpression and ce . rexpr . rexpr . kind_of? :: Integer } rename = lambda { | var , name | var = var . rexpr if var . kind_of? C :: CExpression and not var . op next if not var . kind_of? C :: Variable or not scope . symbol [ var . name ] or not name next if ( var . name !~ / ^(var|arg)_ / and not var . storage == :register ) or not scope . symbol [ var . name ] or name =~ / ^(var|arg)_ / s = scope . symbol_ancestors n = name i = 0 n = name + "#{i+=1}" while s [ n ] scope . symbol [ n ] = scope . symbol . delete ( var . name ) var . name = n } funcs . each { | ce | next if not ce . lexpr . kind_of? C :: Variable or not ce . lexpr . type . kind_of? C :: Function ce . rexpr . to_a . zip ( ce . lexpr . type . args . to_a ) . each { | a , fa | rename [ a , fa . name ] if fa } } funcs . each { | ce | next if not ce . lexpr . kind_of? C :: Variable or not ce . lexpr . type . kind_of? C :: Function ce . rexpr . to_a . zip ( ce . lexpr . type . args . to_a ) . each { | a , fa | next if not a . kind_of? C :: CExpression or a . op != :& or a . lexpr next if not fa or not fa . name rename [ a . rexpr , fa . name . sub ( / ^l?p / , '' ) ] } } ( cntrs & cmpi ) . each { | v | rename [ v , 'cntr' ] } end
rename local variables from subfunc arg names
rename local variables from subfunc arg names
scope