-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathr2.cpp
101 lines (81 loc) · 2.25 KB
/
r2.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <vector>
#include <assert.h>
#include <string>
#include <r_socket.h>
#include "include/strtk.hpp"
#include "include/genotype.hpp"
#include "include/r2.hpp"
using namespace std;
R2::R2() {
cout << "opening link" << endl;
this->r2 = r2pipe_open("r2 -w -q0 malloc://32");
cout << "link ok" << endl;
}
R2::~R2() {
cout << "closing link" << endl;
r2pipe_close(this->r2);
cout << "link closed" << endl;
}
void R2::load(Genotype *g) {
assert(g->size() == 0x20);
char *buff = g->read();
char hex[68];
memset(hex, 0, 68);
hex[0] = 'w'; hex[1] = 'x'; hex[2] = ' ';
for (int i; i<32; i++) {
sprintf(hex, "%s %.2x", hex, *((unsigned char *)&buff[i]));
}
hex[67] = 0x00;
cout << hex << endl;
r2pipe_cmd(this->r2, hex);
}
vector<unsigned int> R2::get_instruction_sizes() {
vector<unsigned int> opcodes_sz;
if (!this->r2) {
cout << "Genotype::r2cmd() r2 was not opened" << endl;
return opcodes_sz;
}
string::size_type sz;
char *szs = r2pipe_cmd(this->r2, "pdl 0x20 ~!0");
string sszs = string(szs);
vector<string> snums;
if (strtk::parse(sszs, "\n", snums)) {
for (int i=0; i<snums.size()-1; i++) {
//string s = snums[i];
opcodes_sz.push_back( stoi (snums[i],&sz) );
}
}
free(szs);
return opcodes_sz;
}
void R2::print_asm(void) {
char *out;
if (!this->r2) {
cout << "Genotype::r2cmd() r2 was not opened" << endl;
return;
}
cout << "showing r2pipe cmd pD" << endl;
printf("this->r2: %p\n", this->r2);
out = r2pipe_cmd(this->r2, "pD 0x20"); // TODO: don't hardcode the size
printf("%s\n", out);
free(out);
cout << "done." << endl;
}
bool R2::has_invalid_instructions(void) {
if (!this->r2) {
cout << "Genotype::r2cmd() r2 was not opened" << endl;
return false;
}
vector<unsigned int> opcodes_sz;
string::size_type sz;
char *szs = r2pipe_cmd(this->r2, "pdl 0x20");
string sszs = string(szs);
vector<string> snums;
int sum = 0;
if (strtk::parse(sszs, "\n", snums)) {
for (int i=0; i<snums.size()-1; i++) {
sum += stoi (snums[i], &sz);
}
}
return (sum < 0x20);
}