Skip to content

Commit

Permalink
Improve reading of strings with unknown lengths.
Browse files Browse the repository at this point in the history
  • Loading branch information
p-x9 committed Jun 3, 2024
1 parent 41f0bb3 commit cd9d698
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
17 changes: 17 additions & 0 deletions Sources/MachOKit/Extension/FileHandle+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,23 @@ extension FileHandle {
return String(cString: data)
}

@_spi(Support)
public func readString(
offset: UInt64,
step: UInt64 = 10
) -> String? {
var data = Data()
var offset = offset
while true {
let new = readData(offset: offset, size: Int(step))
if new.contains(0) || new.isEmpty { break }
data.append(new)
offset += UInt64(new.count)
}

return String(cString: data)
}

@_spi(Support)
public func readData(
offset: UInt64,
Expand Down
3 changes: 1 addition & 2 deletions Sources/MachOKit/Model/DyldCache/DyldCacheImageInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ extension DyldCacheImageInfo {
/// - Returns: Path for image
public func path(in cache: DyldCache) -> String? {
cache.fileHandle.readString(
offset: numericCast(layout.pathFileOffset),
size: 1000 // FIXME
offset: numericCast(layout.pathFileOffset)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ extension DyldCacheImageTextInfo {
/// - Returns: Path for image text
public func path(in cache: DyldCache) -> String? {
cache.fileHandle.readString(
offset: numericCast(layout.pathOffset),
size: 1000 // FIXME
offset: numericCast(layout.pathOffset)
)
}
}

0 comments on commit cd9d698

Please sign in to comment.