diff options
| author | Dustin Mierau <dustin@mierau.me> | 2024-01-01 11:35:49 -0800 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2024-01-01 11:35:49 -0800 |
| commit | 5d2aadbc834393b532d043d6298d6a23b80c2a5c (patch) | |
| tree | dcf139b67179d5bc3ad3ed69e949a1e7c1d18177 /Hotline/Utility | |
| parent | a4cbaea0b12b7dd42133b4bd14f1371f1000fcc0 (diff) | |
File downloads now working in macOS client.1.0beta4
Diffstat (limited to 'Hotline/Utility')
| -rw-r--r-- | Hotline/Utility/FoundationExtensions.swift | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/Hotline/Utility/FoundationExtensions.swift b/Hotline/Utility/FoundationExtensions.swift index 3797dea..3183b39 100644 --- a/Hotline/Utility/FoundationExtensions.swift +++ b/Hotline/Utility/FoundationExtensions.swift @@ -50,12 +50,23 @@ extension Data { } return withUnsafeBytes { $0.load(as: UInt64.self ) } + } + + func readDate(at offset: Int) -> Date? { + guard offset >= 0, offset + 2 + 2 + 4 <= self.count else { + return nil + } -// return 0 + if + let year = self.readUInt16(at: offset), + let ms = self.readUInt16(at: offset + 2), + let secs = self.readUInt32(at: offset + 2 + 2) { + return convertHotlineDate(year: year, seconds: secs, milliseconds: ms) + } -// return (UInt64(self[offset]) << 56) + (UInt64(self[offset + 1]) << 48) + (UInt64(self[offset + 2]) << 40) + (UInt64(self[offset + 3]) << 32) + (UInt64(self[offset + 4]) << 24) + (UInt64(self[offset + 5]) << 16) + (UInt64(self[offset + 6]) << 8) + UInt64(self[offset + 7]) + return nil } - + func readData(at offset: Int, length: Int) -> Data? { guard offset >= 0, offset + length <= self.count else { return nil @@ -96,13 +107,28 @@ extension Data { } func readPString(at offset: Int) -> (String?, Int) { + guard offset >= 0, offset + 1 <= self.count else { + return (nil, 0) + } let len = Int(self.readUInt8(at: offset)!) + guard offset + 1 + len <= self.count else { + return (nil, 0) + } return (self.readString(at: offset+1, length: len), 1 + len) } func readLongPString(at offset: Int) -> (String?, Int) { + guard offset >= 0, offset + 2 <= self.count else { + return (nil, 0) + } let len = Int(self.readUInt16(at: offset)!) - return (self.readString(at: offset+2, length: len), 2 + len) + guard len > 0 else { + return ("", 0) + } + guard offset + 2 + len <= self.count else { + return (nil, 0) + } + return (self.readString(at: offset+2, length: len), len) } @@ -128,14 +154,24 @@ extension Data { } } -extension UInt32 { - func toStringLiteral() -> String { +extension String { + func fourCharCode() -> FourCharCode { + guard self.count == 4 else { + return 0 + } + + return self.utf16.reduce(0, {$0 << 8 + FourCharCode($1)}) + } +} + +extension FourCharCode { + func fourCharCode() -> String { let bytes = [ UInt8((self >> 24) & 0xFF), UInt8((self >> 16) & 0xFF), UInt8((self >> 8) & 0xFF), UInt8(self & 0xFF) ] - return String(bytes: bytes, encoding: .utf8) ?? "" + return String(bytes: bytes, encoding: .ascii) ?? "" } } |