Fix encoding error

This commit is contained in:
Christoph Hagen 2023-12-20 09:25:53 +01:00
parent b749a80f5d
commit ad94588b3c
6 changed files with 30 additions and 13 deletions

View File

@ -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 = "<group>"; };
888362332A80F3F90032BBB2 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
888362352A80F4420032BBB2 /* HistoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryView.swift; sourceTree = "<group>"; };
888A11322B32DBBB0099A12B /* UInt8+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt8+Extensions.swift"; sourceTree = "<group>"; };
88AEE37E2B2217050034EDA9 /* MessageType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageType.swift; sourceTree = "<group>"; };
88AEE3802B22327F0034EDA9 /* UInt32+Random.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt32+Random.swift"; sourceTree = "<group>"; };
88AEE3832B2236DC0034EDA9 /* SignedMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignedMessage.swift; sourceTree = "<group>"; };
@ -290,6 +293,7 @@
E24EE77127FDCCC00011CFD2 /* Data+Hex.swift */,
8860D74D2B232AED00849FAC /* Data+Coding.swift */,
E2C5C1DC281B3AC400769EF6 /* UInt32+Coding.swift */,
888A11322B32DBBB0099A12B /* UInt8+Extensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
@ -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 */,

View File

@ -7,7 +7,7 @@
<key>Sesame Watch App.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>1</integer>
</dict>
<key>Sesame-Watch Watch App.xcscheme_^#shared#^_</key>
<dict>
@ -17,12 +17,12 @@
<key>Sesame-WidgetExtension.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
<key>Sesame.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>2</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>

View File

@ -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..<clientChallengeEndIndex])
self.clientChallenge = UInt32(bytes: clientChallengeData)
let serverChallengeEndIndex = clientChallengeEndIndex + UInt32.byteSize
let serverChallengeData = Array(data[clientChallengeEndIndex..<serverChallengeEndIndex])
self.serverChallenge = UInt32(bytes: serverChallengeData)
guard let result = MessageResult(rawValue: data.last!) else {
print("Invalid message result \(data.last!)")
throw MessageResult.unknownMessageResultFromDevice

View File

@ -2,12 +2,6 @@ import Foundation
extension Data {
func convert<T>(into value: T) -> T {
withUnsafeBytes {
$0.baseAddress!.load(as: T.self)
}
}
init<T>(from value: T) {
var target = value
self = Swift.withUnsafeBytes(of: &target) {

View File

@ -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)
}

View File

@ -0,0 +1,12 @@
import Foundation
extension Array where Element == UInt8 {
func convert<T>(to _: T.Type) -> T {
withUnsafeBufferPointer {
$0.baseAddress!.withMemoryRebound(to: T.self, capacity: 1) {
$0.pointee
}
}
}
}