@@ -15,7 +15,8 @@ import Foundation
15
15
@_exported import Compression
16
16
17
17
/// Compression algorithms, wraps the C API constants.
18
- public enum Algorithm : CaseIterable {
18
+ @available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
19
+ public enum Algorithm : CaseIterable , RawRepresentable {
19
20
20
21
/// LZFSE
21
22
case lzfse
@@ -29,6 +30,16 @@ public enum Algorithm: CaseIterable {
29
30
/// LZMA in a XZ container
30
31
case lzma
31
32
33
+ public init ? ( rawValue: compression_algorithm ) {
34
+ switch rawValue {
35
+ case COMPRESSION_LZFSE: self = . lzfse
36
+ case COMPRESSION_ZLIB: self = . zlib
37
+ case COMPRESSION_LZ4: self = . lz4
38
+ case COMPRESSION_LZMA: self = . lzma
39
+ default : return nil
40
+ }
41
+ }
42
+
32
43
public var rawValue : compression_algorithm {
33
44
switch self {
34
45
case . lzfse: return COMPRESSION_LZFSE
@@ -39,26 +50,24 @@ public enum Algorithm: CaseIterable {
39
50
}
40
51
}
41
52
42
- /// Compression errors
43
- public enum FilterError : Error {
44
- /// Filter failed to initialize,
45
- /// invalid internal state,
46
- /// invalid parameters
47
- case invalidState
48
-
49
- /// Invalid data in a call to compression_stream_process,
50
- /// non-empty write after an output filter has been finalized
51
- case invalidData
52
- }
53
-
54
53
/// Compression filter direction of operation, compress/decompress
55
- public enum FilterOperation {
54
+ @available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
55
+ public enum FilterOperation : RawRepresentable {
56
+
56
57
/// Compress raw data to a compressed payload
57
58
case compress
58
59
59
60
/// Decompress a compressed payload to raw data
60
61
case decompress
61
62
63
+ public init ? ( rawValue: compression_stream_operation ) {
64
+ switch rawValue {
65
+ case COMPRESSION_STREAM_ENCODE: self = . compress
66
+ case COMPRESSION_STREAM_DECODE: self = . decompress
67
+ default : return nil
68
+ }
69
+ }
70
+
62
71
public var rawValue : compression_stream_operation {
63
72
switch self {
64
73
case . compress: return COMPRESSION_STREAM_ENCODE
@@ -67,6 +76,20 @@ public enum FilterOperation {
67
76
}
68
77
}
69
78
79
+ /// Compression errors
80
+ @available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
81
+ public enum FilterError : Error {
82
+
83
+ /// Filter failed to initialize,
84
+ /// or invalid internal state,
85
+ /// or invalid parameters
86
+ case invalidState
87
+
88
+ /// Invalid data in a call to compression_stream_process,
89
+ /// or non-empty write after an output filter has been finalized
90
+ case invalidData
91
+ }
92
+
70
93
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
71
94
extension compression_stream {
72
95
@@ -94,7 +117,7 @@ public class OutputFilter {
94
117
private var _stream : compression_stream
95
118
private var _buf : UnsafeMutablePointer < UInt8 >
96
119
private let _bufCapacity : Int
97
- private let _writeFunc : ( Data ? ) throws -> ( )
120
+ private let _writeFunc : ( Data ? ) throws -> Void
98
121
private var _finalized : Bool = false
99
122
100
123
/// Initialize an output filter
@@ -110,7 +133,7 @@ public class OutputFilter {
110
133
_ operation: FilterOperation ,
111
134
using algorithm: Algorithm ,
112
135
bufferCapacity: Int = 65536 ,
113
- writingTo writeFunc: @escaping ( Data ? ) throws -> ( )
136
+ writingTo writeFunc: @escaping ( Data ? ) throws -> Void
114
137
) throws {
115
138
_stream = try compression_stream ( operation: operation, algorithm: algorithm)
116
139
_buf = UnsafeMutablePointer< UInt8> . allocate( capacity: bufferCapacity)
@@ -141,7 +164,7 @@ public class OutputFilter {
141
164
try region. withUnsafeBytes { ( raw_src_ptr: UnsafeRawBufferPointer ) in
142
165
_stream. src_size = region. count
143
166
_stream. src_ptr = raw_src_ptr. baseAddress!. assumingMemoryBound ( to: UInt8 . self)
144
- while ( _stream. src_size > 0 ) { _ = try process ( finalizing: false ) }
167
+ while ( _stream. src_size > 0 ) { _ = try _process ( finalizing: false ) }
145
168
}
146
169
}
147
170
}
@@ -160,7 +183,7 @@ public class OutputFilter {
160
183
// Finalize stream
161
184
_stream. src_size = 0
162
185
var status = COMPRESSION_STATUS_OK
163
- while ( status != COMPRESSION_STATUS_END) { status = try process ( finalizing: true ) }
186
+ while ( status != COMPRESSION_STATUS_END) { status = try _process ( finalizing: true ) }
164
187
165
188
// Update state
166
189
_finalized = true
@@ -181,7 +204,7 @@ public class OutputFilter {
181
204
182
205
// Call compression_stream_process with current src, and dst set to _buf, then write output to the closure
183
206
// Return status
184
- private func process ( finalizing finalize: Bool ) throws -> compression_status {
207
+ private func _process ( finalizing finalize: Bool ) throws -> compression_status {
185
208
// Process current input, and write to buf
186
209
_stream. dst_ptr = _buf
187
210
_stream. dst_size = _bufCapacity
@@ -212,6 +235,7 @@ public class InputFilter<D: DataProtocol> {
212
235
private var _remaining : Int // total bytes remaining to process in _data
213
236
private var _regionIndex : D . Regions . Index // region being read in _data
214
237
private var _regionRemaining : Int // remaining bytes to read in region being read in _data
238
+
215
239
public init ( _ data: D ) throws {
216
240
_data = data
217
241
_remaining = _data. count
0 commit comments