Skip to content

Commit

Permalink
Merge pull request #89 from p-x9/feature/improve-read-string-from-file
Browse files Browse the repository at this point in the history
Improve null terminated string reading
  • Loading branch information
p-x9 authored Jun 13, 2024
2 parents 41f0bb3 + 1b21b68 commit 9212cd2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
18 changes: 18 additions & 0 deletions Sources/MachOKit/Extension/FileHandle+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ 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.isEmpty { break }
data.append(new)
if new.contains(0) { break }
offset += UInt64(new.count)
}

return String(cString: data)
}

@_spi(Support)
public func readData(
offset: UInt64,
Expand Down
9 changes: 9 additions & 0 deletions Sources/MachOKit/Extension/UnsafePointer+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ extension UnsafePointer<UInt8> {
return (string, offset)
}
}

extension UnsafePointer<CChar> {
func readString() -> (String, Int) {
let offset = Int(bitPattern: strchr(self, 0)) + 1 - Int(bitPattern: self)
let string = String(cString: self)

return (string, offset)
}
}
9 changes: 3 additions & 6 deletions Sources/MachOKit/MachOFile+Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@ extension MachOFile.Strings {

let ptr = baseAddress
.advanced(by: nextOffset)
.assumingMemoryBound(to: CChar.self)
let string = String(cString: ptr)

let nextPointer = UnsafePointer(strchr(ptr, 0))
.advanced(by: 1)
.assumingMemoryBound(to: UInt8.self)
let (string, offset) = ptr.readString()

let result = Element(string: string, offset: nextOffset)

nextOffset += Int(bitPattern: nextPointer) - Int(bitPattern: ptr)
nextOffset += offset

return result
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/MachOKit/MachOImage+Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ extension MachOImage.Strings {
if offset >= tableSize {
return nil
}
let string = String(cString: nextPointer)
nextPointer = UnsafePointer(strchr(nextPointer, 0))
.advanced(by: 1)
let (string, nextOffset) = nextPointer.readString()
nextPointer = nextPointer.advanced(by: nextOffset)

return .init(string: string, offset: offset)
}
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 9212cd2

Please sign in to comment.