@@ -35,27 +35,34 @@ public class Process {
35
35
}
36
36
37
37
// 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 ] {
40
50
var readAddress : UInt64 = address
41
51
let chunkSize : UInt = 64
52
+ var result : [ UInt8 ] = [ ]
42
53
43
54
while true {
44
55
let chunk : [ UInt8 ] = try readArray ( address: readAddress, upToCount: chunkSize)
45
56
46
57
if let nullIndex = chunk. firstIndex ( of: 0 ) {
47
- accumulatedBytes . append ( contentsOf: chunk. prefix ( nullIndex) )
58
+ result . append ( contentsOf: chunk. prefix ( nullIndex) )
48
59
break
49
60
}
50
61
51
- accumulatedBytes . append ( contentsOf: chunk)
62
+ result . append ( contentsOf: chunk)
52
63
readAddress += UInt64 ( chunkSize)
53
64
}
54
65
55
- guard let result = String ( bytes: accumulatedBytes, encoding: . utf8) else {
56
- throw Error . InvalidString ( address: address)
57
- }
58
-
59
66
return result
60
67
}
61
68
0 commit comments