libmagic_rb 0.1.2.pre.beta → 0.1.2.pre.gamma

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f249db73a864f2f290165eb9dc8d943f8016e25d202e0a592754418510f4110
4
- data.tar.gz: c3026e23ac5e7ed0416b0c7e0694982baed1b7f78ec95f3845a5e616076b88b1
3
+ metadata.gz: 116d3dc132643a74f2a887128844a9faa5a4b0a53b7aded5e70bd296087ad005
4
+ data.tar.gz: 77f551eddaae7a4d1e70840608ec008cc509569d5c9153d0e07f7d710fc371e7
5
5
  SHA512:
6
- metadata.gz: '065198fe056d97dd295fd472697e0b1dcdef27dee5a995eca8718035beb64bf8b2ed3ca7eff6c6fd4462efcb6892f4f05ed33d8139872a193e2d2296c2e138e1'
7
- data.tar.gz: 6a85f647db0401541b18cebdb2020518a3d8421c4e15452f0d47bab17808f8ad950aba613e3d7188b610034f3f3e58ca86b0e53d45939ed7709942630e2f4523
6
+ metadata.gz: 804a95966aeca5e305ff5b6d18a733528dcbf179617cefb461bd779c0dbbb8aa044d6edeca9a55b2b04b62c8c6b8c69b82f8d0a4c7663eba43e6798b8ab51a31
7
+ data.tar.gz: fefe9f019f2decd1e85f7836eacbac01bc6c5e9f22beb22d5e271b903eed83d7b6e25d942e3460be4a2f92d9312faa4eaac477775b6c75c36e23d68035634813
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # LibmagicRb
1
+ # LibmagicRb [![Gem Version](https://badge.fury.io/rb/libmagic_rb.svg)](https://rubygems.org/gems/libmagic_rb) ![Workflow Status](https://github.com/cybergizer-hq/libmagicrb/workflows/LibmagicRb%20Test/badge.svg)
2
+
2
3
  Adds ability to check mime-type of a file using the libmagic ([magic(4)](https://man7.org/linux/man-pages/man4/magic.4.html)).
3
4
  It uses native extensions and it's quite performant.
4
5
 
data/ext/libmagic/func.h CHANGED
@@ -1,3 +1,15 @@
1
+ /*
2
+ Closes a magic cookie. For example:
3
+
4
+ > cookie = LibmagicRb.new(file: '/usr/share/dict/words')
5
+ # => #<LibmagicRb:0x00005581019fa7e0 @closed=false, @db=nil, @file="/usr/share/dict/words", @mode=1106>
6
+
7
+ > cookie.close
8
+ # => #<LibmagicRb:0x00005581019fa7e0 @closed=true, @db=nil, @file="/usr/share/dict/words", @mode=1106>
9
+
10
+ Returns self.
11
+ */
12
+
1
13
  VALUE _closeGlobal_(volatile VALUE self) {
2
14
  RB_UNWRAP(cookie) ;
3
15
 
@@ -7,6 +19,23 @@ VALUE _closeGlobal_(volatile VALUE self) {
7
19
  return self ;
8
20
  }
9
21
 
22
+ /*
23
+ Changes the database path. For example:
24
+ > cookie = LibmagicRb.new(file: '/usr/share/dict/words')
25
+ # => #<LibmagicRb:0x00005581019181b0 @closed=false, @db=nil, @file="/usr/share/dict/words", @mode=1106>
26
+
27
+ > cookie.db = '/usr/share/file/misc/magic.mgc'
28
+ # => "/usr/share/file/misc/magic.mgc"
29
+
30
+ > cookie.check
31
+ # => "text/plain; charset=utf-8"
32
+
33
+ > cookie.close
34
+ # => #<LibmagicRb:0x00005581019181b0 @closed=true, @db="/usr/share/file/misc/magic.mgc", @file="/usr/share/dict/words", @mode=1106>
35
+
36
+ Returns self.
37
+ */
38
+
10
39
  VALUE _loadGlobal_(volatile VALUE self, volatile VALUE dbPath) {
11
40
  char *databasePath = NULL ;
12
41
 
@@ -27,6 +56,20 @@ VALUE _loadGlobal_(volatile VALUE self, volatile VALUE dbPath) {
27
56
  return self ;
28
57
  }
29
58
 
59
+ /*
60
+ Check a file with the magic database. For example:
61
+ > cookie = LibmagicRb.new(file: '/usr/share/dict/words')
62
+ # => #<LibmagicRb:0x00005581019181b0 @closed=false, @db=nil, @file="/usr/share/dict/words", @mode=1106>
63
+
64
+ > cookie.check
65
+ # => "text/plain; charset=utf-8"
66
+
67
+ > cookie.close
68
+ # => #<LibmagicRb:0x00005581019181b0 @closed=true, @db="/usr/share/file/misc/magic.mgc", @file="/usr/share/dict/words", @mode=1106>
69
+
70
+ Returns String or nil.
71
+ */
72
+
30
73
  VALUE _checkGlobal_(volatile VALUE self) {
31
74
  RB_UNWRAP(cookie) ;
32
75
 
@@ -51,6 +94,18 @@ VALUE _checkGlobal_(volatile VALUE self) {
51
94
  return mt ? rb_str_new_cstr(mt) : Qnil ;
52
95
  }
53
96
 
97
+ /*
98
+ Get parameters for a cookie. For example:
99
+ cookie = LibmagicRb.new(file: '/usr/share/dict/words')
100
+ # => #<LibmagicRb:0x00005581018f35e0 @closed=false, @db=nil, @file="/usr/share/dict/words", @mode=1106>
101
+
102
+ > cookie.getparam(LibmagicRb::MAGIC_PARAM_NAME_MAX)
103
+ # => 50
104
+
105
+ > cookie.close
106
+ # => #<LibmagicRb:0x00005581018f35e0 @closed=true, @db=nil, @file="/usr/share/dict/words", @mode=1106>
107
+ */
108
+
54
109
  VALUE _getParamGlobal_(volatile VALUE self, volatile VALUE param) {
55
110
  #if MAGIC_VERSION > 525
56
111
  RB_UNWRAP(cookie) ;
@@ -66,6 +121,27 @@ VALUE _getParamGlobal_(volatile VALUE self, volatile VALUE param) {
66
121
  #endif
67
122
  }
68
123
 
124
+ /*
125
+ Sets parameter for a cookie. For example:
126
+
127
+ > cookie = LibmagicRb.new(file: '/usr/share/dict/words')
128
+ # => #<LibmagicRb:0x00005581019f9840 @closed=false, @db=nil, @file="/usr/share/dict/words", @mode=1106>
129
+
130
+ > cookie.getparam(LibmagicRb::MAGIC_PARAM_NAME_MAX)
131
+ => 50
132
+
133
+ > cookie.setparam(LibmagicRb::MAGIC_PARAM_NAME_MAX, 101)
134
+ # => 101
135
+
136
+ > cookie.getparam(LibmagicRb::MAGIC_PARAM_NAME_MAX)
137
+ # => 101
138
+
139
+ > cookie.close
140
+ # => #<LibmagicRb:0x00005581019f9840 @closed=true, @db=nil, @file="/usr/share/dict/words", @mode=1106>
141
+
142
+ Returns Integer or nil on failure.
143
+ */
144
+
69
145
  VALUE _setParamGlobal_(volatile VALUE self, volatile VALUE param, volatile VALUE paramVal) {
70
146
  #if MAGIC_VERSION > 525
71
147
  unsigned int _param = NUM2UINT(param) ;
@@ -85,25 +161,121 @@ VALUE _setParamGlobal_(volatile VALUE self, volatile VALUE param, volatile VALUE
85
161
  #endif
86
162
  }
87
163
 
164
+ /*
165
+ Returns a textual description of the contents of the buffer argument with length bytes size.
166
+
167
+ For example:
168
+
169
+ > cookie = LibmagicRb.new(file: '.')
170
+ # => #<LibmagicRb:0x00005582de0d1bf8 @closed=false, @db=nil, @file=".", @mode=1106>
171
+
172
+ > cookie.magic_buffer("%PDF-1.3\r\n")
173
+ # => "application/pdf; charset=us-ascii"
174
+
175
+ > cookie.close
176
+ # => #<LibmagicRb:0x00005582de0d1bf8 @closed=true, @db=nil, @file=".", @mode=1106>
177
+
178
+ Note that it automatically loads the database.
179
+
180
+ Returns either String or nil.
181
+ */
182
+
88
183
  VALUE _bufferGlobal_(volatile VALUE self, volatile VALUE string) {
89
184
  RB_UNWRAP(cookie) ;
90
185
 
186
+ VALUE db = rb_iv_get(self, "@db") ;
187
+
188
+ char *database = NULL ;
189
+ if(RB_TYPE_P(db, T_STRING)) {
190
+ database = StringValuePtr(db) ;
191
+ }
192
+
193
+ if(database) magic_validate_db(*cookie, database) ;
194
+ magic_load(*cookie, database) ;
195
+
91
196
  char *buffer = StringValuePtr(string) ;
92
- const char *buf = magic_buffer(*cookie, buffer, sizeof(buffer)) ;
197
+ const char *buf = magic_buffer(*cookie, buffer, strlen(buffer)) ;
93
198
 
94
- return rb_str_new_cstr(buf) ;
199
+ return buf ? rb_str_new_cstr(buf) : Qnil ;
95
200
  }
96
201
 
202
+ /*
203
+ Dumps all magic entries in a human
204
+ readable format, dumping first the entries that are matched against
205
+ binary files and then the ones that match text files.
206
+
207
+ For example:
208
+
209
+
210
+ > cookie = LibmagicRb.new(file: '.')
211
+ # => #<LibmagicRb:0x000055cf280a9b30 @closed=false, @db=nil, @file=".", @mode=1106>
212
+
213
+ > cookie.magic_list
214
+ Set 0:
215
+ Binary patterns:
216
+ Strength = 500@47: Biosig/Brainvision Marker file [biosig/brainvision]
217
+ Strength = 490@122: Biosig/TMSiLOG [biosig/tmsilog]
218
+ Strength = 461@127: Biosig/SYNERGY [biosig/synergy]
219
+ Strength = 460@46: Biosig/Brainvision V-Amp file []
220
+ Strength = 410@45: Biosig/Brainvision data file []
221
+ Strength = 380@6: OpenSSH private key []
222
+ Strength = 370@1266: Novell message librarian data []
223
+ .
224
+ .
225
+ .
226
+ # => 0
227
+
228
+ > cookie.close
229
+ => #<LibmagicRb:0x000055cf280a9b30 @closed=true, @db=nil, @file=".", @mode=1106>
230
+ */
231
+
97
232
  VALUE _listGlobal_(volatile VALUE self) {
98
233
  RB_UNWRAP(cookie) ;
99
234
 
100
235
  VALUE db = rb_iv_get(self, "@db") ;
101
- char *database = StringValuePtr(db) ;
102
236
 
237
+ char *database = NULL ;
238
+ if (RB_TYPE_P(db, T_STRING)) {
239
+ database = StringValuePtr(db) ;
240
+ }
241
+
242
+ if(database) magic_validate_db(*cookie, database) ;
103
243
  int status = magic_list(*cookie, database) ;
244
+
104
245
  return INT2FIX(status) ;
105
246
  }
106
247
 
248
+ /*
249
+ Set flags/modes for a cookie (not to be confused with params).
250
+ It's same setting mode through the setter cookie.mode = mode.
251
+
252
+ For example:
253
+
254
+ > cookie = LibmagicRb.new(file: '.')
255
+ # => #<LibmagicRb:0x00005583732e1070 @closed=false, @db=nil, @file=".", @mode=1106>
256
+
257
+ > cookie.check
258
+ # => "inode/directory; charset=binary"
259
+
260
+ > cookie.setflags(LibmagicRb::MAGIC_RAW)
261
+ # => 256
262
+
263
+ > cookie.check
264
+ # => "directory"
265
+
266
+ #### Similarly you can also do this ####
267
+
268
+ > cookie.mode = LibmagicRb::MAGIC_MIME
269
+ # => 1040
270
+
271
+ > cookie.check
272
+ # => "inode/directory; charset=binary"
273
+
274
+ #### Close the cookie when done ####
275
+ > cookie.close
276
+ # => #<LibmagicRb:0x00005583732e1070 @closed=true, @db=nil, @file=".", @mode=256>
277
+ */
278
+
107
279
  VALUE _setflagsGlobal_(volatile VALUE self, volatile VALUE flags) {
108
280
  unsigned int flag = NUM2UINT(flags) ;
109
281
 
data/ext/libmagic/magic.c CHANGED
@@ -53,7 +53,21 @@ static rb_data_type_t fileType = {
53
53
  #include "validations.h"
54
54
  #include "func.h"
55
55
 
56
- VALUE _check_(volatile VALUE obj, volatile VALUE args) {
56
+ /*
57
+ Directly check a file with LibmagicRb, without creating any sort of cookies:
58
+
59
+ LibmagicRb.check(file: '/tmp/', db: '/usr/share/file/misc/magic.mgc', mode: LibmagicRb::MAGIC_CHECK) # => "sticky, directory"
60
+
61
+ [file] The key `file:` is the filename to check. Should be a string
62
+
63
+ [db] The key `db:` can be left as nil. Or you can give it the path of the current magic database.
64
+
65
+ [mode] The key `mode` can be any of the LibmagicRb.lsmodes().
66
+ To combine modes you can use `|`. For example:
67
+ `mode: LibmagicRb::MAGIC_CHECK | LibmagicRb::MAGIC_SYMLINK | Libmagic_MAGIC_MIME`
68
+ If `mode` key is nil, it will default to `MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK`
69
+ */
70
+ static VALUE _check_(volatile VALUE obj, volatile VALUE args) {
57
71
  if(!RB_TYPE_P(args, T_HASH)) {
58
72
  rb_raise(rb_eArgError, "Expected hash as argument.") ;
59
73
  }
@@ -111,6 +125,43 @@ VALUE _check_(volatile VALUE obj, volatile VALUE args) {
111
125
  return retVal ;
112
126
  }
113
127
 
128
+ /*
129
+ Intializes a magic cookie that can be used multiple times.
130
+ The benefit of this is to assgign various cookies and change flags of each cookie.
131
+
132
+ For example:
133
+
134
+ > cookie = LibmagicRb.new(file: '/tmp')
135
+ # => #<LibmagicRb:0x000055810139f738 @closed=false, @db=nil, @file="/tmp", @mode=1106>
136
+
137
+ > cookie.check
138
+ # => "inode/directory; charset=binary"
139
+
140
+ > cookie.setflags(LibmagicRb::MAGIC_RAW)
141
+ # => 256
142
+
143
+ > cookie.check
144
+ # => "sticky, directory"
145
+
146
+ > cookie2 = LibmagicRb.new(file: '/usr/share/dict/words')
147
+ # => #<LibmagicRb:0x000055810190a060 @closed=false, @db=nil, @file="/usr/share/dict/words", @mode=1106>
148
+
149
+ > cookie2.check
150
+ # => "text/plain; charset=utf-8"
151
+
152
+ > cookie.close
153
+ # => #<LibmagicRb:0x000055810139f738 @closed=true, @db=nil, @file="/tmp", @mode=256>
154
+
155
+ > cookie.closed?
156
+ # => true
157
+
158
+ > cookie2.closed?
159
+ # => false
160
+
161
+ Here in this example, we can't use cookie, but cookie2 is a different magic_t wrapper, so we can continue using that.
162
+ Flags/modes applied to cookie, will not affect cookie2 as well. Think of them as totally different containers.
163
+ Of course, you must close cookies when you don't need them. Otherwise it can use memories unless GC is triggered.
164
+ */
114
165
  VALUE rb_libmagicRb_initialize(volatile VALUE self, volatile VALUE args) {
115
166
  // Database Path
116
167
  if(!RB_TYPE_P(args, T_HASH)) {
@@ -174,6 +225,10 @@ void Init_main() {
174
225
  /*
175
226
  * Libmagic Errors
176
227
  */
228
+
229
+ /*
230
+ Adds ability to check mime-type of a file using the libmagic (magic(4)). It uses native extensions and it's quite performant.
231
+ */
177
232
  VALUE cLibmagicRb = rb_define_class("LibmagicRb", rb_cObject) ;
178
233
 
179
234
  rb_eFileNotFoundError = rb_define_class_under(cLibmagicRb, "FileNotFound", rb_eRuntimeError) ;
@@ -188,9 +243,15 @@ void Init_main() {
188
243
  modes(cLibmagicRb) ;
189
244
  params(cLibmagicRb) ;
190
245
 
246
+
191
247
  #if MAGIC_VERSION > 525
192
248
  char version[6] ;
193
249
  sprintf(version, "%0.2f", magic_version() / 100.0) ;
250
+
251
+ /*
252
+ LibmagicRb::MAGIC_VERSION returns the magic version of the library.
253
+ For older libmagic version, this can be undefined, so this method will return "0" instead.
254
+ */
194
255
  rb_define_const(cLibmagicRb, "MAGIC_VERSION", rb_str_new_cstr(version)) ;
195
256
  #else
196
257
  rb_define_const(cLibmagicRb, "MAGIC_VERSION", rb_str_new_cstr("0")) ;
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class LibmagicRb
4
- VERSION = "0.1.2-beta"
4
+ VERSION = "0.1.2-gamma"
5
5
  end
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.1.2.pre.beta
4
+ version: 0.1.2.pre.gamma
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cybergizer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-05 00:00:00.000000000 Z
11
+ date: 2021-09-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Check filetype with libmagic
14
14
  email: