Fix retarded Swift pointer API somehow

This commit is contained in:
Davide De Rosa 2019-03-30 20:30:29 +01:00
parent 7b59276875
commit 0bfc1e08eb
6 changed files with 35 additions and 14 deletions

View File

@ -76,7 +76,8 @@ public class DNSResolver {
var ipAddresses: [String] = []
for case var rawAddress as Data in rawAddresses {
var ipAddress = [CChar](repeating: 0, count: Int(NI_MAXHOST))
let result = rawAddress.withUnsafeBytes { (addr: UnsafePointer<sockaddr>) in
let result: Int32 = rawAddress.withUnsafeBytes {
let addr = $0.bindMemory(to: sockaddr.self).baseAddress!
return getnameinfo(
addr,
socklen_t(rawAddress.count),
@ -99,7 +100,8 @@ public class DNSResolver {
var addr = in_addr(s_addr: CFSwapInt32HostToBig(ipv4))
var buf = Data(count: Int(INET_ADDRSTRLEN))
let bufCount = socklen_t(buf.count)
let resultPtr = buf.withUnsafeMutableBytes { (bufPtr) in
let resultPtr: UnsafePointer<CChar>? = buf.withUnsafeMutableBytes {
let bufPtr = $0.bindMemory(to: CChar.self).baseAddress!
return withUnsafePointer(to: &addr) {
return inet_ntop(AF_INET, $0, bufPtr, bufCount)
}

View File

@ -192,7 +192,8 @@ extension ControlChannel {
// needs a copy for swapping
var authPacket = packet
let authCount = authPacket.count
try authPacket.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
try authPacket.withUnsafeMutableBytes {
let ptr = $0.bytePointer
PacketSwapCopy(ptr, packet, prefixLength, authLength)
try decrypter.verifyBytes(ptr, length: authCount, flags: nil)
}
@ -265,9 +266,11 @@ extension ControlChannel {
let encryptedCount = packet.count - adLength
var decryptedPacket = Data(count: decrypter.encryptionCapacity(withLength: encryptedCount))
var decryptedCount = 0
try packet.withUnsafeBytes { (src: UnsafePointer<UInt8>) in
try packet.withUnsafeBytes {
let src = $0.bytePointer
var flags = CryptoFlags(iv: nil, ivLength: 0, ad: src, adLength: adLength)
try decryptedPacket.withUnsafeMutableBytes { (dest: UnsafeMutablePointer<UInt8>) in
try decryptedPacket.withUnsafeMutableBytes {
let dest = $0.bytePointer
try decrypter.decryptBytes(src + flags.adLength, length: encryptedCount, dest: dest + headerLength, destLength: &decryptedCount, flags: &flags)
memcpy(dest, src, headerLength)
}

View File

@ -115,9 +115,10 @@ extension CustomStringConvertible {
return description
}
var data = description.data(using: .utf8)!
let dataCount = CC_LONG(data.count)
var md = Data(count: Int(CC_SHA1_DIGEST_LENGTH))
md.withUnsafeMutableBytes {
_ = CC_SHA1(&data, CC_LONG(data.count), $0)
_ = CC_SHA1(&data, dataCount, $0.bytePointer)
}
return "#\(md.toHex().prefix(16))#"
}

View File

@ -164,7 +164,9 @@ extension Data {
// best
func UInt32Value(from: Int) -> UInt32 {
return subdata(in: from..<(from + 4)).withUnsafeBytes { $0.pointee }
return subdata(in: from..<(from + 4)).withUnsafeBytes {
$0.load(as: UInt32.self)
}
}
@available(*, deprecated)
@ -182,13 +184,13 @@ extension Data {
func networkUInt16Value(from: Int) -> UInt16 {
return UInt16(bigEndian: subdata(in: from..<(from + 2)).withUnsafeBytes {
$0.pointee
$0.load(as: UInt16.self)
})
}
func networkUInt32Value(from: Int) -> UInt32 {
return UInt32(bigEndian: subdata(in: from..<(from + 4)).withUnsafeBytes {
$0.pointee
$0.load(as: UInt32.self)
})
}
}
@ -204,3 +206,15 @@ extension Array where Element == Data {
return map { $0.count }.reduce(0) { $0 + $1 }
}
}
extension UnsafeRawBufferPointer {
var bytePointer: UnsafePointer<Element> {
return bindMemory(to: Element.self).baseAddress!
}
}
extension UnsafeMutableRawBufferPointer {
var bytePointer: UnsafeMutablePointer<Element> {
return bindMemory(to: Element.self).baseAddress!
}
}

View File

@ -77,8 +77,9 @@ class SecureRandom {
static func data(length: Int) throws -> Data {
var randomData = Data(count: length)
try randomData.withUnsafeMutableBytes { (randomBytes: UnsafeMutablePointer<UInt8>) -> Void in
guard (SecRandomCopyBytes(kSecRandomDefault, length, randomBytes) == 0) else {
try randomData.withUnsafeMutableBytes {
let randomBytes = $0.bytePointer
guard SecRandomCopyBytes(kSecRandomDefault, length, randomBytes) == 0 else {
throw SecureRandomError.randomGenerator
}
}

View File

@ -62,7 +62,7 @@ extension Encrypter {
var dest: [UInt8] = Array(repeating: 0, count: srcLength + 256)
var destLength = 0
try data.withUnsafeBytes {
try encryptBytes($0, length: srcLength, dest: &dest, destLength: &destLength, flags: flags)
try encryptBytes($0.bytePointer, length: srcLength, dest: &dest, destLength: &destLength, flags: flags)
}
dest.removeSubrange(destLength..<dest.count)
return Data(dest)
@ -75,7 +75,7 @@ extension Decrypter {
var dest: [UInt8] = Array(repeating: 0, count: srcLength + 256)
var destLength = 0
try data.withUnsafeBytes {
try decryptBytes($0, length: srcLength, dest: &dest, destLength: &destLength, flags: flags)
try decryptBytes($0.bytePointer, length: srcLength, dest: &dest, destLength: &destLength, flags: flags)
}
dest.removeSubrange(destLength..<dest.count)
return Data(dest)
@ -84,7 +84,7 @@ extension Decrypter {
func verifyData(_ data: Data, flags: UnsafePointer<CryptoFlags>?) throws {
let srcLength = data.count
try data.withUnsafeBytes {
try verifyBytes($0, length: srcLength, flags: flags)
try verifyBytes($0.bytePointer, length: srcLength, flags: flags)
}
}
}