-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.cpp
81 lines (75 loc) · 2.7 KB
/
memory.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
//
// Created by michal on 16.09.17.
//
#include <sstream>
#include <cstring>
#include <algorithm>
#include "memory.h"
void Memory::initialize(std::vector<char> &buffer) {
stored_data.resize(size);
if (init_data_size > 0) {
std::vector<unsigned char> temp_stored_data(buffer.end() - init_data_size, buffer.end());
for (auto i = 0; i < init_data_size; i++) {
stored_data[i] = temp_stored_data[i];
}
}
}
void Memory::save_data(std::vector<unsigned char> data, int64_t offset) {
for (unsigned long i = size, y = data.size(); y != 0; i--, y--) {
stored_data[i - 1 - offset] = data[y - 1];
}
}
std::vector<unsigned char> Memory::access_data(int offset, DataType data_type) {
std::vector<unsigned char> accessed_data;
for (int i = 0; i < data_type; i++) {
auto value = stored_data[size - data_type - offset + i];
accessed_data.push_back(value);
}
return accessed_data;
}
int64_t Memory::memory_to_int(std::vector<unsigned char> memory_slice) {
std::stringstream ss;
std::string string_argument;
for (unsigned char cell : memory_slice) {
auto cell_int = (int) cell;
ss << std::hex << cell_int;
if (cell_int == 0) {
ss << std::hex << cell_int;
}
string_argument += ss.str();
ss.str("");
}
const char *const_argument = string_argument.c_str();
auto memory_value = (int64_t) strtoull(const_argument, nullptr, 16);
return memory_value;
}
void Memory::update(int64_t update_value, uint64_t offset, DataType data_type) {
std::string string_value = std::to_string(update_value);
std::vector<unsigned char> data;
if (data_type == Byte) {
auto value = (int8_t) strtoll(string_value.c_str(), nullptr, 10);
if (data.size() < sizeof(value))
data.resize(sizeof(value));
std::memcpy(data.data(), &value, sizeof(value));
}
if (data_type == Word) {
auto value = (int16_t) strtoll(string_value.c_str(), nullptr, 10);
if (data.size() < sizeof(value))
data.resize(sizeof(value));
std::memcpy(data.data(), &value, sizeof(value));
}
if (data_type == Dword) {
auto value = (int32_t) strtoll(string_value.c_str(), nullptr, 10);
if (data.size() < sizeof(value))
data.resize(sizeof(value));
std::memcpy(data.data(), &value, sizeof(value));
}
if (data_type == Qword) {
auto value = (uint64_t) strtoull(string_value.c_str(), nullptr, 10);
if (data.size() < sizeof(value))
data.resize(sizeof(value));
std::memcpy(data.data(), &value, sizeof(value));
}
std::reverse(data.begin(), data.end());
save_data(data, offset);
}