Commit 4a26581f by Daniel Mendler

fix magic matching for 1.9.3

parent 143dd14e
*.swp
*.gem
Gemfile.lock
.bundle
.yardoc
...@@ -79,10 +79,11 @@ class MimeMagic ...@@ -79,10 +79,11 @@ class MimeMagic
mime = mime =
unless io.respond_to?(:seek) && io.respond_to?(:read) unless io.respond_to?(:seek) && io.respond_to?(:read)
str = io.respond_to?(:read) ? io.read : io.to_s 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) } MAGIC.find {|type, matches| magic_match_str(str, matches) }
else else
io.binmode io.binmode
io.set_encoding(Encoding::BINARY, Encoding::BINARY) if io.respond_to?(:set_encoding)
MAGIC.find {|type, matches| magic_match_io(io, matches) } MAGIC.find {|type, matches| magic_match_io(io, matches) }
end end
mime && new(mime[0]) mime && new(mime[0])
...@@ -113,29 +114,27 @@ class MimeMagic ...@@ -113,29 +114,27 @@ class MimeMagic
match = match =
if Range === offset if Range === offset
io.seek(offset.begin) 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 else
io.seek(offset) io.seek(offset)
io.read(value.length) == value io.read(value.bytesize) == value
end end
match && (!children || magic_match_io(io, children)) match && (!children || magic_match_io(io, children))
end end
rescue
false
end end
def self.magic_match_str(str, matches) def self.magic_match_str(str, matches)
matches.any? do |offset, value, children| matches.any? do |offset, value, children|
match = match =
if Range === offset 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 else
str[offset, value.length] == value str[offset, value.bytesize] == value
end end
match && (!children || magic_match_str(str, children)) match && (!children || magic_match_str(str, children))
end end
rescue
false
end end
private_class_method :magic_match_io, :magic_match_str private_class_method :magic_match_io, :magic_match_str
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment