ruby-xz 0.2.2 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/xz/lib_lzma.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  #--
3
- # The MIT License
4
- #
5
3
  # Basic liblzma-bindings for Ruby.
6
4
  #
7
- # Copyright © 2011,2013,2015 Marvin Gülker et al.
5
+ # Copyright © 2011-2018 Marvin Gülker et al.
6
+ #
7
+ # See AUTHORS for the full list of contributors.
8
8
  #
9
9
  # Permission is hereby granted, free of charge, to any person obtaining a
10
10
  # copy of this software and associated documentation files (the ‘Software’),
@@ -27,9 +27,31 @@
27
27
 
28
28
  module XZ
29
29
 
30
- # 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.
31
34
  module LibLZMA
32
- extend FFI::Library
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
33
55
 
34
56
  # The maximum value of an uint64_t, as defined by liblzma.
35
57
  # Should be the same as
@@ -39,54 +61,100 @@ module XZ
39
61
  # Activates extreme compression. Same as xz's "-e" commandline switch.
40
62
  LZMA_PRESET_EXTREME = 1 << 31
41
63
 
42
- LZMA_TELL_NO_CHECK = 0x02
64
+ LZMA_TELL_NO_CHECK = 0x01
43
65
  LZMA_TELL_UNSUPPORTED_CHECK = 0x02
44
66
  LZMA_TELL_ANY_CHECK = 0x04
45
67
  LZMA_CONCATENATED = 0x08
68
+ LZMA_IGNORE_CHECK = 0x10
46
69
 
47
70
  # For access convenience of the above flags.
48
71
  LZMA_DECODE_FLAGS = {
49
72
  :tell_no_check => LZMA_TELL_NO_CHECK,
50
73
  :tell_unsupported_check => LZMA_TELL_UNSUPPORTED_CHECK,
51
74
  :tell_any_check => LZMA_TELL_ANY_CHECK,
52
- :concatenated => LZMA_CONCATENATED
75
+ :concatenated => LZMA_CONCATENATED,
76
+ :ignore_check => LZMA_IGNORE_CHECK
53
77
  }.freeze
54
78
 
55
79
  # Placeholder enum used by liblzma for later additions.
56
- LZMA_RESERVED_ENUM = enum :lzma_reserved_enum, 0
57
-
58
- # Actions that can be passed to the lzma_code() function.
59
- LZMA_ACTION = enum :lzma_run, 0,
60
- :lzma_sync_flush,
61
- :lzma_full_flush,
62
- :lzma_finish
63
-
64
- # Integrity check algorithms supported by liblzma.
65
- LZMA_CHECK = enum :lzma_check_none, 0,
66
- :lzma_check_crc32, 1,
67
- :lzma_check_crc64, 4,
68
- :lzma_check_sha256, 10
69
-
70
- # Possible return values of liblzma functions.
71
- LZMA_RET = enum :lzma_ok, 0,
72
- :lzma_stream_end,
73
- :lzma_no_check,
74
- :lzma_unsupported_check,
75
- :lzma_get_check,
76
- :lzma_mem_error,
77
- :lzma_memlimit_error,
78
- :lzma_format_error,
79
- :lzma_options_error,
80
- :lzma_data_error,
81
- :lzma_buf_error,
82
- :lzma_prog_error
83
-
84
- ffi_lib ['lzma.so.5', 'lzma.so', 'lzma']
85
-
86
- attach_function :lzma_easy_encoder, [:pointer, :uint32, :int], :int, :blocking => true
87
- attach_function :lzma_code, [:pointer, :int], :int, :blocking => true
88
- attach_function :lzma_stream_decoder, [:pointer, :uint64, :uint32], :int, :blocking => true
89
- attach_function :lzma_end, [:pointer], :void, :blocking => true
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
153
+
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*)"
90
158
 
91
159
  end
92
160
 
@@ -95,71 +163,17 @@ module XZ
95
163
 
96
164
  # Raises an appropriate exception if +val+ isn't a liblzma success code.
97
165
  def self.raise_if_necessary(val)
98
- case LibLZMA::LZMA_RET[val]
99
- when :lzma_mem_error then raise(self, "Couldn't allocate memory!")
100
- when :lzma_memlimit_error then raise(self, "Decoder ran out of (allowed) memory!")
101
- when :lzma_format_error then raise(self, "Unrecognized file format!")
102
- when :lzma_options_error then raise(self, "Invalid options passed!")
103
- when :lzma_data_error then raise(self, "Archive is currupt.")
104
- when :lzma_buf_error then raise(self, "Buffer unusable!")
105
- 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.")
106
174
  end
107
175
  end
108
176
 
109
177
  end
110
178
 
111
- # The main struct of the liblzma library.
112
- class LZMAStream < FFI::Struct
113
- layout :next_in, :pointer, #uint8
114
- :avail_in, :size_t,
115
- :total_in, :uint64,
116
- :next_out, :pointer, #uint8
117
- :avail_out, :size_t,
118
- :total_out, :uint64,
119
- :lzma_allocator, :pointer,
120
- :lzma_internal, :pointer,
121
- :reserved_ptr1, :pointer,
122
- :reserved_ptr2, :pointer,
123
- :reserved_ptr3, :pointer,
124
- :reserved_ptr4, :pointer,
125
- :reserved_int1, :uint64,
126
- :reserved_int2, :uint64,
127
- :reserved_int3, :size_t,
128
- :reserved_int4, :size_t,
129
- :reserved_enum1, :int,
130
- :reserved_enum2, :int
131
-
132
- # This method does basicly the same thing as the
133
- # LZMA_STREAM_INIT macro of liblzma. Creates a new LZMAStream
134
- # that has been initialized for usage. If any argument is passed,
135
- # it is assumed to be a FFI::Pointer to a lzma_stream structure
136
- # and that structure is wrapped.
137
- def initialize(*args)
138
- if !args.empty? #Got a pointer, want to wrap it
139
- super
140
- else
141
- s = super()
142
- s[:next_in] = nil
143
- s[:avail_in] = 0
144
- s[:total_in] = 0
145
- s[:next_out] = nil
146
- s[:avail_out] = 0
147
- s[:total_out] = 0
148
- s[:lzma_allocator] = nil
149
- s[:lzma_internal] = nil
150
- s[:reserved_ptr1] = nil
151
- s[:reserved_ptr2] = nil
152
- s[:reserved_ptr3] = nil
153
- s[:reserved_ptr4] = nil
154
- s[:reserved_int1] = 0
155
- s[:reserved_int2] = 0
156
- s[:reserved_int3] = 0
157
- s[:reserved_int4] = 0
158
- s[:reserved_enum1] = LibLZMA::LZMA_RESERVED_ENUM[:lzma_reserved_enum]
159
- s[:reserved_enum2] = LibLZMA::LZMA_RESERVED_ENUM[:lzma_reserved_enum]
160
- s
161
- end
162
- end
163
- end
164
-
165
179
  end