ruby-filemagic 0.6.2-x86-mingw32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ChangeLog +92 -0
- data/README +101 -0
- data/Rakefile +34 -0
- data/TODO +7 -0
- data/ext/filemagic/extconf.rb +16 -0
- data/ext/filemagic/filemagic.c +310 -0
- data/ext/filemagic/filemagic.h +98 -0
- data/lib/filemagic/1.9/ruby_filemagic.so +0 -0
- data/lib/filemagic/2.0/ruby_filemagic.so +0 -0
- data/lib/filemagic/2.1/ruby_filemagic.so +0 -0
- data/lib/filemagic/2.2/ruby_filemagic.so +0 -0
- data/lib/filemagic/ext.rb +99 -0
- data/lib/filemagic/magic.mgc +0 -0
- data/lib/filemagic/version.rb +27 -0
- data/lib/filemagic.rb +142 -0
- data/lib/ruby-filemagic.rb +1 -0
- data/test/excel-example.xls +0 -0
- data/test/filemagic_test.rb +227 -0
- data/test/leaktest.rb +16 -0
- data/test/mahoro.c +187 -0
- data/test/perl +19 -0
- data/test/perl.mgc +0 -0
- data/test/pyfile +1 -0
- data/test/pyfile-compressed.gz +0 -0
- data/test/pylink +1 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 119c4a11a9e3df55d70340d9b32ff3c508741ef0
|
4
|
+
data.tar.gz: 89d8685a9ccf5762433c78b873135be2bd0af1fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6257197e6afb3f26a54ba3045f14ffa69d549c37099dd25d03aa394831f0c5aa156038dc28302b93cae7373264e0a45382c8947835bf271fae5ad4ebca1898e7
|
7
|
+
data.tar.gz: 544f82f2f42d3c337fec1bfb985f33c737697423a954790588420c3c7f0e4820a503232cef16e581c2faca684b81277be3d2d83eb2b1d6bdfeb6dc4b3e06d084
|
data/ChangeLog
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# markup: rd
|
2
|
+
|
3
|
+
= Revision history for ruby-filemagic
|
4
|
+
|
5
|
+
== 0.6.2 [2015-02-06]
|
6
|
+
|
7
|
+
* Improved Windows support (issue #10 by Matt Hoyle).
|
8
|
+
* Fixed FileMagic.path to account for missing magic file.
|
9
|
+
|
10
|
+
== 0.6.1 [2014-08-19]
|
11
|
+
|
12
|
+
* Account for options passed to FileMagic.fm (issue #7 by Adam Wróbel).
|
13
|
+
|
14
|
+
== 0.6.0 [2014-05-16]
|
15
|
+
|
16
|
+
* Required Ruby version >= 1.9.3.
|
17
|
+
* New method FileMagic#list (+magic_list+).
|
18
|
+
* New method FileMagic#load (+magic_load+).
|
19
|
+
* New method FileMagic.path (+magic_getpath+).
|
20
|
+
* New method FileMagic.magic_version.
|
21
|
+
* New method FileMagic.flags.
|
22
|
+
* New flag +no_check_builtin+.
|
23
|
+
* Tests print libmagic version and path.
|
24
|
+
* Internal refactoring.
|
25
|
+
|
26
|
+
== 0.5.2 [2014-04-24]
|
27
|
+
|
28
|
+
* Use MAGIC_VERSION if available.
|
29
|
+
* Made tests more robust.
|
30
|
+
|
31
|
+
== 0.5.1 [2014-04-15]
|
32
|
+
|
33
|
+
* New method FileMagic#io.
|
34
|
+
* Housekeeping.
|
35
|
+
|
36
|
+
== 0.5.0 [2013-12-19]
|
37
|
+
|
38
|
+
* Removed usage of +RARRAY_PTR+ (cf. Ruby bug
|
39
|
+
{#8399}[https://bugs.ruby-lang.org/issues/8399]).
|
40
|
+
* Housekeeping.
|
41
|
+
|
42
|
+
== 0.4.2 [2010-09-16]
|
43
|
+
|
44
|
+
* Not all versions/distributions have file/patchlevel.h
|
45
|
+
|
46
|
+
== 0.4.1 [2010-09-15]
|
47
|
+
|
48
|
+
* Added MAGIC_VERSION
|
49
|
+
* Fixed example script
|
50
|
+
|
51
|
+
== 0.4.0 [2010-09-14]
|
52
|
+
|
53
|
+
* Brushed up C layer
|
54
|
+
* Moved most of the Ruby stuff to C
|
55
|
+
* No longer expose internal state (@closed, @flags)
|
56
|
+
* No longer expose internal C methods (fm_*)
|
57
|
+
* Updated for magic(4) version 5.04
|
58
|
+
|
59
|
+
== 0.3.0 [2010-09-10]
|
60
|
+
|
61
|
+
* Ruby 1.9.2 compatibility (Martin Carpenter)
|
62
|
+
* Exposed flags as symbols (Martin Carpenter)
|
63
|
+
|
64
|
+
== 0.2.2 [2010-03-02]
|
65
|
+
|
66
|
+
* Allow '.' when abbreviating mime types (Eric Schwartz)
|
67
|
+
* Cleanup and project file fixes
|
68
|
+
|
69
|
+
== 0.2.1 [2008-09-18]
|
70
|
+
|
71
|
+
* Added mahoro source file and tests for reference and inspiration
|
72
|
+
* We have a Rubyforge project now! :-)
|
73
|
+
|
74
|
+
== 0.2.0 [2008-09-12]
|
75
|
+
|
76
|
+
* Modified C API
|
77
|
+
* Uniform C function prefix rb_magic_ (instead of magick_)
|
78
|
+
* Uniform Ruby method prefix fm_ (instead of none)
|
79
|
+
* More magic(4) constants (if available)
|
80
|
+
* Added Ruby wrapper for more rubyish interface
|
81
|
+
* Added extensions for File and String core classes
|
82
|
+
* Added/updated project files
|
83
|
+
* Now available as a Rubygem!
|
84
|
+
|
85
|
+
== 0.1.1 [2003-07-30]
|
86
|
+
|
87
|
+
* Added manual close method
|
88
|
+
* Added unit test suite
|
89
|
+
|
90
|
+
== 0.1.0 [2003-07-28]
|
91
|
+
|
92
|
+
* Initial release.
|
data/README
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
= FileMagic Library Binding
|
2
|
+
|
3
|
+
== VERSION
|
4
|
+
|
5
|
+
This documentation refers to filemagic version 0.6.2
|
6
|
+
|
7
|
+
|
8
|
+
== DESCRIPTION
|
9
|
+
|
10
|
+
FileMagic extension module. See also libmagic(3), file(1) and magic(4).
|
11
|
+
|
12
|
+
=== Constants
|
13
|
+
|
14
|
+
<tt>MAGIC_NONE</tt>:: No flags
|
15
|
+
<tt>MAGIC_DEBUG</tt>:: Turn on debugging
|
16
|
+
<tt>MAGIC_SYMLINK</tt>:: Follow symlinks
|
17
|
+
<tt>MAGIC_COMPRESS</tt>:: Check inside compressed files
|
18
|
+
<tt>MAGIC_DEVICES</tt>:: Look at the contents of devices
|
19
|
+
<tt>MAGIC_MIME</tt>:: Return a mime string
|
20
|
+
<tt>MAGIC_CONTINUE</tt>:: Return all matches, not just the first
|
21
|
+
<tt>MAGIC_CHECK</tt>:: Print warnings to stderr
|
22
|
+
|
23
|
+
=== Methods
|
24
|
+
|
25
|
+
<tt>file(filename)</tt>:: Returns a textual description of the contents
|
26
|
+
of the filename argument
|
27
|
+
<tt>buffer(string)</tt>:: Returns a textual description of the contents
|
28
|
+
of the string argument
|
29
|
+
<tt>check(filename)</tt>:: Checks the validity of entries in the database
|
30
|
+
file passed in as filename
|
31
|
+
<tt>compile(filename)</tt>:: Compiles the database file passed in as filename
|
32
|
+
<tt>close()</tt>:: Closes the magic database and frees any memory
|
33
|
+
allocated
|
34
|
+
|
35
|
+
=== Synospis
|
36
|
+
|
37
|
+
require 'filemagic'
|
38
|
+
|
39
|
+
p FileMagic::VERSION
|
40
|
+
p FileMagic::MAGIC_VERSION
|
41
|
+
|
42
|
+
p FileMagic.new.flags
|
43
|
+
|
44
|
+
FileMagic.open(:mime) { |fm|
|
45
|
+
p fm.flags
|
46
|
+
p fm.file(__FILE__)
|
47
|
+
p fm.file(__FILE__, true)
|
48
|
+
|
49
|
+
fm.flags = [:raw, :continue]
|
50
|
+
p fm.flags
|
51
|
+
}
|
52
|
+
|
53
|
+
fm = FileMagic.new
|
54
|
+
p fm.flags
|
55
|
+
|
56
|
+
mime = FileMagic.mime
|
57
|
+
p mime.flags
|
58
|
+
|
59
|
+
=== Installation
|
60
|
+
|
61
|
+
Install the gem:
|
62
|
+
|
63
|
+
sudo gem install ruby-filemagic
|
64
|
+
|
65
|
+
The file(1) library and headers are required.
|
66
|
+
|
67
|
+
|
68
|
+
== LINKS
|
69
|
+
|
70
|
+
Homepage:: http://www.darwinsys.com/file/
|
71
|
+
Documentation:: https://blackwinter.github.com/ruby-filemagic
|
72
|
+
Source code:: https://github.com/blackwinter/ruby-filemagic
|
73
|
+
RubyGem:: https://rubygems.org/gems/ruby-filemagic
|
74
|
+
Travis CI:: https://travis-ci.org/blackwinter/ruby-filemagic
|
75
|
+
|
76
|
+
|
77
|
+
== AUTHORS
|
78
|
+
|
79
|
+
* Travis Whitton <mailto:tinymountain@gmail.com> (Original author)
|
80
|
+
* Jens Wille <mailto:jens.wille@gmail.com>
|
81
|
+
|
82
|
+
|
83
|
+
== CREDITS
|
84
|
+
|
85
|
+
* Martin Carpenter <mailto:mcarpenter@free.fr> for Ruby 1.9.2 compatibility
|
86
|
+
and other improvements.
|
87
|
+
|
88
|
+
|
89
|
+
== COPYING
|
90
|
+
|
91
|
+
The filemagic extension library is copywrited free software by Travis Whitton
|
92
|
+
<tinymountain@gmail.com>. You can redistribute it under the terms specified in
|
93
|
+
the COPYING file of the Ruby distribution.
|
94
|
+
|
95
|
+
|
96
|
+
== WARRANTY
|
97
|
+
|
98
|
+
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
99
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
100
|
+
WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
|
101
|
+
PURPOSE.
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'lib/filemagic/version'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'hen'
|
5
|
+
|
6
|
+
file mgc = 'lib/filemagic/magic.mgc' do
|
7
|
+
dir = ENV['WITH_CROSS_MAGIC']
|
8
|
+
|
9
|
+
dir && File.exist?(src = File.join(dir, 'magic', File.basename(mgc))) ?
|
10
|
+
cp(src, mgc) : File.binwrite(mgc, "\x1C\x04\x1E\xF1\x0C".ljust(248, "\0"))
|
11
|
+
end
|
12
|
+
|
13
|
+
Hen.lay! {{
|
14
|
+
gem: {
|
15
|
+
name: %q{ruby-filemagic},
|
16
|
+
version: FileMagic::VERSION,
|
17
|
+
summary: 'Ruby bindings to the magic(4) library',
|
18
|
+
authors: ['Travis Whitton', 'Jens Wille'],
|
19
|
+
email: 'jens.wille@gmail.com',
|
20
|
+
license: %q{Ruby},
|
21
|
+
homepage: :blackwinter,
|
22
|
+
local_files: [mgc],
|
23
|
+
extension: {
|
24
|
+
with_cross_gnurx: lambda { |dir| [dir] },
|
25
|
+
with_cross_magic: lambda { |dir| [src =
|
26
|
+
File.join(dir, 'src'), File.join(src, '.libs')] }
|
27
|
+
},
|
28
|
+
|
29
|
+
required_ruby_version: '>= 1.9.3'
|
30
|
+
}
|
31
|
+
}}
|
32
|
+
rescue LoadError => err
|
33
|
+
warn "Please install the `hen' gem. (#{err})"
|
34
|
+
end
|
data/TODO
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
$CFLAGS << ' -Wall' if ENV['WALL']
|
4
|
+
$LDFLAGS << ' -static-libgcc'
|
5
|
+
|
6
|
+
dir_config('magic')
|
7
|
+
dir_config('gnurx')
|
8
|
+
|
9
|
+
have_library('gnurx')
|
10
|
+
|
11
|
+
if have_library('magic', 'magic_open') && have_header('magic.h')
|
12
|
+
have_header('file/patchlevel.h')
|
13
|
+
create_makefile('filemagic/ruby_filemagic')
|
14
|
+
else
|
15
|
+
abort '*** ERROR: missing required library to compile this module'
|
16
|
+
end
|
@@ -0,0 +1,310 @@
|
|
1
|
+
#include "filemagic.h"
|
2
|
+
|
3
|
+
/* Returns the magic path */
|
4
|
+
static VALUE
|
5
|
+
rb_magic_getpath(VALUE klass) {
|
6
|
+
const char *path = magic_getpath(NULL, 0);
|
7
|
+
return path != NULL ? rb_str_new2(path) : Qnil;
|
8
|
+
}
|
9
|
+
|
10
|
+
/* Converts flags to integer */
|
11
|
+
static VALUE
|
12
|
+
rb_magic_flags(VALUE klass, VALUE flags) {
|
13
|
+
VALUE map = rb_const_get(cFileMagic, rb_intern("FLAGS_BY_SYM")), f, g;
|
14
|
+
int i = MAGIC_NONE, j;
|
15
|
+
|
16
|
+
if (TYPE(flags) != T_ARRAY) {
|
17
|
+
rb_raise(rb_eTypeError,
|
18
|
+
"wrong argument type %s (expected Array)",
|
19
|
+
rb_obj_classname(flags));
|
20
|
+
}
|
21
|
+
|
22
|
+
for (j = 0; j < RARRAY_LEN(flags); j++) {
|
23
|
+
f = rb_ary_entry(flags, j);
|
24
|
+
|
25
|
+
switch (TYPE(f)) {
|
26
|
+
case T_SYMBOL:
|
27
|
+
if (RTEST(g = rb_hash_aref(map, f))) {
|
28
|
+
f = g;
|
29
|
+
/* fall through */
|
30
|
+
}
|
31
|
+
else {
|
32
|
+
f = rb_funcall(f, rb_intern("inspect"), 0);
|
33
|
+
rb_raise(rb_eArgError, "%s: %s",
|
34
|
+
NIL_P(g) ? "no such flag" : "flag not available",
|
35
|
+
StringValueCStr(f));
|
36
|
+
|
37
|
+
break;
|
38
|
+
}
|
39
|
+
case T_FIXNUM:
|
40
|
+
i |= NUM2INT(f);
|
41
|
+
break;
|
42
|
+
default:
|
43
|
+
rb_raise(rb_eTypeError,
|
44
|
+
"wrong argument type %s (expected Fixnum or Symbol)",
|
45
|
+
rb_obj_classname(f));
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
return INT2FIX(i);
|
50
|
+
}
|
51
|
+
|
52
|
+
/* FileMagic.new */
|
53
|
+
static VALUE
|
54
|
+
rb_magic_new(int argc, VALUE *argv, VALUE klass) {
|
55
|
+
VALUE obj, args[2];
|
56
|
+
magic_t ms;
|
57
|
+
|
58
|
+
if (rb_block_given_p()) {
|
59
|
+
rb_warn(
|
60
|
+
"FileMagic.new() does not take a block; use FileMagic.open() instead");
|
61
|
+
}
|
62
|
+
|
63
|
+
if (argc > 0 && TYPE(args[1] = argv[argc - 1]) == T_HASH) {
|
64
|
+
argc--;
|
65
|
+
}
|
66
|
+
else {
|
67
|
+
args[1] = rb_hash_new();
|
68
|
+
}
|
69
|
+
|
70
|
+
args[0] = rb_magic_flags(klass, rb_ary_new4(argc, argv));
|
71
|
+
|
72
|
+
if ((ms = magic_open(NUM2INT(args[0]))) == NULL) {
|
73
|
+
rb_raise(rb_FileMagicError,
|
74
|
+
"failed to initialize magic cookie (%d)", errno || -1);
|
75
|
+
}
|
76
|
+
|
77
|
+
if (magic_load(ms, NULL) == -1) {
|
78
|
+
rb_raise(rb_FileMagicError,
|
79
|
+
"failed to load database: %s", magic_error(ms));
|
80
|
+
}
|
81
|
+
|
82
|
+
obj = Data_Wrap_Struct(klass, 0, rb_magic_free, ms);
|
83
|
+
rb_obj_call_init(obj, 2, args);
|
84
|
+
|
85
|
+
return obj;
|
86
|
+
}
|
87
|
+
|
88
|
+
static void
|
89
|
+
rb_magic_free(magic_t ms) {
|
90
|
+
magic_close(ms);
|
91
|
+
}
|
92
|
+
|
93
|
+
static VALUE
|
94
|
+
rb_magic_init(int argc, VALUE *argv, VALUE self) {
|
95
|
+
VALUE flags, options, keys, k, m;
|
96
|
+
ID mid;
|
97
|
+
int i;
|
98
|
+
|
99
|
+
if (rb_scan_args(argc, argv, "11", &flags, &options) == 1) {
|
100
|
+
options = rb_hash_new();
|
101
|
+
}
|
102
|
+
|
103
|
+
rb_iv_set(self, "iflags", flags);
|
104
|
+
rb_iv_set(self, "closed", Qfalse);
|
105
|
+
|
106
|
+
keys = rb_funcall(options, rb_intern("keys"), 0);
|
107
|
+
|
108
|
+
for (i = 0; i < RARRAY_LEN(keys); i++) {
|
109
|
+
k = rb_ary_entry(keys, i);
|
110
|
+
m = rb_str_plus(rb_obj_as_string(k), rb_str_new2("="));
|
111
|
+
|
112
|
+
if (rb_respond_to(self, mid = rb_intern(StringValueCStr(m)))) {
|
113
|
+
rb_funcall(self, mid, 1, rb_hash_aref(options, k));
|
114
|
+
}
|
115
|
+
else {
|
116
|
+
k = rb_funcall(k, rb_intern("inspect"), 0);
|
117
|
+
rb_raise(rb_eArgError, "illegal option: %s", StringValueCStr(k));
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
return Qnil;
|
122
|
+
}
|
123
|
+
|
124
|
+
/* Frees resources allocated */
|
125
|
+
static VALUE
|
126
|
+
rb_magic_close(VALUE self) {
|
127
|
+
magic_t ms;
|
128
|
+
|
129
|
+
if (RTEST(rb_magic_closed_p(self))) {
|
130
|
+
return Qnil;
|
131
|
+
}
|
132
|
+
|
133
|
+
GetMagicSet(self, ms);
|
134
|
+
rb_magic_free(ms);
|
135
|
+
|
136
|
+
/* This keeps rb_magic_free from trying to free closed objects */
|
137
|
+
DATA_PTR(self) = NULL;
|
138
|
+
|
139
|
+
rb_iv_set(self, "closed", Qtrue);
|
140
|
+
|
141
|
+
return Qnil;
|
142
|
+
}
|
143
|
+
|
144
|
+
static VALUE
|
145
|
+
rb_magic_closed_p(VALUE self) {
|
146
|
+
return rb_attr_get(self, rb_intern("closed"));
|
147
|
+
}
|
148
|
+
|
149
|
+
/* Return a string describing file */
|
150
|
+
RB_MAGIC_TYPE(file, FILE)
|
151
|
+
|
152
|
+
/* Return a string describing the string buffer */
|
153
|
+
RB_MAGIC_TYPE(buffer, BUFFER)
|
154
|
+
|
155
|
+
/* Get the flags as array of symbols */
|
156
|
+
static VALUE
|
157
|
+
rb_magic_getflags(VALUE self) {
|
158
|
+
VALUE ary = rb_ary_new();
|
159
|
+
VALUE map = rb_const_get(cFileMagic, rb_intern("FLAGS_BY_INT"));
|
160
|
+
int i = NUM2INT(rb_attr_get(self, rb_intern("iflags"))), j = 0;
|
161
|
+
|
162
|
+
while ((i -= j) > 0) {
|
163
|
+
j = pow(2, (int)(log(i) / log(2)));
|
164
|
+
rb_ary_unshift(ary, rb_hash_aref(map, INT2FIX(j)));
|
165
|
+
}
|
166
|
+
|
167
|
+
return ary;
|
168
|
+
}
|
169
|
+
|
170
|
+
/* Set flags on the ms object */
|
171
|
+
static VALUE
|
172
|
+
rb_magic_setflags(VALUE self, VALUE flags) {
|
173
|
+
magic_t ms;
|
174
|
+
|
175
|
+
GetMagicSet(self, ms);
|
176
|
+
|
177
|
+
rb_iv_set(self, "iflags",
|
178
|
+
flags = rb_magic_flags(CLASS_OF(self), rb_Array(flags)));
|
179
|
+
|
180
|
+
return INT2FIX(magic_setflags(ms, NUM2INT(flags)));
|
181
|
+
}
|
182
|
+
|
183
|
+
/* Lists a magic database file */
|
184
|
+
RB_MAGIC_APPRENTICE(list)
|
185
|
+
|
186
|
+
/* Loads a magic database file */
|
187
|
+
RB_MAGIC_APPRENTICE(load)
|
188
|
+
|
189
|
+
/* Checks validity of a magic database file */
|
190
|
+
RB_MAGIC_APPRENTICE(check)
|
191
|
+
|
192
|
+
/* Compiles a magic database file */
|
193
|
+
RB_MAGIC_APPRENTICE(compile)
|
194
|
+
|
195
|
+
void
|
196
|
+
Init_ruby_filemagic() {
|
197
|
+
char version[8] = "0";
|
198
|
+
cFileMagic = rb_define_class("FileMagic", rb_cObject);
|
199
|
+
|
200
|
+
#if defined(FILE_VERSION_MAJOR)
|
201
|
+
RB_MAGIC_SET_VERSION(FILE_VERSION_MAJOR, patchlevel)
|
202
|
+
#elif defined(MAGIC_VERSION)
|
203
|
+
RB_MAGIC_SET_VERSION(MAGIC_VERSION / 100, MAGIC_VERSION % 100)
|
204
|
+
#endif
|
205
|
+
|
206
|
+
rb_define_const(cFileMagic, "MAGIC_VERSION", rb_str_new2(version));
|
207
|
+
|
208
|
+
rb_define_singleton_method(cFileMagic, "path", rb_magic_getpath, 0);
|
209
|
+
rb_define_singleton_method(cFileMagic, "flags", rb_magic_flags, 1);
|
210
|
+
rb_define_singleton_method(cFileMagic, "new", rb_magic_new, -1);
|
211
|
+
|
212
|
+
rb_define_method(cFileMagic, "initialize", rb_magic_init, -1);
|
213
|
+
rb_define_method(cFileMagic, "close", rb_magic_close, 0);
|
214
|
+
rb_define_method(cFileMagic, "closed?", rb_magic_closed_p, 0);
|
215
|
+
rb_define_method(cFileMagic, "file", rb_magic_file, -1);
|
216
|
+
rb_define_method(cFileMagic, "buffer", rb_magic_buffer, -1);
|
217
|
+
rb_define_method(cFileMagic, "flags", rb_magic_getflags, 0);
|
218
|
+
rb_define_method(cFileMagic, "flags=", rb_magic_setflags, 1);
|
219
|
+
rb_define_method(cFileMagic, "list", rb_magic_list, -1);
|
220
|
+
rb_define_method(cFileMagic, "load", rb_magic_load, -1);
|
221
|
+
rb_define_method(cFileMagic, "check", rb_magic_check, -1);
|
222
|
+
rb_define_method(cFileMagic, "compile", rb_magic_compile, -1);
|
223
|
+
|
224
|
+
rb_alias(cFileMagic, rb_intern("valid?"), rb_intern("check"));
|
225
|
+
|
226
|
+
rb_FileMagicError = rb_define_class_under(cFileMagic, "FileMagicError", rb_eStandardError);
|
227
|
+
|
228
|
+
#ifdef MAGIC_NONE
|
229
|
+
rb_define_const(cFileMagic, "MAGIC_NONE", INT2FIX(MAGIC_NONE));
|
230
|
+
#endif
|
231
|
+
#ifdef MAGIC_DEBUG
|
232
|
+
rb_define_const(cFileMagic, "MAGIC_DEBUG", INT2FIX(MAGIC_DEBUG));
|
233
|
+
#endif
|
234
|
+
#ifdef MAGIC_SYMLINK
|
235
|
+
rb_define_const(cFileMagic, "MAGIC_SYMLINK", INT2FIX(MAGIC_SYMLINK));
|
236
|
+
#endif
|
237
|
+
#ifdef MAGIC_COMPRESS
|
238
|
+
rb_define_const(cFileMagic, "MAGIC_COMPRESS", INT2FIX(MAGIC_COMPRESS));
|
239
|
+
#endif
|
240
|
+
#ifdef MAGIC_DEVICES
|
241
|
+
rb_define_const(cFileMagic, "MAGIC_DEVICES", INT2FIX(MAGIC_DEVICES));
|
242
|
+
#endif
|
243
|
+
#ifdef MAGIC_MIME_TYPE
|
244
|
+
rb_define_const(cFileMagic, "MAGIC_MIME_TYPE", INT2FIX(MAGIC_MIME_TYPE));
|
245
|
+
#endif
|
246
|
+
#ifdef MAGIC_CONTINUE
|
247
|
+
rb_define_const(cFileMagic, "MAGIC_CONTINUE", INT2FIX(MAGIC_CONTINUE));
|
248
|
+
#endif
|
249
|
+
#ifdef MAGIC_CHECK
|
250
|
+
rb_define_const(cFileMagic, "MAGIC_CHECK", INT2FIX(MAGIC_CHECK));
|
251
|
+
#endif
|
252
|
+
#ifdef MAGIC_PRESERVE_ATIME
|
253
|
+
rb_define_const(cFileMagic, "MAGIC_PRESERVE_ATIME", INT2FIX(MAGIC_PRESERVE_ATIME));
|
254
|
+
#endif
|
255
|
+
#ifdef MAGIC_RAW
|
256
|
+
rb_define_const(cFileMagic, "MAGIC_RAW", INT2FIX(MAGIC_RAW));
|
257
|
+
#endif
|
258
|
+
#ifdef MAGIC_ERROR
|
259
|
+
rb_define_const(cFileMagic, "MAGIC_ERROR", INT2FIX(MAGIC_ERROR));
|
260
|
+
#endif
|
261
|
+
#ifdef MAGIC_MIME_ENCODING
|
262
|
+
rb_define_const(cFileMagic, "MAGIC_MIME_ENCODING", INT2FIX(MAGIC_MIME_ENCODING));
|
263
|
+
#endif
|
264
|
+
#ifdef MAGIC_MIME
|
265
|
+
rb_define_const(cFileMagic, "MAGIC_MIME", INT2FIX(MAGIC_MIME));
|
266
|
+
#endif
|
267
|
+
#ifdef MAGIC_APPLE
|
268
|
+
rb_define_const(cFileMagic, "MAGIC_APPLE", INT2FIX(MAGIC_APPLE));
|
269
|
+
#endif
|
270
|
+
#ifdef MAGIC_NO_CHECK_COMPRESS
|
271
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_COMPRESS", INT2FIX(MAGIC_NO_CHECK_COMPRESS));
|
272
|
+
#endif
|
273
|
+
#ifdef MAGIC_NO_CHECK_TAR
|
274
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_TAR", INT2FIX(MAGIC_NO_CHECK_TAR));
|
275
|
+
#endif
|
276
|
+
#ifdef MAGIC_NO_CHECK_SOFT
|
277
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_SOFT", INT2FIX(MAGIC_NO_CHECK_SOFT));
|
278
|
+
#endif
|
279
|
+
#ifdef MAGIC_NO_CHECK_APPTYPE
|
280
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_APPTYPE", INT2FIX(MAGIC_NO_CHECK_APPTYPE));
|
281
|
+
#endif
|
282
|
+
#ifdef MAGIC_NO_CHECK_ELF
|
283
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_ELF", INT2FIX(MAGIC_NO_CHECK_ELF));
|
284
|
+
#endif
|
285
|
+
#ifdef MAGIC_NO_CHECK_TEXT
|
286
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_TEXT", INT2FIX(MAGIC_NO_CHECK_TEXT));
|
287
|
+
#endif
|
288
|
+
#ifdef MAGIC_NO_CHECK_CDF
|
289
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_CDF", INT2FIX(MAGIC_NO_CHECK_CDF));
|
290
|
+
#endif
|
291
|
+
#ifdef MAGIC_NO_CHECK_TOKENS
|
292
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_TOKENS", INT2FIX(MAGIC_NO_CHECK_TOKENS));
|
293
|
+
#endif
|
294
|
+
#ifdef MAGIC_NO_CHECK_ENCODING
|
295
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_ENCODING", INT2FIX(MAGIC_NO_CHECK_ENCODING));
|
296
|
+
#endif
|
297
|
+
#if defined(MAGIC_NO_CHECK_BUILTIN) && MAGIC_VERSION > 514
|
298
|
+
/* defined in b5be901 (2010-01-28, 5.05), but broken until 38e0136 (2013-08-15, 5.15) */
|
299
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_BUILTIN", INT2FIX(MAGIC_NO_CHECK_BUILTIN));
|
300
|
+
#endif
|
301
|
+
#ifdef MAGIC_NO_CHECK_ASCII
|
302
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_ASCII", INT2FIX(MAGIC_NO_CHECK_ASCII));
|
303
|
+
#endif
|
304
|
+
#ifdef MAGIC_NO_CHECK_FORTRAN
|
305
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_FORTRAN", INT2FIX(MAGIC_NO_CHECK_FORTRAN));
|
306
|
+
#endif
|
307
|
+
#ifdef MAGIC_NO_CHECK_TROFF
|
308
|
+
rb_define_const(cFileMagic, "MAGIC_NO_CHECK_TROFF", INT2FIX(MAGIC_NO_CHECK_TROFF));
|
309
|
+
#endif
|
310
|
+
}
|
@@ -0,0 +1,98 @@
|
|
1
|
+
#ifndef FILEMAGIC_H
|
2
|
+
#define FILEMAGIC_H
|
3
|
+
|
4
|
+
#include "ruby.h"
|
5
|
+
#include <math.h>
|
6
|
+
#include <errno.h>
|
7
|
+
#include <magic.h>
|
8
|
+
#ifdef HAVE_FILE_PATCHLEVEL_H
|
9
|
+
#include <file/patchlevel.h>
|
10
|
+
#endif
|
11
|
+
|
12
|
+
#define GetMagicSet(obj, ms) {\
|
13
|
+
if (RTEST(rb_magic_closed_p(obj))) {\
|
14
|
+
rb_raise(rb_eRuntimeError, "closed stream");\
|
15
|
+
}\
|
16
|
+
else {\
|
17
|
+
Data_Get_Struct((obj), struct magic_set, (ms));\
|
18
|
+
}\
|
19
|
+
}
|
20
|
+
|
21
|
+
#define RB_MAGIC_TYPE_FILE magic_file(ms, str)
|
22
|
+
#define RB_MAGIC_TYPE_BUFFER magic_buffer(ms, str, RSTRING_LEN(arg))
|
23
|
+
|
24
|
+
#define RB_MAGIC_TYPE(what, WHAT) \
|
25
|
+
static VALUE \
|
26
|
+
rb_magic_##what(int argc, VALUE *argv, VALUE self) {\
|
27
|
+
VALUE arg, simple, res;\
|
28
|
+
const char *str, *type;\
|
29
|
+
magic_t ms;\
|
30
|
+
\
|
31
|
+
rb_scan_args(argc, argv, "11", &arg, &simple);\
|
32
|
+
\
|
33
|
+
str = StringValuePtr(arg);\
|
34
|
+
GetMagicSet(self, ms);\
|
35
|
+
\
|
36
|
+
if ((type = RB_MAGIC_TYPE_##WHAT) == NULL) {\
|
37
|
+
rb_raise(rb_FileMagicError, "failed lookup: %s", magic_error(ms));\
|
38
|
+
}\
|
39
|
+
\
|
40
|
+
res = rb_str_new2(type);\
|
41
|
+
\
|
42
|
+
if (NIL_P(simple)) {\
|
43
|
+
simple = rb_attr_get(self, rb_intern("@simplified"));\
|
44
|
+
}\
|
45
|
+
\
|
46
|
+
if (RTEST(simple)) {\
|
47
|
+
rb_funcall(res, rb_intern("downcase!"), 0);\
|
48
|
+
\
|
49
|
+
return rb_funcall(res, rb_intern("slice"), 2,\
|
50
|
+
rb_const_get(cFileMagic, rb_intern("SIMPLE_RE")), INT2FIX(1));\
|
51
|
+
}\
|
52
|
+
else {\
|
53
|
+
return res;\
|
54
|
+
}\
|
55
|
+
}
|
56
|
+
|
57
|
+
#define RB_MAGIC_APPRENTICE(what) \
|
58
|
+
static VALUE \
|
59
|
+
rb_magic_##what(int argc, VALUE *argv, VALUE self) {\
|
60
|
+
VALUE str;\
|
61
|
+
const char *file;\
|
62
|
+
magic_t ms;\
|
63
|
+
\
|
64
|
+
file = rb_scan_args(argc, argv, "01", &str) == 1 ? StringValuePtr(str) : NULL;\
|
65
|
+
\
|
66
|
+
GetMagicSet(self, ms);\
|
67
|
+
\
|
68
|
+
return magic_##what(ms, file) ? Qfalse : Qtrue;\
|
69
|
+
}
|
70
|
+
|
71
|
+
#define RB_MAGIC_SET_VERSION(m, p) sprintf(version, "%d.%02d", m, p);
|
72
|
+
|
73
|
+
static VALUE cFileMagic, rb_FileMagicError;
|
74
|
+
|
75
|
+
static VALUE rb_magic_getpath(VALUE);
|
76
|
+
static VALUE rb_magic_flags(VALUE, VALUE);
|
77
|
+
|
78
|
+
static VALUE rb_magic_new(int, VALUE*, VALUE);
|
79
|
+
static void rb_magic_free(magic_t);
|
80
|
+
static VALUE rb_magic_init(int, VALUE*, VALUE);
|
81
|
+
|
82
|
+
static VALUE rb_magic_close(VALUE);
|
83
|
+
static VALUE rb_magic_closed_p(VALUE);
|
84
|
+
|
85
|
+
static VALUE rb_magic_file(int, VALUE*, VALUE);
|
86
|
+
static VALUE rb_magic_buffer(int, VALUE*, VALUE);
|
87
|
+
|
88
|
+
static VALUE rb_magic_getflags(VALUE);
|
89
|
+
static VALUE rb_magic_setflags(VALUE, VALUE);
|
90
|
+
|
91
|
+
static VALUE rb_magic_list(int, VALUE*, VALUE);
|
92
|
+
static VALUE rb_magic_load(int, VALUE*, VALUE);
|
93
|
+
static VALUE rb_magic_check(int, VALUE*, VALUE);
|
94
|
+
static VALUE rb_magic_compile(int, VALUE*, VALUE);
|
95
|
+
|
96
|
+
void Init_ruby_filemagic(void);
|
97
|
+
|
98
|
+
#endif /* FILEMAGIC_H */
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|