lzma-ffi 0.0.1.1 → 0.0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lzma.rb +12 -19
- metadata +2 -2
data/lib/lzma.rb
CHANGED
@@ -53,11 +53,11 @@ class LZMA
|
|
53
53
|
class LZMAStream < FFI::Struct
|
54
54
|
layout \
|
55
55
|
:next_in, :pointer,
|
56
|
-
:avail_in, :
|
56
|
+
:avail_in, :size_t,
|
57
57
|
:total_in, :uint64,
|
58
58
|
|
59
59
|
:next_out, :pointer,
|
60
|
-
:avail_out, :
|
60
|
+
:avail_out, :size_t,
|
61
61
|
:total_out, :uint64,
|
62
62
|
|
63
63
|
:allocator, :pointer,
|
@@ -69,8 +69,8 @@ class LZMA
|
|
69
69
|
:reserved_ptr4, :pointer,
|
70
70
|
:reserved_int1, :uint64,
|
71
71
|
:reserved_int2, :uint64,
|
72
|
-
:reserved_int3, :
|
73
|
-
:reserved_int4, :
|
72
|
+
:reserved_int3, :size_t,
|
73
|
+
:reserved_int4, :size_t,
|
74
74
|
:reserved_enum1, :lzma_reserved_enum,
|
75
75
|
:reserved_enum2, :lzma_reserved_enum
|
76
76
|
end
|
@@ -81,20 +81,14 @@ class LZMA
|
|
81
81
|
end
|
82
82
|
|
83
83
|
class Stream
|
84
|
-
INIT = [nil, 0, 0, nil, 0, 0, nil, nil, nil, nil, nil, nil, 0, 0, 0, 0, 0, 0].pack('PL!QPL!QPPPPPPQQL!L!i!i!').pointer.freeze
|
85
|
-
|
86
84
|
def initialize(stream, buf_len=4096)
|
87
85
|
@stream, @buf_len = stream, buf_len || 4096
|
88
86
|
@buf = (' ' * @buf_len).pointer
|
89
|
-
@struct = C::LZMAStream.new
|
87
|
+
@struct = C::LZMAStream.new
|
90
88
|
|
91
89
|
ObjectSpace.define_finalizer(self, method(:finalize))
|
92
90
|
end
|
93
91
|
|
94
|
-
def next_in
|
95
|
-
@struct[:next_in].read_string rescue nil
|
96
|
-
end
|
97
|
-
|
98
92
|
def avail_in
|
99
93
|
@struct[:avail_in]
|
100
94
|
end
|
@@ -103,10 +97,6 @@ class LZMA
|
|
103
97
|
@struct[:total_in]
|
104
98
|
end
|
105
99
|
|
106
|
-
def next_out
|
107
|
-
@struct[:next_out].read_string rescue nil
|
108
|
-
end
|
109
|
-
|
110
100
|
def avail_out
|
111
101
|
@struct[:avail_out]
|
112
102
|
end
|
@@ -135,8 +125,9 @@ class LZMA
|
|
135
125
|
}
|
136
126
|
end
|
137
127
|
|
138
|
-
def decoder(limit, flags)
|
139
|
-
|
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
|
140
131
|
self
|
141
132
|
end
|
142
133
|
|
@@ -169,14 +160,15 @@ class LZMA
|
|
169
160
|
res = ''
|
170
161
|
blk = lambda {|chunk| res << chunk } unless block_given?
|
171
162
|
|
172
|
-
stream = Stream.new(what, buf_len).decoder
|
163
|
+
stream = Stream.new(what, buf_len).decoder
|
173
164
|
|
174
165
|
until what.eof?
|
175
166
|
stream.read
|
176
167
|
action = what.eof? ? :FINISH : :RUN
|
177
168
|
|
178
169
|
begin
|
179
|
-
|
170
|
+
code = nil
|
171
|
+
raise RuntimeError, "lzma_code error: #{code}" unless [:OK, :STREAM_END].include?(code = stream.code(action))
|
180
172
|
|
181
173
|
blk.call(stream.next_out)
|
182
174
|
end while stream.avail_out.zero?
|
@@ -189,6 +181,7 @@ class LZMA
|
|
189
181
|
end
|
190
182
|
|
191
183
|
def self.extract(file, to=file.gsub(/\.(xz|lzma)$/, ''))
|
184
|
+
File.unlink(to) if File.file?(to)
|
192
185
|
File.open(to, 'wb') {|f|
|
193
186
|
decompress(File.open(file)) {|chunk|
|
194
187
|
f.write(chunk)
|