From f9bb3bb89cfc4ca162c4c2d754f8e0380d03fc41 Mon Sep 17 00:00:00 2001 From: Alex Babrykovich Date: Sat, 6 Apr 2024 14:58:01 +0200 Subject: [PATCH] fix: performance issue in debug info calculation (#146) --- HCaptcha/Classes/HCaptchaDebugInfo.swift | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/HCaptcha/Classes/HCaptchaDebugInfo.swift b/HCaptcha/Classes/HCaptchaDebugInfo.swift index 24ed189..ffb12c5 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,9 @@ class HCaptchaDebugInfo { } private class func buildDebugInfo() -> [String] { + let depth: UInt32 = 16 + var depsCount = 0 + var sysCount = 0 let depsCtx = UnsafeMutablePointer.allocate(capacity: 1) let sysCtx = UnsafeMutablePointer.allocate(capacity: 1) let appCtx = UnsafeMutablePointer.allocate(capacity: 1) @@ -67,6 +70,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) }