From a03cc3b72fec18cd04532f0317d9e1556ea4e8da Mon Sep 17 00:00:00 2001 From: Alexande B Date: Sat, 6 Apr 2024 10:55:17 +0200 Subject: [PATCH 1/2] fix: performance issue in debug info calculation --- HCaptcha/Classes/HCaptchaDebugInfo.swift | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/HCaptcha/Classes/HCaptchaDebugInfo.swift b/HCaptcha/Classes/HCaptchaDebugInfo.swift index 24ed189..1ebea8d 100644 --- a/HCaptcha/Classes/HCaptchaDebugInfo.swift +++ b/HCaptcha/Classes/HCaptchaDebugInfo.swift @@ -2,7 +2,7 @@ // HCaptchaDebugInfo.m // HCaptcha // -// Copyright © 2022 HCaptcha. All rights reserved. +// Copyright © 2024 HCaptcha. All rights reserved. // import Foundation @@ -16,11 +16,11 @@ extension String { } } -private func updateInfoFor(_ image: String, _ ctx: UnsafeMutablePointer) { +private func updateInfoFor(_ image: String, _ ctx: UnsafeMutablePointer, depth: UInt32 = 16) { var count: UInt32 = 0 if let imagePtr = (image as NSString).utf8String { let classes = objc_copyClassNamesForImage(imagePtr, &count) - for cls in UnsafeBufferPointer>(start: classes, count: Int(count)) { + for cls in UnsafeBufferPointer>(start: classes, count: Int(min(depth, count))) { CC_MD5_Update(ctx, cls, CC_LONG(strlen(cls))) } classes?.deallocate() @@ -53,6 +53,8 @@ class HCaptchaDebugInfo { } private class func buildDebugInfo() -> [String] { + let depth: UInt32 = 16 + var depsCount = 0, sysCount = 0 let depsCtx = UnsafeMutablePointer.allocate(capacity: 1) let sysCtx = UnsafeMutablePointer.allocate(capacity: 1) let appCtx = UnsafeMutablePointer.allocate(capacity: 1) @@ -67,6 +69,16 @@ class HCaptchaDebugInfo { let systemFramework = image.contains("/Library/PrivateFrameworks/") || image.contains("/System/Library/Frameworks/") + if systemFramework && sysCount < depth { + sysCount += 1 + } else if !systemFramework && depsCount < depth { + depsCount += 1 + } else if sysCount < depth || depsCount < depth { + continue + } else { + break + } + let md5Ctx = systemFramework ? sysCtx : depsCtx updateInfoFor(image, md5Ctx) } From ce5b3f6b4e45a9cd18b9fb800bae49214e1b4968 Mon Sep 17 00:00:00 2001 From: Alexande B Date: Sat, 6 Apr 2024 11:27:20 +0200 Subject: [PATCH 2/2] chore: declare vars on separate lines --- HCaptcha/Classes/HCaptchaDebugInfo.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HCaptcha/Classes/HCaptchaDebugInfo.swift b/HCaptcha/Classes/HCaptchaDebugInfo.swift index 1ebea8d..ffb12c5 100644 --- a/HCaptcha/Classes/HCaptchaDebugInfo.swift +++ b/HCaptcha/Classes/HCaptchaDebugInfo.swift @@ -54,7 +54,8 @@ class HCaptchaDebugInfo { private class func buildDebugInfo() -> [String] { let depth: UInt32 = 16 - var depsCount = 0, sysCount = 0 + var depsCount = 0 + var sysCount = 0 let depsCtx = UnsafeMutablePointer.allocate(capacity: 1) let sysCtx = UnsafeMutablePointer.allocate(capacity: 1) let appCtx = UnsafeMutablePointer.allocate(capacity: 1)