ruby-xz 0.2.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8c1956c341bfbdbb6a5b002b47f8c418fafe3c8c
4
- data.tar.gz: 8af67a57895f50f871a4509fc94f57932be150a4
2
+ SHA256:
3
+ metadata.gz: 9826b10c06d748022a2246bf9888c7b033f0d7fca2383303d8e75b7f871dac21
4
+ data.tar.gz: f8b6047714b8390890f8a097b98caf78b92f3d902f8c48898c0bbdc5353c4b96
5
5
  SHA512:
6
- metadata.gz: 2c04ff79076e081427e827e87fe489a882bf8b22674ebb3e0c3c8af26877c54b9b2140da1d61527da305bbcf062d4d5e687ce011a702a8300ce68a426bdba127
7
- data.tar.gz: faac093525a0ab41c0fd35a2b3e39a95c70ca70c312dc6accf051343c17f770d2ed396b9266f93c7a67ff887bf450b9cc2b649cb69df7c074c03cc980054d455
6
+ metadata.gz: b59d8b288be8180a15d1718bce8836138b7efb3d26c6e0e41b24f493561bbb10c8821932d6dc10dbba05774948c2e2990c14af7bf7acb24afce78cb980481b90
7
+ data.tar.gz: 0a1f54092cfe6122ff1857e7917c232d7333c87f16a3e82dc5bec90d098c2e431ef15ffa6a3d23fa8a67595717e28bfce174a83f3b9d9663b01799d3adbd6087
data/AUTHORS ADDED
@@ -0,0 +1,7 @@
1
+ = List of contributors
2
+
3
+ All the people who worked on this project, in alphabetical order.
4
+
5
+ * Marvin Gülker (github.com/Quintus) <m-guelker@phoenixmail.de>
6
+ * Alex Gittemeier (github.com/win93) <me@a.lexg.dev>
7
+ * Christoph Plank (github.com/chrisistuff)
data/HISTORY.rdoc CHANGED
@@ -1,16 +1,93 @@
1
- == 0.2.1
1
+ = Version history
2
2
 
3
- * Build the gem properly on Ruby 2.0+ (PR #8 by Nana Sakisaka (saki7))
4
- * Release the GIL when interfacing with liblzma (PR #7 by Lars Christensen (larsch))
3
+ == 1.0.1 (2021-11-13)
4
+ * Fix a buffer overflow error in XZ::Stream (PR Quintus#17 by genail)
5
+ * Fix an issue with transcode_options that was introduced by Ruby 3 (PR win93#1 by Alex Gittemeier)
6
+ * Update project README, gemspec, and other metadata to reflect a change in
7
+ maintainership. All prior releases were released by Marvin Gülker.
5
8
 
6
- == 0.2.0
9
+ == 1.0.0 (2018-05-20)
7
10
 
8
- * Fix #6 (errors on JRuby) by Ben Nagy
11
+ * *BreakingChange* The XZ module's methods now take any parameters
12
+ beyond the IO object as real Ruby keyword arguments rather than
13
+ a long argument list.
14
+ * *BreakingChange* XZ.decompress_stream now honours Ruby's
15
+ external and internal encoding concept instead of just
16
+ returning BINARY-tagged strings.
17
+ * *BreakingChange* Remove deprecated API on stream reader/writer
18
+ class and instead sync the API with Ruby's zlib library
19
+ (Ticket Quintus#12 by Quintus).
20
+ * *BreakingChange* StreamWriter.new and StreamReader.new do not accept
21
+ a block anymore. This is part of syncing with Ruby's zlib API.
22
+ * *BreakingChange* StreamReader.open and StreamWriter.open always
23
+ return the new instance, even if a block is given to the method
24
+ (previous behaviour was to return the return value of the block).
25
+ This is part of the syncing with Ruby's zlib API.
26
+ * *BreakingChange* StreamReader.new and StreamWriter.new as well as
27
+ the ::open variants take additional arguments as real Ruby keyword
28
+ arguments now instead of a long parameter list plus options hash.
29
+ This is different from Ruby's own zlib API as that one takes both
30
+ a long parameter list and a hash of additional options. ruby-xz
31
+ is meant to follow zlib's semantics mostly, but not as a drop-in
32
+ replacement, so this divergence from zlib's API is okay (also
33
+ given that it isn't possible to replicate all possible options
34
+ 1:1 anyway, since liblzma simply accepts different options as
35
+ libz). If you've never used these methods' optional arguments,
36
+ you should be fine.
37
+ * *BreakingChange* Stream#close now returns nil instead of the
38
+ number of bytes written. This syncs Stream#close with Ruby's
39
+ own IO#close, which also returns nil.
40
+ * *BreakingChange* Remove Stream#pos=, Stream#seek, Stream#stat. These
41
+ methods irritated the minitar gem, which doesn't expect them to
42
+ raise NotImplementedError, but directly to be missing if the object
43
+ does not support seeking.
44
+ * *BreakingChange* StreamReader and StreamWriter now honour Ruby's
45
+ encoding system instead of returning only BINARY-tagged strings.
46
+ * *Dependency* Remove dependency on ffi. ruby-xz now uses fiddle from
47
+ the stdlib instead.
48
+ * *Dependency* Remove dependency on io-like. ruby-xz now implements
49
+ all the IO mechanics itself. (Ticket Quintus#10 by Quintus)
50
+ * *Dependency* Bump required Ruby version to 2.3.0.
51
+ * *Fix* libzlma.dylib not being found on OS X (Ticket Quintus#15 by
52
+ s0nspark).
53
+
54
+ == 0.2.3 (2015-12-29)
55
+
56
+ * *Fix* documentation of XZ module (a :nodoc: was causing havoc
57
+ in the XZ module so it appeared to have no methods).
58
+ * No other changes this release.
59
+
60
+ == 0.2.2 (2015-12-27)
61
+
62
+ * *Add* XZ.disable_deprecation_notices
63
+ * *Deprecate* use of XZ::StreamReader.open with an IO argument
64
+ * *Deprecate* use of XZ::StreamReader.new with a filename argument
65
+ * *Deprecate* use of XZ::StreamWriter.open with an IO argument
66
+ * *Deprecate* use of XZ::StreamWriter.new with a filename argument
67
+ * *Deprecate* nonautomatic IO close in XZ::StreamReader#close
68
+ * *Deprecate* nonautomatic IO close in XZ::StreamWriter#close
69
+ * *Fix* incompatibility with Resolv.getaddress() in Ruby 2.2 (Ticket Quintus#13
70
+ by Ken Simon)
71
+ * Goal of these deprecations is to sync the API with Ruby’s own
72
+ Zlib::GzipWriter and Zlib::GzipReader mostly.
73
+ * Add required versions to gemspec.
74
+ * Comment format cleanup, results in better docs.
75
+ * Internal code cleanup
76
+ * Add more tests.
77
+
78
+ == 0.2.1 (2014-02-08)
79
+
80
+ * Build the gem properly on Ruby 2.0+ (PR Quintus#8 by Nana Sakisaka (saki7))
81
+ * Release the GIL when interfacing with liblzma (PR Quintus#7 by Lars Christensen (larsch))
82
+
83
+ == 0.2.0 (2013-06-23)
84
+
85
+ * Fix Quintus#6 (errors on JRuby) by Ben Nagy
9
86
  * <b>Remove 1.8 compatibility</b>
10
87
 
11
- == 0.1.0
88
+ == 0.1.0 (2013-02-17)
12
89
 
13
90
  * <b>Add XZ::StreamReader and XZ::StreamWriter for io-like behaviour.</b>
14
91
  * New dependency on the +io-like+ gem.
15
92
  * <b>Add Ruby 1.8 compatibility.</b> Thanks to Christoph Plank.
16
- * We now have proper unit tests.
93
+ * We now have proper unit tests.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright © 2011-2018 Marvin Gülker et al.
2
+
3
+ See AUTHORS for the full list of contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a
6
+ copy of this software and associated documentation files (the ‘Software’),
7
+ to deal in the Software without restriction, including without limitation
8
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
+ and/or sell copies of the Software, and to permit persons to whom the Software
10
+ is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # ruby-xz
2
+
3
+ **ruby-xz** is a basic binding to the famous [liblzma] library,
4
+ best known for the extreme compression-ratio it's native *XZ* format achieves.
5
+ ruby-xz gives you the possibility of creating and extracting XZ archives on any
6
+ platform where liblzma is installed.
7
+ No compilation is needed, because ruby-xz is written on top of Ruby's [fiddle]
8
+ library (part of the standard library).
9
+ ruby-xz does not have any dependencies other than Ruby itself.
10
+
11
+ rubv-xz supports compression and decompression via methods that operate on
12
+ strings and files, and it also supports compression and decompression on IO
13
+ streams.
14
+ the XZ::StreamReader and XZ::StreamWriter offer advanced interfaces that allow
15
+ you to treat XZ-compressed data as IO streams, both for reading and for writing.
16
+ <!-- TODO: link to relevant parts of the source code and/or documentation -->
17
+
18
+ **Note**: Version 1.0.0 breaks the API quite heavily.
19
+ Refer to HISTORY.rdoc for details.
20
+
21
+ ## Installation
22
+
23
+ Install with `gem` from your Ruby installation:
24
+
25
+ ```sh
26
+ gem install ruby-xz
27
+ ```
28
+
29
+ Alternatively, add it to your Gemfile via:
30
+
31
+ ```sh
32
+ bundle add ruby-xz
33
+ ```
34
+
35
+ If you want to be on the bleeding edge, you can clone the repository and build
36
+ the most recent code yourself:
37
+
38
+ ```sh
39
+ git clone https://github.com/win93/ruby-xz.git
40
+ cd ruby-xz
41
+ rake gem
42
+ gem install pkg/ruby-xz-*.gem
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ You should be able to find everything you need to use ruby-xz in the
48
+ documentation.
49
+ It's small but powerful: You can create and extract whole archive files,
50
+ compress or decompress whole files, strings, or streams of data.
51
+
52
+ You can read the documentation on your local gemserver, or browse it [online][rubydoc].
53
+ <!-- TODO how do I read the docs on my local gemserver? -->
54
+
55
+ ### Examples
56
+
57
+ ``` ruby
58
+ require 'xz'
59
+
60
+ # Compress a file
61
+ XZ.compress_file("myfile.txt", "myfile.txt.xz")
62
+ # Decompress it
63
+ XZ.decompress_file("myfile.txt.xz", "myfile.txt")
64
+
65
+ # Compress everything you get from a socket (note that there HAS to be a EOF
66
+ # sometime, otherwise this will run infinitely)
67
+ XZ.compress_stream(socket){|chunk| opened_file.write(chunk)}
68
+
69
+ # Compress a string
70
+ comp = XZ.compress("Mydata")
71
+ # Decompress it
72
+ data = XZ.decompress(comp)
73
+ ```
74
+
75
+ Have a look at the XZ module's documentation for an in-depth description of what
76
+ is possible.
77
+ <!-- TODO link to such documentation -->
78
+
79
+ ### Usage with the minitar gem
80
+
81
+ ruby-xz can be used together with the [minitar] library (formerly
82
+ “archive-tar-minitar”) to create XZ-compressed tarballs.
83
+ This works by employing the IO-like classes XZ::StreamReader and
84
+ XZ::StreamWriter analogous to how one would use Ruby's “zlib” library together
85
+ with “minitar”:
86
+
87
+ ``` ruby
88
+ require "xz"
89
+ require "minitar"
90
+
91
+ # Create an XZ-compressed tarball
92
+ XZ::StreamWriter.open("tarball.tar.xz") do |txz|
93
+ Minitar.pack("path/to/directory", txz)
94
+ end
95
+
96
+ # Unpack it again
97
+ XZ::StreamReader.open("tarball.tar.xz") do |txz|
98
+ Minitar.unpack(txz, "path/to/target/directory")
99
+ end
100
+ ```
101
+
102
+ ## Links
103
+
104
+ * Online documentation: <https://rubydoc.info/gems/ruby-xz>
105
+ * Code repository: <https://github.com/win93/ruby-xz>
106
+ * Issue tracker: <https://github.com/win93/ruby-xz/issues>
107
+
108
+ ## License
109
+
110
+ MIT license; see LICENSE for the full license text.
111
+
112
+ ## Acknowledgements
113
+
114
+ On November 2021, I volunteered to take over maintenance of this project, which
115
+ was forked from <https://github.com/Quintus/ruby-xz>.
116
+ @Quintus maintained this project until 1.0.0, see HISTORY.rdoc for more details.
117
+
118
+
119
+ [fiddle]: https://github.com/ruby/fiddle
120
+ [liblzma]: https://tukaani.org/xz/
121
+ [rubydoc]: https://www.rubydoc.info/gems/ruby-xz
122
+ [minitar]: https://github.com/halostatue/minitar
@@ -0,0 +1,91 @@
1
+ # -*- coding: utf-8 -*-
2
+ #--
3
+ # Basic liblzma-bindings for Ruby.
4
+ #
5
+ # Copyright © 2011-2018 Marvin Gülker et al.
6
+ #
7
+ # See AUTHORS for the full list of contributors.
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining a
10
+ # copy of this software and associated documentation files (the ‘Software’),
11
+ # to deal in the Software without restriction, including without limitation
12
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
13
+ # and/or sell copies of the Software, and to permit persons to whom the Software
14
+ # is furnished to do so, subject to the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included in all
17
+ # copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ # THE SOFTWARE.
26
+ #++
27
+
28
+ module XZ
29
+
30
+ # This is an internal API not meant for users of ruby-xz.
31
+ # This mixin modules defines some helper functions on top
32
+ # of Fiddle's functionality.
33
+ module FiddleHelper # :nodoc:
34
+
35
+ # Define constants that have numeric constants assigned as if
36
+ # it was a C enum definition. You can specificy values explicitely
37
+ # or rely on the implicit incrementation; the first implicit value
38
+ # is zero.
39
+ #
40
+ # Example:
41
+ #
42
+ # enum :FOO, :BAR, 5, :BAZ
43
+ #
44
+ # This defines a constant FOO with value 0, BAR with value 5, BAZ
45
+ # with value 6.
46
+ def enum(*args)
47
+ @next_enum_val = 0 # First value of an enum is 0 in C
48
+
49
+ args.each_cons(2) do |val1, val2|
50
+ next if val1.respond_to?(:to_int)
51
+
52
+ if val2.respond_to?(:to_int)
53
+ const_set(val1, val2.to_int)
54
+ @next_enum_val = val2.to_int + 1
55
+ else
56
+ const_set(val1, @next_enum_val)
57
+ @next_enum_val += 1
58
+ end
59
+ end
60
+
61
+ # Cater for the last element in case it is not an explicit
62
+ # value that has already been assigned above.
63
+ unless args.last.respond_to?(:to_int)
64
+ const_set(args.last, @next_enum_val)
65
+ end
66
+
67
+ @next_enum_val = 0
68
+ nil
69
+ end
70
+
71
+ # Try loading any of the given names as a shared
72
+ # object. Raises Fiddle::DLError if none can
73
+ # be opened.
74
+ def dlloadanyof(*names)
75
+ names.each do |name|
76
+ begin
77
+ dlload(name)
78
+ rescue Fiddle::DLError
79
+ # Continue with next one
80
+ else
81
+ # Success
82
+ return name
83
+ end
84
+ end
85
+
86
+ raise Fiddle::DLError, "Failed to open any of these shared object files: #{names.join(', ')}"
87
+ end
88
+
89
+ end
90
+
91
+ end
data/lib/xz/lib_lzma.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  # -*- coding: utf-8 -*-
2
- # The MIT License
3
- #
2
+ #--
4
3
  # Basic liblzma-bindings for Ruby.
5
4
  #
6
- # Copyright © 2011,2013 Marvin Gülker et al.
5
+ # Copyright © 2011-2018 Marvin Gülker et al.
6
+ #
7
+ # See AUTHORS for the full list of contributors.
7
8
  #
8
9
  # Permission is hereby granted, free of charge, to any person obtaining a
9
10
  # copy of this software and associated documentation files (the ‘Software’),
@@ -22,134 +23,157 @@
22
23
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
24
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
25
  # THE SOFTWARE.
26
+ #++
25
27
 
26
28
  module XZ
27
29
 
28
- #This module wraps functions and enums used by liblzma.
30
+ # This module wraps functions and enums provided by liblzma.
31
+ # It contains the direct mapping to the underlying C functions;
32
+ # you should never have to use this. It's the lowlevel API
33
+ # the other methods provided by ruby-xz are based on.
29
34
  module LibLZMA
30
- extend FFI::Library
31
-
32
- #The maximum value of an uint64_t, as defined by liblzma.
33
- #Should be the same as
34
- # (2 ** 64) - 1
35
+ extend Fiddle::Importer
36
+ extend XZ::FiddleHelper
37
+
38
+ dlloadanyof 'liblzma.so.5', 'liblzma.so', 'liblzma.5.dylib', 'liblzma.dylib', 'liblzma'
39
+
40
+ typealias "uint32_t", "unsigned int"
41
+ typealias "uint64_t", "unsigned long long"
42
+
43
+ # lzma_ret enum
44
+ enum :LZMA_OK, 0, :LZMA_STREAM_END, 1, :LZMA_NO_CHECK, 2,
45
+ :LZMA_UNSUPPORTED_CHECK, 3, :LZMA_GET_CHECK, 4,
46
+ :LZMA_MEM_ERROR, 5, :LZMA_MEMLIMIT_ERROR, 6,
47
+ :LZMA_FORMAT_ERROR, 7, :LZMA_OPTIONS_ERROR, 8,
48
+ :LZMA_DATA_ERROR, 9, :LZMA_BUF_ERROR, 10,
49
+ :LZMA_PROG_ERROR, 11
50
+
51
+ # lzma_action enum
52
+ enum :LZMA_RUN, 0, :LZMA_SYNC_FLUSH, 1,
53
+ :LZMA_FULL_FLUSH, 2, :LZMA_FULL_BARRIER, 4,
54
+ :LZMA_FINISH, 3
55
+
56
+ # The maximum value of an uint64_t, as defined by liblzma.
57
+ # Should be the same as
58
+ # (2 ** 64) - 1
35
59
  UINT64_MAX = 18446744073709551615
36
60
 
37
- #Activates extreme compression. Same as xz's "-e" commandline switch.
61
+ # Activates extreme compression. Same as xz's "-e" commandline switch.
38
62
  LZMA_PRESET_EXTREME = 1 << 31
39
63
 
40
- LZMA_TELL_NO_CHECK = 0x02
64
+ LZMA_TELL_NO_CHECK = 0x01
41
65
  LZMA_TELL_UNSUPPORTED_CHECK = 0x02
42
66
  LZMA_TELL_ANY_CHECK = 0x04
43
67
  LZMA_CONCATENATED = 0x08
68
+ LZMA_IGNORE_CHECK = 0x10
69
+
70
+ # For access convenience of the above flags.
71
+ LZMA_DECODE_FLAGS = {
72
+ :tell_no_check => LZMA_TELL_NO_CHECK,
73
+ :tell_unsupported_check => LZMA_TELL_UNSUPPORTED_CHECK,
74
+ :tell_any_check => LZMA_TELL_ANY_CHECK,
75
+ :concatenated => LZMA_CONCATENATED,
76
+ :ignore_check => LZMA_IGNORE_CHECK
77
+ }.freeze
78
+
79
+ # Placeholder enum used by liblzma for later additions.
80
+ enum :LZMA_RESERVED_ENUM, 0
81
+
82
+ # lzma_check enum
83
+ enum :LZMA_CHECK_NONE, 0, :LZMA_CHECK_CRC32, 1,
84
+ :LZMA_CHECK_CRC64, 4, :LZMA_CHECK_SHA256, 10
85
+
86
+ # Aliases for the enums as fiddle only understands plain int
87
+ typealias "lzma_ret", "int"
88
+ typealias "lzma_check", "int"
89
+ typealias "lzma_action", "int"
90
+ typealias "lzma_reserved_enum", "int"
91
+
92
+ # lzma_stream struct. When creating one with ::malloc, use
93
+ # ::LZMA_STREAM_INIT to make it ready for use.
94
+ #
95
+ # This is a Fiddle::CStruct. As such, this has a class method
96
+ # ::malloc for allocating an instance of it on the heap, and
97
+ # instances of it have a #to_ptr method that returns a
98
+ # Fiddle::Pointer. That pointer needs to be freed with
99
+ # Fiddle::free if the instance was created with ::malloc.
100
+ # To wrap an existing instance, call ::new with the
101
+ # Fiddle::Pointer to wrap as an argument.
102
+ LZMAStream = struct [
103
+ "uint8_t* next_in",
104
+ "size_t avail_in",
105
+ "uint64_t total_in",
106
+ "uint8_t* next_out",
107
+ "size_t avail_out",
108
+ "uint64_t total_out",
109
+ "void* allocator",
110
+ "void* internal",
111
+ "void* reserved_ptr1",
112
+ "void* reserved_ptr2",
113
+ "void* reserved_ptr3",
114
+ "void* reserved_ptr4",
115
+ "uint64_t reserved_int1",
116
+ "uint64_t reserved_int2",
117
+ "size_t reserved_int3",
118
+ "size_t reserved_int4",
119
+ "lzma_reserved_enum reserved_enum1",
120
+ "lzma_reserved_enum reserved_enum2"
121
+ ]
122
+
123
+ # This method does basicly the same thing as the
124
+ # LZMA_STREAM_INIT macro of liblzma. Pass it an instance of
125
+ # LZMAStream that has not been initialised for use.
126
+ # The intended use of this method is:
127
+ #
128
+ # stream = LibLZMA::LZMAStream.malloc # ::malloc is provided by fiddle
129
+ # LibLZMA.LZMA_STREAM_INIT(stream)
130
+ # # ...do something with the stream...
131
+ # Fiddle.free(stream.to_ptr)
132
+ def self.LZMA_STREAM_INIT(stream)
133
+ stream.next_in = nil
134
+ stream.avail_in = 0
135
+ stream.total_in = 0
136
+ stream.next_out = nil
137
+ stream.avail_out = 0
138
+ stream.total_out = 0
139
+ stream.allocator = nil
140
+ stream.internal = nil
141
+ stream.reserved_ptr1 = nil
142
+ stream.reserved_ptr2 = nil
143
+ stream.reserved_ptr3 = nil
144
+ stream.reserved_ptr4 = nil
145
+ stream.reserved_int1 = 0
146
+ stream.reserved_int2 = 0
147
+ stream.reserved_int3 = 0
148
+ stream.reserved_int4 = 0
149
+ stream.reserved_enum1 = LZMA_RESERVED_ENUM
150
+ stream.reserved_enum2 = LZMA_RESERVED_ENUM
151
+ stream
152
+ end
44
153
 
45
- #Placeholder enum used by liblzma for later additions.
46
- LZMA_RESERVED_ENUM = enum :lzma_reserved_enum, 0
47
-
48
- #Actions that can be passed to the lzma_code() function.
49
- LZMA_ACTION = enum :lzma_run, 0,
50
- :lzma_sync_flush,
51
- :lzma_full_flush,
52
- :lzma_finish
53
-
54
- #Integrity check algorithms supported by liblzma.
55
- LZMA_CHECK = enum :lzma_check_none, 0,
56
- :lzma_check_crc32, 1,
57
- :lzma_check_crc64, 4,
58
- :lzma_check_sha256, 10
59
-
60
- #Possible return values of liblzma functions.
61
- LZMA_RET = enum :lzma_ok, 0,
62
- :lzma_stream_end,
63
- :lzma_no_check,
64
- :lzma_unsupported_check,
65
- :lzma_get_check,
66
- :lzma_mem_error,
67
- :lzma_memlimit_error,
68
- :lzma_format_error,
69
- :lzma_options_error,
70
- :lzma_data_error,
71
- :lzma_buf_error,
72
- :lzma_prog_error
73
-
74
- ffi_lib ['lzma.so.5', 'lzma.so', 'lzma']
75
-
76
- attach_function :lzma_easy_encoder, [:pointer, :uint32, :int], :int, :blocking => true
77
- attach_function :lzma_code, [:pointer, :int], :int, :blocking => true
78
- attach_function :lzma_stream_decoder, [:pointer, :uint64, :uint32], :int, :blocking => true
79
- attach_function :lzma_end, [:pointer], :void, :blocking => true
154
+ extern "lzma_ret lzma_easy_encoder(lzma_stream*, uint32_t, lzma_check)"
155
+ extern "lzma_ret lzma_code(lzma_stream*, lzma_action)"
156
+ extern "lzma_ret lzma_stream_decoder(lzma_stream*, uint64_t, uint32_t)"
157
+ extern "void lzma_end(lzma_stream*)"
80
158
 
81
159
  end
82
160
 
83
- #The class of the error that this library raises.
161
+ # The class of the error that this library raises.
84
162
  class LZMAError < StandardError
85
163
 
86
- #Raises an appropriate exception if +val+ isn't a liblzma success code.
164
+ # Raises an appropriate exception if +val+ isn't a liblzma success code.
87
165
  def self.raise_if_necessary(val)
88
- case LibLZMA::LZMA_RET[val]
89
- when :lzma_mem_error then raise(self, "Couldn't allocate memory!")
90
- when :lzma_memlimit_error then raise(self, "Decoder ran out of (allowed) memory!")
91
- when :lzma_format_error then raise(self, "Unrecognized file format!")
92
- when :lzma_options_error then raise(self, "Invalid options passed!")
93
- when :lzma_data_error then raise(self, "Archive is currupt.")
94
- when :lzma_buf_error then raise(self, "Buffer unusable!")
95
- when :lzma_prog_error then raise(self, "Program error--if you're sure your code is correct, you may have found a bug in liblzma.")
166
+ case val
167
+ when LibLZMA::LZMA_MEM_ERROR then raise(self, "Couldn't allocate memory!")
168
+ when LibLZMA::LZMA_MEMLIMIT_ERROR then raise(self, "Decoder ran out of (allowed) memory!")
169
+ when LibLZMA::LZMA_FORMAT_ERROR then raise(self, "Unrecognized file format!")
170
+ when LibLZMA::LZMA_OPTIONS_ERROR then raise(self, "Invalid options passed!")
171
+ when LibLZMA::LZMA_DATA_ERROR then raise(self, "Archive is currupt.")
172
+ when LibLZMA::LZMA_BUF_ERROR then raise(self, "Buffer unusable!")
173
+ when LibLZMA::LZMA_PROG_ERROR then raise(self, "Program error--if you're sure your code is correct, you may have found a bug in liblzma.")
96
174
  end
97
175
  end
98
176
 
99
177
  end
100
178
 
101
- #The main struct of the liblzma library.
102
- class LZMAStream < FFI::Struct
103
- layout :next_in, :pointer, #uint8
104
- :avail_in, :size_t,
105
- :total_in, :uint64,
106
- :next_out, :pointer, #uint8
107
- :avail_out, :size_t,
108
- :total_out, :uint64,
109
- :lzma_allocator, :pointer,
110
- :lzma_internal, :pointer,
111
- :reserved_ptr1, :pointer,
112
- :reserved_ptr2, :pointer,
113
- :reserved_ptr3, :pointer,
114
- :reserved_ptr4, :pointer,
115
- :reserved_int1, :uint64,
116
- :reserved_int2, :uint64,
117
- :reserved_int3, :size_t,
118
- :reserved_int4, :size_t,
119
- :reserved_enum1, :int,
120
- :reserved_enum2, :int
121
-
122
- #This method does basicly the same thing as the
123
- #LZMA_STREAM_INIT macro of liblzma. Creates a new LZMAStream
124
- #that has been initialized for usage. If any argument is passed,
125
- #it is assumed to be a FFI::Pointer to a lzma_stream structure
126
- #and that structure is wrapped.
127
- def initialize(*args)
128
- if !args.empty? #Got a pointer, want to wrap it
129
- super
130
- else
131
- s = super()
132
- s[:next_in] = nil
133
- s[:avail_in] = 0
134
- s[:total_in] = 0
135
- s[:next_out] = nil
136
- s[:avail_out] = 0
137
- s[:total_out] = 0
138
- s[:lzma_allocator] = nil
139
- s[:lzma_internal] = nil
140
- s[:reserved_ptr1] = nil
141
- s[:reserved_ptr2] = nil
142
- s[:reserved_ptr3] = nil
143
- s[:reserved_ptr4] = nil
144
- s[:reserved_int1] = 0
145
- s[:reserved_int2] = 0
146
- s[:reserved_int3] = 0
147
- s[:reserved_int4] = 0
148
- s[:reserved_enum1] = LibLZMA::LZMA_RESERVED_ENUM[:lzma_reserved_enum]
149
- s[:reserved_enum2] = LibLZMA::LZMA_RESERVED_ENUM[:lzma_reserved_enum]
150
- s
151
- end
152
- end
153
- end
154
-
155
179
  end