-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_helper.py
54 lines (44 loc) · 1.53 KB
/
response_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from contextlib import contextmanager
# https://github.com/NationalSecurityAgency/ghidra/blob/master/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileProcess.java
command_start = b'\x00\x00\x01\x02'
command_end = b'\x00\x00\x01\x03'
query_response_start = b'\x00\x00\x01\x08'
query_response_end = b'\x00\x00\x01\x09'
string_start = b'\x00\x00\x01\x0e'
string_end = b'\x00\x00\x01\x0f'
exception_start = b'\x00\x00\x01\x0a'
exception_end = b'\x00\x00\x01\x0b'
byte_start = b'\x00\x00\x01\x0c'
byte_end = b'\x00\x00\x01\x0d'
@contextmanager
# def write_command(stdin: IO) -> Iterator[IO]:
def write_command(stdin):
stdin.write(command_start)
yield stdin
stdin.write(command_end)
stdin.flush()
@contextmanager
# def write_query_response(stdin: IO) -> Iterator[IO]:
def write_query_response(stdin):
stdin.write(query_response_start)
yield stdin
stdin.write(query_response_end)
stdin.flush()
# def write_string(stdin: IO, data: bytes) -> None:
def write_string(stdin, data):
stdin.write(string_start + data + string_end)
# def write_bytes(stdin: IO, data: bytes) -> None:
def write_bytes(stdin, data):
sz = len(data)
stdin.write(string_start)
stdin.write(chr((sz & 0x3f) + 0x20).encode())
sz = sz >> 6
stdin.write(chr((sz & 0x3f) + 0x20).encode())
sz = sz >> 6
stdin.write(chr((sz & 0x3f) + 0x20).encode())
sz = sz >> 6
stdin.write(chr((sz & 0x3f) + 0x20).encode())
stdin.write(data)
stdin.write(string_end)