From 885119acdd27bd53ed3096285c3eca3e2d99ad70 Mon Sep 17 00:00:00 2001 From: Dustin Mierau Date: Thu, 30 Nov 2023 23:46:57 -0800 Subject: Bunch of work to and now login, user list, handling replies, etc. works. Or at least the framework is there for them to work. --- Hotline/Utility/DataExtensions.swift | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'Hotline/Utility/DataExtensions.swift') 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.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.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.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.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.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? { -- cgit