mirror of
https://github.com/radareorg/radare2-r2papi.git
synced 2026-11-09 04:29:33 -10:00
- R2Base._exec now uses r2pipe.cmd2 and raises R2CommandError on failure - Empty JSON responses return None instead of crashing - Added _cmd_arg helper to quote string/assembly arguments safely - Ported Write.string/assembly and Print to the new execution path - Skip debugger register tests when ptrace is unavailable
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
from r2papi.base import R2Base
|
|
|
|
|
|
class Write(R2Base):
|
|
def __init__(self, r2):
|
|
super().__init__(r2)
|
|
|
|
def reopen(self, mode=""):
|
|
"""
|
|
Reopen the file in write/cache mode.
|
|
If mode is "ow", it will overwrite the file i.e, reopen it in 'rw' mode.
|
|
Default is 'cache' mode.
|
|
"""
|
|
cmd = "e io.cache=1"
|
|
if mode == "ow": # overwrite
|
|
cmd = "oo+"
|
|
super()._exec(cmd)
|
|
|
|
def bytes(self, buf):
|
|
if isinstance(buf, str):
|
|
# Just use this if you want to write utf-8 data, if not, write
|
|
# bytes object.
|
|
hex_data = buf.encode("utf-8").hex()
|
|
elif isinstance(buf, bytes):
|
|
hex_data = buf.hex()
|
|
else:
|
|
raise TypeError("You must write a string or bytes")
|
|
|
|
return self._exec(f"wx {hex_data}|")
|
|
|
|
def hex(self, hex_string):
|
|
return self._exec(f"wx {hex_string}")
|
|
|
|
def string(self, string, final_nullbyte=False):
|
|
if final_nullbyte:
|
|
string = string + "\x00"
|
|
return self._exec(self._cmd_arg("w", string))
|
|
|
|
def base64(self, string, encode=True):
|
|
subcmd = "w6e" if encode else "w6d"
|
|
return self._exec(f"{subcmd} {string}")
|
|
|
|
def assembly(self, asm_str):
|
|
return self._exec(self._cmd_arg("wa", asm_str))
|
|
|
|
def random(self, size=0):
|
|
return self._exec(f"wr {size}")
|
|
|
|
def nop(self):
|
|
return self._exec("wao nop")
|