pax_global_header00006660000000000000000000000064116351133660014517gustar00rootroot0000000000000052 comment=7cf3fecdcb43684c3f772ff2d5c72ccb8c66ce8e ruby-magic-0.2.6/000077500000000000000000000000001163511336600135635ustar00rootroot00000000000000ruby-magic-0.2.6/LICENSE000066400000000000000000000020401163511336600145640ustar00rootroot00000000000000Copyright (c) 2010 Jakub Kuźma Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ruby-magic-0.2.6/README.rdoc000066400000000000000000000013501163511336600153700ustar00rootroot00000000000000= magic Ruby FFI wrapper to the "magic" library, that determines content type and encoding of files and strings. The library does three types of tests: filesystem tests, magic number tests, and language tests. The first test that succeeds causes the file type to be returned. == Installation and Usage See {project page}[http://jah.pl/projects/magic.html] for details. == Links * {project page}[http://jah.pl/projects/magic.html] * gemcutter[http://gemcutter.org/gems/magic] * repository[http://github.com/qoobaa/magic] * {issue tracker}[http://github.com/qoobaa/magic/issues] * rdoc[http://qoobaa.github.com/magic] == Copyright Copyright (c) 2010 Jakub Kuźma. See LICENSE[http://github.com/qoobaa/magic/raw/master/LICENSE] for details. ruby-magic-0.2.6/Rakefile000066400000000000000000000016441163511336600152350ustar00rootroot00000000000000$:.unshift File.expand_path("../lib", __FILE__) require "rubygems" require "rubygems/specification" require "rake/testtask" require "rake/rdoctask" require "rake/gempackagetask" require "magic" def gemspec file = File.expand_path('../magic.gemspec', __FILE__) eval(File.read(file), binding, file) end Rake::TestTask.new(:test) do |test| test.libs << "lib" << "test" test.pattern = "test/**/test_*.rb" test.verbose = true end Rake::RDocTask.new do |rdoc| rdoc.rdoc_dir = "rdoc" rdoc.title = "magic #{Magic::VERSION}" rdoc.rdoc_files.include("README.rdoc") rdoc.rdoc_files.include("lib/**/*.rb") end Rake::GemPackageTask.new(gemspec) do |pkg| pkg.gem_spec = gemspec end desc "Install the gem locally" task :install => :package do sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}} end desc "Validate the gemspec" task :gemspec do gemspec.validate end task :gem => :gemspec task :default => :test ruby-magic-0.2.6/lib/000077500000000000000000000000001163511336600143315ustar00rootroot00000000000000ruby-magic-0.2.6/lib/magic.rb000066400000000000000000000041271163511336600157420ustar00rootroot00000000000000# encoding: utf-8 require "ffi" require "magic/errors" require "magic/api" require "magic/constants" require "magic/database" require "magic/version" module Magic class << self # Guesses mime of given file # # ====== Example # Magic.guess_file_mime("public/images/rails.png") # # => "image/png; charset=binary" def guess_file_mime(filename, *args) guess(*args.unshift(:mime)) { |db| db.file(filename) } end # Guesses mime encoding of given file # # ===== Example # Magic.guess_file_mime_encoding("public/images/rails.png") # # => "binary" def guess_file_mime_encoding(filename, *args) guess(*args.unshift(:mime_encoding)) { |db| db.file(filename) } end # Guesses mime type of given file # # ===== Example # Magic.guess_file_mime_type("public/images/rails.png") # # => "image/png" def guess_file_mime_type(filename, *args) guess(*args.unshift(:mime_type)) { |db| db.file(filename) } end # Guesses mime type of given string # # ===== Example # Magic.guess_string_mime("Magic® File™") # # => "text/plain; charset=utf-8" def guess_string_mime(string, *args) guess(*args.unshift(:mime)) { |db| db.buffer(string) } end # Guesses mime type of given string # # ===== Example # Magic.guess_string_mime_encoding("Magic® File™") # # => "utf-8" def guess_string_mime_encoding(string, *args) guess(*args.unshift(:mime_type)) { |db| db.buffer(string) } end # Guesses mime type of given string # # ===== Example # Magic.guess_string_mime_type("Magic® File™") # # => "text/plain" def guess_string_mime_type(string, *args) guess(*args.unshift(:mime_type)) { |db| db.buffer(string) } end # Creates magic database and yields it to the given block # # ===== Example # Magic.guess(:mime) { |db| db.buffer("Magic® File™") } # # => "text/plain; charset=utf-8" def guess(*args) db = Database.new(*args) result = yield(db) db.close result end end end ruby-magic-0.2.6/lib/magic/000077500000000000000000000000001163511336600154115ustar00rootroot00000000000000ruby-magic-0.2.6/lib/magic/api.rb000066400000000000000000000016531163511336600165140ustar00rootroot00000000000000module Magic module Api #:nodoc: extend FFI::Library lib_paths = Array(ENV["MAGIC_LIB"] || Dir["/{opt,usr}/{,local/}lib{,64}/libmagic.{1.dylib,so.1*}"]) fallback_names = %w(libmagic.1.dylib libmagic.so.1 magic1.dll) ffi_lib(lib_paths + fallback_names) attach_function :magic_open, [:int], :pointer attach_function :magic_close, [:pointer], :void attach_function :magic_file, [:pointer, :string], :pointer # attach_function :magic_descriptor, [:pointer, :int], :string attach_function :magic_buffer, [:pointer, :pointer, :uint], :pointer attach_function :magic_error, [:pointer], :string attach_function :magic_setflags, [:pointer, :int], :int attach_function :magic_load, [:pointer, :string], :int # attach_function :magic_compile, [:pointer, :string], :int # attach_function :magic_check, [:pointer, :string], :int # attach_function :magic_errno, [:pointer], :int end end ruby-magic-0.2.6/lib/magic/constants.rb000066400000000000000000000032311163511336600177510ustar00rootroot00000000000000module Magic module Constants #:nodoc: module Flag # No flags NONE = 0x000000 # Turn on debugging DEBUG = 0x000001 # Follow symlinks SYMLINK = 0x000002 # Check inside compressed files COMPRESS = 0x000004 # Look at the contents of devices DEVICES = 0x000008 # Return the MIME type MIME_TYPE = 0x000010 # Return all matches CONTINUE = 0x000020 # Print warnings to stderr CHECK = 0x000040 # Restore access time on exit PRESERVE_ATIME = 0x000080 # Don't translate unprintable chars RAW = 0x000100 # Handle ENOENT etc as real errors ERROR = 0x000200 # Return the MIME encoding MIME_ENCODING = 0x000400 # Return the MIME "type; charset=encoding" MIME = (MIME_TYPE | MIME_ENCODING) # Return the Apple creator and type APPLE = 0x000800 # Don't check for compressed files NO_CHECK_COMPRESS = 0x001000 # Don't check for tar files NO_CHECK_TAR = 0x002000 # Don't check magic entries NO_CHECK_SOFT = 0x004000 # Don't check application type NO_CHECK_APPTYPE = 0x008000 # Don't check for elf details NO_CHECK_ELF = 0x010000 # Don't check for text files NO_CHECK_TEXT = 0x020000 # Don't check for cdf files NO_CHECK_CDF = 0x040000 # Don't check tokens NO_CHECK_TOKENS = 0x100000 # Don't check text encodings NO_CHECK_ENCODING = 0x200000 end end end ruby-magic-0.2.6/lib/magic/database.rb000066400000000000000000000030571163511336600175070ustar00rootroot00000000000000module Magic class Database attr_reader :flags # Creates an instance of +Magic::Database+ using given database # file and flags def initialize(*args) options = args.last.is_a?(Hash) ? args.pop : {} # extract options database = options.delete(:database) open(*args) load(database) end # Opens magic db using given flags def open(*flags) @flags = calculate_flags(*flags) @magic_set = Api.magic_open(@flags) end # Closes the database def close Api.magic_close(@magic_set) end # Loads given database file (or default if +nil+ given) def load(database = nil) Api.magic_load(@magic_set, database) end # Determine type of a file at given path def file(filename) result = Api.magic_file(@magic_set, filename.to_s) if result.null? raise Exception, error else result.get_string(0) end end # Determine type of given string def buffer(string) result = Api.magic_buffer(@magic_set, string, string.bytesize) if result.null? raise Exception, error else result.get_string(0) end end # Returns the last error occured def error Api.magic_error(@magic_set) end # Sets the flags def flags=(*flags) @flags = calculate_flags(*flags) Api.magic_setflags(@magic_set, @flags) end protected def calculate_flags(*flags) flags.inject(0) { |calculated, flag| calculated |= Constants::Flag.const_get(flag.to_s.upcase) } end end end ruby-magic-0.2.6/lib/magic/errors.rb000066400000000000000000000001251163511336600172500ustar00rootroot00000000000000module Magic # General Exception class class Exception < StandardError end end ruby-magic-0.2.6/lib/magic/version.rb000066400000000000000000000000451163511336600174220ustar00rootroot00000000000000module Magic VERSION = "0.2.6" end ruby-magic-0.2.6/magic.gemspec000066400000000000000000000046661163511336600162240ustar00rootroot00000000000000# -*- coding: utf-8 -*- lib = File.expand_path("../lib/", __FILE__) $:.unshift lib unless $:.include?(lib) require "magic/version" Gem::Specification.new do |s| s.name = "magic" s.version = Magic::VERSION s.platform = Gem::Platform::RUBY s.authors = ["Jakub Kuźma"] s.email = "qoobaa@gmail.com" s.homepage = "http://jah.pl/projects/magic.html" s.summary = 'Determine file type and encoding using "magic" numbers' s.description = "Ruby FFI bindings to libmagic" s.required_rubygems_version = ">= 1.3.6" s.add_runtime_dependency "ffi", [">= 0.6.3"] s.add_development_dependency "test-unit", [">= 2.0"] s.files = Dir.glob("lib/**/*") + %w(LICENSE README.rdoc) s.post_install_message = <<-EOM +-NOTE FOR LINUX USERS----------------------------------------------+ | | | Install libmagic using your package manager, e.g. | | | | sudo apt-get install file | | | +-NOTE FOR WINDOWS USERS -------------------------------------------+ | | | Install File for Windows from | | | | http://gnuwin32.sourceforge.net/packages/file.htm | | | | You'll also need to set your PATH environment variable to the | | directory of the magic1.dll file | | | | set PATH=C:\\Program Files\\GnuWin32\\bin;%PATH% | | | +-NOTE FOR MAC OS USERS --------------------------------------------+ | | | If you don't have libmagic.1.dylib file in your system, you need | | to install it using port command | | | | sudo port install file | | | +-------------------------------------------------------------------+ EOM end ruby-magic-0.2.6/test/000077500000000000000000000000001163511336600145425ustar00rootroot00000000000000ruby-magic-0.2.6/test/fixtures/000077500000000000000000000000001163511336600164135ustar00rootroot00000000000000ruby-magic-0.2.6/test/fixtures/filelogo.jpg000066400000000000000000000127271163511336600207260ustar00rootroot00000000000000JFIFHHC    $.' ",#(7),01444'9=82<.342C  2!!22222222222222222222222222222222222222222222222222J"A!"1AQ2aq#r35R$4BCcs%DTb+!"123Aq#BQ4Ca ?!xM/%-2FJjr--P.\:䎝 mX<ԭGقGT@l/C%4uar:tju7]l!R@!@!FD\*Κׂa~*6ul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t8_:3mpLRPU~MIhO4q8/}laOq-_Ǣr`d9Vak@+ b)ʨErK+UQXe^xm~L4é-}Tt]]`C=lz=Bx3v;PWogdu{r~-QUʙk] >|> lyF{8u n6 Ԍ.j;خ4JlRرatUS {0 )o;Kx,a2YdzcomU Rdžx* kZͦ¨Xʸgh 8kЛ-Z4AVЫ2Yc:4[QG4"\և;&}ϙ'"=OD;-Yޡ{dy e*Xi,!b7O[=,YZ::\hew\82mɘ,HB"BFA!FRhiN:+hFtFtVPɕ 8F#De_G㢴h9!lu T#G< ӃZB4JE?GoEЀJy%El$W >z@r!c+жP$ p{C o:OTE<}<WEe3[elbD ӃZBE|OчEhl"謮Knəs~K01##c_%4h.y'u J!n&;PSE R+rS } Eh଴X 6jFg-' !B!B!B!B!B!B@Yz B B,@X/P!@ruby-magic-0.2.6/test/fixtures/magic.txt000066400000000000000000000000171163511336600202320ustar00rootroot00000000000000Magic® File™ruby-magic-0.2.6/test/fixtures/magic_empty000066400000000000000000000000471163511336600206350ustar00rootroot00000000000000# This is an empty magic database file ruby-magic-0.2.6/test/fixtures/magic_jpeg000066400000000000000000000024211163511336600204220ustar00rootroot00000000000000#------------------------------------------------------------------------------ # JPEG images # SunOS 5.5.1 had # # 0 string \377\330\377\340 JPEG file # 0 string \377\330\377\356 JPG file # # both of which turn into "JPEG image data" here. # 0 beshort 0xffd8 JPEG image data !:mime image/jpeg !:apple 8BIMJPEG !:strength +1 >6 string JFIF \b, JFIF standard # The following added by Erik Rossen 1999-09-06 # in a vain attempt to add image size reporting for JFIF. Note that these # tests are not fool-proof since some perfectly valid JPEGs are currently # impossible to specify in magic(4) format. # First, a little JFIF version info: >>11 byte x \b %d. >>12 byte x \b%02d # Next, the resolution or aspect ratio of the image: #>>13 byte 0 \b, aspect ratio #>>13 byte 1 \b, resolution (DPI) #>>13 byte 2 \b, resolution (DPCM) #>>4 beshort x \b, segment length %d # Next, show thumbnail info, if it exists: >>18 byte !0 \b, thumbnail %dx >>>19 byte x \b%d ruby-magic-0.2.6/test/helper.rb000066400000000000000000000004601163511336600163460ustar00rootroot00000000000000require "rubygems" gem "test-unit" require "test/unit" $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) $LOAD_PATH.unshift(File.dirname(__FILE__)) require "magic" class Test::Unit::TestCase def fixture(filename) File.join(File.dirname(__FILE__), "fixtures", filename) end end ruby-magic-0.2.6/test/test_magic.rb000066400000000000000000000030611163511336600172060ustar00rootroot00000000000000require "helper" class TestMagic < Test::Unit::TestCase test "guess magic.txt mime" do assert_equal "text/plain; charset=utf-8", Magic.guess_file_mime(fixture("magic.txt")) end test "guess magic.txt mime type" do assert_equal "text/plain", Magic.guess_file_mime_type(fixture("magic.txt")) end test "guess magic.txt mime encoding" do assert_equal "utf-8", Magic.guess_file_mime_encoding(fixture("magic.txt")) end test "guess filelogo.jpg mime" do assert_equal "image/jpeg; charset=binary", Magic.guess_file_mime(fixture("filelogo.jpg")) end test "guess filelogo.jpg mime type" do assert_equal "image/jpeg", Magic.guess_file_mime_type(fixture("filelogo.jpg")) end test "guess filelogo.jpg mime encoding" do assert_equal "binary", Magic.guess_file_mime_encoding(fixture("filelogo.jpg")) end test "guess non-existing file mime" do assert_raises Magic::Exception do Magic.guess_file_mime(fixture("non-existing.file")) end end test "guess filelogo.jpg mime with magic_jpeg database" do assert_equal "image/jpeg; charset=binary", Magic.guess_file_mime(fixture("filelogo.jpg"), :database => fixture("magic_jpeg")) end test "guess filelogo.jpg mime with empty database" do assert_equal "application/octet-stream; charset=binary", Magic.guess_file_mime(fixture("filelogo.jpg"), :database => fixture("magic_empty")) end test "guess with block" do result = nil Magic.guess(:mime) { |db| result = db.file(fixture("filelogo.jpg")) } assert_equal "image/jpeg; charset=binary", result end end