@@ -35,27 +35,34 @@ public class Process {
3535 }
3636
3737 // read a null-terminated string from the target process
38- public func readString( address: UInt64 ) throws -> String {
39- var accumulatedBytes = [ UInt8] ( )
38+ public func readString( address: UInt64 , encoding: String . Encoding = . utf8) throws -> String {
39+ let rawBytes = try readRawString ( address: address)
40+ guard let result = String ( bytes: rawBytes, encoding: encoding) else {
41+ throw Error . InvalidString ( address: address)
42+ }
43+
44+ return result
45+ }
46+
47+ // read bytes from the remote process until a zero-byte is encountered; the
48+ // zero-byte is not included in the result
49+ public func readRawString( address: UInt64 ) throws -> [ UInt8 ] {
4050 var readAddress : UInt64 = address
4151 let chunkSize : UInt = 64
52+ var result : [ UInt8 ] = [ ]
4253
4354 while true {
4455 let chunk : [ UInt8 ] = try readArray ( address: readAddress, upToCount: chunkSize)
4556
4657 if let nullIndex = chunk. firstIndex ( of: 0 ) {
47- accumulatedBytes . append ( contentsOf: chunk. prefix ( nullIndex) )
58+ result . append ( contentsOf: chunk. prefix ( nullIndex) )
4859 break
4960 }
5061
51- accumulatedBytes . append ( contentsOf: chunk)
62+ result . append ( contentsOf: chunk)
5263 readAddress += UInt64 ( chunkSize)
5364 }
5465
55- guard let result = String ( bytes: accumulatedBytes, encoding: . utf8) else {
56- throw Error . InvalidString ( address: address)
57- }
58-
5966 return result
6067 }
6168
0 commit comments