25
25
//
26
26
//////////////////////////////////////////////////////////////////////////////////////////////////
27
27
import Foundation
28
- import zlib
28
+ import CZLib
29
29
30
30
class Decompressor {
31
31
private var strm = z_stream ( )
32
32
private var buffer = [ UInt8] ( repeating: 0 , count: 0x2000 )
33
33
private var inflateInitialized = false
34
34
private let windowBits : Int
35
-
35
+
36
36
init ? ( windowBits: Int ) {
37
37
self . windowBits = windowBits
38
38
guard initInflate ( ) else { return nil }
39
39
}
40
-
40
+
41
41
private func initInflate( ) -> Bool {
42
42
if Z_OK == inflateInit2_ ( & strm, - CInt( windowBits) ,
43
43
ZLIB_VERSION, CInt ( MemoryLayout< z_stream> . size) )
@@ -47,59 +47,59 @@ class Decompressor {
47
47
}
48
48
return false
49
49
}
50
-
50
+
51
51
func reset( ) throws {
52
52
teardownInflate ( )
53
53
guard initInflate ( ) else { throw NSError ( ) }
54
54
}
55
-
55
+
56
56
func decompress( _ data: Data , finish: Bool ) throws -> Data {
57
57
return try data. withUnsafeBytes { ( bytes: UnsafePointer < UInt8 > ) -> Data in
58
58
return try decompress ( bytes: bytes, count: data. count, finish: finish)
59
59
}
60
60
}
61
-
61
+
62
62
func decompress( bytes: UnsafePointer < UInt8 > , count: Int , finish: Bool ) throws -> Data {
63
63
var decompressed = Data ( )
64
64
try decompress ( bytes: bytes, count: count, out: & decompressed)
65
-
65
+
66
66
if finish {
67
67
let tail : [ UInt8 ] = [ 0x00 , 0x00 , 0xFF , 0xFF ]
68
68
try decompress ( bytes: tail, count: tail. count, out: & decompressed)
69
69
}
70
-
70
+
71
71
return decompressed
72
-
72
+
73
73
}
74
-
74
+
75
75
private func decompress( bytes: UnsafePointer < UInt8 > , count: Int , out: inout Data ) throws {
76
76
var res : CInt = 0
77
77
strm. next_in = UnsafeMutablePointer < UInt8 > ( mutating: bytes)
78
78
strm. avail_in = CUnsignedInt ( count)
79
-
79
+
80
80
repeat {
81
81
strm. next_out = UnsafeMutablePointer < UInt8 > ( & buffer)
82
82
strm. avail_out = CUnsignedInt ( buffer. count)
83
-
83
+
84
84
res = inflate ( & strm, 0 )
85
-
85
+
86
86
let byteCount = buffer. count - Int( strm. avail_out)
87
87
out. append ( buffer, count: byteCount)
88
88
} while res == Z_OK && strm. avail_out == 0
89
-
89
+
90
90
guard ( res == Z_OK && strm. avail_out > 0 )
91
91
|| ( res == Z_BUF_ERROR && Int ( strm. avail_out) == buffer. count)
92
92
else {
93
93
throw NSError ( domain: WebSocket . ErrorDomain, code: Int ( WebSocket . InternalErrorCode. compressionError. rawValue) , userInfo: nil )
94
94
}
95
95
}
96
-
96
+
97
97
private func teardownInflate( ) {
98
98
if inflateInitialized, Z_OK == inflateEnd ( & strm) {
99
99
inflateInitialized = false
100
100
}
101
101
}
102
-
102
+
103
103
deinit {
104
104
teardownInflate ( )
105
105
}
@@ -110,12 +110,12 @@ class Compressor {
110
110
private var buffer = [ UInt8] ( repeating: 0 , count: 0x2000 )
111
111
private var deflateInitialized = false
112
112
private let windowBits : Int
113
-
113
+
114
114
init ? ( windowBits: Int ) {
115
115
self . windowBits = windowBits
116
116
guard initDeflate ( ) else { return nil }
117
117
}
118
-
118
+
119
119
private func initDeflate( ) -> Bool {
120
120
if Z_OK == deflateInit2_ ( & strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
121
121
- CInt( windowBits) , 8 , Z_DEFAULT_STRATEGY,
@@ -126,48 +126,48 @@ class Compressor {
126
126
}
127
127
return false
128
128
}
129
-
129
+
130
130
func reset( ) throws {
131
131
teardownDeflate ( )
132
132
guard initDeflate ( ) else { throw NSError ( ) }
133
133
}
134
-
134
+
135
135
func compress( _ data: Data ) throws -> Data {
136
136
var compressed = Data ( )
137
137
var res : CInt = 0
138
138
data. withUnsafeBytes { ( ptr: UnsafePointer < UInt8 > ) -> Void in
139
139
strm. next_in = UnsafeMutablePointer < UInt8 > ( mutating: ptr)
140
140
strm. avail_in = CUnsignedInt ( data. count)
141
-
141
+
142
142
repeat {
143
143
strm. next_out = UnsafeMutablePointer < UInt8 > ( & buffer)
144
144
strm. avail_out = CUnsignedInt ( buffer. count)
145
-
145
+
146
146
res = deflate ( & strm, Z_SYNC_FLUSH)
147
-
147
+
148
148
let byteCount = buffer. count - Int( strm. avail_out)
149
149
compressed. append ( buffer, count: byteCount)
150
150
}
151
151
while res == Z_OK && strm. avail_out == 0
152
-
152
+
153
153
}
154
-
154
+
155
155
guard res == Z_OK && strm. avail_out > 0
156
156
|| ( res == Z_BUF_ERROR && Int ( strm. avail_out) == buffer. count)
157
157
else {
158
158
throw NSError ( domain: WebSocket . ErrorDomain, code: Int ( WebSocket . InternalErrorCode. compressionError. rawValue) , userInfo: nil )
159
159
}
160
-
160
+
161
161
compressed. removeLast ( 4 )
162
162
return compressed
163
163
}
164
-
164
+
165
165
private func teardownDeflate( ) {
166
166
if deflateInitialized, Z_OK == deflateEnd ( & strm) {
167
167
deflateInitialized = false
168
168
}
169
169
}
170
-
170
+
171
171
deinit {
172
172
teardownDeflate ( )
173
173
}
0 commit comments