2525//
2626//////////////////////////////////////////////////////////////////////////////////////////////////
2727import Foundation
28- import zlib
28+ import CZLib
2929
3030class Decompressor {
3131 private var strm = z_stream ( )
3232 private var buffer = [ UInt8] ( repeating: 0 , count: 0x2000 )
3333 private var inflateInitialized = false
3434 private let windowBits : Int
35-
35+
3636 init ? ( windowBits: Int ) {
3737 self . windowBits = windowBits
3838 guard initInflate ( ) else { return nil }
3939 }
40-
40+
4141 private func initInflate( ) -> Bool {
4242 if Z_OK == inflateInit2_ ( & strm, - CInt( windowBits) ,
4343 ZLIB_VERSION, CInt ( MemoryLayout< z_stream> . size) )
@@ -47,59 +47,59 @@ class Decompressor {
4747 }
4848 return false
4949 }
50-
50+
5151 func reset( ) throws {
5252 teardownInflate ( )
5353 guard initInflate ( ) else { throw NSError ( ) }
5454 }
55-
55+
5656 func decompress( _ data: Data , finish: Bool ) throws -> Data {
5757 return try data. withUnsafeBytes { ( bytes: UnsafePointer < UInt8 > ) -> Data in
5858 return try decompress ( bytes: bytes, count: data. count, finish: finish)
5959 }
6060 }
61-
61+
6262 func decompress( bytes: UnsafePointer < UInt8 > , count: Int , finish: Bool ) throws -> Data {
6363 var decompressed = Data ( )
6464 try decompress ( bytes: bytes, count: count, out: & decompressed)
65-
65+
6666 if finish {
6767 let tail : [ UInt8 ] = [ 0x00 , 0x00 , 0xFF , 0xFF ]
6868 try decompress ( bytes: tail, count: tail. count, out: & decompressed)
6969 }
70-
70+
7171 return decompressed
72-
72+
7373 }
74-
74+
7575 private func decompress( bytes: UnsafePointer < UInt8 > , count: Int , out: inout Data ) throws {
7676 var res : CInt = 0
7777 strm. next_in = UnsafeMutablePointer < UInt8 > ( mutating: bytes)
7878 strm. avail_in = CUnsignedInt ( count)
79-
79+
8080 repeat {
8181 strm. next_out = UnsafeMutablePointer < UInt8 > ( & buffer)
8282 strm. avail_out = CUnsignedInt ( buffer. count)
83-
83+
8484 res = inflate ( & strm, 0 )
85-
85+
8686 let byteCount = buffer. count - Int( strm. avail_out)
8787 out. append ( buffer, count: byteCount)
8888 } while res == Z_OK && strm. avail_out == 0
89-
89+
9090 guard ( res == Z_OK && strm. avail_out > 0 )
9191 || ( res == Z_BUF_ERROR && Int ( strm. avail_out) == buffer. count)
9292 else {
9393 throw NSError ( domain: WebSocket . ErrorDomain, code: Int ( WebSocket . InternalErrorCode. compressionError. rawValue) , userInfo: nil )
9494 }
9595 }
96-
96+
9797 private func teardownInflate( ) {
9898 if inflateInitialized, Z_OK == inflateEnd ( & strm) {
9999 inflateInitialized = false
100100 }
101101 }
102-
102+
103103 deinit {
104104 teardownInflate ( )
105105 }
@@ -110,12 +110,12 @@ class Compressor {
110110 private var buffer = [ UInt8] ( repeating: 0 , count: 0x2000 )
111111 private var deflateInitialized = false
112112 private let windowBits : Int
113-
113+
114114 init ? ( windowBits: Int ) {
115115 self . windowBits = windowBits
116116 guard initDeflate ( ) else { return nil }
117117 }
118-
118+
119119 private func initDeflate( ) -> Bool {
120120 if Z_OK == deflateInit2_ ( & strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
121121 - CInt( windowBits) , 8 , Z_DEFAULT_STRATEGY,
@@ -126,48 +126,48 @@ class Compressor {
126126 }
127127 return false
128128 }
129-
129+
130130 func reset( ) throws {
131131 teardownDeflate ( )
132132 guard initDeflate ( ) else { throw NSError ( ) }
133133 }
134-
134+
135135 func compress( _ data: Data ) throws -> Data {
136136 var compressed = Data ( )
137137 var res : CInt = 0
138138 data. withUnsafeBytes { ( ptr: UnsafePointer < UInt8 > ) -> Void in
139139 strm. next_in = UnsafeMutablePointer < UInt8 > ( mutating: ptr)
140140 strm. avail_in = CUnsignedInt ( data. count)
141-
141+
142142 repeat {
143143 strm. next_out = UnsafeMutablePointer < UInt8 > ( & buffer)
144144 strm. avail_out = CUnsignedInt ( buffer. count)
145-
145+
146146 res = deflate ( & strm, Z_SYNC_FLUSH)
147-
147+
148148 let byteCount = buffer. count - Int( strm. avail_out)
149149 compressed. append ( buffer, count: byteCount)
150150 }
151151 while res == Z_OK && strm. avail_out == 0
152-
152+
153153 }
154-
154+
155155 guard res == Z_OK && strm. avail_out > 0
156156 || ( res == Z_BUF_ERROR && Int ( strm. avail_out) == buffer. count)
157157 else {
158158 throw NSError ( domain: WebSocket . ErrorDomain, code: Int ( WebSocket . InternalErrorCode. compressionError. rawValue) , userInfo: nil )
159159 }
160-
160+
161161 compressed. removeLast ( 4 )
162162 return compressed
163163 }
164-
164+
165165 private func teardownDeflate( ) {
166166 if deflateInitialized, Z_OK == deflateEnd ( & strm) {
167167 deflateInitialized = false
168168 }
169169 }
170-
170+
171171 deinit {
172172 teardownDeflate ( )
173173 }
0 commit comments