malmal 0.4.0 → 0.4.1
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/lib/malmal/scanner.rb +77 -23
- data/lib/malmal/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e546c793eb5e47344ab11331e7214807c3e8976ae2d543bd12f956ef4387aa41
|
|
4
|
+
data.tar.gz: df31fd840f841b1b90a94a325766d2ee6624e3e3b415dd06d850f8036d8c4f8a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 71b126043c70f4e90b7c884675039fec27c37e1c1ef4cf27a2b14ffa9e9bf3479a4b1d2f1f781baf63f64ca622cad24555609b4c25d7e2c625dbe19e98643cc4
|
|
7
|
+
data.tar.gz: 0d49fbf5be3241697e39164a053bc67a6436cda79a6aa12655d1dc99ff2da94ee0d32753081205ae3bf7ed7fe51b1c656549adc44b7d63808bad619b174b5451
|
data/lib/malmal/scanner.rb
CHANGED
|
@@ -62,6 +62,8 @@ module Malmal
|
|
|
62
62
|
@options.merge!(limits)
|
|
63
63
|
@handle = build(JSON.generate(@options))
|
|
64
64
|
@mutex = Mutex.new
|
|
65
|
+
@in_use = 0
|
|
66
|
+
@closed = false
|
|
65
67
|
end
|
|
66
68
|
|
|
67
69
|
# Scan +data+: a String of bytes, or an IO read up to the size limit and
|
|
@@ -69,14 +71,18 @@ module Malmal
|
|
|
69
71
|
# claimed. Returns a Report; raises ScanError if the file was not scanned.
|
|
70
72
|
def scan(data, filename: nil, content_type: nil)
|
|
71
73
|
bytes = read(data)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
filename = claim(filename)
|
|
75
|
+
content_type = claim(content_type)
|
|
76
|
+
verdict, json = native_call do |scanner|
|
|
77
|
+
with_input(bytes) do |pointer|
|
|
78
|
+
report = FFI::MemoryPointer.new(:pointer)
|
|
79
|
+
verdict = Native.malmal_scan(scanner, pointer, bytes.bytesize, filename, content_type, report)
|
|
80
|
+
[verdict, take_string(report.read_pointer)]
|
|
81
|
+
end
|
|
79
82
|
end
|
|
83
|
+
raise ScanError, "the scanner failed; the file was not scanned" if verdict == Native::ERROR || json.nil?
|
|
84
|
+
|
|
85
|
+
Report.from_h(JSON.parse(json))
|
|
80
86
|
end
|
|
81
87
|
|
|
82
88
|
# Scan +data+ and, if it is clean and of a type that can be, write it
|
|
@@ -93,22 +99,32 @@ module Malmal
|
|
|
93
99
|
options[:types] = Array(options[:types]).map(&:to_s) if options.key?(:types)
|
|
94
100
|
options[:jpeg_quality] = jpeg_quality if jpeg_quality
|
|
95
101
|
bytes = read(data)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
filename = claim(filename)
|
|
103
|
+
content_type = claim(content_type)
|
|
104
|
+
options = JSON.generate(options)
|
|
105
|
+
status, rebuilt, json = native_call do |scanner|
|
|
106
|
+
with_input(bytes) do |pointer|
|
|
107
|
+
image = FFI::MemoryPointer.new(:pointer)
|
|
108
|
+
length = FFI::MemoryPointer.new(:size_t)
|
|
109
|
+
info = FFI::MemoryPointer.new(:pointer)
|
|
110
|
+
status = Native.malmal_sanitize_json(scanner, pointer, bytes.bytesize, filename, content_type, options,
|
|
111
|
+
image, length, info)
|
|
112
|
+
# Both are taken, and freed, whichever of them fails.
|
|
113
|
+
begin
|
|
114
|
+
rebuilt = take_bytes(image.read_pointer, length.read(:size_t))
|
|
115
|
+
ensure
|
|
116
|
+
json = take_string(info.read_pointer)
|
|
117
|
+
end
|
|
118
|
+
[status, rebuilt, json]
|
|
119
|
+
end
|
|
105
120
|
end
|
|
121
|
+
sanitized(status, rebuilt, json ? JSON.parse(json) : {})
|
|
106
122
|
end
|
|
107
123
|
|
|
108
124
|
# Scans that +wait+ stopped waiting for and that are still running.
|
|
109
125
|
# Above 0, restart the process when it can be.
|
|
110
126
|
def overdue
|
|
111
|
-
Native.malmal_scanner_overdue(
|
|
127
|
+
native_call { |scanner| Native.malmal_scanner_overdue(scanner) }
|
|
112
128
|
end
|
|
113
129
|
|
|
114
130
|
# The version of the loaded libmalmal_ffi.
|
|
@@ -116,16 +132,19 @@ module Malmal
|
|
|
116
132
|
Native.version
|
|
117
133
|
end
|
|
118
134
|
|
|
119
|
-
# Free the scanner now rather than when it is collected.
|
|
135
|
+
# Free the scanner now rather than when it is collected. Scans already
|
|
136
|
+
# running finish first, and the scanner is freed as the last of them
|
|
137
|
+
# returns; new ones raise ScanError.
|
|
120
138
|
def close
|
|
121
139
|
@mutex.synchronize do
|
|
122
|
-
@
|
|
123
|
-
|
|
140
|
+
@closed = true
|
|
141
|
+
free_if_unused
|
|
124
142
|
end
|
|
143
|
+
nil
|
|
125
144
|
end
|
|
126
145
|
|
|
127
146
|
def closed?
|
|
128
|
-
@
|
|
147
|
+
@closed
|
|
129
148
|
end
|
|
130
149
|
|
|
131
150
|
def inspect
|
|
@@ -144,8 +163,43 @@ module Malmal
|
|
|
144
163
|
Handle.new(pointer)
|
|
145
164
|
end
|
|
146
165
|
|
|
147
|
-
|
|
148
|
-
|
|
166
|
+
# Call into the library with the handle, which #close leaves alone until
|
|
167
|
+
# every call holding it has returned. Interrupts, such as Timeout's, wait
|
|
168
|
+
# until the call's results are taken and freed: raised as the native
|
|
169
|
+
# function returns, which is when Ruby raises them, they would leave
|
|
170
|
+
# what it allocated for the caller to free with no one to free it.
|
|
171
|
+
def native_call
|
|
172
|
+
Thread.handle_interrupt(Object => :never) do
|
|
173
|
+
scanner = acquire
|
|
174
|
+
begin
|
|
175
|
+
yield scanner
|
|
176
|
+
ensure
|
|
177
|
+
release
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def acquire
|
|
183
|
+
@mutex.synchronize do
|
|
184
|
+
raise ScanError, "the scanner is closed" if @closed
|
|
185
|
+
|
|
186
|
+
@in_use += 1
|
|
187
|
+
@handle
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def release
|
|
192
|
+
@mutex.synchronize do
|
|
193
|
+
@in_use -= 1
|
|
194
|
+
free_if_unused
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def free_if_unused
|
|
199
|
+
return unless @closed && @in_use.zero? && @handle
|
|
200
|
+
|
|
201
|
+
@handle.free
|
|
202
|
+
@handle = nil
|
|
149
203
|
end
|
|
150
204
|
|
|
151
205
|
def password_json(password)
|
data/lib/malmal/version.rb
CHANGED