diff --git a/SHNFileDecryptor/AbState.shn b/SHNFileDecryptor/AbState.shn index 49c9c46..ba3ba56 100644 Binary files a/SHNFileDecryptor/AbState.shn and b/SHNFileDecryptor/AbState.shn differ diff --git a/SHNFileDecryptor/AbState.shn.bak b/SHNFileDecryptor/AbState.shn.bak index 0841484..aa926f4 100644 Binary files a/SHNFileDecryptor/AbState.shn.bak and b/SHNFileDecryptor/AbState.shn.bak differ diff --git a/SHNFileDecryptor/SHNFileDecryptor.cpp b/SHNFileDecryptor/SHNFileDecryptor.cpp deleted file mode 100644 index f669827..0000000 --- a/SHNFileDecryptor/SHNFileDecryptor.cpp +++ /dev/null @@ -1,449 +0,0 @@ -#include -#include -#include -#include -#include -#include - -typedef std::byte byte; -typedef char sbyte; - -class SHNFile{ - std::vectorReadBytes(std::ifstream&file){ - std::vectorbyteArr; - while(!file.eof()){ - byteArr.push_back(byte(file.get())); - } - return byteArr; - } - std::vectorReadBytes(std::ifstream&file,int bytes){ - std::vectorbyteArr; - for(int i=0;i&data){ - for(int i=0;iintBytes=ReadBytes(file,4); - int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]); - return numb; - } - void Encrypt(){ - Decrypt(); - } - void Decrypt(){ - byte num = byte(data.size()); - for(int i=data.size()-1;i>=0;i--){ - data[i] = byte(data[i]^num); - byte num3 = byte(i); - num3 = byte(num3&byte(15)); - num3 = byte(int(num3)+0x55); - num3 = byte(num3 ^ (byte((int(byte(i))*11)))); - num3 = byte(num3^num); - num3 = byte(int(num3)^170); - num = num3; - } - } - std::vectorReadBytes(int bytes){ - std::vectorbyteArr={}; - for(int i=0;ib=ReadBytes(1); - if(b.size()>0){ - return b[0]; - } else { - return byte(0); - } - } - void WriteByte(std::ofstream&f,byte b){ - f<intBytes=ReadBytes(2); - uint16_t numb = uint16_t(intBytes[1])<<8|uint16_t(intBytes[0]); - return numb; - } - void WriteUInt16(std::ofstream&f,uint16_t val){ - f<>8)&0xFF); - } - int16_t ReadInt16(){ - std::vectorintBytes=ReadBytes(2); - int16_t numb = int16_t(intBytes[1])<<8|int16_t(intBytes[0]); - return numb; - } - void WriteInt16(std::ofstream&f,int16_t val){ - f<>8)&0xFF); - } - uint32_t ReadUInt32(){ - std::vectorintBytes=ReadBytes(4); - uint32_t numb = uint32_t(intBytes[3])<<24|uint32_t(intBytes[2])<<16|uint32_t(intBytes[1])<<8|uint32_t(intBytes[0]); - return numb; - } - void WriteUInt32(std::ofstream&f,uint32_t val){ - f<>8)&0xFF)<>16)&0xFF)<>24)&0xFF); - } - int ReadInt32(){ - std::vectorintBytes=ReadBytes(4); - int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]); - return numb; - } - void WriteInt32(std::ofstream&f,int32_t val){ - f<>8)&0xFF)<>16)&0xFF)<>24)&0xFF); - } - struct Column{ - std::string name; - uint32_t type=0; - int length=0; - }; - std::string ReadString(int bytes){ - std::vectorstrBytes=ReadBytes(bytes); - std::string str=""; - for(int i=0;istrBytes=ReadBytes(4); - byte bytes[]={strBytes[0],strBytes[1],strBytes[2],strBytes[3]}; - float f; - memcpy(&f,&bytes,4); - return f; - } - void WriteSingle(std::ofstream&f,float n){ - byte bytes[4]; - memcpy(&n,&bytes,4); - for(int i=0;i<4;i++){ - f<byteArr=ReadBytes(1); - if(byteArr.size()>0&&byteArr[0]!=byte(0)){ - str+=unsigned char(byteArr[0]); - }else{ - break; - } - } - return str; - } - uint32_t GetRecordLength(){ - uint32_t start=2; - for(Column&col:columns){ - start+=uint32_t(col.length); - } - return start; - } - public: - struct Data{ - std::shared_ptrdata; - int type=0; - Data(byte b){ - std::shared_ptrptr=std::make_shared(byte(b)); - data=ptr; - type=1; - } - Data(sbyte b){ - std::shared_ptrptr=std::make_shared(sbyte(b)); - data=ptr; - type=7; - } - Data(uint16_t n){ - std::shared_ptrptr=std::make_shared(uint16_t(n)); - data=ptr; - type=2; - } - Data(int16_t n){ - std::shared_ptrptr=std::make_shared(int16_t(n)); - data=ptr; - type=6; - } - Data(uint32_t n){ - std::shared_ptrptr=std::make_shared(uint32_t(n)); - data=ptr; - type=3; - } - Data(int32_t n){ - std::shared_ptrptr=std::make_shared(int32_t(n)); - data=ptr; - type=8; - } - Data(float n){ - std::shared_ptrptr=std::make_shared(float(n)); - data=ptr; - type=4; - } - Data(std::string str){ - std::shared_ptrptr=std::make_shared(str); - data=ptr; - type=5; - } - template - T Get(){ - return *std::static_pointer_cast(data); - } - template - void Set(T b){ - data=std::make_shared(T(b)); - } - std::string GetDisplayText(){ - switch(type){ - case 1:{ - return std::to_string(int(Get())); - }break; - case 7:{ - return std::to_string(int(Get())); - }break; - case 2:{ - return std::to_string(Get()); - }break; - case 6:{ - return std::to_string(Get()); - }break; - case 3:{ - return std::to_string(Get()); - }break; - case 8:{ - return std::to_string(Get()); - }break; - case 4:{ - return std::to_string(Get()); - }break; - case 5:{ - return Get(); - }break; - } - } - }; - private: - friend std::ostream&operator<<(std::ostream&out,SHNFile::Data&d){ - out<cryptHeader; - std::vectordata,rawData; - uint32_t header=0,recordCount=0,defaultRecordLength=0,columnCount=0; - std::vectorcolumns; - std::vector>contents; - std::string filename; -public: - void Load(std::string file){ - cryptHeader.clear(); - data.clear(); - header=recordCount=defaultRecordLength=columnCount=0; - columns.clear(); - contents.clear(); - rawData.clear(); - marker=0; - std::chrono::time_pointtimer=std::chrono::high_resolution_clock::now(); - filename=file; - - //FILE OPERATIONS! - std::ifstream f(filename,std::ios::binary); - //Since we don't just read in the entire file raw, we have to do some additional work here as the header provides us with some basic info on how to decrypt this file. - //The backup itself needs all the data from the original file, so we're appending it to rawData as we continue reading it. - cryptHeader=ReadBytes(f,0x20); - std::copy(cryptHeader.begin(),cryptHeader.end(),std::back_inserter(rawData)); - int readAmt=ReadInt32(f); - rawData.push_back(byte(readAmt&0xFF)); - rawData.push_back(byte((readAmt>>8)&0xFF)); - rawData.push_back(byte((readAmt>>16)&0xFF)); - rawData.push_back(byte((readAmt>>24)&0xFF)); - data=ReadBytes(f,readAmt-0x24); - std::copy(data.begin(),data.end(),std::back_inserter(rawData)); - - Decrypt(); - header=ReadUInt32(); - recordCount=ReadUInt32(); - defaultRecordLength=ReadUInt32(); - columnCount=ReadUInt32(); - - int num2=2; - for(int i=0;irow; - for(int j=0;jdur=std::chrono::high_resolution_clock::now()-timer; - std::cout<<"Loaded "<&row:contents){ - std::streampos marker = f.tellp(); - WriteUInt16(f,uint16_t(0)); - int colNum=0; - for(Data&col:row){ - switch(columns[colNum].type){ - case 1: - case 12: - case 0x10:{ - WriteByte(f,col.Get()); - }break; - case 2:{ - WriteUInt16(f,col.Get()); - }break; - case 3: - case 11: - case 0x12: - case 0x1b:{ - WriteUInt32(f,col.Get()); - }break; - case 5:{ - WriteSingle(f,col.Get()); - }break; - case 9: - case 0x18:{ - WriteString(f,col.Get(),columns[colNum].length); - }break; - case 13: - case 0x15:{ - WriteInt16(f,col.Get()); - }break; - case 20:{ - WriteSByte(f,col.Get()); - }break; - case 0x16:{ - WriteInt32(f,col.Get()); - }break; - case 0x1a:{ - WriteString(f,col.Get(),-1); - }break; - } - colNum++; - } - std::streampos offset = f.tellp() - marker; - std::streampos newMarker = f.tellp(); - f.seekp(marker); - WriteUInt16(f,uint16_t(offset)); - f.seekp(newMarker); - } - f.close(); - std::ifstream finishedFile(filename,std::ios::binary); - data=ReadBytes(finishedFile); - std::ofstream encryptedFile(filename,std::ios::binary); - std::cout<<"Encrypting..."< +#include +#include +#include +#include +#include + +typedef std::byte byte; +typedef char sbyte; + +class SHNFile{ + std::vectorReadBytes(std::ifstream&file); + std::vectorReadBytes(std::ifstream&file,int bytes); + void WriteBytes(std::ofstream&file,std::vector&data); + int ReadInt32(std::ifstream&file); + void Encrypt(); + void Decrypt(); + std::vectorReadBytes(int bytes); + byte ReadByte(); + void WriteByte(std::ofstream&f,byte b); + void WriteSByte(std::ofstream&f,sbyte b); + uint16_t ReadUInt16(); + void WriteUInt16(std::ofstream&f,uint16_t val); + int16_t ReadInt16(); + void WriteInt16(std::ofstream&f,int16_t val); + uint32_t ReadUInt32(); + void WriteUInt32(std::ofstream&f,uint32_t val); + int Read(); + int32_t ReadInt32(); + void WriteInt32(std::ofstream&f,int32_t val); + std::string ReadString(int bytes); + void WriteString(std::ofstream&f,std::string str,int bytes); + float ReadSingle(); + void WriteSingle(std::ofstream&f,float n); + sbyte ReadSByte(); + std::string ReadString(); + uint32_t GetRecordLength(); +public: + struct Data{ + std::shared_ptrdata; + int type=0; + Data(byte b); + Data(sbyte b); + Data(uint16_t n); + Data(int16_t n); + Data(uint32_t n); + Data(int32_t n); + Data(float n); + Data(std::string str); + template + T Get(); + template + void Set(T b); + std::string GetDisplayText(); + }; +private: + friend std::ostream&operator<<(std::ostream&out,SHNFile::Data&d){ + out<cryptHeader; + std::vectordata,rawData; + uint32_t header=0,recordCount=0,defaultRecordLength=0,columnCount=0; + std::vectorcolumns; + std::vector>contents; + std::string filename; +public: + void Load(std::string file); + void Save(); + SHNFile(); +}; +#ifdef OLC_PGEX_SHNFile +std::vectorSHNFile::ReadBytes(std::ifstream&file){ + std::vectorbyteArr; + while(!file.eof()){ + byteArr.push_back(byte(file.get())); + } + return byteArr; +} +std::vectorSHNFile::ReadBytes(std::ifstream&file,int bytes){ + std::vectorbyteArr; + for(int i=0;i&data){ + for(int i=0;iintBytes=ReadBytes(file,4); + int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]); + return numb; +} +void SHNFile::Encrypt(){ + Decrypt(); +} +void SHNFile::Decrypt(){ + byte num = byte(data.size()); + for(int i=data.size()-1;i>=0;i--){ + data[i] = byte(data[i]^num); + byte num3 = byte(i); + num3 = byte(num3&byte(15)); + num3 = byte(int(num3)+0x55); + num3 = byte(num3 ^ (byte((int(byte(i))*11)))); + num3 = byte(num3^num); + num3 = byte(int(num3)^170); + num = num3; + } +} +std::vectorSHNFile::ReadBytes(int bytes){ + std::vectorbyteArr={}; + for(int i=0;ib=ReadBytes(1); + if(b.size()>0){ + return b[0]; + } else { + return byte(0); + } +} +void SHNFile::WriteByte(std::ofstream&f,byte b){ + f<intBytes=ReadBytes(2); + uint16_t numb = uint16_t(intBytes[1])<<8|uint16_t(intBytes[0]); + return numb; +} +void SHNFile::WriteUInt16(std::ofstream&f,uint16_t val){ + f<>8)&0xFF); +} +int16_t SHNFile::ReadInt16(){ + std::vectorintBytes=ReadBytes(2); + int16_t numb = int16_t(intBytes[1])<<8|int16_t(intBytes[0]); + return numb; +} +void SHNFile::WriteInt16(std::ofstream&f,int16_t val){ + f<>8)&0xFF); +} +uint32_t SHNFile::ReadUInt32(){ + std::vectorintBytes=ReadBytes(4); + uint32_t numb = uint32_t(intBytes[3])<<24|uint32_t(intBytes[2])<<16|uint32_t(intBytes[1])<<8|uint32_t(intBytes[0]); + return numb; +} +void SHNFile::WriteUInt32(std::ofstream&f,uint32_t val){ + f<>8)&0xFF)<>16)&0xFF)<>24)&0xFF); +} +int SHNFile::Read(){ + std::vectorintBytes=ReadBytes(4); + int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]); + return numb; +} +int32_t SHNFile::ReadInt32(){ + std::vectorintBytes=ReadBytes(4); + int32_t numb = int32_t(intBytes[3])<<24|int32_t(intBytes[2])<<16|int32_t(intBytes[1])<<8|uint32_t(intBytes[0]); + return numb; +} +void SHNFile::WriteInt32(std::ofstream&f,int32_t val){ + f<>8)&0xFF)<>16)&0xFF)<>24)&0xFF); +} +std::string SHNFile::ReadString(int bytes){ + std::vectorstrBytes=ReadBytes(bytes); + std::string str=""; + for(int i=0;istrBytes=ReadBytes(4); + byte bytes[]={strBytes[0],strBytes[1],strBytes[2],strBytes[3]}; + float f; + memcpy(&f,&bytes,4); + return f; +} +void SHNFile::WriteSingle(std::ofstream&f,float n){ + byte bytes[4]; + memcpy(&n,&bytes,4); + for(int i=0;i<4;i++){ + f<byteArr=ReadBytes(1); + if(byteArr.size()>0&&byteArr[0]!=byte(0)){ + str+=unsigned char(byteArr[0]); + }else{ + break; + } + } + return str; +} +uint32_t SHNFile::GetRecordLength(){ + uint32_t start=2; + for(Column&col:columns){ + start+=uint32_t(col.length); + } + return start; +} +SHNFile::Data::Data(byte b){ + std::shared_ptrptr=std::make_shared(byte(b)); + data=ptr; + type=1; +} +SHNFile::Data::Data(sbyte b){ + std::shared_ptrptr=std::make_shared(sbyte(b)); + data=ptr; + type=7; +} +SHNFile::Data::Data(uint16_t n){ + std::shared_ptrptr=std::make_shared(uint16_t(n)); + data=ptr; + type=2; +} +SHNFile::Data::Data(int16_t n){ + std::shared_ptrptr=std::make_shared(int16_t(n)); + data=ptr; + type=6; +} +SHNFile::Data::Data(uint32_t n){ + std::shared_ptrptr=std::make_shared(uint32_t(n)); + data=ptr; + type=3; +} +SHNFile::Data::Data(int32_t n){ + std::shared_ptrptr=std::make_shared(int32_t(n)); + data=ptr; + type=8; +} +SHNFile::Data::Data(float n){ + std::shared_ptrptr=std::make_shared(float(n)); + data=ptr; + type=4; +} +SHNFile::Data::Data(std::string str){ + std::shared_ptrptr=std::make_shared(str); + data=ptr; + type=5; +} +template +T SHNFile::Data::Get(){ + return *std::static_pointer_cast(data); +} +template +void SHNFile::Data::Set(T b){ + data=std::make_shared(T(b)); +} +std::string SHNFile::Data::GetDisplayText(){ + switch(type){ + case 1:{ + return std::to_string(int(Get())); + }break; + case 7:{ + return std::to_string(int(Get())); + }break; + case 2:{ + return std::to_string(Get()); + }break; + case 6:{ + return std::to_string(Get()); + }break; + case 3:{ + return std::to_string(Get()); + }break; + case 8:{ + return std::to_string(Get()); + }break; + case 4:{ + return std::to_string(Get()); + }break; + case 5:{ + return Get(); + }break; + } +} +void SHNFile::Load(std::string file){ + cryptHeader.clear(); + data.clear(); + header=recordCount=defaultRecordLength=columnCount=0; + columns.clear(); + contents.clear(); + rawData.clear(); + marker=0; + std::chrono::time_pointtimer=std::chrono::high_resolution_clock::now(); + filename=file; + + //FILE OPERATIONS! + std::ifstream f(filename,std::ios::binary); + //Since we don't just read in the entire file raw, we have to do some additional work here as the header provides us with some basic info on how to decrypt this file. + //The backup itself needs all the data from the original file, so we're appending it to rawData as we continue reading it. + cryptHeader=ReadBytes(f,0x20); + std::copy(cryptHeader.begin(),cryptHeader.end(),std::back_inserter(rawData)); + int readAmt=ReadInt32(f); + rawData.push_back(byte(readAmt&0xFF)); + rawData.push_back(byte((readAmt>>8)&0xFF)); + rawData.push_back(byte((readAmt>>16)&0xFF)); + rawData.push_back(byte((readAmt>>24)&0xFF)); + data=ReadBytes(f,readAmt-0x24); + std::copy(data.begin(),data.end(),std::back_inserter(rawData)); + + Decrypt(); + header=ReadUInt32(); + recordCount=ReadUInt32(); + defaultRecordLength=ReadUInt32(); + columnCount=ReadUInt32(); + + int num2=2; + for(int i=0;irow; + for(int j=0;jdur=std::chrono::high_resolution_clock::now()-timer; + std::cout<<"Loaded "<&row:contents){ + std::streampos marker = f.tellp(); + WriteUInt16(f,uint16_t(0)); + int colNum=0; + for(Data&col:row){ + switch(columns[colNum].type){ + case 1: + case 12: + case 0x10:{ + WriteByte(f,col.Get()); + }break; + case 2:{ + WriteUInt16(f,col.Get()); + }break; + case 3: + case 11: + case 0x12: + case 0x1b:{ + WriteUInt32(f,col.Get()); + }break; + case 5:{ + WriteSingle(f,col.Get()); + }break; + case 9: + case 0x18:{ + WriteString(f,col.Get(),columns[colNum].length); + }break; + case 13: + case 0x15:{ + WriteInt16(f,col.Get()); + }break; + case 20:{ + WriteSByte(f,col.Get()); + }break; + case 0x16:{ + WriteInt32(f,col.Get()); + }break; + case 0x1a:{ + WriteString(f,col.Get(),-1); + }break; + } + colNum++; + } + std::streampos offset = f.tellp() - marker; + std::streampos newMarker = f.tellp(); + f.seekp(marker); + WriteUInt16(f,uint16_t(offset)); + f.seekp(newMarker); + } + f.close(); + std::ifstream finishedFile(filename,std::ios::binary); + data=ReadBytes(finishedFile); + std::ofstream encryptedFile(filename,std::ios::binary); + std::cout<<"Encrypting..."< - + diff --git a/SHNFileDecryptor/SHNFileDecryptor.vcxproj.filters b/SHNFileDecryptor/SHNFileDecryptor.vcxproj.filters index 1529261..3ff10b4 100644 --- a/SHNFileDecryptor/SHNFileDecryptor.vcxproj.filters +++ b/SHNFileDecryptor/SHNFileDecryptor.vcxproj.filters @@ -15,7 +15,7 @@ - + Source Files diff --git a/SHNFileDecryptor/SHNFile_example.cpp b/SHNFileDecryptor/SHNFile_example.cpp new file mode 100644 index 0000000..232d3eb --- /dev/null +++ b/SHNFileDecryptor/SHNFile_example.cpp @@ -0,0 +1,8 @@ +#define OLC_PGEX_SHNFile +#include "SHNFileDecryptor.h" + +int main(){ + SHNFile shn; + shn.Load("AbState.shn"); + shn.Save(); +} \ No newline at end of file