Skip to content

Commit

Permalink
refactor file type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
zed-0xff committed Jul 27, 2020
1 parent c3e5820 commit 3c34425
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
60 changes: 37 additions & 23 deletions lib/pedump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,26 +320,6 @@ def self.quiet
@@logger.level = oldlevel
end

def supported_file? f=@io
pos = f.tell
sig = f.read(2)
f.seek(pos)
if SUPPORTED_SIGNATURES.include?(sig)
true
else
unless @not_supported_sig_warned
msg = "no supported signature. want: #{SUPPORTED_SIGNATURES.join("/")}, got: #{sig.inspect}"
if @force
logger.warn "[?] #{msg}"
else
logger.error "[!] #{msg}. (not forced)"
end
@not_supported_sig_warned = true
end
false
end
end

def mz f=@io
@mz ||= f && MZ.read(f).tap do |mz|
if mz.signature != 'MZ' && mz.signature != 'ZM'
Expand Down Expand Up @@ -482,12 +462,46 @@ def sections f=@io
end
alias :section_table :sections

def ne?
@pe ? false : (@ne ? true : (pe ? false : (ne ? true : false)))
def supported_file? f=@io
pos = f.tell
sig = f.read(2)
f.seek(pos)
if SUPPORTED_SIGNATURES.include?(sig)
true
else
unless @not_supported_sig_warned
msg = "no supported signature. want: #{SUPPORTED_SIGNATURES.join("/")}, got: #{sig.inspect}"
if @force
logger.warn "[?] #{msg}"
else
logger.error "[!] #{msg}. (not forced)"
end
@not_supported_sig_warned = true
end
false
end
end

def _detect_format
return :pe if @pe
return :ne if @ne
return :te if @te
return :pe if pe()
return :ne if ne()
return :te if te()
nil
end

def pe?
@pe ? true : (@ne ? false : (pe ? true : false ))
_detect_format() == :pe
end

def ne?
_detect_format() == :ne
end

def te?
_detect_format() == :te
end

##############################################################################
Expand Down
5 changes: 5 additions & 0 deletions spec/te_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@

te.sections.size.should == 3
end
it "should be TE and not PE or NE" do
sample.should be_te
sample.should_not be_pe
sample.should_not be_ne
end
end

0 comments on commit 3c34425

Please sign in to comment.