Skip to content

Commit

Permalink
fix: performance issue in debug info calculation (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP authored Apr 6, 2024
1 parent ab8f2ec commit f9bb3bb
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions HCaptcha/Classes/HCaptchaDebugInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// HCaptchaDebugInfo.m
// HCaptcha
//
// Copyright © 2022 HCaptcha. All rights reserved.
// Copyright © 2024 HCaptcha. All rights reserved.
//

import Foundation
Expand All @@ -16,11 +16,11 @@ extension String {
}
}

private func updateInfoFor(_ image: String, _ ctx: UnsafeMutablePointer<CC_MD5_CTX>) {
private func updateInfoFor(_ image: String, _ ctx: UnsafeMutablePointer<CC_MD5_CTX>, depth: UInt32 = 16) {
var count: UInt32 = 0
if let imagePtr = (image as NSString).utf8String {
let classes = objc_copyClassNamesForImage(imagePtr, &count)
for cls in UnsafeBufferPointer<UnsafePointer<CChar>>(start: classes, count: Int(count)) {
for cls in UnsafeBufferPointer<UnsafePointer<CChar>>(start: classes, count: Int(min(depth, count))) {
CC_MD5_Update(ctx, cls, CC_LONG(strlen(cls)))
}
classes?.deallocate()
Expand Down Expand Up @@ -53,6 +53,9 @@ class HCaptchaDebugInfo {
}

private class func buildDebugInfo() -> [String] {
let depth: UInt32 = 16
var depsCount = 0
var sysCount = 0
let depsCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
let sysCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
let appCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
Expand All @@ -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)
}
Expand Down

0 comments on commit f9bb3bb

Please sign in to comment.