Return a C-string read from memory.
(self,
address: int,
max_length: int = GEF_MAX_STRING_LENGTH,
encoding: Optional[str] = None)
| 10161 | return unpack(mem) |
| 10162 | |
| 10163 | def read_cstring(self, |
| 10164 | address: int, |
| 10165 | max_length: int = GEF_MAX_STRING_LENGTH, |
| 10166 | encoding: Optional[str] = None) -> str: |
| 10167 | """Return a C-string read from memory.""" |
| 10168 | encoding = encoding or "unicode-escape" |
| 10169 | length = min(address | (DEFAULT_PAGE_SIZE-1), max_length+1) |
| 10170 | |
| 10171 | try: |
| 10172 | res_bytes = self.read(address, length) |
| 10173 | except gdb.error: |
| 10174 | err(f"Can't read memory at '{address}'") |
| 10175 | return "" |
| 10176 | try: |
| 10177 | with warnings.catch_warnings(): |
| 10178 | # ignore DeprecationWarnings (see #735) |
| 10179 | warnings.simplefilter("ignore") |
| 10180 | res = res_bytes.decode(encoding, "strict") |
| 10181 | except UnicodeDecodeError: |
| 10182 | # latin-1 as fallback due to its single-byte to glyph mapping |
| 10183 | res = res_bytes.decode("latin-1", "replace") |
| 10184 | |
| 10185 | res = res.split("\x00", 1)[0] |
| 10186 | ustr = res.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t") |
| 10187 | if max_length and len(res) > max_length: |
| 10188 | return f"{ustr[:max_length]}[...]" |
| 10189 | return ustr |
| 10190 | |
| 10191 | def read_ascii_string(self, address: int) -> Optional[str]: |
| 10192 | """Read an ASCII string from memory""" |
no test coverage detected