ruby-magic 0.2.0 → 0.3.0
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 +5 -5
- data/AUTHORS +1 -1
- data/CHANGELOG.md +60 -0
- data/COPYRIGHT +1 -1
- data/NOTICE +466 -0
- data/README.md +43 -0
- data/VERSION +1 -1
- data/ext/magic/common.h +80 -47
- data/ext/magic/extconf.rb +141 -82
- data/ext/magic/functions.c +251 -236
- data/ext/magic/functions.h +44 -88
- data/ext/magic/ruby-magic.c +1235 -627
- data/ext/magic/ruby-magic.h +222 -159
- data/kwilczynski-public.pem +25 -0
- data/lib/magic.rb +78 -89
- data/lib/magic/core/file.rb +9 -66
- data/lib/magic/core/string.rb +1 -43
- data/lib/magic/version.rb +14 -48
- metadata +24 -25
- data/CHANGES +0 -50
- data/CHANGES.rdoc +0 -50
- data/README +0 -1
- data/README.rdoc +0 -8
- data/Rakefile +0 -79
- data/TODO +0 -24
- data/bin/magic +0 -33
- data/ruby-magic.gemspec +0 -62
- data/test/helpers/magic_test_helper.rb +0 -35
- data/test/test_constants.rb +0 -96
- data/test/test_magic.rb +0 -461
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# File Magic in Ruby
|
2
|
+
|
3
|
+
[](https://travis-ci.org/kwilczynski/ruby-magic)
|
4
|
+
[](https://inch-ci.org/github/kwilczynski/ruby-magic)
|
5
|
+
[](http://badge.fury.io/rb/ruby-magic)
|
6
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
7
|
+
|
8
|
+
## Introduction
|
9
|
+
|
10
|
+
Provides simple interface to [libmagic][1] for Ruby Programming Language.
|
11
|
+
|
12
|
+
## Table of Contents
|
13
|
+
|
14
|
+
1. [Contributing](#contributing)
|
15
|
+
2. [Versioning](#versioning)
|
16
|
+
3. [Author](#author)
|
17
|
+
4. [Copyright](#copyright)
|
18
|
+
5. [License](#license)
|
19
|
+
|
20
|
+
## Contributing
|
21
|
+
|
22
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for best practices and instructions on
|
23
|
+
setting up your development environment.
|
24
|
+
|
25
|
+
## Versioning
|
26
|
+
|
27
|
+
This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
28
|
+
For the versions available, see the tags on this repository.
|
29
|
+
|
30
|
+
## Author
|
31
|
+
|
32
|
+
Krzysztof Wilczyński (<kw@linux.com>)
|
33
|
+
|
34
|
+
## Copyright
|
35
|
+
|
36
|
+
Copyright 2013-2021 Krzysztof Wilczyński
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
This project is licensed under the terms of the Apache License, Version 2.0 license.
|
41
|
+
To view the full license see the included [LICENSE](LICENSE) file.
|
42
|
+
|
43
|
+
[1]: https://en.wikipedia.org/wiki/File_(command)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/ext/magic/common.h
CHANGED
@@ -1,23 +1,3 @@
|
|
1
|
-
/* :enddoc: */
|
2
|
-
|
3
|
-
/*
|
4
|
-
* common.h
|
5
|
-
*
|
6
|
-
* Copyright 2013-2015 Krzysztof Wilczynski
|
7
|
-
*
|
8
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
-
* you may not use this file except in compliance with the License.
|
10
|
-
* You may obtain a copy of the License at
|
11
|
-
*
|
12
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
13
|
-
*
|
14
|
-
* Unless required by applicable law or agreed to in writing, software
|
15
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
-
* See the License for the specific language governing permissions and
|
18
|
-
* limitations under the License.
|
19
|
-
*/
|
20
|
-
|
21
1
|
#if !defined(_COMMON_H)
|
22
2
|
#define _COMMON_H 1
|
23
3
|
|
@@ -29,12 +9,17 @@
|
|
29
9
|
# define _BSD_SOURCE 1
|
30
10
|
#endif
|
31
11
|
|
12
|
+
#if !defined(_DEFAULT_SOURCE)
|
13
|
+
# define _DEFAULT_SOURCE 1
|
14
|
+
#endif
|
15
|
+
|
32
16
|
#if defined(__cplusplus)
|
33
17
|
extern "C" {
|
34
18
|
#endif
|
35
19
|
|
36
20
|
#include <stdio.h>
|
37
21
|
#include <stdlib.h>
|
22
|
+
#include <limits.h>
|
38
23
|
#include <string.h>
|
39
24
|
#include <unistd.h>
|
40
25
|
#include <fcntl.h>
|
@@ -44,50 +29,100 @@ extern "C" {
|
|
44
29
|
#include <magic.h>
|
45
30
|
#include <ruby.h>
|
46
31
|
|
47
|
-
#if defined(
|
48
|
-
# include <
|
32
|
+
#if defined(HAVE_RUBY_IO_H)
|
33
|
+
# include <ruby/io.h>
|
34
|
+
#else
|
35
|
+
# include <rubyio.h>
|
49
36
|
#endif
|
50
37
|
|
51
|
-
#if defined(
|
52
|
-
#
|
38
|
+
#if !defined(BIT)
|
39
|
+
# define BIT(n) (1 << (n))
|
53
40
|
#endif
|
54
41
|
|
55
|
-
#if !defined(
|
56
|
-
# define
|
42
|
+
#if !defined(UNUSED)
|
43
|
+
# define UNUSED(x) (void)(x)
|
57
44
|
#endif
|
58
45
|
|
59
|
-
#if
|
60
|
-
# define
|
46
|
+
#if defined(F_DUPFD_CLOEXEC)
|
47
|
+
# define HAVE_F_DUPFD_CLOEXEC 1
|
61
48
|
#endif
|
62
49
|
|
63
|
-
#if
|
64
|
-
# define
|
50
|
+
#if defined(O_CLOEXEC)
|
51
|
+
# define HAVE_O_CLOEXEC 1
|
65
52
|
#endif
|
66
53
|
|
67
|
-
#if
|
68
|
-
# define
|
54
|
+
#if defined(POSIX_CLOSE_RESTART)
|
55
|
+
# define HAVE_POSIX_CLOSE_RESTART 1
|
69
56
|
#endif
|
70
57
|
|
71
|
-
#
|
72
|
-
#
|
73
|
-
|
74
|
-
#
|
75
|
-
#
|
76
|
-
# endif
|
58
|
+
#define MAGIC_EXTENSION_SEPARATOR "/"
|
59
|
+
#define MAGIC_CONTINUE_SEPARATOR "\n- "
|
60
|
+
|
61
|
+
#if !defined(MAGIC_NO_CHECK_CSV)
|
62
|
+
# define MAGIC_NO_CHECK_CSV 0
|
77
63
|
#endif
|
78
64
|
|
79
|
-
#if defined(
|
80
|
-
#
|
65
|
+
#if !defined(MAGIC_NO_CHECK_JSON)
|
66
|
+
# define MAGIC_NO_CHECK_JSON 0
|
67
|
+
#endif
|
68
|
+
|
69
|
+
#define DATA_P(x) (RB_TYPE_P((x), T_DATA))
|
70
|
+
#define BOOLEAN_P(x) (RB_TYPE_P((x), T_TRUE) || RB_TYPE_P((x), T_FALSE))
|
71
|
+
#define STRING_P(x) (RB_TYPE_P((x), T_STRING))
|
72
|
+
#define ARRAY_P(x) (RB_TYPE_P((x), T_ARRAY))
|
73
|
+
#define FILE_P(x) (RB_TYPE_P((x), T_FILE))
|
74
|
+
|
75
|
+
#define RVAL2CBOOL(x) (RTEST(x))
|
76
|
+
#define CBOOL2RVAL(x) ((x) ? Qtrue : Qfalse)
|
77
|
+
|
78
|
+
#define RVAL2CSTR(x) (NIL_P(x) ? NULL : StringValueCStr(x))
|
79
|
+
#define CSTR2RVAL(x) ((x) == NULL ? Qnil : rb_str_new2(x))
|
80
|
+
|
81
|
+
#define RARRAY_EMPTY rb_ary_new()
|
82
|
+
#define RARRAY_EMPTY_P(a) (RARRAY_LEN(a) == 0)
|
83
|
+
#define RARRAY_FIRST(a) (RARRAY_EMPTY_P(a) ? Qnil : rb_ary_entry((a), 0))
|
84
|
+
|
85
|
+
#define CLASS_NAME(o) (NIL_P((o)) ? "nil" : rb_obj_classname((o)))
|
86
|
+
|
87
|
+
#if !defined(T_INTEGER)
|
88
|
+
# define T_INTEGER rb_cInteger
|
81
89
|
#endif
|
82
90
|
|
83
|
-
#
|
91
|
+
#if !defined(HAVE_RB_IO_T)
|
92
|
+
# define rb_io_t OpenFile
|
93
|
+
#endif
|
84
94
|
|
85
|
-
#if !defined(
|
86
|
-
# define
|
95
|
+
#if !defined(GetReadFile)
|
96
|
+
# define FPTR_TO_FD(p) ((p)->fd)
|
97
|
+
#else
|
98
|
+
# define FPTR_TO_FD(p) (fileno(GetReadFile(p)))
|
87
99
|
#endif
|
88
100
|
|
89
|
-
#
|
90
|
-
|
101
|
+
#define NOGVL_FUNCTION (VALUE (*)(void *))
|
102
|
+
|
103
|
+
#if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL) && \
|
104
|
+
defined(HAVE_RUBY_THREAD_H) && HAVE_RUBY_THREAD_H
|
105
|
+
# include <ruby/thread.h>
|
106
|
+
# define NOGVL(f, d) \
|
107
|
+
rb_thread_call_without_gvl((f), (d), RUBY_UBF_IO, NULL)
|
108
|
+
#elif defined(HAVE_RB_THREAD_BLOCKING_REGION)
|
109
|
+
# define NOGVL(f, d) \
|
110
|
+
rb_thread_blocking_region(NOGVL_FUNCTION(f), (d), RUBY_UBF_IO, NULL)
|
111
|
+
#else
|
112
|
+
# include <rubysig.h>
|
113
|
+
static inline VALUE
|
114
|
+
fake_blocking_region(VALUE (*f)(ANYARGS), void *data)
|
115
|
+
{
|
116
|
+
VALUE rv;
|
117
|
+
|
118
|
+
TRAP_BEG;
|
119
|
+
rv = f(data);
|
120
|
+
TRAP_END;
|
121
|
+
|
122
|
+
return rv;
|
123
|
+
}
|
124
|
+
# define NOGVL(f, d) \
|
125
|
+
fake_blocking_region(NOGVL_FUNCTION(f), (d))
|
91
126
|
#endif
|
92
127
|
|
93
128
|
#if defined(__cplusplus)
|
@@ -95,5 +130,3 @@ extern "C" {
|
|
95
130
|
#endif
|
96
131
|
|
97
132
|
#endif /* _COMMON_H */
|
98
|
-
|
99
|
-
/* vim: set ts=8 sw=4 sts=2 et : */
|
data/ext/magic/extconf.rb
CHANGED
@@ -1,135 +1,194 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
# :enddoc:
|
4
|
-
|
5
|
-
#
|
6
|
-
# extconf.rb
|
7
|
-
#
|
8
|
-
# Copyright 2013-2015 Krzysztof Wilczynski
|
9
|
-
#
|
10
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
11
|
-
# you may not use this file except in compliance with the License.
|
12
|
-
# You may obtain a copy of the License at
|
13
|
-
#
|
14
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
15
|
-
#
|
16
|
-
# Unless required by applicable law or agreed to in writing, software
|
17
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
18
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
19
|
-
# See the License for the specific language governing permissions and
|
20
|
-
# limitations under the License.
|
21
|
-
#
|
1
|
+
# frozen_string_literal: true
|
22
2
|
|
23
3
|
require 'mkmf'
|
4
|
+
require 'digest'
|
5
|
+
require 'open-uri'
|
6
|
+
|
7
|
+
LIBMAGIC_TAG = '5.39'.freeze
|
8
|
+
|
9
|
+
workdir = Dir.pwd
|
10
|
+
libdir = File.join(workdir, 'file-' + LIBMAGIC_TAG)
|
11
|
+
gemdir = File.expand_path(File.join(__dir__, '../..'))
|
12
|
+
gem_ext_dir = File.join(gemdir, 'lib', 'ext')
|
13
|
+
gem_include_dir = File.join(gem_ext_dir, 'include')
|
14
|
+
gem_lib_dir = File.join(gem_ext_dir, 'lib')
|
15
|
+
build_lib_dir = File.join(libdir, 'src', '.libs')
|
16
|
+
|
17
|
+
expected_sha256 = 'f05d286a76d9556243d0cb05814929c2ecf3a5ba07963f8f70bfaaa70517fad1'
|
18
|
+
filename = "#{workdir}/file.tar.gz"
|
19
|
+
|
20
|
+
unless File.exist?(filename)
|
21
|
+
File.open(filename, 'wb') do |target_file|
|
22
|
+
URI.open("https://fossies.org/linux/misc/file-#{LIBMAGIC_TAG}.tar.gz", 'rb') do |read_file|
|
23
|
+
target_file.write(read_file.read)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
checksum = Digest::SHA256.hexdigest(File.read(filename))
|
28
|
+
|
29
|
+
if checksum != expected_sha256
|
30
|
+
raise "SHA256 of #{filename} does not match: got #{checksum}, expected #{expected_sha256}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
system("tar -xzf #{filename}") || raise('ERROR')
|
35
|
+
system("cd #{libdir} && ./configure --prefix=#{gem_ext_dir} && make install") || raise('ERROR')
|
36
|
+
|
37
|
+
$LOCAL_LIBS << '-lmagic'
|
38
|
+
$LIBPATH << gem_lib_dir
|
39
|
+
$CFLAGS << " -I #{libdir}/src"
|
24
40
|
|
25
|
-
|
41
|
+
def darwin?
|
42
|
+
RbConfig::CONFIG['target_os'] =~ /darwin/
|
43
|
+
end
|
26
44
|
|
27
|
-
|
45
|
+
def windows?
|
46
|
+
RbConfig::CONFIG['target_os'] =~ /mswin|mingw32|windows/
|
47
|
+
end
|
28
48
|
|
29
|
-
|
30
|
-
|
49
|
+
if ENV['CC']
|
50
|
+
RbConfig::CONFIG['CC'] = RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC']
|
31
51
|
end
|
32
52
|
|
33
|
-
|
53
|
+
ENV['CC'] = RbConfig::CONFIG['CC']
|
54
|
+
|
55
|
+
$CFLAGS += ' -std=c99 -fPIC'
|
56
|
+
$CFLAGS += ' -Wall -Wextra -pedantic'
|
34
57
|
|
35
|
-
|
36
|
-
$CFLAGS
|
58
|
+
if RbConfig::CONFIG['CC'] =~ /gcc/
|
59
|
+
$CFLAGS += ' -O3' unless $CFLAGS =~ /-O\d/
|
60
|
+
$CFLAGS += ' -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline'
|
37
61
|
end
|
38
62
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
63
|
+
unless darwin?
|
64
|
+
$LDFLAGS += ' -Wl,--as-needed -Wl,--no-undefined'
|
65
|
+
end
|
66
|
+
|
67
|
+
if windows?
|
68
|
+
$LDFLAGS += ' -static-libgcc'
|
69
|
+
end
|
70
|
+
|
71
|
+
%w[
|
72
|
+
CFLAGS
|
73
|
+
CXXFLAGS
|
74
|
+
CPPFLAGS
|
75
|
+
].each do |variable|
|
76
|
+
$CFLAGS += format(' %s', ENV[variable]) if ENV[variable]
|
77
|
+
end
|
44
78
|
|
45
|
-
|
46
|
-
have_magic_h = have_header('magic.h')
|
79
|
+
$LDFLAGS += format(' %s', ENV['LDFLAGS']) if ENV['LDFLAGS']
|
47
80
|
|
48
|
-
|
49
|
-
|
50
|
-
|
81
|
+
unless have_header('ruby.h')
|
82
|
+
abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
|
83
|
+
You appear to be missing Ruby development libraries and/or header
|
84
|
+
files. You can install missing compile-time dependencies in one of
|
85
|
+
the following ways:
|
51
86
|
|
52
|
-
|
53
|
-
have_func('utimes', 'sys/time.h')
|
87
|
+
- Debian / Ubuntu
|
54
88
|
|
55
|
-
|
56
|
-
abort <<-EOS
|
89
|
+
apt-get install ruby-dev
|
57
90
|
|
58
|
-
|
59
|
-
files. You can install missing compile-time dependencies in one of
|
60
|
-
the following ways:
|
91
|
+
- Red Hat / CentOS / Fedora
|
61
92
|
|
62
|
-
|
93
|
+
yum install ruby-devel or dnf install ruby-devel
|
63
94
|
|
64
|
-
|
95
|
+
- Mac OS X (Darwin)
|
65
96
|
|
66
|
-
|
97
|
+
brew install ruby (for Homebrew, see https://brew.sh)
|
98
|
+
port install ruby2.6 (for MacPorts, see https://www.macports.org)
|
67
99
|
|
68
|
-
|
100
|
+
- OpenBSD / NetBSD
|
69
101
|
|
102
|
+
pkg_add ruby (for pkgsrc, see https://www.pkgsrc.org)
|
70
103
|
|
71
|
-
|
72
|
-
managers in order to install Ruby locally (for your user only)
|
73
|
-
and/or system-wide:
|
104
|
+
- FreeBSD
|
74
105
|
|
75
|
-
|
76
|
-
- Ruby Environment (for rbenv, see http://github.com/sstephenson/rbenv)
|
106
|
+
pkg install ruby (for FreeBSD Ports, see https://www.freebsd.org/ports)
|
77
107
|
|
108
|
+
Alternatively, you can use either of the following Ruby version
|
109
|
+
managers in order to install Ruby locally (for your user only)
|
110
|
+
and/or system-wide:
|
111
|
+
|
112
|
+
- Ruby Version Manager (for RVM, see https://rvm.io)
|
113
|
+
- Ruby Environment (for rbenv, see https://github.com/sstephenson/rbenv)
|
114
|
+
- Change Ruby (for chruby, see https://github.com/postmodern/chruby)
|
115
|
+
|
116
|
+
More information about how to install Ruby on various platforms
|
117
|
+
available at the following web site:
|
118
|
+
|
119
|
+
https://www.ruby-lang.org/en/documentation/installation
|
78
120
|
EOS
|
79
121
|
end
|
80
122
|
|
81
123
|
have_func('rb_thread_call_without_gvl')
|
82
124
|
have_func('rb_thread_blocking_region')
|
83
125
|
|
84
|
-
unless
|
85
|
-
abort <<-EOS
|
126
|
+
unless have_header('magic.h')
|
127
|
+
abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
|
128
|
+
You appear to be missing libmagic(3) library and/or necessary header
|
129
|
+
files. You can install missing compile-time dependencies in one of
|
130
|
+
the following ways:
|
86
131
|
|
87
|
-
|
88
|
-
files. You can install missing compile-time dependencies in one of
|
89
|
-
the following ways:
|
132
|
+
- Debian / Ubuntu
|
90
133
|
|
91
|
-
|
134
|
+
apt-get install libmagic-dev
|
92
135
|
|
93
|
-
|
136
|
+
- Red Hat / CentOS / Fedora
|
94
137
|
|
95
|
-
|
138
|
+
yum install file-devel or dns install file-devel
|
96
139
|
|
97
|
-
|
140
|
+
- Mac OS X (Darwin)
|
98
141
|
|
99
|
-
|
142
|
+
brew install libmagic (for Homebrew, see https://brew.sh)
|
143
|
+
port install libmagic (for MacPorts, see https://www.macports.org)
|
100
144
|
|
101
|
-
|
102
|
-
port install libmagic (for MacPorts, see http://www.macports.org/)
|
145
|
+
- OpenBSD / NetBSD
|
103
146
|
|
147
|
+
pkg_add file (for pkgsrc, see https://www.pkgsrc.org)
|
104
148
|
|
105
|
-
|
106
|
-
from the following web site and attempt to compile libmagic(3) manually:
|
149
|
+
- FreeBSD
|
107
150
|
|
108
|
-
|
151
|
+
pkg install file (for FreeBSD Ports, see https://www.freebsd.org/ports)
|
109
152
|
|
153
|
+
Alternatively, you can download recent release of the file(1) package
|
154
|
+
from the following web site and attempt to compile libmagic(3) manually:
|
155
|
+
|
156
|
+
https://www.darwinsys.com/file
|
110
157
|
EOS
|
111
158
|
end
|
112
159
|
|
113
|
-
|
114
|
-
abort <<-EOS
|
115
|
-
|
116
|
-
Your version of libmagic(3) appears to be too old. Please, consider
|
117
|
-
upgrading to at least version 5.09 or newer if possible ...
|
160
|
+
have_library('magic')
|
118
161
|
|
119
|
-
|
120
|
-
|
162
|
+
unless have_func('magic_getpath')
|
163
|
+
abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
|
164
|
+
Your version of libmagic(3) appears to be too old.
|
121
165
|
|
122
|
-
|
166
|
+
Please, consider upgrading to at least version 5.29 or newer,
|
167
|
+
if possible. For more information about file(1) command and
|
168
|
+
libmagic(3) please visit the following web site:
|
123
169
|
|
170
|
+
https://www.darwinsys.com/file
|
124
171
|
EOS
|
125
172
|
end
|
126
173
|
|
127
|
-
have_func('
|
174
|
+
have_func('magic_getflags')
|
128
175
|
|
129
|
-
|
176
|
+
%w[
|
177
|
+
utime.h
|
178
|
+
sys/types.h
|
179
|
+
sys/time.h
|
180
|
+
].each do |h|
|
181
|
+
have_header(h)
|
182
|
+
end
|
183
|
+
|
184
|
+
%w[
|
185
|
+
utime
|
186
|
+
utimes
|
187
|
+
].each do |f|
|
188
|
+
have_func(f)
|
189
|
+
end
|
190
|
+
|
191
|
+
dir_config('magic', [gem_include_dir], [gem_lib_dir])
|
130
192
|
|
131
193
|
create_header
|
132
194
|
create_makefile('magic/magic')
|
133
|
-
|
134
|
-
# vim: set ts=2 sw=2 sts=2 et :
|
135
|
-
# encoding: utf-8
|