-
Notifications
You must be signed in to change notification settings - Fork 5
/
writer.cpp
139 lines (108 loc) · 3.46 KB
/
writer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright (C) 2019 AO Kaspersky Lab
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "writer.h"
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <sstream>
std::string & writer::offset( uint32_t offset )
{
std::stringstream tmp;
tmp << std::setfill('0') << std::setw(8) << std::hex << offset;
return m_offset = tmp.str();
}
std::string & writer::bytecode()
{
std::stringstream tmp;
tmp << std::setfill(' ') << std::setw(BYTECODE_MAX_LENGHT) << std::left << " ";
return m_bytecode = tmp.str();
}
std::string & writer::bytecode(const uint8_t *src, size_t len)
{
std::stringstream tmp;
char pcHEX[3];
const uint8_t *src_ptr = src;
std::string dots = "...";
std::string delimeter = " ";
if ( len )
{
uint32_t max_length = BYTECODE_MAX_LENGHT;
//FIXME magic number for more beautiful layout
max_length -= dots.length() + 3;
for (uint32_t i=0; i<len; ++i)
{
if ( m_bytecode.length() >= max_length )
{
m_bytecode += dots;
break;
}
else
{
if ( i == len - 1 )
delimeter = "";
snprintf(pcHEX, sizeof(pcHEX), "%02X", src_ptr[i]);
m_bytecode += pcHEX + delimeter;
}
}
tmp << std::setfill(' ') << std::setw(BYTECODE_MAX_LENGHT) << std::left << m_bytecode;
return m_bytecode = tmp.str();
}
return m_bytecode;
}
std::string & writer::instructions( const char *format, ... )
{
std::string result;
va_list args;
va_start(args, format);
result = _do_format(format, args);
va_end(args);
std::stringstream tmp;
tmp << std::setfill(' ') << std::setw(INSTRUCTIONS_MAX_LEN) << std::left << result;
return m_instructions = tmp.str();
}
std::string & writer::comment( const char *format, ... )
{
std::string result;
va_list args;
va_start(args, format);
result = _do_format(format, args);
va_end(args);
return m_comment = result;
}
std::string writer::_do_format(const char* format, va_list vlist)
{
std::string result;
va_list args, args_size;
va_copy(args, vlist);
va_copy(args_size, args);
size_t size_required = std::vsnprintf(nullptr, 0, format, args_size) + 1;
result.resize( size_required );
va_end(args_size);
// ok for C++11
std::vsnprintf((char*)( result.data() ), result.size(), format, args);
va_end(args);
return result;
}
void writer::clear()
{
m_offset.clear();
m_bytecode.clear();
m_instructions.clear();
m_comment.clear();
}
void writer::print()
{
std::cout << link() << std::endl;
};