MCPcopy Create free account
hub / github.com/qilingframework/qiling / read_string

Method read_string

qiling/os/utils.py:45–80  ·  view source on GitHub ↗

Read a null-terminated string from memory. Args: address : starting address encoding: string encoding to use maxlen : limit number of characters to read before reaching null terminator, 0 for unlimited length Returns: decod

(self, address: int, encoding: str, maxlen: int = 0)

Source from the content-addressed store, hash-verified

43 return -(value & ~(msb - 1)) | value
44
45 def read_string(self, address: int, encoding: str, maxlen: int = 0) -> str:
46 """Read a null-terminated string from memory.
47
48 Args:
49 address : starting address
50 encoding: string encoding to use
51 maxlen : limit number of characters to read before reaching null terminator,
52 0 for unlimited length
53
54 Returns: decoded string
55 """
56
57 terminator = '\x00'.encode(encoding)
58
59 data = bytearray()
60 charlen = len(terminator)
61 strlen = 0
62
63 while True:
64 char = self.ql.mem.read(address, charlen)
65
66 if char == terminator:
67 break
68
69 data += char
70 strlen += 1
71
72 if strlen == maxlen:
73 break
74
75 address += charlen
76
77 s = data.decode(encoding, errors='backslashreplace')
78 self.ql.os.stats.log_string(s)
79
80 return s
81
82 def read_wstring(self, address: int, maxlen: int = 0) -> str:
83 """Read a null-terminated wide string from memory.

Callers 2

read_wstringMethod · 0.95
read_cstringMethod · 0.95

Calls 2

readMethod · 0.45
log_stringMethod · 0.45

Tested by

no test coverage detected