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.
Files changed (3) hide show
  1. data/lib/lzma.jar +0 -0
  2. data/lib/lzma.rb +179 -135
  3. metadata +27 -37
Binary file
@@ -17,177 +17,221 @@
17
17
  # along with lzma-ffi. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
 
20
- require 'ffi'
21
- require 'stringio'
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
- class String
24
- def pointer
25
- FFI::Pointer.new([self].pack('P').unpack('L!').first)
26
- end
27
- end
37
+ dis.close
28
38
 
29
- class LZMA
30
- module C
31
- extend FFI::Library
39
+ block_given? ? what : res
40
+ end
32
41
 
33
- if RUBY_PLATFORM =~ /(?<!dar)win|w32/
34
- case 1.size * 8
35
- when 64
36
- ffi_lib File.realpath(File.join('..', 'liblzma_x86_64.dll'), __FILE__)
37
- when 32
38
- ffi_lib File.realpath(File.join('..', 'liblzma_x86.dll'), __FILE__)
39
- else
40
- raise LoadError, "Windows architecture not supported"
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
- enum :lzma_ret, [:OK, 0, :STREAM_END, :NO_CHECK, :UNSUPPORTED_CHECK,
47
- :GET_CHECK, :MEM_ERROR, :MEMLIMIT_ERROR, :FORMAT_ERROR, :OPTIONS_ERROR,
48
- :DATA_ERROR, :BUF_ERROR, :PROG_ERROR]
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
- attach_function :lzma_auto_decoder, [:pointer, :uint64, :uint32], :lzma_ret
79
- attach_function :lzma_code, [:pointer, :lzma_action], :lzma_ret
80
- attach_function :lzma_end, [:pointer], :void
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 Stream
84
- def initialize(stream, buf_len=4096)
85
- @stream, @buf_len = stream, buf_len || 4096
86
- @buf = (' ' * @buf_len).pointer
87
- @struct = C::LZMAStream.new
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
- ObjectSpace.define_finalizer(self, method(:finalize))
90
- end
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
- def avail_in
93
- @struct[:avail_in]
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
- def total_in
97
- @struct[:total_in]
98
- end
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
- def avail_out
101
- @struct[:avail_out]
102
- end
131
+ ObjectSpace.define_finalizer(self, method(:finalize))
132
+ end
103
133
 
104
- def total_out
105
- @struct[:total_out]
106
- end
134
+ def avail_in
135
+ @struct[:avail_in]
136
+ end
107
137
 
108
- def to_ffi
109
- @struct
110
- end
138
+ def total_in
139
+ @struct[:total_in]
140
+ end
111
141
 
112
- def ptr
113
- @struct.pointer
114
- end
142
+ def avail_out
143
+ @struct[:avail_out]
144
+ end
115
145
 
116
- def size
117
- @struct.size
118
- end
146
+ def total_out
147
+ @struct[:total_out]
148
+ end
149
+
150
+ def to_ffi
151
+ @struct
152
+ end
119
153
 
120
- def to_s
121
- %w[in out].flat_map {|i|
122
- %w[next avail total].map {|w|
123
- send("#{w}_#{i}".to_sym)
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
- def decoder(limit=-1, flags=0)
129
- code = nil
130
- raise RuntimeError, "lzma_stream_decoder error: #{code}" if (code = C.lzma_auto_decoder(@struct.pointer, limit, flags)) != :OK
131
- self
132
- end
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
- def read
135
- @struct[:next_in] = FFI::MemoryPointer.from_string(str = @stream.read(@buf_len))
136
- @struct[:avail_in] = str.bytesize
137
- end
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
- def code(action)
140
- @struct[:next_out] = @buf
141
- @struct[:avail_out] = @buf_len
142
- C.lzma_code(@struct.pointer, action)
143
- end
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
- def next_out
146
- @buf.read_string_length(@buf_len - @struct[:avail_out])
147
- end
187
+ def next_out
188
+ @buf.read_string_length(@buf_len - @struct[:avail_out])
189
+ end
148
190
 
149
- def finalize
150
- C.lzma_end(@struct.pointer)
151
- end
191
+ def finalize
192
+ C.lzma_end(@struct.pointer)
193
+ end
152
194
 
153
- def self.size
154
- C::LZMAStream.size
195
+ def self.size
196
+ C::LZMAStream.size
197
+ end
155
198
  end
156
- end
157
199
 
158
- def self.decompress(what, buf_len=4096, &blk)
159
- what = StringIO.new(what.to_s) unless what.is_a?(IO)
160
- res = ''
161
- blk = lambda {|chunk| res << chunk } unless block_given?
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
- stream = Stream.new(what, buf_len).decoder
205
+ stream = Stream.new(what, buf_len).decoder
164
206
 
165
- until what.eof?
166
- stream.read
167
- action = what.eof? ? :FINISH : :RUN
207
+ until what.eof?
208
+ stream.read
209
+ action = what.eof? ? :FINISH : :RUN
168
210
 
169
- begin
170
- code = nil
171
- raise RuntimeError, "lzma_code error: #{code}" unless [:OK, :STREAM_END].include?(code = stream.code(action))
211
+ begin
212
+ code = nil
213
+ raise RuntimeError, "lzma_code error: #{code}" unless [:OK, :STREAM_END].include?(code = stream.code(action))
172
214
 
173
- blk.call(stream.next_out)
174
- end while stream.avail_out.zero?
175
- end
215
+ blk.call(stream.next_out)
216
+ end while stream.avail_out.zero?
217
+ end
176
218
 
177
- stream.finalize
178
- what.close
219
+ stream.finalize
220
+ what.close
179
221
 
180
- block_given? ? what : res
181
- end
222
+ block_given? ? what : res
223
+ end
182
224
 
183
- def self.extract(file, to=file.gsub(/\.(xz|lzma)$/, ''))
184
- File.unlink(to) if File.file?(to)
185
- File.open(to, 'wb') {|f|
186
- decompress(File.open(file)) {|chunk|
187
- f.write(chunk)
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
- to
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: false
5
- segments:
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-28 00:00:00 +02:00
13
+ date: 2011-09-29 00:00:00 +02:00
19
14
  default_executable:
20
15
  dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: ffi
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- segments:
30
- - 0
31
- version: "0"
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.rb
44
- - lib/liblzma_x86.dll
45
- - lib/liblzma_x86_64.dll
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
- - !ruby/object:Gem::Version
60
- segments:
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
- - !ruby/object:Gem::Version
68
- segments:
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.3.7
64
+ rubygems_version: 1.5.1
75
65
  signing_key:
76
66
  specification_version: 3
77
67
  summary: liblzma bindings for ruby