import Foundation enum DeviceInfoError: Error { case missingData } extension Data { mutating func getByte() throws -> UInt8 { guard count >= 1 else { throw DeviceInfoError.missingData } return removeFirst() } mutating func decodeUInt16() throws -> UInt16 { guard count >= 2 else { throw DeviceInfoError.missingData } let low = removeFirst() let high = removeFirst() return UInt16(high: high, low: low) } mutating func decodeTwoByteInteger() throws -> Int { try decodeUInt16().integer } mutating func decodeFourByteInteger() throws -> Int { guard count >= 4 else { throw DeviceInfoError.missingData } let byte0 = removeFirst() let byte1 = removeFirst() let byte2 = removeFirst() let byte3 = removeFirst() return (Int(byte3) << 24) | (Int(byte2) << 16) | (Int(byte1) << 8) | Int(byte0) } }