mahoro 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/magic.sample +5 -0
  2. data/mahoro.c +171 -42
  3. data/test.rb +34 -0
  4. metadata +5 -14
@@ -0,0 +1,5 @@
1
+ # Ruby scripts
2
+ 0 search/1/b #!\ /usr/bin/ruby Ruby script text executable
3
+ 0 search/1/b #!\ /usr/local/bin/ruby Ruby script text executable
4
+ 0 search/1 #!/usr/bin/env\ ruby Ruby script text executable
5
+ 0 search/1 #!\ /usr/bin/env\ ruby Ruby script text executable
data/mahoro.c CHANGED
@@ -8,21 +8,19 @@
8
8
  #ifndef RSTRING_LEN
9
9
  # define RSTRING_LEN(s)->len
10
10
  #endif
11
-
12
- struct MagicCookie
13
- {
14
- magic_t cookie;
15
- };
11
+ #ifndef RSTRING_PTR
12
+ # define RSTRING_PTR(s)->ptr
13
+ #endif
16
14
 
17
15
  static VALUE cMahoro;
18
16
  static VALUE eMahoroError;
19
17
 
20
18
  static void
21
19
  mahoro_free(ptr)
22
- struct MagicCookie *ptr;
20
+ void *ptr;
23
21
  {
24
- magic_close(ptr->cookie);
25
- xfree(ptr);
22
+ if (ptr)
23
+ magic_close((magic_t)ptr);
26
24
  }
27
25
 
28
26
  static VALUE
@@ -39,14 +37,13 @@ mahoro_initialize(argc, argv, self)
39
37
  {
40
38
  int flags = MAGIC_NONE;
41
39
  char *path = 0;
42
- struct MagicCookie *ptr;
43
40
  magic_t cookie;
44
41
  VALUE vpath, vflags;
45
42
 
46
43
  switch(rb_scan_args(argc, argv, "02", &vflags, &vpath)) {
47
44
  case 2:
48
45
  if(!NIL_P(vpath)) {
49
- path = StringValuePtr(vpath);
46
+ path = StringValueCStr(vpath);
50
47
  }
51
48
  /* fallthrough */
52
49
  case 1:
@@ -63,9 +60,7 @@ mahoro_initialize(argc, argv, self)
63
60
  magic_error(cookie));
64
61
  }
65
62
 
66
- ptr = ALLOC(struct MagicCookie);
67
- ptr->cookie = cookie;
68
- DATA_PTR(self) = ptr;
63
+ DATA_PTR(self) = cookie;
69
64
 
70
65
  return self;
71
66
  }
@@ -75,9 +70,9 @@ mahoro_file(self, path)
75
70
  VALUE self, path;
76
71
  {
77
72
  const char *msg;
78
- magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
73
+ magic_t cookie = (magic_t)DATA_PTR(self);
79
74
 
80
- if(!(msg = magic_file(cookie, StringValuePtr(path)))) {
75
+ if(!(msg = magic_file(cookie, StringValueCStr(path)))) {
81
76
  rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
82
77
  }
83
78
 
@@ -89,10 +84,12 @@ mahoro_buffer(self, input)
89
84
  VALUE self, input;
90
85
  {
91
86
  const char *msg;
92
- magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
87
+ magic_t cookie = (magic_t)DATA_PTR(self);
88
+
89
+ StringValue(input);
93
90
 
94
- if(!(msg = magic_buffer(cookie, StringValuePtr(input),
95
- RSTRING_LEN(StringValue(input))))) {
91
+ if(!(msg = magic_buffer(cookie, RSTRING_PTR(input),
92
+ RSTRING_LEN(input)))) {
96
93
  rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
97
94
  }
98
95
 
@@ -103,7 +100,7 @@ static VALUE
103
100
  mahoro_set_flags(self, flags)
104
101
  VALUE self, flags;
105
102
  {
106
- magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
103
+ magic_t cookie = (magic_t)DATA_PTR(self);
107
104
 
108
105
  return INT2FIX(magic_setflags(cookie, FIX2INT(flags)));
109
106
  }
@@ -115,12 +112,12 @@ mahoro_check(argc, argv, self)
115
112
  {
116
113
  char *path = 0;
117
114
  VALUE vpath;
118
- magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
115
+ magic_t cookie = (magic_t)DATA_PTR(self);
119
116
 
120
117
  switch(rb_scan_args(argc, argv, "01", &vpath)) {
121
118
  case 1:
122
- if(!NIL_P(vpath)) {
123
- path = StringValuePtr(vpath);
119
+ if(!NIL_P(vpath)) {
120
+ path = StringValueCStr(vpath);
124
121
  }
125
122
  break;
126
123
  }
@@ -133,27 +130,34 @@ mahoro_check(argc, argv, self)
133
130
  }
134
131
 
135
132
  static VALUE
136
- mahoro_compile(klass, path)
137
- VALUE klass, path;
133
+ mahoro_compile(self, path)
134
+ VALUE self, path;
138
135
  {
139
- magic_t cookie = magic_open(MAGIC_NONE);
136
+ magic_t cookie = (magic_t)DATA_PTR(self);
140
137
 
141
- if(magic_compile(cookie, StringValuePtr(path))) {
138
+ if(magic_compile(cookie, StringValueCStr(path))) {
142
139
  rb_raise(eMahoroError, "failed compile: %s", magic_error(cookie));
143
140
  }
144
141
 
145
- magic_close(cookie);
146
-
147
142
  return Qtrue;
148
143
  }
149
144
 
145
+ static VALUE
146
+ mahoro_s_compile(klass, path)
147
+ VALUE klass, path;
148
+ {
149
+ VALUE m = rb_funcall(klass, rb_intern("new"), 0, 0);
150
+
151
+ return mahoro_compile(m, path);
152
+ }
153
+
150
154
  static VALUE
151
155
  mahoro_load(self, path)
152
156
  VALUE self, path;
153
157
  {
154
- magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
158
+ magic_t cookie = (magic_t)DATA_PTR(self);
155
159
 
156
- if(magic_load(cookie, StringValuePtr(path))) {
160
+ if(magic_load(cookie, StringValueCStr(path))) {
157
161
  rb_raise(eMahoroError, "failed load: %s", magic_error(cookie));
158
162
  }
159
163
 
@@ -165,18 +169,142 @@ void Init_mahoro(void)
165
169
  cMahoro = rb_define_class("Mahoro", rb_cObject);
166
170
  eMahoroError = rb_define_class_under(cMahoro, "Error", rb_eStandardError);
167
171
 
168
- rb_const_set(cMahoro, rb_intern("NONE"), INT2FIX(MAGIC_NONE));
169
- rb_const_set(cMahoro, rb_intern("DEBUG"), INT2FIX(MAGIC_DEBUG));
170
- rb_const_set(cMahoro, rb_intern("SYMLINK"), INT2FIX(MAGIC_SYMLINK));
171
- rb_const_set(cMahoro, rb_intern("COMPRESS"), INT2FIX(MAGIC_COMPRESS));
172
- rb_const_set(cMahoro, rb_intern("DEVICES"), INT2FIX(MAGIC_DEVICES));
173
- rb_const_set(cMahoro, rb_intern("MIME"), INT2FIX(MAGIC_MIME));
174
- rb_const_set(cMahoro, rb_intern("CONTINUE"), INT2FIX(MAGIC_CONTINUE));
175
- rb_const_set(cMahoro, rb_intern("CHECK"), INT2FIX(MAGIC_CHECK));
176
- rb_const_set(cMahoro, rb_intern("PRESERVE_ATIME"),
172
+ /* No special handling, the default */
173
+ rb_define_const(cMahoro, "NONE", INT2FIX(MAGIC_NONE));
174
+
175
+ /* print debugging messages to stderr */
176
+ rb_define_const(cMahoro, "DEBUG", INT2FIX(MAGIC_DEBUG));
177
+
178
+ /* Follow symlinks */
179
+ rb_define_const(cMahoro, "SYMLINK", INT2FIX(MAGIC_SYMLINK));
180
+
181
+ /* Check inside compressed files */
182
+ rb_define_const(cMahoro, "COMPRESS", INT2FIX(MAGIC_COMPRESS));
183
+
184
+ /* Look at the contents of devices */
185
+ rb_define_const(cMahoro, "DEVICES", INT2FIX(MAGIC_DEVICES));
186
+
187
+ #ifdef MAGIC_MIME_TYPE
188
+ /*
189
+ * Return only the MIME type
190
+ * This constant may not be defined on older systems.
191
+ */
192
+ rb_define_const(cMahoro, "MIME_TYPE", INT2FIX(MAGIC_MIME_TYPE));
193
+ #endif
194
+
195
+ /* Return all matches */
196
+ rb_define_const(cMahoro, "CONTINUE", INT2FIX(MAGIC_CONTINUE));
197
+
198
+ /*
199
+ * Check the magic database for consistency and
200
+ * print warnings to stderr
201
+ */
202
+ rb_define_const(cMahoro, "CHECK", INT2FIX(MAGIC_CHECK));
203
+
204
+ /* preserve access time of files analyzed */
205
+ rb_define_const(cMahoro, "PRESERVE_ATIME",
177
206
  INT2FIX(MAGIC_PRESERVE_ATIME));
178
- rb_const_set(cMahoro, rb_intern("RAW"), INT2FIX(MAGIC_RAW));
179
- rb_const_set(cMahoro, rb_intern("ERROR"), INT2FIX(MAGIC_ERROR));
207
+
208
+ /*
209
+ * Don't translate unprintable characters to a \\ooo octal
210
+ * representation
211
+ */
212
+ rb_define_const(cMahoro, "RAW", INT2FIX(MAGIC_RAW));
213
+
214
+ /*
215
+ * Treat operating system errors while trying to open files
216
+ * and follow symlinks as real errors, instead of printing
217
+ * them in the magic buffer.
218
+ */
219
+ rb_define_const(cMahoro, "ERROR", INT2FIX(MAGIC_ERROR));
220
+
221
+ #ifdef MAGIC_MIME_ENCODING
222
+ /*
223
+ * Return a MIME encoding, instead of a textual description.
224
+ * This constant may not be defined on older systems.
225
+ */
226
+ rb_define_const(cMahoro, "MIME_ENCODING", INT2FIX(MAGIC_MIME_ENCODING));
227
+ #endif
228
+
229
+ /* return both MIME type and encoding */
230
+ rb_define_const(cMahoro, "MIME", INT2FIX(MAGIC_MIME));
231
+
232
+ #ifdef MAGIC_APPLE
233
+ /*
234
+ * Return both Apple creator and type.
235
+ * This constant may not be defined on older systems.
236
+ */
237
+ rb_define_const(cMahoro, "APPLE", INT2FIX(MAGIC_APPLE));
238
+ #endif
239
+
240
+ #ifdef MAGIC_NO_CHECK_COMPRESS
241
+ /*
242
+ * Don't check for or inside compressed files.
243
+ * This constant may not be defined on older systems.
244
+ */
245
+ rb_define_const(cMahoro, "NO_CHECK_COMPRESS",
246
+ INT2FIX(MAGIC_NO_CHECK_COMPRESS));
247
+ #endif
248
+
249
+ #ifdef MAGIC_NO_CHECK_TAR
250
+ /*
251
+ * Don't examine tar files.
252
+ * This constant may not be defined on older systems.
253
+ */
254
+ rb_define_const(cMahoro, "NO_CHECK_TAR", INT2FIX(MAGIC_NO_CHECK_TAR));
255
+ #endif
256
+
257
+ #ifdef MAGIC_NO_CHECK_SOFT
258
+ /*
259
+ * Don't consult magic files.
260
+ * This constant may not be defined on older systems.
261
+ */
262
+ rb_define_const(cMahoro, "NO_CHECK_SOFT", INT2FIX(MAGIC_NO_CHECK_SOFT));
263
+ #endif
264
+
265
+ #ifdef MAGIC_NO_CHECK_APPTYPE
266
+ /*
267
+ * Don't check application type (EMX only).
268
+ * This constant may not be defined on older systems.
269
+ */
270
+ rb_define_const(cMahoro, "NO_CHECK_APPTYPE",
271
+ INT2FIX(MAGIC_NO_CHECK_APPTYPE));
272
+ #endif
273
+
274
+ #ifdef MAGIC_NO_CHECK_ELF
275
+ /*
276
+ * Don't check for ELF details.
277
+ * This constant may not be defined on older systems.
278
+ */
279
+ rb_define_const(cMahoro, "NO_CHECK_ELF", INT2FIX(MAGIC_NO_CHECK_ELF));
280
+ #endif
281
+
282
+ #ifdef MAGIC_NO_CHECK_ASCII
283
+ /*
284
+ * Don't check for various types of ASCII text files.
285
+ * This constant may not be defined on older systems.
286
+ */
287
+ rb_define_const(cMahoro, "NO_CHECK_TEXT",
288
+ INT2FIX(MAGIC_NO_CHECK_ASCII));
289
+ #endif
290
+
291
+ #ifdef MAGIC_NO_CHECK_TOKENS
292
+ /*
293
+ * Don't check for known tokens inside ASCII files.
294
+ * This constant may not be defined on older systems.
295
+ */
296
+ rb_define_const(cMahoro, "NO_CHECK_TOKENS",
297
+ INT2FIX(MAGIC_NO_CHECK_TOKENS));
298
+ #endif
299
+
300
+ #ifdef MAGIC_NO_CHECK_ENCODING
301
+ /*
302
+ * Don't check for text encodings.
303
+ * This constant may not be defined on older systems.
304
+ */
305
+ rb_define_const(cMahoro, "NO_CHECK_ENCODING",
306
+ INT2FIX(MAGIC_NO_CHECK_ENCODING));
307
+ #endif
180
308
 
181
309
  rb_define_alloc_func(cMahoro, mahoro_allocate);
182
310
  rb_define_method(cMahoro, "initialize", mahoro_initialize, -1);
@@ -184,7 +312,8 @@ void Init_mahoro(void)
184
312
  rb_define_method(cMahoro, "buffer", mahoro_buffer, 1);
185
313
  rb_define_method(cMahoro, "flags=", mahoro_set_flags, 1);
186
314
  rb_define_method(cMahoro, "valid?", mahoro_check, -1);
187
- rb_define_singleton_method(cMahoro, "compile", mahoro_compile, 1);
315
+ rb_define_singleton_method(cMahoro, "compile", mahoro_s_compile, 1);
316
+ rb_define_method(cMahoro, "compile", mahoro_compile, 1);
188
317
  rb_define_method(cMahoro, "load", mahoro_load, 1);
189
318
  }
190
319
 
data/test.rb CHANGED
@@ -22,12 +22,27 @@ class MahoroTestCase < Test::Unit::TestCase
22
22
  }.include?(@m.file('mahoro.c')))
23
23
  end
24
24
 
25
+ def test_null_byte_in_path
26
+ assert_raises(ArgumentError) { @m.file("mahoro.c\0foo") }
27
+ end
28
+
25
29
  def test_buffer
26
30
  @m.flags = Mahoro::NONE
27
31
  assert_equal('ASCII C program text',
28
32
  @m.buffer(File.read('mahoro.c')))
29
33
  end
30
34
 
35
+ def test_buffer_string_convert
36
+ tmp = File.read('mahoro.c')
37
+ buf = Struct.new(:to_str).new(tmp)
38
+ assert_equal('ASCII C program text', @m.buffer(buf))
39
+ end
40
+
41
+ def test_buffer_invalid
42
+ @m.flags = Mahoro::NONE
43
+ assert_raises(TypeError) { @m.buffer @m }
44
+ end
45
+
31
46
  def test_mime_buffer
32
47
  @m.flags = Mahoro::MIME
33
48
  assert({
@@ -40,6 +55,25 @@ class MahoroTestCase < Test::Unit::TestCase
40
55
  assert(@m.valid?, 'Default database was not valid.')
41
56
  end
42
57
 
58
+ def test_valid_with_null
59
+ assert_raises(ArgumentError) { @m.valid? "mahoro.c\0" }
60
+ end
61
+
62
+ def test_compile
63
+ File.open(__FILE__) do |fp|
64
+ fp.flock File::LOCK_EX
65
+ assert Mahoro.compile("magic.sample")
66
+ assert_nothing_raised do
67
+ File.unlink("magic.sample.mgc")
68
+ end
69
+ end
70
+ end
71
+
72
+ def test_compile_bad
73
+ assert_raises(ArgumentError) do
74
+ Mahoro.compile "magic.sample\0"
75
+ end
76
+ end
43
77
  end
44
78
 
45
79
  # arch-tag: test
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mahoro
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 2
9
- version: "0.2"
4
+ prerelease:
5
+ version: "0.3"
10
6
  platform: ruby
11
7
  authors:
12
8
  - Shu-yu Guo
@@ -15,7 +11,7 @@ autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
13
 
18
- date: 2011-01-31 00:00:00 +00:00
14
+ date: 2011-03-15 00:00:00 +00:00
19
15
  default_executable:
20
16
  dependencies: []
21
17
 
@@ -36,6 +32,7 @@ extra_rdoc_files: []
36
32
  files:
37
33
  - INSTALL
38
34
  - extconf.rb
35
+ - magic.sample
39
36
  - mahoro.c
40
37
  - mahoro.gemspec
41
38
  - maintainership-transfer.mbox.gz
@@ -54,23 +51,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
51
  requirements:
55
52
  - - ">="
56
53
  - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
54
  version: "0"
61
55
  required_rubygems_version: !ruby/object:Gem::Requirement
62
56
  none: false
63
57
  requirements:
64
58
  - - ">="
65
59
  - !ruby/object:Gem::Version
66
- hash: 3
67
- segments:
68
- - 0
69
60
  version: "0"
70
61
  requirements: []
71
62
 
72
63
  rubyforge_project: mahoro
73
- rubygems_version: 1.3.7
64
+ rubygems_version: 1.6.2
74
65
  signing_key:
75
66
  specification_version: 3
76
67
  summary: An interface to libmagic