-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.cpp
More file actions
96 lines (80 loc) · 2.48 KB
/
analyze.cpp
File metadata and controls
96 lines (80 loc) · 2.48 KB
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
#include "analyze.h"
#include <QFile>
#include <QString>
#include <QStringList>
#include <QVector>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#define MIN_STRING_LEN 5
#define is_char(x) ((x == 9) || ((x >= 32) && (x <= 126)))
#define is_printable_string(str) \
(std::all_of(str.begin(), str.end(), [](char c) { return is_char(c); }))
Analyze::Analyze() {
this->timer.start();
this->extracted_strings = new QVector<QString>();
}
Analyze::~Analyze() {}
Analyze::Analyze(QString filepath) {
this->filepath = filepath;
this->timer.start();
this->extracted_strings = new QVector<QString>();
}
QMap<QString, QString>* Analyze::ParseHeader() {}
QVector<BinarySection>* Analyze::GetSections() {}
QVector<QVector<QString>*>* Analyze::GetImports() {}
enum FILETYPE Analyze::GetFileType(QString filepath) {
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
return OTHER;
}
char magic[4];
if (file.read(magic, 4) != 4) {
file.close();
return OTHER;
}
file.close();
if (magic[0] == 0x7f && magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
return ELF;
} else {
return OTHER;
}
}
QVector<QString>* Analyze::GetStrings(bool utf8, bool utf16) {
if (!this->extracted_strings->empty()) return this->extracted_strings;
QVector<QString>* result = new QVector<QString>();
std::ifstream file(this->filepath.toStdString(), std::ios::binary);
if (!file.is_open()) {
std::cerr << "Error opening file \n";
}
std::string buffer;
std::string line;
char c;
while (file.get(c)) {
if (is_char(c)) { // printable character
buffer += c;
} else if (!buffer.empty()) { // end of a string
if (buffer.size() >= MIN_STRING_LEN && is_printable_string(buffer)) {
QString* nstr = new QString(buffer.c_str());
result->push_back(*nstr);
}
buffer.clear();
}
}
if (!buffer.empty()) {
if (buffer.size() >= MIN_STRING_LEN && is_printable_string(buffer)) {
QString* nstr = new QString(buffer.c_str());
result->push_back(*nstr);
}
buffer.clear();
}
file.close();
return result;
}
qint64 Analyze::getDuration() const {
return duration;
}
void Analyze::setDuration(qint64 newDuration) {
duration = newDuration;
}