diff options
| author | Dustin Mierau <dustin@mierau.me> | 2023-11-30 23:46:57 -0800 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2023-11-30 23:46:57 -0800 |
| commit | 885119acdd27bd53ed3096285c3eca3e2d99ad70 (patch) | |
| tree | 7dce98c071859684ff2aaa157f090b7cdc941a04 /Hotline/Utility/DataExtensions.swift | |
| parent | 30255476781789df318a571622ab732681696139 (diff) | |
Bunch of work to and now login, user list, handling replies, etc. works. Or at least the framework is there for them to work.
Diffstat (limited to 'Hotline/Utility/DataExtensions.swift')
| -rw-r--r-- | Hotline/Utility/DataExtensions.swift | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/Hotline/Utility/DataExtensions.swift b/Hotline/Utility/DataExtensions.swift index caf26a9..9f4c967 100644 --- a/Hotline/Utility/DataExtensions.swift +++ b/Hotline/Utility/DataExtensions.swift @@ -22,33 +22,33 @@ extension Data { } func readUInt8(at offset: Int) -> UInt8? { - guard offset >= 0, offset + MemoryLayout<UInt8>.size <= self.count else { + guard offset >= 0, offset + 1 <= self.count else { return nil } return self[offset] } - func readUInt16(at offset: Int, endianness: Endianness = .big) -> UInt16? { - guard offset >= 0, offset + MemoryLayout<UInt16>.size <= self.count else { + func readUInt16(at offset: Int) -> UInt16? { + guard offset >= 0, offset + 2 <= self.count else { return nil } - let value = self.subdata(in: offset..<(offset + MemoryLayout<UInt16>.size)).withUnsafeBytes { $0.load(as: UInt16.self) } - return (endianness == .big) ? value.bigEndian : value.littleEndian + + return (UInt16(self[offset]) << 8) + UInt16(self[offset + 1]) } - func readUInt32(at offset: Int, endianness: Endianness = .big) -> UInt32? { - guard offset >= 0, offset + MemoryLayout<UInt32>.size <= self.count else { + func readUInt32(at offset: Int) -> UInt32? { + guard offset >= 0, offset + 4 <= self.count else { return nil } - let value = self.subdata(in: offset..<(offset + MemoryLayout<UInt32>.size)).withUnsafeBytes { $0.load(as: UInt32.self) } - return (endianness == .big) ? value.bigEndian : value.littleEndian + + return (UInt32(self[offset]) << 24) + (UInt32(self[offset + 1]) << 16) + (UInt32(self[offset + 2]) << 8) + UInt32(self[offset + 3]) } func readData(at offset: Int, length: Int) -> Data? { guard offset >= 0, offset + length <= self.count else { return nil } - return self[offset..<(offset + length)] + return self.subdata(in: offset..<(offset + length)) } func readString(at offset: Int, length: Int, encoding: String.Encoding) -> String? { |