lzma-ffi 0.0.1.2 → 0.0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lzma.jar +0 -0
- data/lib/lzma.rb +179 -135
- metadata +27 -37
data/lib/lzma.jar
ADDED
Binary file
|
data/lib/lzma.rb
CHANGED
@@ -17,177 +17,221 @@
|
|
17
17
|
# along with lzma-ffi. If not, see <http://www.gnu.org/licenses/>.
|
18
18
|
#++
|
19
19
|
|
20
|
-
|
21
|
-
require '
|
20
|
+
if RUBY_PLATFORM =~ /java/
|
21
|
+
require 'java'
|
22
|
+
require File.realpath(File.join(__FILE__, '..', 'lzma.jar'))
|
23
|
+
|
24
|
+
class LZMA
|
25
|
+
def self.decompress(what, buf_len=4096, &blk)
|
26
|
+
bais = what.is_a?(IO) ? what.to_inputstream : Java::java.io.ByteArrayInputStream.new(what.to_s.to_java_bytes)
|
27
|
+
lis = Java::net.contrapunctus.lzma.LzmaInputStream.new(bais)
|
28
|
+
dis = Java::java.io.DataInputStream.new(lis)
|
29
|
+
res = ''
|
30
|
+
blk ||= lambda {|chunk| res << chunk }
|
31
|
+
expanded, read = Java::byte[buf_len].new, 0
|
32
|
+
|
33
|
+
while (read = dis.read(expanded, 0, buf_len)) > 0
|
34
|
+
blk.call(String.from_java_bytes(expanded[0, read]))
|
35
|
+
end
|
22
36
|
|
23
|
-
|
24
|
-
def pointer
|
25
|
-
FFI::Pointer.new([self].pack('P').unpack('L!').first)
|
26
|
-
end
|
27
|
-
end
|
37
|
+
dis.close
|
28
38
|
|
29
|
-
|
30
|
-
|
31
|
-
extend FFI::Library
|
39
|
+
block_given? ? what : res
|
40
|
+
end
|
32
41
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
42
|
+
def self.extract(file, to=nil, buf_len=4096)
|
43
|
+
to ||= file.gsub(/\.(xz|lzma)$/, '')
|
44
|
+
File.unlink(to) if File.file?(to)
|
45
|
+
|
46
|
+
bais = Java::java.io.FileInputStream.new(file)
|
47
|
+
lis = Java::net.contrapunctus.lzma.LzmaInputStream.new(bais)
|
48
|
+
dis = Java::java.io.DataInputStream.new(lis)
|
49
|
+
out = Java::java.io.FileOutputStream.new(to)
|
50
|
+
expanded, read = Java::byte[buf_len].new, 0
|
51
|
+
|
52
|
+
while (read = dis.read(expanded, 0, buf_len)) > 0
|
53
|
+
out.write(expanded[0, read])
|
41
54
|
end
|
42
|
-
else
|
43
|
-
ffi_lib ['lzma.so.5.0.3', 'lzma.so.5', 'lzma.so', 'lzma']
|
44
|
-
end
|
45
55
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
enum :lzma_action, [:RUN, 0, :SYNC_FLUSH, :FULL_FLUSH, :FINISH]
|
51
|
-
enum :lzma_reserved_enum, [:LZMA_RESERVED_ENUM, 0]
|
52
|
-
|
53
|
-
class LZMAStream < FFI::Struct
|
54
|
-
layout \
|
55
|
-
:next_in, :pointer,
|
56
|
-
:avail_in, :size_t,
|
57
|
-
:total_in, :uint64,
|
58
|
-
|
59
|
-
:next_out, :pointer,
|
60
|
-
:avail_out, :size_t,
|
61
|
-
:total_out, :uint64,
|
62
|
-
|
63
|
-
:allocator, :pointer,
|
64
|
-
:internal, :pointer,
|
65
|
-
|
66
|
-
:reserved_ptr1, :pointer,
|
67
|
-
:reserved_ptr2, :pointer,
|
68
|
-
:reserved_ptr3, :pointer,
|
69
|
-
:reserved_ptr4, :pointer,
|
70
|
-
:reserved_int1, :uint64,
|
71
|
-
:reserved_int2, :uint64,
|
72
|
-
:reserved_int3, :size_t,
|
73
|
-
:reserved_int4, :size_t,
|
74
|
-
:reserved_enum1, :lzma_reserved_enum,
|
75
|
-
:reserved_enum2, :lzma_reserved_enum
|
56
|
+
dis.close
|
57
|
+
|
58
|
+
to
|
76
59
|
end
|
60
|
+
end
|
61
|
+
else
|
62
|
+
require 'ffi'
|
63
|
+
require 'stringio'
|
77
64
|
|
78
|
-
|
79
|
-
|
80
|
-
|
65
|
+
class String
|
66
|
+
def pointer
|
67
|
+
FFI::Pointer.new([self].pack('P').unpack('L!').first)
|
68
|
+
end
|
81
69
|
end
|
82
70
|
|
83
|
-
class
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
71
|
+
class LZMA
|
72
|
+
module C
|
73
|
+
extend FFI::Library
|
74
|
+
|
75
|
+
if RUBY_PLATFORM =~ /(?<!dar)win|w32/
|
76
|
+
case 1.size * 8
|
77
|
+
when 64
|
78
|
+
ffi_lib File.realpath(File.join('..', 'liblzma_x86_64.dll'), __FILE__)
|
79
|
+
when 32
|
80
|
+
ffi_lib File.realpath(File.join('..', 'liblzma_x86.dll'), __FILE__)
|
81
|
+
else
|
82
|
+
raise LoadError, "Windows architecture not supported"
|
83
|
+
end
|
84
|
+
else
|
85
|
+
ffi_lib ['lzma.so.5.0.3', 'lzma.so.5', 'lzma.so', 'lzma']
|
86
|
+
end
|
88
87
|
|
89
|
-
|
90
|
-
|
88
|
+
enum :lzma_ret, [:OK, 0, :STREAM_END, :NO_CHECK, :UNSUPPORTED_CHECK,
|
89
|
+
:GET_CHECK, :MEM_ERROR, :MEMLIMIT_ERROR, :FORMAT_ERROR, :OPTIONS_ERROR,
|
90
|
+
:DATA_ERROR, :BUF_ERROR, :PROG_ERROR]
|
91
|
+
|
92
|
+
enum :lzma_action, [:RUN, 0, :SYNC_FLUSH, :FULL_FLUSH, :FINISH]
|
93
|
+
enum :lzma_reserved_enum, [:LZMA_RESERVED_ENUM, 0]
|
94
|
+
|
95
|
+
class LZMAStream < FFI::Struct
|
96
|
+
layout \
|
97
|
+
:next_in, :pointer,
|
98
|
+
:avail_in, :size_t,
|
99
|
+
:total_in, :uint64,
|
100
|
+
|
101
|
+
:next_out, :pointer,
|
102
|
+
:avail_out, :size_t,
|
103
|
+
:total_out, :uint64,
|
104
|
+
|
105
|
+
:allocator, :pointer,
|
106
|
+
:internal, :pointer,
|
107
|
+
|
108
|
+
:reserved_ptr1, :pointer,
|
109
|
+
:reserved_ptr2, :pointer,
|
110
|
+
:reserved_ptr3, :pointer,
|
111
|
+
:reserved_ptr4, :pointer,
|
112
|
+
:reserved_int1, :uint64,
|
113
|
+
:reserved_int2, :uint64,
|
114
|
+
:reserved_int3, :size_t,
|
115
|
+
:reserved_int4, :size_t,
|
116
|
+
:reserved_enum1, :lzma_reserved_enum,
|
117
|
+
:reserved_enum2, :lzma_reserved_enum
|
118
|
+
end
|
91
119
|
|
92
|
-
|
93
|
-
|
120
|
+
attach_function :lzma_auto_decoder, [:pointer, :uint64, :uint32], :lzma_ret
|
121
|
+
attach_function :lzma_code, [:pointer, :lzma_action], :lzma_ret
|
122
|
+
attach_function :lzma_end, [:pointer], :void
|
94
123
|
end
|
95
124
|
|
96
|
-
|
97
|
-
|
98
|
-
|
125
|
+
class Stream
|
126
|
+
def initialize(stream, buf_len=4096)
|
127
|
+
@stream, @buf_len = stream, buf_len || 4096
|
128
|
+
@buf = (' ' * @buf_len).pointer
|
129
|
+
@struct = C::LZMAStream.new
|
99
130
|
|
100
|
-
|
101
|
-
|
102
|
-
end
|
131
|
+
ObjectSpace.define_finalizer(self, method(:finalize))
|
132
|
+
end
|
103
133
|
|
104
|
-
|
105
|
-
|
106
|
-
|
134
|
+
def avail_in
|
135
|
+
@struct[:avail_in]
|
136
|
+
end
|
107
137
|
|
108
|
-
|
109
|
-
|
110
|
-
|
138
|
+
def total_in
|
139
|
+
@struct[:total_in]
|
140
|
+
end
|
111
141
|
|
112
|
-
|
113
|
-
|
114
|
-
|
142
|
+
def avail_out
|
143
|
+
@struct[:avail_out]
|
144
|
+
end
|
115
145
|
|
116
|
-
|
117
|
-
|
118
|
-
|
146
|
+
def total_out
|
147
|
+
@struct[:total_out]
|
148
|
+
end
|
149
|
+
|
150
|
+
def to_ffi
|
151
|
+
@struct
|
152
|
+
end
|
119
153
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
154
|
+
def ptr
|
155
|
+
@struct.pointer
|
156
|
+
end
|
157
|
+
|
158
|
+
def size
|
159
|
+
@struct.size
|
160
|
+
end
|
161
|
+
|
162
|
+
def to_s
|
163
|
+
%w[in out].flat_map {|i|
|
164
|
+
%w[next avail total].map {|w|
|
165
|
+
send("#{w}_#{i}".to_sym)
|
166
|
+
}
|
124
167
|
}
|
125
|
-
|
126
|
-
end
|
168
|
+
end
|
127
169
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
170
|
+
def decoder(limit=-1, flags=0)
|
171
|
+
code = nil
|
172
|
+
raise RuntimeError, "lzma_stream_decoder error: #{code}" if (code = C.lzma_auto_decoder(@struct.pointer, limit, flags)) != :OK
|
173
|
+
self
|
174
|
+
end
|
133
175
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
176
|
+
def read
|
177
|
+
@struct[:next_in] = FFI::MemoryPointer.from_string(str = @stream.read(@buf_len))
|
178
|
+
@struct[:avail_in] = str.bytesize
|
179
|
+
end
|
138
180
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
181
|
+
def code(action)
|
182
|
+
@struct[:next_out] = @buf
|
183
|
+
@struct[:avail_out] = @buf_len
|
184
|
+
C.lzma_code(@struct.pointer, action)
|
185
|
+
end
|
144
186
|
|
145
|
-
|
146
|
-
|
147
|
-
|
187
|
+
def next_out
|
188
|
+
@buf.read_string_length(@buf_len - @struct[:avail_out])
|
189
|
+
end
|
148
190
|
|
149
|
-
|
150
|
-
|
151
|
-
|
191
|
+
def finalize
|
192
|
+
C.lzma_end(@struct.pointer)
|
193
|
+
end
|
152
194
|
|
153
|
-
|
154
|
-
|
195
|
+
def self.size
|
196
|
+
C::LZMAStream.size
|
197
|
+
end
|
155
198
|
end
|
156
|
-
end
|
157
199
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
200
|
+
def self.decompress(what, buf_len=4096, &blk)
|
201
|
+
what = StringIO.new(what.to_s) unless what.is_a?(IO)
|
202
|
+
res = ''
|
203
|
+
blk = lambda {|chunk| res << chunk } unless block_given?
|
162
204
|
|
163
|
-
|
205
|
+
stream = Stream.new(what, buf_len).decoder
|
164
206
|
|
165
|
-
|
166
|
-
|
167
|
-
|
207
|
+
until what.eof?
|
208
|
+
stream.read
|
209
|
+
action = what.eof? ? :FINISH : :RUN
|
168
210
|
|
169
|
-
|
170
|
-
|
171
|
-
|
211
|
+
begin
|
212
|
+
code = nil
|
213
|
+
raise RuntimeError, "lzma_code error: #{code}" unless [:OK, :STREAM_END].include?(code = stream.code(action))
|
172
214
|
|
173
|
-
|
174
|
-
|
175
|
-
|
215
|
+
blk.call(stream.next_out)
|
216
|
+
end while stream.avail_out.zero?
|
217
|
+
end
|
176
218
|
|
177
|
-
|
178
|
-
|
219
|
+
stream.finalize
|
220
|
+
what.close
|
179
221
|
|
180
|
-
|
181
|
-
|
222
|
+
block_given? ? what : res
|
223
|
+
end
|
182
224
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
225
|
+
def self.extract(file, to=nil, buf_len=4096)
|
226
|
+
to ||=file.gsub(/\.(xz|lzma)$/, '')
|
227
|
+
File.unlink(to) if File.file?(to)
|
228
|
+
File.open(to, 'wb') {|f|
|
229
|
+
decompress(File.open(file), buf_len) {|chunk|
|
230
|
+
f.write(chunk)
|
231
|
+
}
|
188
232
|
}
|
189
|
-
}
|
190
233
|
|
191
|
-
|
234
|
+
to
|
235
|
+
end
|
192
236
|
end
|
193
237
|
end
|
metadata
CHANGED
@@ -1,36 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lzma-ffi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 2
|
10
|
-
version: 0.0.1.2
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1.3
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
|
-
- shura
|
8
|
+
- shura
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2011-09-
|
13
|
+
date: 2011-09-29 00:00:00 +02:00
|
19
14
|
default_executable:
|
20
15
|
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
type: :runtime
|
33
|
-
version_requirements: *id001
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: ffi
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
34
27
|
description: liblzma bindings for ruby
|
35
28
|
email: shura1991@gmail.com
|
36
29
|
executables: []
|
@@ -40,9 +33,10 @@ extensions: []
|
|
40
33
|
extra_rdoc_files: []
|
41
34
|
|
42
35
|
files:
|
43
|
-
- lib/lzma.
|
44
|
-
- lib/
|
45
|
-
- lib/
|
36
|
+
- lib/lzma.jar
|
37
|
+
- lib/lzma.rb
|
38
|
+
- lib/liblzma_x86.dll
|
39
|
+
- lib/liblzma_x86_64.dll
|
46
40
|
has_rdoc: true
|
47
41
|
homepage: http://github.com/shurizzle/ruby-lzma-ffi
|
48
42
|
licenses: []
|
@@ -51,27 +45,23 @@ post_install_message:
|
|
51
45
|
rdoc_options: []
|
52
46
|
|
53
47
|
require_paths:
|
54
|
-
- lib
|
48
|
+
- lib
|
55
49
|
required_ruby_version: !ruby/object:Gem::Requirement
|
56
50
|
none: false
|
57
51
|
requirements:
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
- 0
|
62
|
-
version: "0"
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
63
55
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
56
|
none: false
|
65
57
|
requirements:
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
- 0
|
70
|
-
version: "0"
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
71
61
|
requirements: []
|
72
62
|
|
73
63
|
rubyforge_project:
|
74
|
-
rubygems_version: 1.
|
64
|
+
rubygems_version: 1.5.1
|
75
65
|
signing_key:
|
76
66
|
specification_version: 3
|
77
67
|
summary: liblzma bindings for ruby
|