2020-04-29 18:02:36 +02:00
|
|
|
#include "PluginData.h"
|
|
|
|
|
2020-05-17 20:43:04 +02:00
|
|
|
|
|
|
|
PluginData::PluginData(const PluginData &obj) {
|
|
|
|
this->buffer = obj.buffer;
|
|
|
|
this->heapHandle = obj.heapHandle;
|
|
|
|
this->memoryType = obj.memoryType;
|
|
|
|
this->length = obj.length;
|
|
|
|
loadReader();
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:02:36 +02:00
|
|
|
void PluginData::freeMemory() {
|
2020-05-03 12:30:15 +02:00
|
|
|
if (buffer == NULL) {
|
2020-04-29 18:02:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
switch (memoryType) {
|
|
|
|
default:
|
|
|
|
case eMemTypeExpHeap:
|
|
|
|
MEMFreeToExpHeap(this->heapHandle, buffer);
|
|
|
|
this->buffer = NULL;
|
|
|
|
break;
|
|
|
|
case eMemTypeMEM2:
|
|
|
|
free(this->buffer);
|
|
|
|
this->buffer = NULL;
|
|
|
|
break;
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginData::PluginData(std::vector<uint8_t> buffer) : PluginData(buffer, 0, eMemTypeMEM2) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginData::loadReader() {
|
2020-05-03 12:30:15 +02:00
|
|
|
if (this->buffer == NULL) {
|
2020-04-29 18:02:36 +02:00
|
|
|
this->reader = std::nullopt;
|
|
|
|
} else {
|
2020-05-03 12:30:15 +02:00
|
|
|
elfio *nReader = new elfio;
|
|
|
|
if (nReader != NULL && nReader->load((char *) this->buffer, length)) {
|
2020-04-29 18:02:36 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Loading was okay");
|
|
|
|
this->reader = nReader;
|
|
|
|
} else {
|
2020-05-03 12:30:15 +02:00
|
|
|
if (nReader) {
|
2020-04-29 18:02:36 +02:00
|
|
|
delete nReader;
|
|
|
|
nReader = NULL;
|
|
|
|
}
|
|
|
|
DEBUG_FUNCTION_LINE("Loading failed");
|
|
|
|
this->reader = std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
PluginData::PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) :
|
|
|
|
heapHandle(heapHandle),
|
|
|
|
memoryType(memoryType),
|
|
|
|
length(input.size()) {
|
|
|
|
void *data_copy = NULL;
|
|
|
|
switch (memoryType) {
|
|
|
|
default:
|
|
|
|
case eMemTypeExpHeap:
|
|
|
|
data_copy = MEMAllocFromExpHeapEx(heapHandle, length, 4);
|
2020-05-03 14:14:22 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", length / 1024);
|
2020-05-03 12:30:15 +02:00
|
|
|
if (data_copy == NULL) {
|
|
|
|
DEBUG_FUNCTION_LINE("Failed to allocate space on exp heap");
|
|
|
|
} else {
|
|
|
|
memcpy(data_copy, &input[0], length);
|
|
|
|
}
|
|
|
|
this->buffer = data_copy;
|
|
|
|
DEBUG_FUNCTION_LINE("copied data to exp heap");
|
|
|
|
break;
|
|
|
|
case eMemTypeMEM2:
|
|
|
|
data_copy = memalign(length, 4);
|
|
|
|
if (data_copy == NULL) {
|
|
|
|
DEBUG_FUNCTION_LINE("Failed to allocate space on default heap");
|
|
|
|
} else {
|
|
|
|
memcpy(data_copy, &input[0], length);
|
|
|
|
}
|
|
|
|
this->buffer = data_copy;
|
|
|
|
break;
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
loadReader();
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
std::optional<PluginData> PluginData::createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length) {
|
2020-04-29 18:02:36 +02:00
|
|
|
return std::nullopt;
|
|
|
|
}
|