aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Model
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-04-09 11:11:28 +0530
committerRoopesh Chander <roop@roopc.net>2019-04-09 11:25:04 +0530
commitcef3957875230874e5c45442b01ca295d373ac41 (patch)
treeab6809aa4bdd5ed78b7a02697126f0e17701100c /WireGuard/Shared/Model
parentSwift 5 migration: Fix switch warnings (diff)
downloadwireguard-apple-cef3957875230874e5c45442b01ca295d373ac41.tar.xz
wireguard-apple-cef3957875230874e5c45442b01ca295d373ac41.zip
Swift 5 migration: Handle changes in Data's pointer interface
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/Shared/Model')
-rw-r--r--WireGuard/Shared/Model/Data+KeyEncoding.swift38
1 files changed, 32 insertions, 6 deletions
diff --git a/WireGuard/Shared/Model/Data+KeyEncoding.swift b/WireGuard/Shared/Model/Data+KeyEncoding.swift
index a1abdd7a..5c7aee94 100644
--- a/WireGuard/Shared/Model/Data+KeyEncoding.swift
+++ b/WireGuard/Shared/Model/Data+KeyEncoding.swift
@@ -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
+ }
+ }
+}