]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Swift 5 migration: Handle changes in Data's pointer interface
authorRoopesh Chander <roop@roopc.net>
Tue, 9 Apr 2019 05:41:28 +0000 (11:11 +0530)
committerRoopesh Chander <roop@roopc.net>
Tue, 9 Apr 2019 05:55:04 +0000 (11:25 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/Shared/Model/Data+KeyEncoding.swift
WireGuard/WireGuard/Crypto/Curve25519.swift
WireGuard/WireGuard/Tunnel/TunnelsManager.swift
WireGuard/WireGuard/ZipArchive/ZipArchive.swift

index a1abdd7ac2f7a365e1f3572cdbd850b579261767..5c7aee9482e65b034e0d13bfdcf98a6ee25efc24 100644 (file)
@@ -13,8 +13,8 @@ extension Data {
             return nil
         }
         var out = Data(repeating: 0, count: Int(WG_KEY_LEN_HEX))
-        out.withUnsafeMutableBytes { outBytes in
-            self.withUnsafeBytes { inBytes in
+        out.withUnsafeMutableInt8Bytes { outBytes in
+            self.withUnsafeUInt8Bytes { inBytes in
                 key_to_hex(outBytes, inBytes)
             }
         }
@@ -25,7 +25,7 @@ extension Data {
     init?(hexKey hexString: String) {
         self.init(repeating: 0, count: Int(WG_KEY_LEN))
 
-        if !self.withUnsafeMutableBytes { key_from_hex($0, hexString) } {
+        if !self.withUnsafeMutableUInt8Bytes { key_from_hex($0, hexString) } {
             return nil
         }
     }
@@ -35,8 +35,8 @@ extension Data {
             return nil
         }
         var out = Data(repeating: 0, count: Int(WG_KEY_LEN_BASE64))
-        out.withUnsafeMutableBytes { outBytes in
-            self.withUnsafeBytes { inBytes in
+        out.withUnsafeMutableInt8Bytes { outBytes in
+            self.withUnsafeUInt8Bytes { inBytes in
                 key_to_base64(outBytes, inBytes)
             }
         }
@@ -47,8 +47,34 @@ extension Data {
     init?(base64Key base64String: String) {
         self.init(repeating: 0, count: Int(WG_KEY_LEN))
 
-        if !self.withUnsafeMutableBytes { key_from_base64($0, base64String) } {
+        if !self.withUnsafeMutableUInt8Bytes { key_from_base64($0, base64String) } {
             return nil
         }
     }
 }
+
+extension Data {
+    func withUnsafeUInt8Bytes<R>(_ body: (UnsafePointer<UInt8>) -> R) -> R {
+        assert(!isEmpty)
+        return self.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) -> R in
+            let bytes = ptr.bindMemory(to: UInt8.self)
+            return body(bytes.baseAddress!) // might crash if self.count == 0
+        }
+    }
+
+    mutating func withUnsafeMutableUInt8Bytes<R>(_ body: (UnsafeMutablePointer<UInt8>) -> R) -> R {
+        assert(!isEmpty)
+        return self.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) -> R in
+            let bytes = ptr.bindMemory(to: UInt8.self)
+            return body(bytes.baseAddress!) // might crash if self.count == 0
+        }
+    }
+
+    mutating func withUnsafeMutableInt8Bytes<R>(_ body: (UnsafeMutablePointer<Int8>) -> R) -> R {
+        assert(!isEmpty)
+        return self.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) -> R in
+            let bytes = ptr.bindMemory(to: Int8.self)
+            return body(bytes.baseAddress!) // might crash if self.count == 0
+        }
+    }
+}
index d498a649597d72bec662f2099d3dd4f50e3c7dbc..602cd2d7eb0d6e1fc76e1c3e59a89a8e88fd1925 100644 (file)
@@ -9,7 +9,7 @@ struct Curve25519 {
 
     static func generatePrivateKey() -> Data {
         var privateKey = Data(repeating: 0, count: TunnelConfiguration.keyLength)
-        privateKey.withUnsafeMutableBytes { bytes in
+        privateKey.withUnsafeMutableUInt8Bytes { bytes in
             curve25519_generate_private_key(bytes)
         }
         assert(privateKey.count == TunnelConfiguration.keyLength)
@@ -19,8 +19,8 @@ struct Curve25519 {
     static func generatePublicKey(fromPrivateKey privateKey: Data) -> Data {
         assert(privateKey.count == TunnelConfiguration.keyLength)
         var publicKey = Data(repeating: 0, count: TunnelConfiguration.keyLength)
-        privateKey.withUnsafeBytes { privateKeyBytes in
-            publicKey.withUnsafeMutableBytes { bytes in
+        privateKey.withUnsafeUInt8Bytes { privateKeyBytes in
+            publicKey.withUnsafeMutableUInt8Bytes { bytes in
                 curve25519_derive_public_key(bytes, privateKeyBytes)
             }
         }
index b0bf0cb5256ec73459f3f6d9c03601a42aa960a6..ce8008c0c603fbf2e8a184fbb377f703f5183a36 100644 (file)
@@ -486,7 +486,7 @@ class TunnelContainer: NSObject {
             completionHandler(tunnelConfiguration)
             return
         }
-        guard nil != (try? session.sendProviderMessage(Data(bytes: [ 0 ]), responseHandler: {
+        guard nil != (try? session.sendProviderMessage(Data([ UInt8(0) ]), responseHandler: {
             guard self.status != .inactive, let data = $0, let base = self.tunnelConfiguration, let settings = String(data: data, encoding: .utf8) else {
                 completionHandler(self.tunnelConfiguration)
                 return
index 2cca880a479570cbdba2448ea45852cd33ff4434..85623da6d4d3f55580f7238cb11912878812fc69 100644 (file)
@@ -31,7 +31,7 @@ class ZipArchive {
             let fileName = input.fileName
             let contents = input.contents
             zipOpenNewFileInZip(zipFile, fileName.cString(using: .utf8), nil, nil, 0, nil, 0, nil, Z_DEFLATED, Z_DEFAULT_COMPRESSION)
-            contents.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> Void in
+            contents.withUnsafeUInt8Bytes { ptr -> Void in
                 zipWriteInFileInZip(zipFile, UnsafeRawPointer(ptr), UInt32(contents.count))
             }
             zipCloseFileInZip(zipFile)