Skip to content

Commit

Permalink
PC dump with compression
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvita committed Sep 10, 2020
1 parent dc01c40 commit 3a21c4d
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"DEBUG",
"SWITCH",
"__SWITCH__",
"VERSION_STRING=\"3.7.3\""
"VERSION_STRING=\"3.7.4\""
],
"compilerPath": "F:/devkitPro/devkitA64/bin/aarch64-none-elf-g++",
"cStandard": "c11",
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
VERSION_MAJOR := 3
VERSION_MINOR := 7
VERSION_MICRO := 3
VERSION_MICRO := 4
NIGHTLY :=

APP_TITLE := EdiZon SE
Expand Down
1 change: 1 addition & 0 deletions include/guis/gui_cheats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class GuiCheats : public Gui
bool m_havesave = true;
void iconloadcheck();
void autoattachcheck();
void testlz();
struct PointerSearch_state
{
u64 depth = 0; // depth and index[depth] is where the search is at, pointersearch2 will increment depth and call itself with nexttarget
Expand Down
2 changes: 2 additions & 0 deletions include/helpers/memory_dump.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <vector>
extern bool print_details;
// extern bool m_compress = false;
class MemoryDump
{
public:
Expand All @@ -26,6 +27,7 @@ class MemoryDump

void setDumpType(enum DumpType dumpType);
void flushBuffer();
bool m_compress = false;

private:
FILE *m_dumpFile;
Expand Down
5 changes: 5 additions & 0 deletions include/lz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// #include <switch.h>

// bool m_compress = false;
int LZ_Compress(const unsigned char *in, unsigned char *out, unsigned int insize);
int LZ_Uncompress(const unsigned char *in, unsigned char *out, unsigned int insize);
48 changes: 46 additions & 2 deletions source/guis/gui_cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "helpers/util.h"
#include "helpers/config.hpp"
#include "edizon_logo_bin.h"
#include "lz.h"
// #define checkheap
// #define printpointerchain
#define MAX_BUFFER_SIZE 0x1000000 // increase size for faster speed
Expand Down Expand Up @@ -596,7 +597,7 @@ void GuiCheats::draw()
Gui::drawTextAligned(font14, 700, 142, currTheme.textColor, "Others", ALIGNED_LEFT);

ss.str("");
ss << "EdiZon SE : 3.7.3";
ss << "EdiZon SE : 3.7.4";
if (m_32bitmode)
ss << " 32 bit pointer mode";
Gui::drawTextAligned(font14, 900, 62, currTheme.textColor, ss.str().c_str(), ALIGNED_LEFT);
Expand Down Expand Up @@ -5846,6 +5847,46 @@ void GuiCheats::autoattachcheck()
{
dmntchtForceOpenCheatProcess();
}
// testlz();
}
void GuiCheats::testlz()
{
time_t unixTime1 = time(NULL);
std::stringstream filenoiconStr;
filenoiconStr << EDIZON_DIR "/ff756020d95b3ec5.dmp2";
MemoryDump *PCDump,*PCDump2;
u64 bufferSize = 0x1000000;
u8 *buffer = new u8[bufferSize];
u8 *outbuffer = new u8[bufferSize + 0x50000];
PCDump = new MemoryDump(filenoiconStr.str().c_str(), DumpType::DATA, false);
filenoiconStr << "a";
PCDump2 = new MemoryDump(filenoiconStr.str().c_str(), DumpType::DATA, true);
u64 S = PCDump->size();
u64 total = 0;
for (u64 index = 0; index < S;)
{
if ((S - index) < bufferSize)
bufferSize = S - index;
PCDump->getData(index, buffer, bufferSize);
printf("Start LZ \n");
u64 count = LZ_Compress(buffer, outbuffer, bufferSize);
PCDump2->addData((u8*)&count, sizeof(count));
PCDump2->addData(outbuffer, count);

float r = (float)count / (float)bufferSize;
printf("Index = %lx , End LZ bufferSize = %lx , outsize = %x , ration = %f\n",index, bufferSize, count, r);
index += bufferSize;
total +=count;
}
delete buffer;
delete outbuffer;
time_t unixTime2 = time(NULL);
printf("%s%ld\n", "Stop Time ", unixTime2 - unixTime1);
float r = (float) total / (float) S;
printf("Size = %lx , outsize = %x , ration = %f\n", S, total, r);
delete PCDump;
PCDump2->flushBuffer();
delete PCDump2;
}
bool GuiCheats::dumpcodetofile()
{
Expand Down Expand Up @@ -6184,7 +6225,8 @@ void GuiCheats::searchMemoryAddressesPrimary2(Debugger *debugger, searchValue_t
PCDump->addData((u8 *)&m_heapBaseAddr, sizeof(u64));
PCDump->addData((u8 *)&m_heapEnd, sizeof(u64));
PCDump->addData((u8 *)&m_EditorBaseAddr, sizeof(u64)); // first entry is the target address

PCDump->flushBuffer();
PCDump->m_compress = true;
bool ledOn = false;

time_t unixTime1 = time(NULL);
Expand Down Expand Up @@ -6289,6 +6331,8 @@ void GuiCheats::searchMemoryAddressesPrimary2(Debugger *debugger, searchValue_t
time_t unixTime2 = time(NULL);
printf("%s%lx\n", "Stop Time ", unixTime2);
printf("%s%ld\n", "Stop Time ", unixTime2 - unixTime1);
if (PCDump->m_compress)
printf("mcompress = true\n");
PCDump->flushBuffer();
delete PCDump;
PCAttr->flushBuffer();
Expand Down
101 changes: 101 additions & 0 deletions source/guis/lz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// #include <switch.h>
// #include "pch.h"
#include <inttypes.h>
//#include <string>
typedef uint64_t u64;
/*************************************************************************
* LZ_Compress() - Compress a block of data using an LZ8 coder.
* in - Input (uncompressed) buffer.
* out - Output (compressed) buffer. This buffer must be 1/8 larger
* than the input buffer to cater for the worst case.
* insize - Number of input bytes.
* The function returns the size of the compressed data.
*************************************************************************/
int LZ_Compress(const unsigned char *in, unsigned char *out, unsigned int insize) {
unsigned int inpos, outpos;
struct marker_t {
unsigned char back : 4;
unsigned char front : 4;
} marker = {0};
#define MAXRANGE 16
if (insize < 1) {
return 0;
}
inpos = 0;
outpos = 0;
do {
marker.front = 8;
for (int front = 0; front <= 8; front++) {
for (unsigned int back = 1; back <= MAXRANGE; back++) {
if (inpos < back * 8) {
break;
}
if ((*(u64 *)(&in[inpos - back * 8]) & (0xFFFFFFFFFFFFFFFF << 8 * front)) == (*(u64 *)(&in[inpos]) & (0xFFFFFFFFFFFFFFFF << 8 * front))) {
marker.front = front;
marker.back = back - 1;
front = 8;
break;
}
}
}
out[outpos] = *(unsigned char *)(&marker);
outpos += sizeof(marker_t);
*(unsigned long long *)(&out[outpos]) = *(unsigned long long *)(&in[inpos]);
outpos += marker.front;
inpos += 8;
} while (inpos < MAXRANGE*8);
do {
marker.front = 8;
for (int front = 0; front <= 8; front++) {
for (unsigned int back = 1; back <= MAXRANGE; back++) {
//if (inpos < back * 8) {
// break;
//}
if ((*(u64*)(&in[inpos - back * 8]) & (0xFFFFFFFFFFFFFFFF << 8 * front)) == (*(u64*)(&in[inpos]) & (0xFFFFFFFFFFFFFFFF << 8 * front))) {
marker.front = front;
marker.back = back - 1;
front = 8;
break;
}
}
}
out[outpos] = *(unsigned char*)(&marker);
outpos += sizeof(marker_t);
*(unsigned long long*)(&out[outpos]) = *(unsigned long long*)(&in[inpos]);
outpos += marker.front;
inpos += 8;
} while (inpos < insize);
return outpos;
}
/*************************************************************************
* LZ_Uncompress() - Uncompress a block of data using an LZ8 decoder.
* in - Input (compressed) buffer.
* out - Output (uncompressed) buffer. This buffer must be large
* enough to hold the uncompressed data.
* insize - Number of input bytes.
**************************************************************************/
int LZ_Uncompress(const unsigned char *in, unsigned char *out, unsigned int insize) {
unsigned int inpos, outpos;
struct marker_t {
unsigned char back : 4;
unsigned char front : 4;
} marker = {0};
if (insize < 1) {
return 0;
}
//marker = *(marker_t *)(&in[0]);
inpos = 0;
outpos = 0;
do {
marker = *(marker_t *)(&in[inpos]);
inpos++;
if (marker.front == 8) {
*(u64 *)(&out[outpos]) = *(u64 *)(&in[inpos]);
} else {
*(u64 *)(&out[outpos]) = (*(u64 *)(&in[inpos]) & ~(0xFFFFFFFFFFFFFFFF << (8 * marker.front))) | (*(u64 *)(&out[outpos - (marker.back + 1) * 8]) & (0xFFFFFFFFFFFFFFFF << (8 * marker.front)));
}
inpos += marker.front;
outpos += 8;
} while (inpos < insize);
return outpos;
}
28 changes: 25 additions & 3 deletions source/helpers/memory_dump.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "helpers/memory_dump.hpp"
#include <stdio.h>
#include "lz.h"
//
// #include "guis/gui_cheats.hpp"

Expand All @@ -23,7 +24,6 @@ bool print_details = false;
MemoryDump::MemoryDump(std::string filePath, DumpType dumpType, bool discardFile) : m_filePath(filePath)
{
m_dataHeader = {0};

m_dataHeader.magic = 0x4E5A4445;
m_dataHeader.endOfHeader = '@';
m_dataHeader.dumpType = (char)dumpType;
Expand Down Expand Up @@ -130,7 +130,18 @@ void MemoryDump::addData(u8 *buffer, size_t dataSize)
MemoryDump::flushBuffer();

fseek(m_dumpFile, 0, SEEK_END); // read shouldn't this come after flushbuffer??
fwrite(buffer, sizeof(u8), dataSize, m_dumpFile);
if (m_compress)
{
// u8 *cbuffer = new u8[BUFFER_SIZE * 9 / 8 + 4];
// *(u32 *)&cbuffer[0] = LZ_Compress(buffer, &cbuffer[4], dataSize);
// fwrite(cbuffer, sizeof(u8), *(u32 *)&cbuffer[0], m_dumpFile);
u8 *cbuffer = new u8[m_data.size() * 9 / 8];
int csize = LZ_Compress(&m_data[0], cbuffer, m_data.size());
fwrite(cbuffer, sizeof(u8), csize, m_dumpFile);
delete cbuffer;
}
else
fwrite(buffer, sizeof(u8), dataSize, m_dumpFile);
m_dataHeader.dataSize += dataSize;
MemoryDump::writeHeader(); // Maybe implement this section directly to avoid extra flashbuffer, seek, writeheader
}
Expand Down Expand Up @@ -225,7 +236,16 @@ void MemoryDump::flushBuffer()
if (m_data.size() > 0 && isFileOpen())
{
fseek(m_dumpFile, 0, SEEK_END);
fwrite(&m_data[0], sizeof(u8), m_data.size(), m_dumpFile);
if (m_compress)
{
u8 *cbuffer = new u8[m_data.size() * 9 / 8 ];
int csize = LZ_Compress(&m_data[0], cbuffer, m_data.size());
fwrite(cbuffer, sizeof(u8), csize , m_dumpFile);
delete cbuffer;
printf("mcompress\n");
}
else
fwrite(&m_data[0], sizeof(u8), m_data.size(), m_dumpFile);

m_dataHeader.dataSize += m_data.size();

Expand All @@ -239,6 +259,8 @@ void MemoryDump::writeHeader()
{
if (isFileOpen())
{
if (m_compress)
m_dataHeader.magic = 0x4E5A4665;
fseek(m_dumpFile, 0, SEEK_SET);
fwrite(&m_dataHeader, sizeof(m_dataHeader), 1, m_dumpFile);
fflush(m_dumpFile);
Expand Down

0 comments on commit 3a21c4d

Please sign in to comment.