ffi-lzma 0.0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Binary file
Binary file
data/lib/lzma.jar ADDED
Binary file
data/lib/lzma.rb ADDED
@@ -0,0 +1,237 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of lzma-ffi.
5
+ #
6
+ # lzma-ffi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # lzma-ffi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with lzma-ffi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
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
36
+
37
+ dis.close
38
+
39
+ block_given? ? what : res
40
+ end
41
+
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])
54
+ end
55
+
56
+ dis.close
57
+
58
+ to
59
+ end
60
+ end
61
+ else
62
+ require 'ffi'
63
+ require 'stringio'
64
+
65
+ class String
66
+ def pointer
67
+ FFI::Pointer.new([self].pack('P').unpack('L!').first)
68
+ end
69
+ end
70
+
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
87
+
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
119
+
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
123
+ end
124
+
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
130
+
131
+ ObjectSpace.define_finalizer(self, method(:finalize))
132
+ end
133
+
134
+ def avail_in
135
+ @struct[:avail_in]
136
+ end
137
+
138
+ def total_in
139
+ @struct[:total_in]
140
+ end
141
+
142
+ def avail_out
143
+ @struct[:avail_out]
144
+ end
145
+
146
+ def total_out
147
+ @struct[:total_out]
148
+ end
149
+
150
+ def to_ffi
151
+ @struct
152
+ end
153
+
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
+ }
167
+ }
168
+ end
169
+
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
175
+
176
+ def read
177
+ @struct[:next_in] = FFI::MemoryPointer.from_string(str = @stream.read(@buf_len))
178
+ @struct[:avail_in] = str.bytesize
179
+ end
180
+
181
+ def code(action)
182
+ @struct[:next_out] = @buf
183
+ @struct[:avail_out] = @buf_len
184
+ C.lzma_code(@struct.pointer, action)
185
+ end
186
+
187
+ def next_out
188
+ @buf.read_string_length(@buf_len - @struct[:avail_out])
189
+ end
190
+
191
+ def finalize
192
+ C.lzma_end(@struct.pointer)
193
+ end
194
+
195
+ def self.size
196
+ C::LZMAStream.size
197
+ end
198
+ end
199
+
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?
204
+
205
+ stream = Stream.new(what, buf_len).decoder
206
+
207
+ until what.eof?
208
+ stream.read
209
+ action = what.eof? ? :FINISH : :RUN
210
+
211
+ begin
212
+ code = nil
213
+ raise RuntimeError, "lzma_code error: #{code}" unless [:OK, :STREAM_END].include?(code = stream.code(action))
214
+
215
+ blk.call(stream.next_out)
216
+ end while stream.avail_out.zero?
217
+ end
218
+
219
+ stream.finalize
220
+ what.close
221
+
222
+ block_given? ? what : res
223
+ end
224
+
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
+ }
232
+ }
233
+
234
+ to
235
+ end
236
+ end
237
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-lzma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - shura
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ffi
16
+ requirement: &9002740 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *9002740
25
+ description: liblzma bindings for ruby
26
+ email: shura1991@gmail.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - lib/liblzma_x86_64.dll
32
+ - lib/lzma.rb
33
+ - lib/liblzma_x86.dll
34
+ - lib/lzma.jar
35
+ homepage: http://github.com/shurizzle/ruby-lzma-ffi
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.16
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: liblzma bindings for ruby
59
+ test_files: []