-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIIPU.h
More file actions
188 lines (154 loc) · 11.3 KB
/
IIPU.h
File metadata and controls
188 lines (154 loc) · 11.3 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
static void saveBufferToFile(
const std::string& destinationFileName,
std::vector<uint8_t> buffer,
uint32_t size
) {
std::ofstream outputFile(destinationFileName, std::ios::binary);
// Пропуск функции, если размер модуля на входе равен 0
if (size == 0) { buffer = { 0 }; goto SkipSave; }
if (!outputFile.is_open()) {
outputFile.close();
buffer.clear();
std::cerr << "Error creating output file: " << destinationFileName << std::endl; exit(1);
}
outputFile.write((char*)buffer.data(), size);
if (outputFile.fail()) {
std::cerr << "Error writing data to " << destinationFileName << std::endl;
outputFile.close();
buffer.clear();
exit(1);
}
SkipSave:
outputFile.close();
buffer.clear();
return;
};
static void saveFileToBuffer(
const std::string& sourceFileName,
std::vector<uint8_t>& buffer
){
std::ifstream inputFile(sourceFileName, std::ios::binary | std::ios::ate); // Модификатор ate is crucial
if (!inputFile.is_open()) {
inputFile.close();
buffer.clear();
std::cerr << "Error opening inputFile file: " + sourceFileName << std::endl; exit(1);
}
std::streamsize fileSize = inputFile.tellg();
if (fileSize <= 0) {
// Если файл пустой или ошибка чтения размера
inputFile.close();
buffer.clear();
return;
}
buffer.resize(static_cast<uint32_t>(fileSize));
inputFile.seekg(0, std::ios::beg);
if (!inputFile.read(reinterpret_cast<char*>(buffer.data()), fileSize)) {
inputFile.close();
buffer.clear();
std::cerr << "Error reading file: " << sourceFileName << std::endl; exit(1);
}
}
// Функция сохраняет данные из файла по смещению
static void saveDataByOffset(
const std::string& sourceFileName,
const std::string& destinationFileName,
uint32_t dataOffset,
uint32_t dataSize
)
{
std::ifstream sourceFile(sourceFileName, std::ios::binary);
std::ofstream destinationFile(destinationFileName, std::ios::binary);
if (!sourceFile.is_open()) {
std::cerr << "Error opening source file on saveDataByOffset function: " << sourceFileName << std::endl;
exit(1);
}
if (!destinationFile.is_open()) {
std::cerr << "Error creating output file on saveDataByOffset function: " << destinationFileName << std::endl;
sourceFile.close();
exit(1);
}
sourceFile.seekg(0, std::ios::end);
std::streampos fileSize = sourceFile.tellg();
sourceFile.seekg(0, std::ios::beg);
if (dataOffset >= fileSize || (dataOffset + dataSize > (uint32_t)fileSize)) {
std::cerr << "Offset " << dataOffset << " is beyond the file size." << std::endl;
sourceFile.close();
destinationFile.close();
exit(1);
}
sourceFile.seekg(dataOffset);
std::vector<char> buffer(dataSize);
sourceFile.read(buffer.data(), dataSize);
if (sourceFile.fail()) {
std::cerr << "Error reading data from: " << sourceFileName << std::endl;
sourceFile.close();
destinationFile.close();
exit(1);
}
destinationFile.write(buffer.data(), dataSize);
if (destinationFile.fail()) {
std::cerr << "Error writing data to " << destinationFileName << std::endl;
sourceFile.close();
destinationFile.close();
exit(1);
}
sourceFile.close();
destinationFile.close();
return;
}
static std::vector<uint8_t> hexStringToBytes(std::string& hex) {
std::vector<uint8_t> bytes;
if (hex.length() % 2 != 0) { std::cerr << "Invalid HEX String: not even length" << std::endl; exit(1); }
for (size_t i = 0; i < hex.length(); i += 2) {
std::string byteStr = hex.substr(i, 2);
uint8_t byte;
try {
byte = std::stoul(byteStr, nullptr, 16);
}
catch (const std::invalid_argument& e) {
std::cerr << "Invalid HEX String: " << std::string(e.what()) << std::endl; exit(1);
}
catch (const std::out_of_range& e) {
std::cerr << "Invalid HEX String: char out of range" << std::endl; exit(1);
}
bytes.push_back(byte);
}
return bytes;
}
// Функция подсчитывает количество вхождений шаблона в бинарном файле, функ. hexStringToBytes помогает
static uint8_t IFLASHcountHexPattern(
const std::string& sourceFileName,
std::string& hexPattern,
std::vector<uint32_t>& instancesOffsets
)
{
std::ifstream file(sourceFileName, std::ios::binary);
if (!file.is_open()) { std::cerr << "Couldn't open: " << sourceFileName << std::endl; exit(1); }
std::vector<uint8_t> pattern = hexStringToBytes(hexPattern);
uint8_t count = 0;
std::vector<uint8_t> buffer(pattern.size());
file.seekg(0, std::ios::end);
uint32_t fileSize = (uint32_t)file.tellg();
file.seekg(0, std::ios::beg);
uint32_t i;
i = fileSize > 0x1000000 ? 0x1200000 : 0xA00000;
//Debug
//printf("Start 0x%X", i);
// файл меньше шаблона
if ((fileSize < pattern.size()) || fileSize < i) { std::cerr << "File is corrupted (too small)" << std::endl; exit(1); }
for (; (i <= fileSize - pattern.size()) || count >= 0xFF; i++) {
file.seekg(i, std::ios::beg);
file.read(reinterpret_cast<char*>(buffer.data()), pattern.size());
if (buffer == pattern) {
instancesOffsets[count] = i;
count++;
}
}
file.close();
return count;
}