From d2920ac99b5c9b977fd0825df2e2dfbf8b8f237d Mon Sep 17 00:00:00 2001 From: Alexande B Date: Sun, 13 Oct 2024 15:59:14 +0200 Subject: [PATCH] fix: improve logging performance by switch from print to NSLog --- HCaptcha/Classes/HCaptchaDebugInfo.swift | 62 ++++++++++-------------- HCaptcha/Classes/HCaptchaLog.swift | 12 ++--- 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/HCaptcha/Classes/HCaptchaDebugInfo.swift b/HCaptcha/Classes/HCaptchaDebugInfo.swift index ffb12c5..37e5314 100644 --- a/HCaptcha/Classes/HCaptchaDebugInfo.swift +++ b/HCaptcha/Classes/HCaptchaDebugInfo.swift @@ -7,23 +7,14 @@ import Foundation import CommonCrypto -import ObjectiveC.runtime -import UIKit -extension String { +private extension String { func jsSanitize() -> String { return self.replacingOccurrences(of: ".", with: "_") } -} -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(min(depth, count))) { - CC_MD5_Update(ctx, cls, CC_LONG(strlen(cls))) - } - classes?.deallocate() + var isSystemFramework: Bool { + return self.contains("/System/Library/") || self.contains("/usr/lib/") } } @@ -41,7 +32,6 @@ private func bundleShortVersion() -> String { } class HCaptchaDebugInfo { - public static let json: String = HCaptchaDebugInfo.buildDebugInfoJson() private class func buildDebugInfoJson() -> String { @@ -53,9 +43,6 @@ 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) @@ -63,29 +50,32 @@ class HCaptchaDebugInfo { CC_MD5_Init(sysCtx) CC_MD5_Init(appCtx) - for framework in Bundle.allFrameworks { - guard let frameworkPath = URL(string: framework.bundlePath) else { continue } - let frameworkBin = frameworkPath.deletingPathExtension().lastPathComponent - let image = frameworkPath.appendingPathComponent(frameworkBin).absoluteString - let systemFramework = image.contains("/Library/PrivateFrameworks/") || - image.contains("/System/Library/Frameworks/") + let loadedCount = Int(min(objc_getClassList(nil, 0), 1024)) + if loadedCount > 0 { + let classes = UnsafeMutablePointer.allocate(capacity: loadedCount) + defer { classes.deallocate() } - if systemFramework && sysCount < depth { - sysCount += 1 - } else if !systemFramework && depsCount < depth { - depsCount += 1 - } else if sysCount < depth || depsCount < depth { - continue - } else { - break - } + _ = objc_getClassList(AutoreleasingUnsafeMutablePointer(classes), Int32(loadedCount)) - let md5Ctx = systemFramework ? sysCtx : depsCtx - updateInfoFor(image, md5Ctx) - } + for i in 0..