-
Notifications
You must be signed in to change notification settings - Fork 9
/
bpt_bytes_io.cpp
85 lines (75 loc) · 2.38 KB
/
bpt_bytes_io.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
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include "bpt_bytes_io.hpp"
namespace bpt { namespace io {
template <typename T1, typename T2>
T1 pointer_cast(T2* ptr) {
return static_cast<T1>(static_cast<void*>(ptr));
}
bytes_type from_reg(REG reg, const CONTEXT* ctx) {
bytes_type data(REG_Size(reg));
PIN_GetContextRegval(ctx, reg,
pointer_cast<UINT8*>(&data[0]));
return data;
}
bytes_type from_mem(ADDRINT addr, UINT32 size) {
bytes_type data(size);
PIN_SafeCopy(static_cast<VOID*>(&data[0]),
reinterpret_cast<VOID*>(addr), size);
return data;
}
struct ios_flag_saver {
explicit ios_flag_saver(std::ostream& os)
: os_(os), f_(os.flags()) {}
~ios_flag_saver() { os_.flags(f_); }
private:
std::ostream& os_;
std::ios::fmtflags f_;
};
struct bytes_printer : std::binary_function<const std::string&,
char,
std::string> {
bytes_printer(std::ostream& o, const std::string& s)
: out(o), sep(s) {}
std::string operator()(const std::string& init, char b) {
ios_flag_saver s(out);
out << init
<< std::hex
<< std::uppercase
<< std::setfill('0')
<< std::setw(2)
<< static_cast<int>(static_cast<unsigned char>(b));
return sep;
}
private:
std::ostream& out;
const std::string& sep;
};
std::ostream& pp_addr(std::ostream& out, ADDRINT addr) {
return out << "0x" << std::hex << addr << std::dec
<< ':' << sizeof(ADDRINT)*8;
}
std::ostream& code::operator<<(std::ostream& out,
const bytes_type& bytes) {
std::accumulate(bytes.begin(), bytes.end(),
std::string(""), bytes_printer(out, " "));
return out;
}
std::ostream& data::operator<<(std::ostream& out,
const bytes_type& data) {
bytes_type::const_reverse_iterator it =
std::find_if(data.rbegin(), data.rend(),
std::bind1st(std::not_equal_to<char>(), 0));
if (it != data.rend()) {
out << "0x";
std::accumulate(it, data.rend(),
std::string(""), bytes_printer(out, ""));
} else {
out << "0x0";
}
return out;
}
}} //namespace bpt::io