Skip to content

Commit

Permalink
fix magic matching for 1.9.3
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Jul 29, 2013
1 parent 143dd14 commit 4a26581
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.swp
*.gem
Gemfile.lock
.bundle
.yardoc
17 changes: 8 additions & 9 deletions lib/mimemagic.rb
Expand Up @@ -79,10 +79,11 @@ def self.by_magic(io)
mime =
unless io.respond_to?(:seek) && io.respond_to?(:read)
str = io.respond_to?(:read) ? io.read : io.to_s
str = str.b if str.respond_to? :b
str = str.force_encoding(Encoding::BINARY) if str.respond_to? :force_encoding
MAGIC.find {|type, matches| magic_match_str(str, matches) }
else
io.binmode
io.set_encoding(Encoding::BINARY, Encoding::BINARY) if io.respond_to?(:set_encoding)
MAGIC.find {|type, matches| magic_match_io(io, matches) }
end
mime && new(mime[0])
Expand Down Expand Up @@ -113,29 +114,27 @@ def self.magic_match_io(io, matches)
match =
if Range === offset
io.seek(offset.begin)
io.read(offset.end - offset.begin + value.length).include?(value)
x = io.read(offset.end - offset.begin + value.bytesize)
x && x.include?(value)
else
io.seek(offset)
io.read(value.length) == value
io.read(value.bytesize) == value
end
match && (!children || magic_match_io(io, children))
end
rescue
false
end

def self.magic_match_str(str, matches)
matches.any? do |offset, value, children|
match =
if Range === offset
str[offset.begin, offset.end - offset.begin + value.length].include?(value)
x = str[offset.begin, offset.end - offset.begin + value.bytesize]
x && x.include?(value)
else
str[offset, value.length] == value
str[offset, value.bytesize] == value
end
match && (!children || magic_match_str(str, children))
end
rescue
false
end

private_class_method :magic_match_io, :magic_match_str
Expand Down

0 comments on commit 4a26581

Please sign in to comment.