Fix max device name chars

This commit is contained in:
Leon Böttger committed 2025-12-26 13:42:46 +01:00
1 parent 728385ff95
commit a9e6ed1ddf
3 files changed
+21 -17

No files matched your search

@@ -44,7 +44,6 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
private static let customDeviceNameKey = "com.leonboettger.quickdrop.deviceName" private static let customDeviceNameKey = "com.leonboettger.quickdrop.deviceName"
public let endpointID: [UInt8] = generateEndpointID() public let endpointID: [UInt8] = generateEndpointID()
public static let maxNameChars = 31
public static let shared = NearbyConnectionManager() public static let shared = NearbyConnectionManager()
@@ -151,13 +150,13 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
let deviceType = isMac() ? RemoteDeviceInfo.DeviceType.computer : (isiPadOrMac() ? .tablet : .phone) let deviceType = isMac() ? RemoteDeviceInfo.DeviceType.computer : (isiPadOrMac() ? .tablet : .phone)
if let customName = getCustomDeviceName() { if let customName = getCustomDeviceName() {
return EndpointInfo(name: String(customName.prefix(Self.maxNameChars)), deviceType: deviceType) return EndpointInfo(name: String(customName), deviceType: deviceType)
} }
#if os(macOS) #if os(macOS)
return EndpointInfo(name: String((Host.current().localizedName ?? "Mac").prefix(Self.maxNameChars)), deviceType: deviceType) return EndpointInfo(name: String((Host.current().localizedName ?? "Mac")), deviceType: deviceType)
#else #else
return EndpointInfo(name: String(Device.current.description.withoutBracketedContent.prefix(Self.maxNameChars)), deviceType: deviceType) return EndpointInfo(name: String(Device.current.description.withoutBracketedContent), deviceType: deviceType)
#endif #endif
} }
+18 -6
View File
@@ -183,9 +183,10 @@ public struct EndpointInfo {
let deviceType: RemoteDeviceInfo.DeviceType let deviceType: RemoteDeviceInfo.DeviceType
let qrCodeData: Data? let qrCodeData: Data?
static let maxNameChars = 32
init(name: String, deviceType: RemoteDeviceInfo.DeviceType) { init(name: String, deviceType: RemoteDeviceInfo.DeviceType) {
self.name = name self.name = String(decoding: Self.sanitizedString(name), as: UTF8.self)
self.deviceType = deviceType self.deviceType = deviceType
self.qrCodeData = nil self.qrCodeData = nil
} }
@@ -254,19 +255,30 @@ public struct EndpointInfo {
var endpointInfo: [UInt8] = [UInt8(deviceType.rawValue << 1)] var endpointInfo: [UInt8] = [UInt8(deviceType.rawValue << 1)]
// 16 bytes: unknown random bytes // 16 bytes: unknown random bytes
for _ in 0 ... 15 { for _ in 0 ... 15 {
endpointInfo.append(UInt8.random(in: 0 ... 255)) endpointInfo.append(UInt8.random(in: 0...255))
} }
// Device name in UTF-8 prefixed with 1-byte length // Device name in UTF-8 prefixed with 1-byte length
var nameChars = [UInt8]((name ?? "").utf8) let nameChars = Self.sanitizedString(self.name ?? "")
if nameChars.count > 255 {
nameChars = [UInt8](nameChars[0 ..< 255])
}
endpointInfo.append(UInt8(nameChars.count)) endpointInfo.append(UInt8(nameChars.count))
for ch in nameChars { for ch in nameChars {
endpointInfo.append(UInt8(ch)) endpointInfo.append(UInt8(ch))
} }
return Data(endpointInfo) return Data(endpointInfo)
} }
private static func sanitizedString(_ string: String) -> [UInt8] {
var out: [UInt8] = []
out.reserveCapacity(min(Self.maxNameChars, string.utf8.count))
for scalar in string.unicodeScalars {
let bytes = Array(scalar.utf8)
if out.count + bytes.count > Self.maxNameChars { break }
out.append(contentsOf: bytes)
}
return out
}
} }
@@ -12,18 +12,11 @@ struct ChangeDeviceNameAlertModifier: ViewModifier {
@Binding var isPresented: Bool @Binding var isPresented: Bool
@State var customName = "" @State var customName = ""
let maxLength = NearbyConnectionManager.maxNameChars
func body(content: Content) -> some View { func body(content: Content) -> some View {
content content
.alert("ChangeDeviceName", isPresented: $isPresented) { .alert("ChangeDeviceName", isPresented: $isPresented) {
TextField("DeviceName", text: $customName) TextField("DeviceName", text: $customName)
.onChange(of: customName) { newValue in
if newValue.count > maxLength {
customName = String(newValue.prefix(maxLength))
}
}
Button("Save") { Button("Save") {
let trimmed = customName.trimmingCharacters(in: .whitespacesAndNewlines) let trimmed = customName.trimmingCharacters(in: .whitespacesAndNewlines)