1
-
2
1
import CCompressZlib
3
2
import NIOCore
4
3
@@ -107,10 +106,18 @@ public struct ZlibConfiguration: Sendable {
107
106
/// and compression level will be reduced. Value between 1 - 9 where 1 is least amount of memory.
108
107
/// - strategy: Strategy when compressing
109
108
@_disfavoredOverload
110
- public init ( windowSize: Int32 = 15 , compressionLevel: Int32 = Z_DEFAULT_COMPRESSION, memoryLevel: Int32 = 8 , strategy: Strategy = . default) {
109
+ public init (
110
+ windowSize: Int32 = 15 , compressionLevel: Int32 = Z_DEFAULT_COMPRESSION,
111
+ memoryLevel: Int32 = 8 , strategy: Strategy = . default
112
+ ) {
111
113
assert ( ( 9 ... 15 ) . contains ( windowSize) , " Window size must be between the values 9 and 15 " )
112
- assert ( ( - 1 ... 9 ) . contains ( compressionLevel) , " Compression level must be between the values 0 and 9, or -1 indicating the default value " )
113
- assert ( ( 1 ... 9 ) . contains ( memoryLevel) , " Compression memory level must be between the values 1 and 9 " )
114
+ assert (
115
+ ( - 1 ... 9 ) . contains ( compressionLevel) ,
116
+ " Compression level must be between the values 0 and 9, or -1 indicating the default value "
117
+ )
118
+ assert (
119
+ ( 1 ... 9 ) . contains ( memoryLevel) ,
120
+ " Compression memory level must be between the values 1 and 9 " )
114
121
self . windowSize = windowSize
115
122
self . compressionLevel = compressionLevel
116
123
self . memoryLevel = memoryLevel
@@ -187,7 +194,7 @@ public final class ZlibCompressor {
187
194
188
195
deinit {
189
196
let rt = deflateEnd ( self . stream)
190
- assert ( rt == Z_OK , " deflateEnd returned error: \( rt ) " )
197
+ assert ( rt != Z_STREAM_ERROR , " deflateEnd returned stream error " )
191
198
self . stream. deinitialize ( count: 1 )
192
199
self . stream. deallocate ( )
193
200
}
@@ -198,7 +205,9 @@ public final class ZlibCompressor {
198
205
/// - to: output bytebuffer
199
206
/// - flush: whether deflate should flush the output
200
207
/// - Throws: ``CompressNIOError`` if deflate fails
201
- public func deflate( from: inout ByteBuffer , to: inout ByteBuffer , flush: CompressNIOFlush ) throws {
208
+ public func deflate( from: inout ByteBuffer , to: inout ByteBuffer , flush: CompressNIOFlush )
209
+ throws
210
+ {
202
211
var bytesRead = 0
203
212
var bytesWritten = 0
204
213
@@ -224,8 +233,12 @@ public final class ZlibCompressor {
224
233
self . stream. pointee. next_out = CCompressZlib_voidPtr_to_BytefPtr ( toBuffer. baseAddress!)
225
234
226
235
let rt = CCompressZlib . deflate ( self . stream, flag)
227
- bytesRead = self . stream. pointee. next_in - CCompressZlib_voidPtr_to_BytefPtr( fromBuffer. baseAddress!)
228
- bytesWritten = self . stream. pointee. next_out - CCompressZlib_voidPtr_to_BytefPtr( toBuffer. baseAddress!)
236
+ bytesRead =
237
+ self . stream. pointee. next_in
238
+ - CCompressZlib_voidPtr_to_BytefPtr( fromBuffer. baseAddress!)
239
+ bytesWritten =
240
+ self . stream. pointee. next_out
241
+ - CCompressZlib_voidPtr_to_BytefPtr( toBuffer. baseAddress!)
229
242
switch rt {
230
243
case Z_OK:
231
244
if flush == . finish {
@@ -320,7 +333,7 @@ public final class ZlibDecompressor {
320
333
321
334
deinit {
322
335
let rt = inflateEnd ( self . stream)
323
- assert ( rt == Z_OK , " inflateEnd returned error: \( rt ) " )
336
+ assert ( rt != Z_STREAM_ERROR , " deflateEnd returned stream error " )
324
337
self . stream. deinitialize ( count: 1 )
325
338
self . stream. deallocate ( )
326
339
}
@@ -348,8 +361,12 @@ public final class ZlibDecompressor {
348
361
349
362
let rt = CCompressZlib . inflate ( self . stream, Z_NO_FLUSH)
350
363
351
- bytesRead = self . stream. pointee. next_in - CCompressZlib_voidPtr_to_BytefPtr( fromBuffer. baseAddress!)
352
- bytesWritten = self . stream. pointee. next_out - CCompressZlib_voidPtr_to_BytefPtr( toBuffer. baseAddress!)
364
+ bytesRead =
365
+ self . stream. pointee. next_in
366
+ - CCompressZlib_voidPtr_to_BytefPtr( fromBuffer. baseAddress!)
367
+ bytesWritten =
368
+ self . stream. pointee. next_out
369
+ - CCompressZlib_voidPtr_to_BytefPtr( toBuffer. baseAddress!)
353
370
switch rt {
354
371
case Z_OK:
355
372
if self . stream. pointee. avail_out == 0 {
0 commit comments