From ad94588b3c17a986ccb01bfa97545af21655ac93 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 20 Dec 2023 09:25:53 +0100 Subject: [PATCH] Fix encoding error --- Sesame.xcodeproj/project.pbxproj | 6 ++++++ .../xcschemes/xcschememanagement.plist | 6 +++--- Sesame/API Extensions/Message.swift | 9 +++++++-- Sesame/API/Extensions/Data+Coding.swift | 6 ------ Sesame/API/Extensions/UInt32+Coding.swift | 4 ++-- Sesame/API/Extensions/UInt8+Extensions.swift | 12 ++++++++++++ 6 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 Sesame/API/Extensions/UInt8+Extensions.swift diff --git a/Sesame.xcodeproj/project.pbxproj b/Sesame.xcodeproj/project.pbxproj index 84ed9df..195369b 100644 --- a/Sesame.xcodeproj/project.pbxproj +++ b/Sesame.xcodeproj/project.pbxproj @@ -46,6 +46,8 @@ 8864665229E5939C004FE2BE /* SFSafeSymbols in Frameworks */ = {isa = PBXBuildFile; productRef = 8864665129E5939C004FE2BE /* SFSafeSymbols */; }; 888362342A80F3F90032BBB2 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 888362332A80F3F90032BBB2 /* SettingsView.swift */; }; 888362362A80F4420032BBB2 /* HistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 888362352A80F4420032BBB2 /* HistoryView.swift */; }; + 888A11332B32DBBB0099A12B /* UInt8+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 888A11322B32DBBB0099A12B /* UInt8+Extensions.swift */; }; + 888A11342B32DC3C0099A12B /* UInt8+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 888A11322B32DBBB0099A12B /* UInt8+Extensions.swift */; }; 88AEE37F2B2217050034EDA9 /* MessageType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88AEE37E2B2217050034EDA9 /* MessageType.swift */; }; 88AEE3812B22327F0034EDA9 /* UInt32+Random.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88AEE3802B22327F0034EDA9 /* UInt32+Random.swift */; }; 88AEE3842B2236DC0034EDA9 /* SignedMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88AEE3832B2236DC0034EDA9 /* SignedMessage.swift */; }; @@ -147,6 +149,7 @@ 8860D76D2B246FC400849FAC /* Text+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Text+Extensions.swift"; sourceTree = ""; }; 888362332A80F3F90032BBB2 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = ""; }; 888362352A80F4420032BBB2 /* HistoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryView.swift; sourceTree = ""; }; + 888A11322B32DBBB0099A12B /* UInt8+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt8+Extensions.swift"; sourceTree = ""; }; 88AEE37E2B2217050034EDA9 /* MessageType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageType.swift; sourceTree = ""; }; 88AEE3802B22327F0034EDA9 /* UInt32+Random.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt32+Random.swift"; sourceTree = ""; }; 88AEE3832B2236DC0034EDA9 /* SignedMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignedMessage.swift; sourceTree = ""; }; @@ -290,6 +293,7 @@ E24EE77127FDCCC00011CFD2 /* Data+Hex.swift */, 8860D74D2B232AED00849FAC /* Data+Coding.swift */, E2C5C1DC281B3AC400769EF6 /* UInt32+Coding.swift */, + 888A11322B32DBBB0099A12B /* UInt8+Extensions.swift */, ); path = Extensions; sourceTree = ""; @@ -543,6 +547,7 @@ 884A45B9279F48C100D6E650 /* ContentView.swift in Sources */, 88AEE3882B226FED0034EDA9 /* MessageResult+UI.swift in Sources */, E28DED2F281E8A0500259690 /* SingleKeyView.swift in Sources */, + 888A11332B32DBBB0099A12B /* UInt8+Extensions.swift in Sources */, E28DED37281EC7FB00259690 /* HistoryManager.swift in Sources */, E2C5C1DB2806FE8900769EF6 /* SesameRoute.swift in Sources */, E2C5C1DD281B3AC400769EF6 /* UInt32+Coding.swift in Sources */, @@ -616,6 +621,7 @@ 88E197C729EDCCBD00BF1D19 /* Client.swift in Sources */, 88E197D429EDCE7600BF1D19 /* UInt32+Coding.swift in Sources */, E240655B2A822397009C1AD8 /* KeyManagement.swift in Sources */, + 888A11342B32DC3C0099A12B /* UInt8+Extensions.swift in Sources */, E24065552A819663009C1AD8 /* SettingsNumberInputView.swift in Sources */, 8860D7572B237FAD00849FAC /* MessageType.swift in Sources */, E240655E2A822E97009C1AD8 /* HistoryListRow.swift in Sources */, diff --git a/Sesame.xcodeproj/xcuserdata/imac.xcuserdatad/xcschemes/xcschememanagement.plist b/Sesame.xcodeproj/xcuserdata/imac.xcuserdatad/xcschemes/xcschememanagement.plist index 75a0224..03745f8 100644 --- a/Sesame.xcodeproj/xcuserdata/imac.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Sesame.xcodeproj/xcuserdata/imac.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ Sesame Watch App.xcscheme_^#shared#^_ orderHint - 2 + 1 Sesame-Watch Watch App.xcscheme_^#shared#^_ @@ -17,12 +17,12 @@ Sesame-WidgetExtension.xcscheme_^#shared#^_ orderHint - 1 + 0 Sesame.xcscheme_^#shared#^_ orderHint - 0 + 2 SuppressBuildableAutocreation diff --git a/Sesame/API Extensions/Message.swift b/Sesame/API Extensions/Message.swift index 175d34d..25b433a 100644 --- a/Sesame/API Extensions/Message.swift +++ b/Sesame/API Extensions/Message.swift @@ -56,8 +56,13 @@ struct Message: Equatable, Hashable { throw MessageResult.invalidMessageTypeFromDevice } self.messageType = messageType - self.clientChallenge = UInt32(data: data.dropFirst().prefix(UInt32.byteSize)) - self.serverChallenge = UInt32(data: data.dropFirst(UInt32.byteSize+1).prefix(UInt32.byteSize)) + let messageTypeEndIndex = data.startIndex+1 + let clientChallengeEndIndex = messageTypeEndIndex + UInt32.byteSize + let clientChallengeData = Array(data[messageTypeEndIndex..(into value: T) -> T { - withUnsafeBytes { - $0.baseAddress!.load(as: T.self) - } - } - init(from value: T) { var target = value self = Swift.withUnsafeBytes(of: &target) { diff --git a/Sesame/API/Extensions/UInt32+Coding.swift b/Sesame/API/Extensions/UInt32+Coding.swift index 35e08ab..0cca4e2 100644 --- a/Sesame/API/Extensions/UInt32+Coding.swift +++ b/Sesame/API/Extensions/UInt32+Coding.swift @@ -6,8 +6,8 @@ extension UInt32 { Create a value from a little-endian data representation (MSB first) - Note: The data must contain exactly four bytes. */ - init(data: Data) { - let value = data.convert(into: UInt32.zero) + init(bytes: [UInt8]) { + let value = bytes.convert(to: UInt32.self) self = CFSwapInt32LittleToHost(value) } diff --git a/Sesame/API/Extensions/UInt8+Extensions.swift b/Sesame/API/Extensions/UInt8+Extensions.swift new file mode 100644 index 0000000..3ef33e2 --- /dev/null +++ b/Sesame/API/Extensions/UInt8+Extensions.swift @@ -0,0 +1,12 @@ +import Foundation + +extension Array where Element == UInt8 { + + func convert(to _: T.Type) -> T { + withUnsafeBufferPointer { + $0.baseAddress!.withMemoryRebound(to: T.self, capacity: 1) { + $0.pointee + } + } + } +}