Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ws): resolve some compiler warnings #385

Merged
merged 2 commits into from
Jul 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions Sources/RTM/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import Foundation
import CoreFoundation
import CommonCrypto
#if canImport(CryptoKit)
import CryptoKit
#endif

let WebsocketDidConnectNotification = "WebsocketDidConnectNotification"
let WebsocketDidDisconnectNotification = "WebsocketDidDisconnectNotification"
Expand Down Expand Up @@ -279,8 +282,10 @@ class FoundationStream : NSObject, WSStream, StreamDelegate {
var peerNameLen: Int = 0
SSLGetPeerDomainNameLength(sslContextOut, &peerNameLen)
var peerName = Data(count: peerNameLen)
let _ = peerName.withUnsafeMutableBytes { (peerNamePtr: UnsafeMutablePointer<Int8>) in
SSLGetPeerDomainName(sslContextOut, peerNamePtr, &peerNameLen)
peerName.withUnsafeMutableBytes { (peerNamePtr: UnsafeMutableRawBufferPointer) in
if let unsafePeerNamePtr = peerNamePtr.baseAddress?.bindMemory(to: Int8.self, capacity: peerNameLen) {
_ = SSLGetPeerDomainName(sslContextOut, unsafePeerNamePtr, &peerNameLen)
}
}
if let peerDomain = String(bytes: peerName, encoding: .utf8), peerDomain.count > 0 {
domain = peerDomain
Expand Down Expand Up @@ -1320,11 +1325,19 @@ class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelegate {
}

private extension String {

func sha1Base64() -> String {
let data = self.data(using: String.Encoding.utf8)!
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
data.withUnsafeBytes { _ = CC_SHA1($0, CC_LONG(data.count), &digest) }
return Data(bytes: digest).base64EncodedString()
let data = self.data(using: .utf8)!
if #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) {
let digest = Insecure.SHA1.hash(data: data)
return Data(digest).base64EncodedString()
} else {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
data.withUnsafeBytes { (pointer: UnsafeRawBufferPointer) in
_ = CC_SHA1(pointer.baseAddress, CC_LONG(data.count), &digest)
}
return Data(digest).base64EncodedString()
}
}
}

Expand Down Expand Up @@ -1768,7 +1781,10 @@ class Decompressor {
}

func decompress(_ data: Data, finish: Bool) throws -> Data {
return try data.withUnsafeBytes { (bytes:UnsafePointer<UInt8>) -> Data in
return try data.withUnsafeBytes { (dataPtr: UnsafeRawBufferPointer) -> Data in
guard let bytes = dataPtr.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
throw WSError(type: .compressionError, message: "Error on decompressing", code: 0)
}
return try decompress(bytes: bytes, count: data.count, finish: finish)
}
}
Expand All @@ -1786,25 +1802,23 @@ class Decompressor {

}

private func decompress(bytes: UnsafePointer<UInt8>, count: Int, out:inout Data) throws {
var res:CInt = 0
private func decompress(bytes: UnsafePointer<UInt8>, count: Int, out: inout Data) throws {
var res: CInt = 0
strm.next_in = UnsafeMutablePointer<UInt8>(mutating: bytes)
strm.avail_in = CUnsignedInt(count)

strm.avail_in = uInt(count)
repeat {
strm.next_out = UnsafeMutablePointer<UInt8>(&buffer)
strm.avail_out = CUnsignedInt(buffer.count)

res = inflate(&strm, 0)

buffer.withUnsafeMutableBytes { (bufferPtr: UnsafeMutableRawBufferPointer) in
let unsafeBufferPtr = bufferPtr.baseAddress?.assumingMemoryBound(to: UInt8.self)
strm.next_out = UnsafeMutablePointer<UInt8>(unsafeBufferPtr)
strm.avail_out = uInt(buffer.count)
res = inflate(&strm, 0)
}
let byteCount = buffer.count - Int(strm.avail_out)
out.append(buffer, count: byteCount)
} while res == Z_OK && strm.avail_out == 0

guard (res == Z_OK && strm.avail_out > 0)
|| (res == Z_BUF_ERROR && Int(strm.avail_out) == buffer.count)
else {
throw WSError(type: .compressionError, message: "Error on decompressing", code: 0)
|| (res == Z_BUF_ERROR && Int(strm.avail_out) == buffer.count) else {
throw WSError(type: .compressionError, message: "Error on decompressing", code: 0)
}
}

Expand Down Expand Up @@ -1848,30 +1862,26 @@ class Compressor {

func compress(_ data: Data) throws -> Data {
var compressed = Data()
var res:CInt = 0
data.withUnsafeBytes { (ptr:UnsafePointer<UInt8>) -> Void in
strm.next_in = UnsafeMutablePointer<UInt8>(mutating: ptr)
strm.avail_in = CUnsignedInt(data.count)

var res: CInt = 0
data.withUnsafeBytes { (dataPtr: UnsafeRawBufferPointer) in
let unsafeDataPtr = dataPtr.baseAddress?.assumingMemoryBound(to: UInt8.self)
strm.next_in = UnsafeMutablePointer<UInt8>(mutating: unsafeDataPtr)
strm.avail_in = uInt(data.count)
repeat {
strm.next_out = UnsafeMutablePointer<UInt8>(&buffer)
strm.avail_out = CUnsignedInt(buffer.count)

res = deflate(&strm, Z_SYNC_FLUSH)

buffer.withUnsafeMutableBytes { (bufferPtr: UnsafeMutableRawBufferPointer) in
let unsafeBufferPtr = bufferPtr.baseAddress?.assumingMemoryBound(to: UInt8.self)
strm.next_out = UnsafeMutablePointer<UInt8>(unsafeBufferPtr)
strm.avail_out = uInt(buffer.count)
res = deflate(&strm, Z_SYNC_FLUSH)
}
let byteCount = buffer.count - Int(strm.avail_out)
compressed.append(buffer, count: byteCount)
}
while res == Z_OK && strm.avail_out == 0

} while res == Z_OK && strm.avail_out == 0
}

guard res == Z_OK && strm.avail_out > 0
|| (res == Z_BUF_ERROR && Int(strm.avail_out) == buffer.count)
else {
guard (res == Z_OK && strm.avail_out > 0)
|| (res == Z_BUF_ERROR && Int(strm.avail_out) == buffer.count) else {
throw WSError(type: .compressionError, message: "Error on compressing", code: 0)
}

compressed.removeLast(4)
return compressed
}
Expand Down