ruby-magic 0.4.0 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,11 +10,8 @@ extern "C" {
10
10
 
11
11
  #define MAGIC_SYNCHRONIZED(f, d) magic_lock(object, (f), (d))
12
12
 
13
- #define MAGIC_OBJECT(o) \
14
- TypedData_Get_Struct(object, magic_object_t, &rb_magic_type, (o))
15
-
16
- #define MAGIC_COOKIE(o, c) \
17
- ((c) = MAGIC_OBJECT((o))->cookie)
13
+ #define MAGIC_OBJECT(o, t) \
14
+ TypedData_Get_Struct((o), rb_mgc_object_t, &rb_mgc_type, (t))
18
15
 
19
16
  #define MAGIC_CLOSED_P(o) RTEST(rb_mgc_close_p((o)))
20
17
  #define MAGIC_LOADED_P(o) RTEST(rb_mgc_load_p((o)))
@@ -25,60 +22,61 @@ extern "C" {
25
22
  rb_mgc_warning |= BIT(i); \
26
23
  rb_warn(__VA_ARGS__); \
27
24
  } \
28
- } while(0)
25
+ } while (0)
26
+
27
+ #define MAGIC_ERRORS(t) ruby_magic_errors[(t)]
29
28
 
30
29
  #define MAGIC_ARGUMENT_TYPE_ERROR(o, ...) \
31
- rb_raise(rb_eTypeError, error(E_ARGUMENT_TYPE_INVALID), CLASS_NAME((o)), __VA_ARGS__)
30
+ rb_raise(rb_eTypeError, MAGIC_ERRORS(E_ARGUMENT_TYPE_INVALID), CLASS_NAME((o)), __VA_ARGS__)
32
31
 
33
32
  #define MAGIC_GENERIC_ERROR(k, e, m) \
34
- rb_exc_raise(magic_generic_error((k), (e), error(m)))
33
+ rb_exc_raise(magic_generic_error((k), (e), MAGIC_ERRORS(m)))
35
34
 
36
35
  #define MAGIC_LIBRARY_ERROR(c) \
37
- rb_exc_raise(magic_library_error(rb_mgc_eMagicError, (c)))
36
+ rb_exc_raise(magic_library_error(rb_mgc_eMagicError, (c)->cookie))
38
37
 
39
38
  #define MAGIC_CHECK_INTEGER_TYPE(o) magic_check_type((o), T_FIXNUM)
40
39
  #define MAGIC_CHECK_STRING_TYPE(o) magic_check_type((o), T_STRING)
41
40
 
42
- #define MAGIC_CHECK_ARGUMENT_MISSING(t, o) \
43
- do { \
44
- if ((t) < (o)) \
45
- rb_raise(rb_eArgError, error(E_ARGUMENT_MISSING), (t), (o)); \
46
- } while(0)
47
-
48
- #define MAGIC_CHECK_ARRAY_EMPTY(o) \
49
- do { \
50
- if (RARRAY_EMPTY_P(o)) \
51
- rb_raise(rb_eArgError, "%s", error(E_ARGUMENT_TYPE_ARRAY_EMPTY)); \
52
- } while(0)
53
-
54
41
  #define MAGIC_CHECK_ARRAY_OF_STRINGS(o) \
55
42
  magic_check_type_array_of_strings((o))
56
43
 
44
+ #define MAGIC_CHECK_ARGUMENT_MISSING(t, o) \
45
+ do { \
46
+ if ((t) < (o)) \
47
+ rb_raise(rb_eArgError, MAGIC_ERRORS(E_ARGUMENT_MISSING), (t), (o)); \
48
+ } while (0)
49
+
50
+ #define MAGIC_CHECK_ARRAY_EMPTY(o) \
51
+ do { \
52
+ if (RARRAY_EMPTY_P(o)) \
53
+ rb_raise(rb_eArgError, "%s", MAGIC_ERRORS(E_ARGUMENT_TYPE_ARRAY_EMPTY)); \
54
+ } while (0)
55
+
56
+
57
57
  #define MAGIC_CHECK_OPEN(o) \
58
58
  do { \
59
59
  if (MAGIC_CLOSED_P(o)) \
60
60
  MAGIC_GENERIC_ERROR(rb_mgc_eLibraryError, EFAULT, \
61
61
  E_MAGIC_LIBRARY_CLOSED); \
62
- } while(0)
62
+ } while (0)
63
63
 
64
64
  #define MAGIC_CHECK_LOADED(o) \
65
65
  do { \
66
66
  if (!MAGIC_LOADED_P(o)) \
67
67
  MAGIC_GENERIC_ERROR(rb_mgc_eMagicError, EFAULT, \
68
68
  E_MAGIC_LIBRARY_NOT_LOADED); \
69
- } while(0)
69
+ } while (0)
70
70
 
71
71
  #define MAGIC_STRINGIFY(s) #s
72
72
 
73
73
  #define MAGIC_DEFINE_FLAG(c) \
74
- rb_define_const(rb_cMagic, MAGIC_STRINGIFY(c), INT2NUM(MAGIC_##c));
74
+ rb_define_const(rb_cMagic, MAGIC_STRINGIFY(c), INT2NUM(MAGIC_##c))
75
75
 
76
76
  #define MAGIC_DEFINE_PARAMETER(c) \
77
- rb_define_const(rb_cMagic, MAGIC_STRINGIFY(PARAM_##c), INT2NUM(MAGIC_PARAM_##c));
77
+ rb_define_const(rb_cMagic, MAGIC_STRINGIFY(PARAM_##c), INT2NUM(MAGIC_PARAM_##c))
78
78
 
79
- #define error(t) errors[(t)]
80
-
81
- enum error {
79
+ enum ruby_magic_error {
82
80
  E_UNKNOWN = 0,
83
81
  E_NOT_ENOUGH_MEMORY,
84
82
  E_ARGUMENT_MISSING,
@@ -95,49 +93,48 @@ enum error {
95
93
  E_FLAG_INVALID_TYPE
96
94
  };
97
95
 
98
- typedef struct parameter {
99
- size_t value;
96
+ struct parameter {
100
97
  int tag;
101
- } parameter_t;
98
+ size_t value;
99
+ };
102
100
 
103
- typedef union file {
101
+ union file {
104
102
  const char *path;
105
103
  int fd;
106
- } file_t;
104
+ };
107
105
 
108
- typedef struct buffers {
106
+ struct buffers {
109
107
  size_t count;
110
108
  size_t *sizes;
111
109
  void **pointers;
112
- } buffers_t;
110
+ };
113
111
 
114
112
  typedef struct magic_object {
115
113
  magic_t cookie;
116
114
  VALUE mutex;
117
115
  unsigned int database_loaded:1;
118
116
  unsigned int stop_on_errors:1;
119
- } magic_object_t;
117
+ } rb_mgc_object_t;
120
118
 
121
119
  typedef struct magic_arguments {
120
+ rb_mgc_object_t *magic_object;
122
121
  union {
123
- file_t file;
124
- parameter_t parameter;
125
- buffers_t buffers;
126
- } type;
127
- magic_t cookie;
122
+ struct parameter parameter;
123
+ union file file;
124
+ struct buffers buffers;
125
+ };
128
126
  const char *result;
129
- int flags;
130
127
  int status;
131
- unsigned int stop_on_errors:1;
132
- } magic_arguments_t;
128
+ int flags;
129
+ } rb_mgc_arguments_t;
133
130
 
134
- typedef struct magic_exception {
131
+ typedef struct magic_error {
135
132
  const char *magic_error;
136
133
  VALUE klass;
137
134
  int magic_errno;
138
- } magic_exception_t;
135
+ } rb_mgc_error_t;
139
136
 
140
- static const char *errors[] = {
137
+ static const char * const ruby_magic_errors[] = {
141
138
  [E_UNKNOWN] = "an unknown error has occurred",
142
139
  [E_NOT_ENOUGH_MEMORY] = "cannot allocate memory",
143
140
  [E_ARGUMENT_MISSING] = "wrong number of arguments (given %d, expected %d)",
@@ -156,7 +153,7 @@ static const char *errors[] = {
156
153
  };
157
154
 
158
155
  #if defined(MAGIC_CUSTOM_CHECK_TYPE)
159
- static const char *ruby_types[] = {
156
+ static const char * const magic_ruby_types[] = {
160
157
  "", /* Not an object */
161
158
  [T_OBJECT] = "Object",
162
159
  [T_CLASS] = "Class",
@@ -188,10 +185,10 @@ magic_ruby_type_name(int type)
188
185
  {
189
186
  const char *name;
190
187
 
191
- if (type >= ARRAY_SIZE(ruby_types))
188
+ if (type >= ARRAY_SIZE(magic_ruby_types))
192
189
  return NULL;
193
190
 
194
- name = ruby_types[type];
191
+ name = magic_ruby_types[type];
195
192
  if (name)
196
193
  return name;
197
194
 
@@ -232,48 +229,81 @@ magic_check_ruby_type(VALUE object, int type)
232
229
 
233
230
  name = magic_ruby_type_name(type);
234
231
  if (name)
235
- rb_raise(rb_eTypeError, error(E_ARGUMENT_TYPE_INVALID),
232
+ rb_raise(rb_eTypeError,
233
+ MAGIC_ERRORS(E_ARGUMENT_TYPE_INVALID),
236
234
  magic_ruby_class_name(object),
237
235
  name);
238
236
  error:
239
- rb_raise(rb_eTypeError, error(E_ARGUMENT_TYPE_UNKNOWN),
237
+ rb_raise(rb_eTypeError,
238
+ MAGIC_ERRORS(E_ARGUMENT_TYPE_UNKNOWN),
240
239
  object_type,
241
240
  type);
242
241
  }
243
242
  #endif /* MAGIC_CUSTOM_CHECK_TYPE */
244
243
 
244
+ static inline VALUE
245
+ magic_strip(VALUE v)
246
+ {
247
+ return (ARRAY_P(v) || STRING_P(v)) ?
248
+ rb_funcall(v, rb_intern("strip"), 0) :
249
+ Qnil;
250
+ }
251
+
245
252
  static inline VALUE
246
253
  magic_shift(VALUE v)
247
254
  {
248
- return ARRAY_P(v) ? \
249
- rb_funcall(v, rb_intern("shift"), 0) : \
255
+ return ARRAY_P(v) ?
256
+ rb_funcall(v, rb_intern("shift"), 0) :
250
257
  Qnil;
251
258
  }
252
259
 
253
260
  static inline VALUE
254
261
  magic_split(VALUE a, VALUE b)
255
262
  {
256
- return (STRING_P(a) && STRING_P(b)) ? \
257
- rb_funcall(a, rb_intern("split"), 1, b) : \
263
+ return (STRING_P(a) && STRING_P(b)) ?
264
+ rb_funcall(a, rb_intern("split"), 1, b) :
258
265
  Qnil;
259
266
  }
260
267
 
261
268
  static inline VALUE
262
269
  magic_join(VALUE a, VALUE b)
263
270
  {
264
- return (ARRAY_P(a) && STRING_P(b)) ? \
265
- rb_funcall(a, rb_intern("join"), 1, b) : \
271
+ return (ARRAY_P(a) && STRING_P(b)) ?
272
+ rb_funcall(a, rb_intern("join"), 1, b) :
266
273
  Qnil;
267
274
  }
268
275
 
269
276
  static inline VALUE
270
277
  magic_flatten(VALUE v)
271
278
  {
272
- return ARRAY_P(v) ? \
273
- rb_funcall(v, rb_intern("flatten"), 0) : \
279
+ return ARRAY_P(v) ?
280
+ rb_funcall(v, rb_intern("flatten"), 0) :
274
281
  Qnil;
275
282
  }
276
283
 
284
+ static inline VALUE
285
+ magic_strip_array(VALUE value) {
286
+ VALUE entry;
287
+ VALUE array = rb_ary_new();
288
+
289
+ for (int i = 0; i < RARRAY_LEN(value); i++) {
290
+ entry = rb_ary_entry(value, i);
291
+ if (NIL_P(entry))
292
+ continue;
293
+
294
+ if (STRING_P(entry)) {
295
+ if (RSTRING_EMPTY_P(entry))
296
+ continue;
297
+
298
+ entry = magic_strip(entry);
299
+ }
300
+
301
+ rb_ary_push(array, entry);
302
+ }
303
+
304
+ return array;
305
+ }
306
+
277
307
  static int
278
308
  magic_fileno(VALUE object)
279
309
  {
@@ -289,7 +319,9 @@ magic_fileno(VALUE object)
289
319
  object = rb_convert_type(object, T_FILE, "IO", "to_io");
290
320
 
291
321
  GetOpenFile(object, io);
292
- if ((fd = FPTR_TO_FD(io)) < 0)
322
+
323
+ fd = FPTR_TO_FD(io);
324
+ if (fd < 0)
293
325
  rb_raise(rb_eIOError, "closed stream");
294
326
 
295
327
  return fd;
@@ -316,7 +348,7 @@ magic_path(VALUE object)
316
348
  static inline void
317
349
  magic_check_type(VALUE object, RVALUE_TYPE type)
318
350
  {
319
- VALUE boolean = Qundef;
351
+ VALUE boolean;
320
352
 
321
353
  boolean = rb_obj_is_kind_of(object, T_INTEGER);
322
354
  if (type == T_FIXNUM && !RVAL2CBOOL(boolean))
@@ -328,35 +360,17 @@ magic_check_type(VALUE object, RVALUE_TYPE type)
328
360
  static inline void
329
361
  magic_check_type_array_of_strings(VALUE object)
330
362
  {
331
- VALUE value = Qundef;
363
+ VALUE value;
332
364
 
333
365
  for (int i = 0; i < RARRAY_LEN(object); i++) {
334
366
  value = RARRAY_AREF(object, (long)i);
335
367
  if (NIL_P(value) || !STRING_P(value))
336
368
  rb_raise(rb_eTypeError,
337
- error(E_ARGUMENT_TYPE_ARRAY_STRINGS),
369
+ MAGIC_ERRORS(E_ARGUMENT_TYPE_ARRAY_STRINGS),
338
370
  CLASS_NAME(value));
339
371
  }
340
372
  }
341
373
 
342
- static int rb_mgc_do_not_auto_load;
343
- static int rb_mgc_do_not_stop_on_error;
344
- static int rb_mgc_warning;
345
-
346
- static ID id_at_flags;
347
- static ID id_at_paths;
348
-
349
- static VALUE rb_cMagic;
350
-
351
- static VALUE rb_mgc_eError;
352
- static VALUE rb_mgc_eMagicError;
353
- static VALUE rb_mgc_eLibraryError;
354
- static VALUE rb_mgc_eNotImplementedError;
355
- static VALUE rb_mgc_eParameterError;
356
- static VALUE rb_mgc_eFlagsError;
357
-
358
- static const rb_data_type_t rb_magic_type;
359
-
360
374
  void Init_magic(void);
361
375
 
362
376
  VALUE rb_mgc_get_do_not_auto_load_global(VALUE object);
@@ -394,59 +408,6 @@ VALUE rb_mgc_descriptor(VALUE object, VALUE value);
394
408
 
395
409
  VALUE rb_mgc_version(VALUE object);
396
410
 
397
- static VALUE magic_get_parameter_internal(void *data);
398
- static VALUE magic_set_parameter_internal(void *data);
399
-
400
- static VALUE magic_get_flags_internal(void *data);
401
- static VALUE magic_set_flags_internal(void *data);
402
-
403
- static VALUE magic_load_internal(void *data);
404
- static VALUE magic_load_buffers_internal(void *data);
405
-
406
- static VALUE magic_compile_internal(void *data);
407
- static VALUE magic_check_internal(void *data);
408
-
409
- static VALUE magic_file_internal(void *data);
410
- static VALUE magic_buffer_internal(void *data);
411
- static VALUE magic_descriptor_internal(void *data);
412
-
413
- static VALUE magic_close_internal(void *data);
414
-
415
- static void* nogvl_magic_load(void *data);
416
- static void* nogvl_magic_compile(void *data);
417
- static void* nogvl_magic_check(void *data);
418
- static void* nogvl_magic_file(void *data);
419
- static void* nogvl_magic_descriptor(void *data);
420
-
421
- static void* magic_library_open(void);
422
- static void magic_library_close(void *data);
423
-
424
- static VALUE magic_allocate(VALUE klass);
425
- static void magic_mark(void *data);
426
- static void magic_free(void *data);
427
- static size_t magic_size(const void *data);
428
- #if defined(HAVE_RUBY_GC_COMPACT)
429
- static void magic_compact(void *data);
430
- #endif
431
-
432
- static VALUE magic_exception_wrapper(VALUE value);
433
- static VALUE magic_exception(void *data);
434
-
435
- static VALUE magic_library_error(VALUE klass, void *data);
436
- static VALUE magic_generic_error(VALUE klass, int magic_errno,
437
- const char *magic_error);
438
-
439
- static VALUE magic_lock(VALUE object, VALUE (*function)(ANYARGS),
440
- void *data);
441
- static VALUE magic_unlock(VALUE object);
442
-
443
- static VALUE magic_return(void *data);
444
-
445
- static int magic_get_flags(VALUE object);
446
- static int magic_set_flags(VALUE object, VALUE value);
447
-
448
- static VALUE magic_set_paths(VALUE object, VALUE value);
449
-
450
411
  #if defined(__cplusplus)
451
412
  }
452
413
  #endif
data/lib/magic/version.rb CHANGED
@@ -4,7 +4,7 @@ class Magic
4
4
  #
5
5
  # Current version of _Magic_.
6
6
  #
7
- VERSION = '0.4.0'.freeze
7
+ VERSION = '0.5.3'.freeze
8
8
 
9
9
  class << self
10
10
  #
data/lib/magic.rb CHANGED
@@ -1,6 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'magic/magic'
3
+ begin
4
+ ::RUBY_VERSION =~ /(\d+\.\d+)/
5
+ require_relative "magic/#{Regexp.last_match(1)}/magic"
6
+ rescue LoadError
7
+ # use "require" instead of "require_relative" because non-native gems will place C extension files
8
+ # in Gem::BasicSpecification#extension_dir after compilation (during normal installation), which
9
+ # is in $LOAD_PATH but not necessarily relative to this file (see #21 and nokogiri#2300)
10
+ require 'magic/magic'
11
+ end
12
+
4
13
  require_relative 'magic/version'
5
14
  require_relative 'magic/core/file'
6
15
  require_relative 'magic/core/string'
@@ -95,6 +104,8 @@ class Magic
95
104
  open(Magic::MIME, &block)
96
105
  end
97
106
 
107
+ alias_method :mime_type, :mime
108
+
98
109
  #
99
110
  # call-seq:
100
111
  # Magic.type -> self
@@ -175,6 +186,13 @@ class Magic
175
186
  end
176
187
 
177
188
  alias_method :fd, :descriptor
189
+
190
+ private
191
+
192
+ def default_paths
193
+ paths = Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), "../ext/magic/share/*.mgc")))
194
+ paths.empty? ? nil : paths
195
+ end
178
196
  end
179
197
 
180
198
  private
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Wilczyński
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
- cert_chain:
11
- - kwilczynski-public.pem
12
- date: 2021-04-08 00:00:00.000000000 Z
10
+ cert_chain: []
11
+ date: 2021-12-22 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: mini_portile2
@@ -17,14 +16,14 @@ dependencies:
17
16
  requirements:
18
17
  - - "~>"
19
18
  - !ruby/object:Gem::Version
20
- version: 2.5.0
19
+ version: '2.6'
21
20
  type: :runtime
22
21
  prerelease: false
23
22
  version_requirements: !ruby/object:Gem::Requirement
24
23
  requirements:
25
24
  - - "~>"
26
25
  - !ruby/object:Gem::Version
27
- version: 2.5.0
26
+ version: '2.6'
28
27
  description: |
29
28
  File Magic in Ruby.
30
29
 
@@ -54,8 +53,6 @@ files:
54
53
  - lib/magic/core/file.rb
55
54
  - lib/magic/core/string.rb
56
55
  - lib/magic/version.rb
57
- - patches/libmagic/0001-Don-t-attempt-to-build-tests-documentation-and-Python-bindings.patch
58
- - ports/archives/file-5.39.tar.gz
59
56
  homepage: https://github.com/kwilczynski/ruby-magic
60
57
  licenses:
61
58
  - Apache-2.0
@@ -67,7 +64,7 @@ metadata:
67
64
  wiki_uri: https://github.com/kwilczynski/ruby-magic/wiki
68
65
  post_install_message: 'Thank you for installing!
69
66
 
70
- '
67
+ '
71
68
  rdoc_options: []
72
69
  require_paths:
73
70
  - lib
@@ -83,8 +80,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
80
  - !ruby/object:Gem::Version
84
81
  version: '0'
85
82
  requirements: []
86
- rubygems_version: 3.1.4
87
- signing_key:
83
+ rubygems_version: 3.2.28
84
+ signing_key:
88
85
  specification_version: 4
89
86
  summary: File Magic in Ruby
90
87
  test_files: []
@@ -1,44 +0,0 @@
1
- From 0f2752ad4d7ea65c80d76493899aca67e90be524 Mon Sep 17 00:00:00 2001
2
- From: =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= <kw@linux.com>
3
- Date: Mon, 5 Apr 2021 16:51:59 +0000
4
- Subject: [PATCH] Don't attempt to build tests, documentation and Python
5
- bindings
6
- MIME-Version: 1.0
7
- Content-Type: text/plain; charset=UTF-8
8
- Content-Transfer-Encoding: 8bit
9
-
10
- Signed-off-by: Krzysztof Wilczyński <kw@linux.com>
11
- ---
12
- Makefile.am | 2 +-
13
- Makefile.in | 2 +-
14
- 2 files changed, 2 insertions(+), 2 deletions(-)
15
-
16
- diff --git a/Makefile.am b/Makefile.am
17
- index 2ab67ed..c7fafeb 100644
18
- --- a/Makefile.am
19
- +++ b/Makefile.am
20
- @@ -2,7 +2,7 @@ ACLOCAL_AMFLAGS = -I m4
21
-
22
- EXTRA_DIST = MAINT
23
-
24
- -SUBDIRS = src magic tests doc python
25
- +SUBDIRS = src magic
26
-
27
- # This variable must have 'exec' in its name, in order to be installed
28
- # by 'install-exec' target (instead of default 'install-data')
29
- diff --git a/Makefile.in b/Makefile.in
30
- index e19b15e..dcb5ce6 100644
31
- --- a/Makefile.in
32
- +++ b/Makefile.in
33
- @@ -358,7 +358,7 @@ top_builddir = @top_builddir@
34
- top_srcdir = @top_srcdir@
35
- ACLOCAL_AMFLAGS = -I m4
36
- EXTRA_DIST = MAINT
37
- -SUBDIRS = src magic tests doc python
38
- +SUBDIRS = src magic
39
-
40
- # This variable must have 'exec' in its name, in order to be installed
41
- # by 'install-exec' target (instead of default 'install-data')
42
- --
43
- 2.31.0
44
-
Binary file