libmagic_rb 0.1.2 → 0.2.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 +4 -4
- data/ext/libmagic/definitions.h +3 -3
- data/ext/libmagic/func.h +79 -71
- data/ext/libmagic/magic.c +111 -101
- data/ext/libmagic/modes.h +75 -59
- data/ext/libmagic/params.h +16 -16
- data/ext/libmagic/validations.h +9 -9
- data/lib/libmagic_rb/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3454c7cacca3a0bdadab38245dce9a2f972adbfb463c04fcc991ce1e811e374b
|
4
|
+
data.tar.gz: 4a370cbf2bdcc7be23d4d01db291999b9e2ce00a6e989c000a3d996130f32249
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa7b33034bc5b16974b895e261014327397086bdf7fdad9aad44f6c74e1f93f712f04976c8f297765b33f887447ef6cd8e04a420e6efa8baf6418c21d13a1525
|
7
|
+
data.tar.gz: 7291f521933719b1f2062c137f6b33697d6dde0a291ec330ebbcc68400b68c5019654802e60df049af7d0b74caf7b7ef46cfd6ff58eef5179d3f1be38d23811e
|
data/ext/libmagic/definitions.h
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
#define RB_UNWRAP(cookie) \
|
2
|
-
magic_t *cookie
|
3
|
-
TypedData_Get_Struct(self, magic_t, &fileType, cookie)
|
4
|
-
if(!*cookie) rb_raise(rb_eFileClosedError, "Magic cookie already closed")
|
2
|
+
magic_t *cookie; \
|
3
|
+
TypedData_Get_Struct(self, magic_t, &fileType, cookie); \
|
4
|
+
if(!*cookie) rb_raise(rb_eFileClosedError, "Magic cookie already closed");
|
data/ext/libmagic/func.h
CHANGED
@@ -10,13 +10,13 @@
|
|
10
10
|
Returns self.
|
11
11
|
*/
|
12
12
|
|
13
|
-
VALUE _closeGlobal_(
|
14
|
-
RB_UNWRAP(cookie)
|
13
|
+
VALUE _closeGlobal_(VALUE self) {
|
14
|
+
RB_UNWRAP(cookie);
|
15
15
|
|
16
|
-
magic_close(*cookie)
|
17
|
-
*cookie = NULL
|
18
|
-
rb_ivar_set(self, rb_intern("@closed"), Qtrue)
|
19
|
-
return self
|
16
|
+
magic_close(*cookie);
|
17
|
+
*cookie = NULL;
|
18
|
+
rb_ivar_set(self, rb_intern("@closed"), Qtrue);
|
19
|
+
return self;
|
20
20
|
}
|
21
21
|
|
22
22
|
/*
|
@@ -36,24 +36,26 @@ VALUE _closeGlobal_(volatile VALUE self) {
|
|
36
36
|
Returns self.
|
37
37
|
*/
|
38
38
|
|
39
|
-
VALUE _loadGlobal_(
|
40
|
-
char *databasePath = NULL
|
39
|
+
VALUE _loadGlobal_(VALUE self, VALUE dbPath) {
|
40
|
+
char *databasePath = NULL;
|
41
41
|
|
42
42
|
if (RB_TYPE_P(dbPath, T_STRING)) {
|
43
|
-
databasePath = StringValuePtr(dbPath)
|
44
|
-
rb_iv_set(self, "@db", dbPath)
|
45
|
-
} else if(RB_TYPE_P(dbPath, T_STRING)) {
|
46
|
-
rb_iv_set(self, "@db", Qnil) ;
|
43
|
+
databasePath = StringValuePtr(dbPath);
|
44
|
+
rb_iv_set(self, "@db", dbPath);
|
47
45
|
}
|
48
46
|
|
49
47
|
// Check if the database is a valid file or not
|
50
48
|
// Raises ruby error which will return.
|
51
|
-
RB_UNWRAP(cookie)
|
49
|
+
RB_UNWRAP(cookie);
|
52
50
|
|
53
|
-
if(databasePath) magic_validate_db(*cookie, databasePath)
|
54
|
-
magic_load(*cookie, databasePath) ;
|
51
|
+
if(databasePath) magic_validate_db(*cookie, databasePath);
|
55
52
|
|
56
|
-
|
53
|
+
if (magic_load(*cookie, databasePath) == -1) {
|
54
|
+
rb_raise(rb_eRuntimeError, "Failed to load magic database: %s", magic_error(*cookie));
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
return self;
|
57
59
|
}
|
58
60
|
|
59
61
|
/*
|
@@ -70,28 +72,31 @@ VALUE _loadGlobal_(volatile VALUE self, volatile VALUE dbPath) {
|
|
70
72
|
Returns String or nil.
|
71
73
|
*/
|
72
74
|
|
73
|
-
VALUE _checkGlobal_(
|
74
|
-
RB_UNWRAP(cookie)
|
75
|
+
VALUE _checkGlobal_(VALUE self) {
|
76
|
+
RB_UNWRAP(cookie);
|
75
77
|
|
76
78
|
// Database path
|
77
|
-
VALUE db = rb_iv_get(self, "@db")
|
79
|
+
VALUE db = rb_iv_get(self, "@db");
|
78
80
|
|
79
|
-
char *database = NULL
|
81
|
+
char *database = NULL;
|
80
82
|
if(RB_TYPE_P(db, T_STRING)) {
|
81
|
-
database = StringValuePtr(db)
|
83
|
+
database = StringValuePtr(db);
|
82
84
|
}
|
83
85
|
|
84
86
|
// File path
|
85
|
-
VALUE f = rb_iv_get(self, "@file")
|
86
|
-
char *file = StringValuePtr(f)
|
87
|
+
VALUE f = rb_iv_get(self, "@file");
|
88
|
+
char *file = StringValuePtr(f);
|
87
89
|
|
88
|
-
if(database) magic_validate_db(*cookie, database)
|
89
|
-
magic_load(*cookie, database) ;
|
90
|
+
if(database) magic_validate_db(*cookie, database);
|
90
91
|
|
91
|
-
|
92
|
-
|
92
|
+
if (magic_load(*cookie, database) == -1) {
|
93
|
+
rb_raise(rb_eRuntimeError, "Failed to load magic database: %s", magic_error(*cookie));
|
94
|
+
}
|
93
95
|
|
94
|
-
|
96
|
+
fileReadable(file);
|
97
|
+
const char *mt = magic_file(*cookie, file);
|
98
|
+
|
99
|
+
return mt ? rb_str_new_cstr(mt) : Qnil;
|
95
100
|
}
|
96
101
|
|
97
102
|
/*
|
@@ -106,18 +111,18 @@ VALUE _checkGlobal_(volatile VALUE self) {
|
|
106
111
|
# => #<LibmagicRb:0x00005581018f35e0 @closed=true, @db=nil, @file="/usr/share/dict/words", @mode=1106>
|
107
112
|
*/
|
108
113
|
|
109
|
-
VALUE _getParamGlobal_(
|
114
|
+
VALUE _getParamGlobal_(VALUE self, VALUE param) {
|
110
115
|
#if MAGIC_VERSION > 525
|
111
|
-
RB_UNWRAP(cookie)
|
116
|
+
RB_UNWRAP(cookie);
|
112
117
|
|
113
|
-
unsigned int _param = NUM2UINT(param)
|
114
|
-
unsigned long value
|
118
|
+
unsigned int _param = NUM2UINT(param);
|
119
|
+
unsigned long value;
|
115
120
|
|
116
|
-
int status = magic_getparam(*cookie, _param, &value)
|
117
|
-
if (status) return Qnil
|
118
|
-
return ULONG2NUM(value)
|
121
|
+
int status = magic_getparam(*cookie, _param, &value);
|
122
|
+
if (status) return Qnil;
|
123
|
+
return ULONG2NUM(value);
|
119
124
|
#else
|
120
|
-
return Qnil
|
125
|
+
return Qnil;
|
121
126
|
#endif
|
122
127
|
}
|
123
128
|
|
@@ -142,22 +147,22 @@ VALUE _getParamGlobal_(volatile VALUE self, volatile VALUE param) {
|
|
142
147
|
Returns Integer or nil on failure.
|
143
148
|
*/
|
144
149
|
|
145
|
-
VALUE _setParamGlobal_(
|
150
|
+
VALUE _setParamGlobal_(VALUE self, VALUE param, VALUE paramVal) {
|
146
151
|
#if MAGIC_VERSION > 525
|
147
|
-
unsigned int _param = NUM2UINT(param)
|
148
|
-
unsigned long _paramVal = NUM2ULONG(paramVal)
|
152
|
+
unsigned int _param = NUM2UINT(param);
|
153
|
+
unsigned long _paramVal = NUM2ULONG(paramVal);
|
149
154
|
|
150
|
-
RB_UNWRAP(cookie)
|
155
|
+
RB_UNWRAP(cookie);
|
151
156
|
|
152
|
-
unsigned long value
|
153
|
-
magic_setparam(*cookie, _param, &_paramVal)
|
157
|
+
unsigned long value;
|
158
|
+
magic_setparam(*cookie, _param, &_paramVal);
|
154
159
|
|
155
|
-
int status = magic_getparam(*cookie, _param, &value)
|
156
|
-
if (status) return Qnil
|
160
|
+
int status = magic_getparam(*cookie, _param, &value);
|
161
|
+
if (status) return Qnil;
|
157
162
|
|
158
|
-
return ULONG2NUM((int)value)
|
163
|
+
return ULONG2NUM((int)value);
|
159
164
|
#else
|
160
|
-
return Qnil
|
165
|
+
return Qnil;
|
161
166
|
#endif
|
162
167
|
}
|
163
168
|
|
@@ -180,23 +185,26 @@ VALUE _setParamGlobal_(volatile VALUE self, volatile VALUE param, volatile VALUE
|
|
180
185
|
Returns either String or nil.
|
181
186
|
*/
|
182
187
|
|
183
|
-
VALUE _bufferGlobal_(
|
184
|
-
RB_UNWRAP(cookie)
|
188
|
+
VALUE _bufferGlobal_(VALUE self, VALUE string) {
|
189
|
+
RB_UNWRAP(cookie);
|
185
190
|
|
186
|
-
VALUE db = rb_iv_get(self, "@db")
|
191
|
+
VALUE db = rb_iv_get(self, "@db");
|
187
192
|
|
188
|
-
char *database = NULL
|
193
|
+
char *database = NULL;
|
189
194
|
if(RB_TYPE_P(db, T_STRING)) {
|
190
|
-
database = StringValuePtr(db)
|
195
|
+
database = StringValuePtr(db);
|
191
196
|
}
|
192
197
|
|
193
|
-
if(database) magic_validate_db(*cookie, database)
|
194
|
-
|
198
|
+
if(database) magic_validate_db(*cookie, database);
|
199
|
+
|
200
|
+
if (magic_load(*cookie, database) == -1) {
|
201
|
+
rb_raise(rb_eRuntimeError, "Failed to load magic database: %s", magic_error(*cookie));
|
202
|
+
}
|
195
203
|
|
196
|
-
char *buffer = StringValuePtr(string)
|
197
|
-
const char *buf = magic_buffer(*cookie, buffer, strlen(buffer))
|
204
|
+
char *buffer = StringValuePtr(string);
|
205
|
+
const char *buf = magic_buffer(*cookie, buffer, strlen(buffer));
|
198
206
|
|
199
|
-
return buf ? rb_str_new_cstr(buf) : Qnil
|
207
|
+
return buf ? rb_str_new_cstr(buf) : Qnil;
|
200
208
|
}
|
201
209
|
|
202
210
|
/*
|
@@ -229,20 +237,20 @@ VALUE _bufferGlobal_(volatile VALUE self, volatile VALUE string) {
|
|
229
237
|
=> #<LibmagicRb:0x000055cf280a9b30 @closed=true, @db=nil, @file=".", @mode=1106>
|
230
238
|
*/
|
231
239
|
|
232
|
-
VALUE _listGlobal_(
|
233
|
-
RB_UNWRAP(cookie)
|
240
|
+
VALUE _listGlobal_(VALUE self) {
|
241
|
+
RB_UNWRAP(cookie);
|
234
242
|
|
235
|
-
VALUE db = rb_iv_get(self, "@db")
|
243
|
+
VALUE db = rb_iv_get(self, "@db");
|
236
244
|
|
237
|
-
char *database = NULL
|
245
|
+
char *database = NULL;
|
238
246
|
if (RB_TYPE_P(db, T_STRING)) {
|
239
|
-
database = StringValuePtr(db)
|
247
|
+
database = StringValuePtr(db);
|
240
248
|
}
|
241
249
|
|
242
|
-
if(database) magic_validate_db(*cookie, database)
|
243
|
-
int status = magic_list(*cookie, database)
|
250
|
+
if(database) magic_validate_db(*cookie, database);
|
251
|
+
int status = magic_list(*cookie, database);
|
244
252
|
|
245
|
-
return INT2FIX(status)
|
253
|
+
return INT2FIX(status);
|
246
254
|
}
|
247
255
|
|
248
256
|
/*
|
@@ -276,16 +284,16 @@ VALUE _listGlobal_(volatile VALUE self) {
|
|
276
284
|
# => #<LibmagicRb:0x00005583732e1070 @closed=true, @db=nil, @file=".", @mode=256>
|
277
285
|
*/
|
278
286
|
|
279
|
-
VALUE _setflagsGlobal_(
|
280
|
-
unsigned int flag = NUM2UINT(flags)
|
287
|
+
VALUE _setflagsGlobal_(VALUE self, VALUE flags) {
|
288
|
+
unsigned int flag = NUM2UINT(flags);
|
281
289
|
|
282
|
-
RB_UNWRAP(cookie)
|
283
|
-
int status = magic_setflags(*cookie, flag)
|
290
|
+
RB_UNWRAP(cookie);
|
291
|
+
int status = magic_setflags(*cookie, flag);
|
284
292
|
|
285
293
|
if (status) {
|
286
|
-
return Qnil
|
294
|
+
return Qnil;
|
287
295
|
} else {
|
288
|
-
rb_ivar_set(self, rb_intern("@mode"), flags)
|
289
|
-
return flags
|
296
|
+
rb_ivar_set(self, rb_intern("@mode"), flags);
|
297
|
+
return flags;
|
290
298
|
}
|
291
299
|
}
|
data/ext/libmagic/magic.c
CHANGED
@@ -18,37 +18,35 @@
|
|
18
18
|
/*
|
19
19
|
* Errors
|
20
20
|
*/
|
21
|
-
VALUE rb_eFileNotFoundError
|
22
|
-
VALUE rb_eFileNotReadableError
|
23
|
-
VALUE rb_eInvalidDBError
|
24
|
-
VALUE rb_eIsDirError
|
25
|
-
VALUE rb_eFileClosedError
|
21
|
+
VALUE rb_eFileNotFoundError;
|
22
|
+
VALUE rb_eFileNotReadableError;
|
23
|
+
VALUE rb_eInvalidDBError;
|
24
|
+
VALUE rb_eIsDirError;
|
25
|
+
VALUE rb_eFileClosedError;
|
26
26
|
|
27
27
|
// Garbage collect
|
28
|
-
void file_free(void
|
29
|
-
if(
|
30
|
-
magic_close(*data)
|
31
|
-
*data = NULL
|
28
|
+
void file_free(void *data) {
|
29
|
+
if(data) {
|
30
|
+
magic_close(*(void **)data);
|
31
|
+
*(void **)data = NULL;
|
32
32
|
}
|
33
33
|
|
34
|
-
free(data)
|
34
|
+
free(data);
|
35
35
|
}
|
36
36
|
|
37
37
|
// Filetype
|
38
38
|
static rb_data_type_t fileType = {
|
39
39
|
.wrap_struct_name = "file",
|
40
|
-
|
41
40
|
.function = {
|
42
41
|
.dmark = NULL,
|
43
42
|
.dfree = file_free,
|
44
43
|
},
|
45
|
-
|
46
44
|
.data = NULL,
|
47
45
|
|
48
46
|
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
|
49
47
|
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
50
48
|
#endif
|
51
|
-
}
|
49
|
+
};
|
52
50
|
|
53
51
|
#include "validations.h"
|
54
52
|
#include "func.h"
|
@@ -67,62 +65,69 @@ static rb_data_type_t fileType = {
|
|
67
65
|
`mode: LibmagicRb::MAGIC_CHECK | LibmagicRb::MAGIC_SYMLINK | Libmagic_MAGIC_MIME`
|
68
66
|
If `mode` key is nil, it will default to `MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK`
|
69
67
|
*/
|
70
|
-
static VALUE _check_(
|
68
|
+
static VALUE _check_(VALUE obj, VALUE args) {
|
71
69
|
if(!RB_TYPE_P(args, T_HASH)) {
|
72
|
-
rb_raise(rb_eArgError, "Expected hash as argument.")
|
70
|
+
rb_raise(rb_eArgError, "Expected hash as argument.");
|
73
71
|
}
|
74
72
|
|
75
73
|
// Database Path
|
76
|
-
VALUE argDBPath = rb_hash_aref(args, ID2SYM(rb_intern("db")))
|
74
|
+
VALUE argDBPath = rb_hash_aref(args, ID2SYM(rb_intern("db")));
|
77
75
|
|
78
|
-
char *databasePath ;
|
79
|
-
if (
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
databasePath = StringValuePtr(argDBPath)
|
76
|
+
char *databasePath = NULL;
|
77
|
+
if (!NIL_P(argDBPath)) {
|
78
|
+
if (!RB_TYPE_P(argDBPath, T_STRING)) {
|
79
|
+
rb_raise(rb_eArgError, "Database name must be an instance of String.");
|
80
|
+
}
|
81
|
+
|
82
|
+
databasePath = StringValuePtr(argDBPath);
|
85
83
|
}
|
86
84
|
|
87
85
|
// File path
|
88
|
-
VALUE argFilePath = rb_hash_aref(args, ID2SYM(rb_intern("file")))
|
86
|
+
VALUE argFilePath = rb_hash_aref(args, ID2SYM(rb_intern("file")));
|
89
87
|
if (RB_TYPE_P(argFilePath, T_NIL)) {
|
90
|
-
rb_raise(rb_eArgError, "Expected `file:\" key as a string, got nil instead")
|
88
|
+
rb_raise(rb_eArgError, "Expected `file:\" key as a string, got nil instead");
|
91
89
|
} else if (!RB_TYPE_P(argFilePath, T_STRING)) {
|
92
|
-
rb_raise(rb_eArgError, "Filename must be an instance of String.")
|
90
|
+
rb_raise(rb_eArgError, "Filename must be an instance of String.");
|
93
91
|
}
|
94
|
-
char *checkPath = StringValuePtr(argFilePath)
|
92
|
+
char *checkPath = StringValuePtr(argFilePath);
|
95
93
|
|
96
94
|
// Modes
|
97
|
-
VALUE argModes = rb_hash_aref(args, ID2SYM(rb_intern("mode")))
|
98
|
-
unsigned int modes
|
95
|
+
VALUE argModes = rb_hash_aref(args, ID2SYM(rb_intern("mode")));
|
96
|
+
unsigned int modes;
|
99
97
|
if(RB_TYPE_P(argModes, T_NIL)) {
|
100
|
-
modes = MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK
|
98
|
+
modes = MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK;
|
101
99
|
} else if (!RB_TYPE_P(argModes, T_FIXNUM)) {
|
102
|
-
rb_raise(rb_eArgError, "Modes must be an instance of Integer. Check LibmagicRb.constants() or LibmagicRb.lsmodes().")
|
100
|
+
rb_raise(rb_eArgError, "Modes must be an instance of Integer. Check LibmagicRb.constants() or LibmagicRb.lsmodes().");
|
103
101
|
} else {
|
104
|
-
modes = FIX2UINT(argModes)
|
102
|
+
modes = FIX2UINT(argModes);
|
105
103
|
}
|
106
104
|
|
107
105
|
// Checks
|
108
|
-
struct magic_set *magic = magic_open(modes)
|
106
|
+
struct magic_set *magic = magic_open(modes);
|
107
|
+
|
108
|
+
if (!magic) {
|
109
|
+
rb_raise(rb_eRuntimeError, "Failed to initialize magic cookie.");
|
110
|
+
}
|
109
111
|
|
110
112
|
// Check if the database is a valid file or not
|
111
113
|
// Raises ruby error which will return.
|
112
|
-
fileReadable(checkPath)
|
114
|
+
fileReadable(checkPath);
|
113
115
|
|
114
116
|
if(databasePath) {
|
115
|
-
magic_validate_db(magic, databasePath)
|
117
|
+
magic_validate_db(magic, databasePath);
|
116
118
|
}
|
117
119
|
|
118
|
-
magic_load(magic, databasePath)
|
120
|
+
if (magic_load(magic, databasePath) == -1) {
|
121
|
+
magic_close(magic);
|
122
|
+
rb_raise(rb_eInvalidDBError, "Failed to load magic database: %s", magic_error(magic));
|
123
|
+
}
|
119
124
|
|
120
|
-
const char *mt = magic_file(magic, checkPath)
|
125
|
+
const char *mt = magic_file(magic, checkPath);
|
121
126
|
|
122
|
-
VALUE retVal = mt ? rb_str_new_cstr(mt) : Qnil
|
123
|
-
magic_close(magic)
|
127
|
+
VALUE retVal = mt ? rb_str_new_cstr(mt) : Qnil;
|
128
|
+
magic_close(magic);
|
124
129
|
|
125
|
-
return retVal
|
130
|
+
return retVal;
|
126
131
|
}
|
127
132
|
|
128
133
|
/*
|
@@ -162,65 +167,70 @@ static VALUE _check_(volatile VALUE obj, volatile VALUE args) {
|
|
162
167
|
Flags/modes applied to cookie, will not affect cookie2 as well. Think of them as totally different containers.
|
163
168
|
Of course, you must close cookies when you don't need them. Otherwise it can use memories unless GC is triggered.
|
164
169
|
*/
|
165
|
-
VALUE rb_libmagicRb_initialize(
|
170
|
+
VALUE rb_libmagicRb_initialize(VALUE self, VALUE args) {
|
166
171
|
// Database Path
|
167
172
|
if(!RB_TYPE_P(args, T_HASH)) {
|
168
|
-
rb_raise(rb_eArgError, "Expected hash as argument.")
|
173
|
+
rb_raise(rb_eArgError, "Expected hash as argument.");
|
169
174
|
}
|
170
175
|
|
171
|
-
VALUE argDBPath = rb_hash_aref(args, ID2SYM(rb_intern("db")))
|
176
|
+
VALUE argDBPath = rb_hash_aref(args, ID2SYM(rb_intern("db")));
|
172
177
|
|
173
178
|
if (RB_TYPE_P(argDBPath, T_NIL)) {
|
174
|
-
rb_ivar_set(self, rb_intern("@db"), Qnil)
|
179
|
+
rb_ivar_set(self, rb_intern("@db"), Qnil);
|
175
180
|
} else if (!RB_TYPE_P(argDBPath, T_STRING)) {
|
176
|
-
rb_raise(rb_eArgError, "Database name must be an instance of String.")
|
181
|
+
rb_raise(rb_eArgError, "Database name must be an instance of String.");
|
177
182
|
} else {
|
178
|
-
rb_ivar_set(self, rb_intern("@db"), argDBPath)
|
183
|
+
rb_ivar_set(self, rb_intern("@db"), argDBPath);
|
179
184
|
}
|
180
185
|
|
181
186
|
// File path
|
182
|
-
VALUE argFilePath = rb_hash_aref(args, ID2SYM(rb_intern("file")))
|
187
|
+
VALUE argFilePath = rb_hash_aref(args, ID2SYM(rb_intern("file")));
|
183
188
|
if (RB_TYPE_P(argFilePath, T_NIL)) {
|
184
|
-
rb_raise(rb_eArgError, "Expected `file:\" key as a string, got nil instead")
|
189
|
+
rb_raise(rb_eArgError, "Expected `file:\" key as a string, got nil instead");
|
185
190
|
} else if (!RB_TYPE_P(argFilePath, T_STRING)) {
|
186
|
-
rb_raise(rb_eArgError, "Filename must be an instance of String.")
|
191
|
+
rb_raise(rb_eArgError, "Filename must be an instance of String.");
|
187
192
|
}
|
188
|
-
rb_ivar_set(self, rb_intern("@file"), argFilePath)
|
193
|
+
rb_ivar_set(self, rb_intern("@file"), argFilePath);
|
189
194
|
|
190
195
|
// Modes
|
191
|
-
VALUE argModes = rb_hash_aref(args, ID2SYM(rb_intern("mode")))
|
192
|
-
unsigned int modes
|
196
|
+
VALUE argModes = rb_hash_aref(args, ID2SYM(rb_intern("mode")));
|
197
|
+
unsigned int modes;
|
193
198
|
if(RB_TYPE_P(argModes, T_NIL)) {
|
194
|
-
modes = MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK
|
199
|
+
modes = MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK;
|
195
200
|
} else if (!RB_TYPE_P(argModes, T_FIXNUM)) {
|
196
|
-
rb_raise(rb_eArgError, "Modes must be an instance of Integer. Check LibmagicRb.constants() or LibmagicRb.lsmodes().")
|
201
|
+
rb_raise(rb_eArgError, "Modes must be an instance of Integer. Check LibmagicRb.constants() or LibmagicRb.lsmodes().");
|
197
202
|
} else {
|
198
|
-
modes = FIX2UINT(argModes)
|
203
|
+
modes = FIX2UINT(argModes);
|
199
204
|
}
|
200
|
-
rb_ivar_set(self, rb_intern("@mode"), UINT2NUM(modes))
|
205
|
+
rb_ivar_set(self, rb_intern("@mode"), UINT2NUM(modes));
|
201
206
|
|
202
|
-
rb_ivar_set(self, rb_intern("@closed"), Qfalse)
|
207
|
+
rb_ivar_set(self, rb_intern("@closed"), Qfalse);
|
203
208
|
|
204
|
-
RB_UNWRAP(cookie)
|
205
|
-
magic_setflags(*cookie, modes)
|
209
|
+
RB_UNWRAP(cookie);
|
210
|
+
magic_setflags(*cookie, modes);
|
206
211
|
|
207
|
-
return self
|
212
|
+
return self;
|
208
213
|
}
|
209
214
|
|
210
|
-
VALUE initAlloc(
|
211
|
-
magic_t *cookie
|
212
|
-
cookie = malloc(sizeof(*cookie))
|
213
|
-
|
215
|
+
VALUE initAlloc(VALUE self) {
|
216
|
+
magic_t *cookie;
|
217
|
+
cookie = malloc(sizeof(*cookie));
|
218
|
+
|
219
|
+
if (!cookie) {
|
220
|
+
rb_raise(rb_eNoMemError, "Failed to allocate memory for magic cookie.");
|
221
|
+
}
|
222
|
+
|
223
|
+
*cookie = magic_open(0);
|
214
224
|
|
215
|
-
return TypedData_Wrap_Struct(self, &fileType, cookie)
|
225
|
+
return TypedData_Wrap_Struct(self, &fileType, cookie);
|
216
226
|
}
|
217
227
|
|
218
228
|
void Init_main() {
|
219
|
-
rb_global_variable(&rb_eFileNotFoundError)
|
220
|
-
rb_global_variable(&rb_eFileNotReadableError)
|
221
|
-
rb_global_variable(&rb_eInvalidDBError)
|
222
|
-
rb_global_variable(&rb_eIsDirError)
|
223
|
-
rb_global_variable(&rb_eFileClosedError)
|
229
|
+
rb_global_variable(&rb_eFileNotFoundError);
|
230
|
+
rb_global_variable(&rb_eFileNotReadableError);
|
231
|
+
rb_global_variable(&rb_eInvalidDBError);
|
232
|
+
rb_global_variable(&rb_eIsDirError);
|
233
|
+
rb_global_variable(&rb_eFileClosedError);
|
224
234
|
|
225
235
|
/*
|
226
236
|
* Libmagic Errors
|
@@ -229,74 +239,74 @@ void Init_main() {
|
|
229
239
|
/*
|
230
240
|
Adds ability to check mime-type of a file using the libmagic (magic(4)). It uses native extensions and it's quite performant.
|
231
241
|
*/
|
232
|
-
VALUE cLibmagicRb = rb_define_class("LibmagicRb", rb_cObject)
|
242
|
+
VALUE cLibmagicRb = rb_define_class("LibmagicRb", rb_cObject);
|
233
243
|
|
234
|
-
rb_eFileNotFoundError = rb_define_class_under(cLibmagicRb, "FileNotFound", rb_eRuntimeError)
|
235
|
-
rb_eFileNotReadableError = rb_define_class_under(cLibmagicRb, "FileUnreadable", rb_eRuntimeError)
|
236
|
-
rb_eInvalidDBError = rb_define_class_under(cLibmagicRb, "InvalidDBError", rb_eRuntimeError)
|
237
|
-
rb_eIsDirError = rb_define_class_under(cLibmagicRb, "IsDirError", rb_eRuntimeError)
|
238
|
-
rb_eFileClosedError = rb_define_class_under(cLibmagicRb, "FileClosedError", rb_eRuntimeError)
|
244
|
+
rb_eFileNotFoundError = rb_define_class_under(cLibmagicRb, "FileNotFound", rb_eRuntimeError);
|
245
|
+
rb_eFileNotReadableError = rb_define_class_under(cLibmagicRb, "FileUnreadable", rb_eRuntimeError);
|
246
|
+
rb_eInvalidDBError = rb_define_class_under(cLibmagicRb, "InvalidDBError", rb_eRuntimeError);
|
247
|
+
rb_eIsDirError = rb_define_class_under(cLibmagicRb, "IsDirError", rb_eRuntimeError);
|
248
|
+
rb_eFileClosedError = rb_define_class_under(cLibmagicRb, "FileClosedError", rb_eRuntimeError);
|
239
249
|
|
240
250
|
/*
|
241
251
|
* Constants
|
242
252
|
*/
|
243
|
-
modes(cLibmagicRb)
|
244
|
-
params(cLibmagicRb)
|
253
|
+
modes(cLibmagicRb);
|
254
|
+
params(cLibmagicRb);
|
245
255
|
|
246
256
|
|
247
257
|
#if MAGIC_VERSION > 525
|
248
|
-
char version[6]
|
249
|
-
sprintf(version, "%0.2f", magic_version() / 100.0)
|
258
|
+
char version[6];
|
259
|
+
sprintf(version, "%0.2f", magic_version() / 100.0);
|
250
260
|
|
251
261
|
/*
|
252
262
|
LibmagicRb::MAGIC_VERSION returns the magic version of the library.
|
253
263
|
For older libmagic version, this can be undefined, so this method will return "0" instead.
|
254
264
|
*/
|
255
|
-
rb_define_const(cLibmagicRb, "MAGIC_VERSION", rb_str_new_cstr(version))
|
265
|
+
rb_define_const(cLibmagicRb, "MAGIC_VERSION", rb_str_new_cstr(version));
|
256
266
|
#else
|
257
|
-
rb_define_const(cLibmagicRb, "MAGIC_VERSION", rb_str_new_cstr("0"))
|
267
|
+
rb_define_const(cLibmagicRb, "MAGIC_VERSION", rb_str_new_cstr("0"));
|
258
268
|
#endif
|
259
269
|
|
260
270
|
/*
|
261
271
|
* Singleton Methods
|
262
272
|
*/
|
263
|
-
rb_define_singleton_method(cLibmagicRb, "check", _check_, 1)
|
264
|
-
rb_define_singleton_method(cLibmagicRb, "lsmodes", lsmodes, 0)
|
265
|
-
rb_define_singleton_method(cLibmagicRb, "lsparams", lsparams, 0)
|
273
|
+
rb_define_singleton_method(cLibmagicRb, "check", _check_, 1);
|
274
|
+
rb_define_singleton_method(cLibmagicRb, "lsmodes", lsmodes, 0);
|
275
|
+
rb_define_singleton_method(cLibmagicRb, "lsparams", lsparams, 0);
|
266
276
|
|
267
277
|
/*
|
268
278
|
* Instance Methods
|
269
279
|
*/
|
270
|
-
rb_define_alloc_func(cLibmagicRb, initAlloc)
|
280
|
+
rb_define_alloc_func(cLibmagicRb, initAlloc);
|
271
281
|
|
272
282
|
// LibmagicRb.new()
|
273
|
-
rb_define_method(cLibmagicRb, "initialize", rb_libmagicRb_initialize, 1)
|
283
|
+
rb_define_method(cLibmagicRb, "initialize", rb_libmagicRb_initialize, 1);
|
274
284
|
|
275
285
|
// Attributes
|
276
|
-
rb_define_attr(cLibmagicRb, "closed", 1, 0)
|
277
|
-
rb_define_attr(cLibmagicRb, "mode", 1, 0)
|
278
|
-
rb_define_attr(cLibmagicRb, "file", 1, 1)
|
279
|
-
rb_define_attr(cLibmagicRb, "db", 1, 1)
|
286
|
+
rb_define_attr(cLibmagicRb, "closed", 1, 0);
|
287
|
+
rb_define_attr(cLibmagicRb, "mode", 1, 0);
|
288
|
+
rb_define_attr(cLibmagicRb, "file", 1, 1);
|
289
|
+
rb_define_attr(cLibmagicRb, "db", 1, 1);
|
280
290
|
|
281
291
|
// Close database
|
282
|
-
rb_define_method(cLibmagicRb, "close", _closeGlobal_, 0)
|
283
|
-
rb_define_alias(cLibmagicRb, "closed?", "closed")
|
292
|
+
rb_define_method(cLibmagicRb, "close", _closeGlobal_, 0);
|
293
|
+
rb_define_alias(cLibmagicRb, "closed?", "closed");
|
284
294
|
|
285
295
|
// Load database
|
286
|
-
rb_define_method(cLibmagicRb, "load", _loadGlobal_, 1)
|
296
|
+
rb_define_method(cLibmagicRb, "load", _loadGlobal_, 1);
|
287
297
|
|
288
298
|
// Check for file mimetype
|
289
|
-
rb_define_method(cLibmagicRb, "check", _checkGlobal_, 0)
|
299
|
+
rb_define_method(cLibmagicRb, "check", _checkGlobal_, 0);
|
290
300
|
|
291
301
|
// Get and set params
|
292
|
-
rb_define_method(cLibmagicRb, "getparam", _getParamGlobal_, 1)
|
293
|
-
rb_define_method(cLibmagicRb, "setparam", _setParamGlobal_, 2)
|
302
|
+
rb_define_method(cLibmagicRb, "getparam", _getParamGlobal_, 1);
|
303
|
+
rb_define_method(cLibmagicRb, "setparam", _setParamGlobal_, 2);
|
294
304
|
|
295
305
|
// Set modes dynamically
|
296
|
-
rb_define_method(cLibmagicRb, "setflags", _setflagsGlobal_, 1)
|
297
|
-
rb_define_method(cLibmagicRb, "mode=", _setflagsGlobal_, 1)
|
306
|
+
rb_define_method(cLibmagicRb, "setflags", _setflagsGlobal_, 1);
|
307
|
+
rb_define_method(cLibmagicRb, "mode=", _setflagsGlobal_, 1);
|
298
308
|
|
299
309
|
// Miscellaneous
|
300
|
-
rb_define_method(cLibmagicRb, "magic_buffer", _bufferGlobal_, 1)
|
301
|
-
rb_define_method(cLibmagicRb, "magic_list", _listGlobal_, 0)
|
310
|
+
rb_define_method(cLibmagicRb, "magic_buffer", _bufferGlobal_, 1);
|
311
|
+
rb_define_method(cLibmagicRb, "magic_list", _listGlobal_, 0);
|
302
312
|
}
|
data/ext/libmagic/modes.h
CHANGED
@@ -1,142 +1,158 @@
|
|
1
1
|
// In the older versions, there were some missing constants. Don't define them at compile time.
|
2
2
|
|
3
3
|
void modes(volatile VALUE rb_klass) {
|
4
|
-
rb_define_const(rb_klass, "MAGIC_NONE", INT2FIX(MAGIC_NONE))
|
5
|
-
rb_define_const(rb_klass, "MAGIC_DEBUG", INT2FIX(MAGIC_DEBUG))
|
6
|
-
rb_define_const(rb_klass, "MAGIC_SYMLINK", INT2FIX(MAGIC_SYMLINK))
|
7
|
-
rb_define_const(rb_klass, "MAGIC_COMPRESS", INT2FIX(MAGIC_COMPRESS))
|
8
|
-
rb_define_const(rb_klass, "MAGIC_DEVICES", INT2FIX(MAGIC_DEVICES))
|
9
|
-
rb_define_const(rb_klass, "MAGIC_MIME_TYPE", INT2FIX(MAGIC_MIME_TYPE))
|
10
|
-
rb_define_const(rb_klass, "MAGIC_MIME_ENCODING", INT2FIX(MAGIC_MIME_ENCODING))
|
11
|
-
rb_define_const(rb_klass, "MAGIC_MIME", INT2FIX(MAGIC_MIME))
|
12
|
-
rb_define_const(rb_klass, "MAGIC_CONTINUE", INT2FIX(MAGIC_CONTINUE))
|
13
|
-
rb_define_const(rb_klass, "MAGIC_CHECK", INT2FIX(MAGIC_CHECK))
|
14
|
-
rb_define_const(rb_klass, "MAGIC_PRESERVE_ATIME", INT2FIX(MAGIC_PRESERVE_ATIME))
|
15
|
-
rb_define_const(rb_klass, "MAGIC_RAW", INT2FIX(MAGIC_RAW))
|
16
|
-
rb_define_const(rb_klass, "MAGIC_ERROR", INT2FIX(MAGIC_ERROR))
|
17
|
-
rb_define_const(rb_klass, "MAGIC_APPLE", INT2FIX(MAGIC_APPLE))
|
4
|
+
rb_define_const(rb_klass, "MAGIC_NONE", INT2FIX(MAGIC_NONE));
|
5
|
+
rb_define_const(rb_klass, "MAGIC_DEBUG", INT2FIX(MAGIC_DEBUG));
|
6
|
+
rb_define_const(rb_klass, "MAGIC_SYMLINK", INT2FIX(MAGIC_SYMLINK));
|
7
|
+
rb_define_const(rb_klass, "MAGIC_COMPRESS", INT2FIX(MAGIC_COMPRESS));
|
8
|
+
rb_define_const(rb_klass, "MAGIC_DEVICES", INT2FIX(MAGIC_DEVICES));
|
9
|
+
rb_define_const(rb_klass, "MAGIC_MIME_TYPE", INT2FIX(MAGIC_MIME_TYPE));
|
10
|
+
rb_define_const(rb_klass, "MAGIC_MIME_ENCODING", INT2FIX(MAGIC_MIME_ENCODING));
|
11
|
+
rb_define_const(rb_klass, "MAGIC_MIME", INT2FIX(MAGIC_MIME));
|
12
|
+
rb_define_const(rb_klass, "MAGIC_CONTINUE", INT2FIX(MAGIC_CONTINUE));
|
13
|
+
rb_define_const(rb_klass, "MAGIC_CHECK", INT2FIX(MAGIC_CHECK));
|
14
|
+
rb_define_const(rb_klass, "MAGIC_PRESERVE_ATIME", INT2FIX(MAGIC_PRESERVE_ATIME));
|
15
|
+
rb_define_const(rb_klass, "MAGIC_RAW", INT2FIX(MAGIC_RAW));
|
16
|
+
rb_define_const(rb_klass, "MAGIC_ERROR", INT2FIX(MAGIC_ERROR));
|
17
|
+
rb_define_const(rb_klass, "MAGIC_APPLE", INT2FIX(MAGIC_APPLE));
|
18
18
|
|
19
19
|
#ifdef MAGIC_EXTENSION
|
20
|
-
rb_define_const(rb_klass, "MAGIC_EXTENSION", INT2FIX(MAGIC_EXTENSION))
|
20
|
+
rb_define_const(rb_klass, "MAGIC_EXTENSION", INT2FIX(MAGIC_EXTENSION));
|
21
21
|
#endif
|
22
22
|
|
23
23
|
#ifdef MAGIC_COMPRESS_TRANSP
|
24
|
-
rb_define_const(rb_klass, "MAGIC_COMPRESS_TRANSP", INT2FIX(MAGIC_COMPRESS_TRANSP))
|
24
|
+
rb_define_const(rb_klass, "MAGIC_COMPRESS_TRANSP", INT2FIX(MAGIC_COMPRESS_TRANSP));
|
25
25
|
#endif
|
26
26
|
|
27
27
|
#ifdef MAGIC_NO_CHECK_APPTYPE
|
28
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_APPTYPE", INT2FIX(MAGIC_NO_CHECK_APPTYPE))
|
28
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_APPTYPE", INT2FIX(MAGIC_NO_CHECK_APPTYPE));
|
29
|
+
#endif
|
30
|
+
|
31
|
+
#ifdef MAGIC_NO_COMPRESS_FORK
|
32
|
+
rb_define_const(rb_klass, "MAGIC_NO_COMPRESS_FORK", INT2FIX(MAGIC_NO_COMPRESS_FORK));
|
29
33
|
#endif
|
30
34
|
|
31
35
|
#ifdef MAGIC_NO_CHECK_CDF
|
32
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_CDF", INT2FIX(MAGIC_NO_CHECK_CDF))
|
36
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_CDF", INT2FIX(MAGIC_NO_CHECK_CDF));
|
33
37
|
#endif
|
34
38
|
|
35
39
|
#ifdef MAGIC_NO_CHECK_COMPRESS
|
36
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_COMPRESS", INT2FIX(MAGIC_NO_CHECK_COMPRESS))
|
40
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_COMPRESS", INT2FIX(MAGIC_NO_CHECK_COMPRESS));
|
37
41
|
#endif
|
38
42
|
|
39
43
|
#ifdef MAGIC_NO_CHECK_ELF
|
40
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_ELF", INT2FIX(MAGIC_NO_CHECK_ELF))
|
44
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_ELF", INT2FIX(MAGIC_NO_CHECK_ELF));
|
41
45
|
#endif
|
42
46
|
|
43
47
|
#ifdef MAGIC_NO_CHECK_ENCODING
|
44
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_ENCODING", INT2FIX(MAGIC_NO_CHECK_ENCODING))
|
48
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_ENCODING", INT2FIX(MAGIC_NO_CHECK_ENCODING));
|
45
49
|
#endif
|
46
50
|
|
47
51
|
#ifdef MAGIC_NO_CHECK_SOFT
|
48
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_SOFT", INT2FIX(MAGIC_NO_CHECK_SOFT))
|
52
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_SOFT", INT2FIX(MAGIC_NO_CHECK_SOFT));
|
49
53
|
#endif
|
50
54
|
|
51
55
|
#ifdef MAGIC_NO_CHECK_TAR
|
52
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_TAR", INT2FIX(MAGIC_NO_CHECK_TAR))
|
56
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_TAR", INT2FIX(MAGIC_NO_CHECK_TAR));
|
53
57
|
#endif
|
54
58
|
|
55
59
|
#ifdef MAGIC_NO_CHECK_TEXT
|
56
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_TEXT", INT2FIX(MAGIC_NO_CHECK_TEXT))
|
60
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_TEXT", INT2FIX(MAGIC_NO_CHECK_TEXT));
|
57
61
|
#endif
|
58
62
|
|
59
63
|
#ifdef MAGIC_NO_CHECK_TOKENS
|
60
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_TOKENS", INT2FIX(MAGIC_NO_CHECK_TOKENS))
|
64
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_TOKENS", INT2FIX(MAGIC_NO_CHECK_TOKENS));
|
61
65
|
#endif
|
62
66
|
|
63
67
|
#ifdef MAGIC_NO_CHECK_JSON
|
64
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_JSON", INT2FIX(MAGIC_NO_CHECK_JSON))
|
68
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_JSON", INT2FIX(MAGIC_NO_CHECK_JSON));
|
65
69
|
#endif
|
66
70
|
|
67
71
|
#ifdef MAGIC_NO_CHECK_CSV
|
68
|
-
rb_define_const(rb_klass, "MAGIC_NO_CHECK_CSV", INT2FIX(MAGIC_NO_CHECK_CSV))
|
72
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_CSV", INT2FIX(MAGIC_NO_CHECK_CSV));
|
73
|
+
#endif
|
74
|
+
|
75
|
+
#ifdef MAGIC_NO_CHECK_SIMH
|
76
|
+
rb_define_const(rb_klass, "MAGIC_NO_CHECK_SIMH", INT2FIX(MAGIC_NO_CHECK_SIMH));
|
69
77
|
#endif
|
70
78
|
}
|
71
79
|
|
72
80
|
VALUE lsmodes(volatile VALUE obj) {
|
73
|
-
VALUE hash = rb_hash_new()
|
74
|
-
|
75
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NONE")), INT2FIX(MAGIC_NONE))
|
76
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_DEBUG")), INT2FIX(MAGIC_DEBUG))
|
77
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_SYMLINK")), INT2FIX(MAGIC_SYMLINK))
|
78
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_COMPRESS")), INT2FIX(MAGIC_COMPRESS))
|
79
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_DEVICES")), INT2FIX(MAGIC_DEVICES))
|
80
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_MIME_TYPE")), INT2FIX(MAGIC_MIME_TYPE))
|
81
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_MIME_ENCODING")), INT2FIX(MAGIC_MIME_ENCODING))
|
82
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_MIME")), INT2FIX(MAGIC_MIME))
|
83
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_CONTINUE")), INT2FIX(MAGIC_CONTINUE))
|
84
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_CHECK")), INT2FIX(MAGIC_CHECK))
|
85
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PRESERVE_ATIME")), INT2FIX(MAGIC_PRESERVE_ATIME))
|
86
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_RAW")), INT2FIX(MAGIC_RAW))
|
87
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_ERROR")), INT2FIX(MAGIC_ERROR))
|
88
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_APPLE")), INT2FIX(MAGIC_APPLE))
|
81
|
+
VALUE hash = rb_hash_new();
|
82
|
+
|
83
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NONE")), INT2FIX(MAGIC_NONE));
|
84
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_DEBUG")), INT2FIX(MAGIC_DEBUG));
|
85
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_SYMLINK")), INT2FIX(MAGIC_SYMLINK));
|
86
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_COMPRESS")), INT2FIX(MAGIC_COMPRESS));
|
87
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_DEVICES")), INT2FIX(MAGIC_DEVICES));
|
88
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_MIME_TYPE")), INT2FIX(MAGIC_MIME_TYPE));
|
89
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_MIME_ENCODING")), INT2FIX(MAGIC_MIME_ENCODING));
|
90
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_MIME")), INT2FIX(MAGIC_MIME));
|
91
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_CONTINUE")), INT2FIX(MAGIC_CONTINUE));
|
92
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_CHECK")), INT2FIX(MAGIC_CHECK));
|
93
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PRESERVE_ATIME")), INT2FIX(MAGIC_PRESERVE_ATIME));
|
94
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_RAW")), INT2FIX(MAGIC_RAW));
|
95
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_ERROR")), INT2FIX(MAGIC_ERROR));
|
96
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_APPLE")), INT2FIX(MAGIC_APPLE));
|
89
97
|
#ifdef MAGIC_EXTENSION
|
90
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_EXTENSION")), INT2FIX(MAGIC_EXTENSION))
|
98
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_EXTENSION")), INT2FIX(MAGIC_EXTENSION));
|
91
99
|
#endif
|
92
100
|
|
93
101
|
#ifdef MAGIC_COMPRESS_TRANSP
|
94
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_COMPRESS_TRANSP")), INT2FIX(MAGIC_COMPRESS_TRANSP))
|
102
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_COMPRESS_TRANSP")), INT2FIX(MAGIC_COMPRESS_TRANSP));
|
95
103
|
#endif
|
96
104
|
|
97
105
|
#ifdef MAGIC_NO_CHECK_APPTYPE
|
98
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_APPTYPE")), INT2FIX(MAGIC_NO_CHECK_APPTYPE))
|
106
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_APPTYPE")), INT2FIX(MAGIC_NO_CHECK_APPTYPE));
|
107
|
+
#endif
|
108
|
+
|
109
|
+
#ifdef MAGIC_NO_COMPRESS_FORK
|
110
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_COMPRESS_FORK")), INT2FIX(MAGIC_NO_COMPRESS_FORK));
|
99
111
|
#endif
|
100
112
|
|
101
113
|
#ifdef MAGIC_NO_CHECK_CDF
|
102
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_CDF")), INT2FIX(MAGIC_NO_CHECK_CDF))
|
114
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_CDF")), INT2FIX(MAGIC_NO_CHECK_CDF));
|
103
115
|
#endif
|
104
116
|
|
105
117
|
#ifdef MAGIC_NO_CHECK_COMPRESS
|
106
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_COMPRESS")), INT2FIX(MAGIC_NO_CHECK_COMPRESS))
|
118
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_COMPRESS")), INT2FIX(MAGIC_NO_CHECK_COMPRESS));
|
107
119
|
#endif
|
108
120
|
|
109
121
|
#ifdef MAGIC_NO_CHECK_ELF
|
110
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_ELF")), INT2FIX(MAGIC_NO_CHECK_ELF))
|
122
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_ELF")), INT2FIX(MAGIC_NO_CHECK_ELF));
|
111
123
|
#endif
|
112
124
|
|
113
125
|
#ifdef MAGIC_NO_CHECK_ENCODING
|
114
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_ENCODING")), INT2FIX(MAGIC_NO_CHECK_ENCODING))
|
126
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_ENCODING")), INT2FIX(MAGIC_NO_CHECK_ENCODING));
|
115
127
|
#endif
|
116
128
|
|
117
129
|
#ifdef MAGIC_NO_CHECK_SOFT
|
118
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_SOFT")), INT2FIX(MAGIC_NO_CHECK_SOFT))
|
130
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_SOFT")), INT2FIX(MAGIC_NO_CHECK_SOFT));
|
119
131
|
#endif
|
120
132
|
|
121
133
|
#ifdef MAGIC_NO_CHECK_TAR
|
122
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_TAR")), INT2FIX(MAGIC_NO_CHECK_TAR))
|
134
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_TAR")), INT2FIX(MAGIC_NO_CHECK_TAR));
|
123
135
|
#endif
|
124
136
|
|
125
137
|
#ifdef MAGIC_NO_CHECK_TEXT
|
126
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_TEXT")), INT2FIX(MAGIC_NO_CHECK_TEXT))
|
138
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_TEXT")), INT2FIX(MAGIC_NO_CHECK_TEXT));
|
127
139
|
#endif
|
128
140
|
|
129
141
|
#ifdef MAGIC_NO_CHECK_TOKENS
|
130
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_TOKENS")), INT2FIX(MAGIC_NO_CHECK_TOKENS))
|
142
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_TOKENS")), INT2FIX(MAGIC_NO_CHECK_TOKENS));
|
131
143
|
#endif
|
132
144
|
|
133
|
-
#ifdef
|
134
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("
|
145
|
+
#ifdef MAGIC_NO_CHECK_JSON
|
146
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_JSON")), INT2FIX(MAGIC_NO_CHECK_JSON));
|
147
|
+
#endif
|
148
|
+
|
149
|
+
#ifdef MAGIC_NO_CHECK_CSV
|
150
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_CSV")), INT2FIX(MAGIC_NO_CHECK_CSV));
|
135
151
|
#endif
|
136
152
|
|
137
|
-
#ifdef
|
138
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("
|
153
|
+
#ifdef MAGIC_NO_CHECK_SIMH
|
154
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_NO_CHECK_SIMH")), INT2FIX(MAGIC_NO_CHECK_SIMH));
|
139
155
|
#endif
|
140
156
|
|
141
|
-
return hash
|
157
|
+
return hash;
|
142
158
|
}
|
data/ext/libmagic/params.h
CHANGED
@@ -1,63 +1,63 @@
|
|
1
1
|
void params(volatile VALUE rb_klass) {
|
2
2
|
#ifdef MAGIC_PARAM_INDIR_MAX
|
3
|
-
rb_define_const(rb_klass, "MAGIC_PARAM_INDIR_MAX", UINT2NUM(MAGIC_PARAM_INDIR_MAX))
|
3
|
+
rb_define_const(rb_klass, "MAGIC_PARAM_INDIR_MAX", UINT2NUM(MAGIC_PARAM_INDIR_MAX));
|
4
4
|
#endif
|
5
5
|
|
6
6
|
#ifdef MAGIC_PARAM_NAME_MAX
|
7
|
-
rb_define_const(rb_klass, "MAGIC_PARAM_NAME_MAX", UINT2NUM(MAGIC_PARAM_NAME_MAX))
|
7
|
+
rb_define_const(rb_klass, "MAGIC_PARAM_NAME_MAX", UINT2NUM(MAGIC_PARAM_NAME_MAX));
|
8
8
|
#endif
|
9
9
|
|
10
10
|
#ifdef MAGIC_PARAM_ELF_NOTES_MAX
|
11
|
-
rb_define_const(rb_klass, "MAGIC_PARAM_ELF_NOTES_MAX", UINT2NUM(MAGIC_PARAM_ELF_NOTES_MAX))
|
11
|
+
rb_define_const(rb_klass, "MAGIC_PARAM_ELF_NOTES_MAX", UINT2NUM(MAGIC_PARAM_ELF_NOTES_MAX));
|
12
12
|
#endif
|
13
13
|
|
14
14
|
#ifdef MAGIC_PARAM_ELF_PHNUM_MAX
|
15
|
-
rb_define_const(rb_klass, "MAGIC_PARAM_ELF_PHNUM_MAX", UINT2NUM(MAGIC_PARAM_ELF_PHNUM_MAX))
|
15
|
+
rb_define_const(rb_klass, "MAGIC_PARAM_ELF_PHNUM_MAX", UINT2NUM(MAGIC_PARAM_ELF_PHNUM_MAX));
|
16
16
|
#endif
|
17
17
|
|
18
18
|
#ifdef MAGIC_PARAM_ELF_SHNUM_MAX
|
19
|
-
rb_define_const(rb_klass, "MAGIC_PARAM_ELF_SHNUM_MAX", UINT2NUM(MAGIC_PARAM_ELF_SHNUM_MAX))
|
19
|
+
rb_define_const(rb_klass, "MAGIC_PARAM_ELF_SHNUM_MAX", UINT2NUM(MAGIC_PARAM_ELF_SHNUM_MAX));
|
20
20
|
#endif
|
21
21
|
|
22
22
|
#ifdef MAGIC_PARAM_REGEX_MAX
|
23
|
-
rb_define_const(rb_klass, "MAGIC_PARAM_REGEX_MAX", UINT2NUM(MAGIC_PARAM_REGEX_MAX))
|
23
|
+
rb_define_const(rb_klass, "MAGIC_PARAM_REGEX_MAX", UINT2NUM(MAGIC_PARAM_REGEX_MAX));
|
24
24
|
#endif
|
25
25
|
|
26
26
|
#ifdef MAGIC_PARAM_BYTES_MAX
|
27
|
-
rb_define_const(rb_klass, "MAGIC_PARAM_BYTES_MAX", UINT2NUM(MAGIC_PARAM_BYTES_MAX))
|
27
|
+
rb_define_const(rb_klass, "MAGIC_PARAM_BYTES_MAX", UINT2NUM(MAGIC_PARAM_BYTES_MAX));
|
28
28
|
#endif
|
29
29
|
}
|
30
30
|
|
31
31
|
VALUE lsparams(volatile VALUE obj) {
|
32
|
-
VALUE hash = rb_hash_new()
|
32
|
+
VALUE hash = rb_hash_new();
|
33
33
|
|
34
34
|
#ifdef MAGIC_PARAM_INDIR_MAX
|
35
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_INDIR_MAX")), UINT2NUM(MAGIC_PARAM_INDIR_MAX))
|
35
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_INDIR_MAX")), UINT2NUM(MAGIC_PARAM_INDIR_MAX));
|
36
36
|
#endif
|
37
37
|
|
38
38
|
#ifdef MAGIC_PARAM_NAME_MAX
|
39
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_NAME_MAX")), UINT2NUM(MAGIC_PARAM_NAME_MAX))
|
39
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_NAME_MAX")), UINT2NUM(MAGIC_PARAM_NAME_MAX));
|
40
40
|
#endif
|
41
41
|
|
42
42
|
#ifdef MAGIC_PARAM_ELF_NOTES_MAX
|
43
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_ELF_NOTES_MAX")), UINT2NUM(MAGIC_PARAM_ELF_NOTES_MAX))
|
43
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_ELF_NOTES_MAX")), UINT2NUM(MAGIC_PARAM_ELF_NOTES_MAX));
|
44
44
|
#endif
|
45
45
|
|
46
46
|
#ifdef MAGIC_PARAM_ELF_PHNUM_MAX
|
47
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_ELF_PHNUM_MAX")), UINT2NUM(MAGIC_PARAM_ELF_PHNUM_MAX))
|
47
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_ELF_PHNUM_MAX")), UINT2NUM(MAGIC_PARAM_ELF_PHNUM_MAX));
|
48
48
|
#endif
|
49
49
|
|
50
50
|
#ifdef MAGIC_PARAM_ELF_SHNUM_MAX
|
51
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_ELF_SHNUM_MAX")), UINT2NUM(MAGIC_PARAM_ELF_SHNUM_MAX))
|
51
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_ELF_SHNUM_MAX")), UINT2NUM(MAGIC_PARAM_ELF_SHNUM_MAX));
|
52
52
|
#endif
|
53
53
|
|
54
54
|
#ifdef MAGIC_PARAM_REGEX_MAX
|
55
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_REGEX_MAX")), UINT2NUM(MAGIC_PARAM_REGEX_MAX))
|
55
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_REGEX_MAX")), UINT2NUM(MAGIC_PARAM_REGEX_MAX));
|
56
56
|
#endif
|
57
57
|
|
58
58
|
#ifdef MAGIC_PARAM_BYTES_MAX
|
59
|
-
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_BYTES_MAX")), UINT2NUM(MAGIC_PARAM_BYTES_MAX))
|
59
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("MAGIC_PARAM_BYTES_MAX")), UINT2NUM(MAGIC_PARAM_BYTES_MAX));
|
60
60
|
#endif
|
61
61
|
|
62
|
-
return hash
|
62
|
+
return hash;
|
63
63
|
}
|
data/ext/libmagic/validations.h
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
void fileReadable(char *filePath) {
|
2
|
-
if(access(filePath, F_OK)) rb_raise(rb_eFileNotFoundError, "%s", filePath)
|
3
|
-
if(access(filePath, R_OK)) rb_raise(rb_eFileNotReadableError, "%s", filePath)
|
2
|
+
if(access(filePath, F_OK)) rb_raise(rb_eFileNotFoundError, "%s", filePath);
|
3
|
+
if(access(filePath, R_OK)) rb_raise(rb_eFileNotReadableError, "%s", filePath);
|
4
4
|
}
|
5
5
|
|
6
6
|
void magic_validate_db(magic_t cookie, char *databasePath) {
|
7
|
-
struct stat statbuf
|
7
|
+
struct stat statbuf;
|
8
8
|
|
9
9
|
if (stat(databasePath, &statbuf) != 0)
|
10
|
-
rb_raise(rb_eFileNotFoundError, "%s", databasePath)
|
10
|
+
rb_raise(rb_eFileNotFoundError, "%s", databasePath);
|
11
11
|
|
12
12
|
if(S_ISDIR(statbuf.st_mode))
|
13
|
-
rb_raise(rb_eIsDirError, "%s", databasePath)
|
13
|
+
rb_raise(rb_eIsDirError, "%s", databasePath);
|
14
14
|
|
15
|
-
if(access(databasePath, R_OK)) rb_raise(rb_eFileNotReadableError, "%s", databasePath)
|
15
|
+
if(access(databasePath, R_OK)) rb_raise(rb_eFileNotReadableError, "%s", databasePath);
|
16
16
|
|
17
|
-
int validFile = magic_check(cookie, databasePath)
|
17
|
+
int validFile = magic_check(cookie, databasePath);
|
18
18
|
|
19
19
|
if (validFile != 0) {
|
20
|
-
const char *err = magic_error(cookie)
|
21
|
-
rb_raise(rb_eInvalidDBError, "%s (%s is not a valid magic file)", err, databasePath)
|
20
|
+
const char *err = magic_error(cookie);
|
21
|
+
rb_raise(rb_eInvalidDBError, "%s (%s is not a valid magic file)", err, databasePath);
|
22
22
|
}
|
23
23
|
}
|
data/lib/libmagic_rb/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libmagic_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cybergizer
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Check filetype with libmagic
|
14
14
|
email:
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '0'
|
53
53
|
requirements: []
|
54
|
-
rubygems_version: 3.
|
54
|
+
rubygems_version: 3.5.16
|
55
55
|
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: Check filetype with libmagic
|