@ -11,8 +11,11 @@
typedef char sbyte ;
typedef char sbyte ;
class SHNFile {
class SHNFile {
SHNFile ( const SHNFile & ) = delete ;
SHNFile ( const SHNFile & & ) = delete ;
std : : vector < std : : byte > ReadBytes ( std : : ifstream & file ) ;
std : : vector < std : : byte > ReadBytes ( std : : ifstream & file ) ;
std : : vector < std : : byte > ReadBytes ( std : : ifstream & file , int bytes ) ;
std : : vector < std : : byte > ReadBytes ( std : : ifstream & file , int bytes ) ;
void WriteBytes ( std : : ofstream & file , char * data , int length ) ;
void WriteBytes ( std : : ofstream & file , std : : vector < std : : byte > & data ) ;
void WriteBytes ( std : : ofstream & file , std : : vector < std : : byte > & data ) ;
int ReadInt32 ( std : : ifstream & file ) ;
int ReadInt32 ( std : : ifstream & file ) ;
void Encrypt ( ) ;
void Encrypt ( ) ;
@ -76,14 +79,15 @@ private:
int length = 0 ;
int length = 0 ;
} ;
} ;
int marker = 0 ;
int marker = 0 ;
std : : vector < std : : byte > cryptHeade r;
char * cryptHeader = nullpt r;
std : : vector < std : : byte > data , rawData ;
char * data = nullptr , * rawData = nullptr ;
uint32_t header = 0 , recordCount = 0 , defaultRecordLength = 0 , columnCount = 0 ;
uint32_t header = 0 , recordCount = 0 , defaultRecordLength = 0 , columnCount = 0 ;
std : : vector < Column > columns ;
std : : vector < Column > columns ;
std : : vector < std : : vector < Data > > contents ;
std : : vector < std : : vector < Data > > contents ;
std : : string filename ;
std : : string filename ;
std : : vector < std : : byte > readArr ;
std : : vector < std : : byte > readArr ;
std : : byte * fileMarker = 0 ;
int readAmt = 0 ;
void Cleanup ( ) ;
public :
public :
void Load ( std : : string file ) ;
void Load ( std : : string file ) ;
void Save ( ) ;
void Save ( ) ;
@ -97,6 +101,7 @@ public:
void Write ( int row , int col , std : : string val ) ;
void Write ( int row , int col , std : : string val ) ;
const SHNFile : : Data Get ( int row , int col ) const ;
const SHNFile : : Data Get ( int row , int col ) const ;
SHNFile ( ) ;
SHNFile ( ) ;
~ SHNFile ( ) ;
} ;
} ;
# ifdef OLC_PGEX_SHNFile
# ifdef OLC_PGEX_SHNFile
std : : vector < std : : byte > SHNFile : : ReadBytes ( std : : ifstream & file ) {
std : : vector < std : : byte > SHNFile : : ReadBytes ( std : : ifstream & file ) {
@ -117,6 +122,11 @@ std::vector<std::byte>SHNFile::ReadBytes(std::ifstream&file,int bytes){
}
}
return byteArr ;
return byteArr ;
}
}
void SHNFile : : WriteBytes ( std : : ofstream & file , char * data , int length ) {
for ( int i = 0 ; i < length ; i + + ) {
file < < unsigned char ( data [ i ] ) ;
}
}
void SHNFile : : WriteBytes ( std : : ofstream & file , std : : vector < std : : byte > & data ) {
void SHNFile : : WriteBytes ( std : : ofstream & file , std : : vector < std : : byte > & data ) {
for ( int i = 0 ; i < data . size ( ) ; i + + ) {
for ( int i = 0 ; i < data . size ( ) ; i + + ) {
file < < unsigned char ( data [ i ] ) ;
file < < unsigned char ( data [ i ] ) ;
@ -131,9 +141,9 @@ void SHNFile::Encrypt(){
Decrypt ( ) ;
Decrypt ( ) ;
}
}
void SHNFile : : Decrypt ( ) {
void SHNFile : : Decrypt ( ) {
std : : byte num = std : : byte ( data . size ( ) ) ;
std : : byte num = std : : byte ( readAmt - 0x24 ) ;
for ( int i = data . size ( ) - 1 ; i > = 0 ; i - - ) {
for ( int i = readAmt - 0x24 - 1 ; i > = 0 ; i - - ) {
data [ i ] = std : : byte ( data [ i ] ^ num ) ;
data [ i ] = char ( std : : byte ( data [ i ] ) ^ num ) ;
std : : byte num3 = std : : byte ( i ) ;
std : : byte num3 = std : : byte ( i ) ;
num3 = std : : byte ( num3 & std : : byte ( 15 ) ) ;
num3 = std : : byte ( num3 & std : : byte ( 15 ) ) ;
num3 = std : : byte ( int ( num3 ) + 0x55 ) ;
num3 = std : : byte ( int ( num3 ) + 0x55 ) ;
@ -145,7 +155,9 @@ void SHNFile::Decrypt(){
}
}
std : : vector < std : : byte > & SHNFile : : ReadBytes ( int bytes ) {
std : : vector < std : : byte > & SHNFile : : ReadBytes ( int bytes ) {
readArr . clear ( ) ;
readArr . clear ( ) ;
std : : copy ( data . begin ( ) + marker , data . begin ( ) + marker + bytes , std : : back_inserter ( readArr ) ) ;
for ( int i = 0 ; i < bytes ; i + + ) {
readArr . push_back ( std : : byte ( * ( data + i + marker ) ) ) ;
}
marker + = bytes ;
marker + = bytes ;
return readArr ;
return readArr ;
}
}
@ -339,23 +351,29 @@ void SHNFile::Load(std::string file){
header = recordCount = defaultRecordLength = columnCount = 0 ;
header = recordCount = defaultRecordLength = columnCount = 0 ;
columns . clear ( ) ;
columns . clear ( ) ;
contents . clear ( ) ;
contents . clear ( ) ;
rawData . clear ( ) ;
marker = 0 ;
marker = 0 ;
std : : chrono : : time_point < std : : chrono : : high_resolution_clock > timer = std : : chrono : : high_resolution_clock : : now ( ) ;
std : : chrono : : time_point < std : : chrono : : high_resolution_clock > timer = std : : chrono : : high_resolution_clock : : now ( ) ;
filename = file ;
filename = file ;
Cleanup ( ) ;
//FILE OPERATIONS!
//FILE OPERATIONS!
std : : ifstream f ( filename , std : : ios : : binary ) ;
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.
//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.
//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 ) ;
int readAmt = ReadInt32 ( f ) ;
cryptHeader = new char [ 0x20 ] ;
rawData . push_back ( std : : byte ( readAmt & 0xFF ) ) ;
f . read ( cryptHeader , 0x20 ) ;
rawData . push_back ( std : : byte ( ( readAmt > > 8 ) & 0xFF ) ) ;
readAmt = ReadInt32 ( f ) ;
rawData . push_back ( std : : byte ( ( readAmt > > 16 ) & 0xFF ) ) ;
rawData = new char [ readAmt - 0x20 ] ;
rawData . push_back ( std : : byte ( ( readAmt > > 24 ) & 0xFF ) ) ;
rawData [ 0 ] = char ( readAmt & 0xFF ) ;
data = ReadBytes ( f , readAmt - 0x24 ) ;
rawData [ 1 ] = char ( ( readAmt > > 8 ) & 0xFF ) ;
std : : copy ( data . begin ( ) , data . end ( ) , std : : back_inserter ( rawData ) ) ;
rawData [ 2 ] = char ( ( readAmt > > 16 ) & 0xFF ) ;
rawData [ 3 ] = char ( ( readAmt > > 24 ) & 0xFF ) ;
data = new char [ readAmt - 0x24 ] ;
f . read ( data , readAmt - 0x24 ) ;
for ( int i = 0 ; i < readAmt - 0x24 ; i + + ) {
rawData [ i + 4 ] = data [ i ] ;
}
Decrypt ( ) ;
Decrypt ( ) ;
header = ReadUInt32 ( ) ;
header = ReadUInt32 ( ) ;
@ -422,7 +440,10 @@ void SHNFile::Load(std::string file){
void SHNFile : : Save ( ) {
void SHNFile : : Save ( ) {
std : : ofstream fBackup ( filename + " .bak " , std : : ios : : binary ) ;
std : : ofstream fBackup ( filename + " .bak " , std : : ios : : binary ) ;
std : : cout < < " Saving a backup to " < < filename + " .bak " < < std : : endl ;
std : : cout < < " Saving a backup to " < < filename + " .bak " < < std : : endl ;
for ( int i = 0 ; i < rawData . size ( ) ; i + + ) {
for ( int i = 0 ; i < 0x20 ; i + + ) {
fBackup < < unsigned char ( cryptHeader [ i ] ) ;
}
for ( int i = 0 ; i < readAmt - 0x20 ; i + + ) {
fBackup < < unsigned char ( rawData [ i ] ) ;
fBackup < < unsigned char ( rawData [ i ] ) ;
}
}
/*//Decoded version only required for debugging.
/*//Decoded version only required for debugging.
@ -493,13 +514,13 @@ void SHNFile::Save(){
}
}
f . close ( ) ;
f . close ( ) ;
std : : ifstream finishedFile ( filename , std : : ios : : binary ) ;
std : : ifstream finishedFile ( filename , std : : ios : : binary ) ;
data = ReadBytes ( finishedFile ) ;
finishedFile . read ( data , readAmt - 0x24 ) ;
std : : ofstream encryptedFile ( filename , std : : ios : : binary ) ;
std : : ofstream encryptedFile ( filename , std : : ios : : binary ) ;
std : : cout < < " Encrypting... " < < std : : endl ;
std : : cout < < " Encrypting... " < < std : : endl ;
Encrypt ( ) ;
Encrypt ( ) ;
WriteBytes ( encryptedFile , cryptHeader ) ;
WriteBytes ( encryptedFile , cryptHeader , 0x20 ) ;
WriteInt32 ( encryptedFile , int32_t ( data . size ( ) + 0x24 ) ) ;
WriteInt32 ( encryptedFile , int32_t ( readAmt ) ) ;
WriteBytes ( encryptedFile , data ) ;
WriteBytes ( encryptedFile , data , readAmt - 0x24 ) ;
std : : cout < < " File " < < filename < < " Saved! " < < std : : endl ;
std : : cout < < " File " < < filename < < " Saved! " < < std : : endl ;
}
}
const SHNFile : : Data SHNFile : : Get ( int row , int col ) const {
const SHNFile : : Data SHNFile : : Get ( int row , int col ) const {
@ -532,4 +553,21 @@ void SHNFile::Write(int row,int col,std::string val){
SHNFile : : SHNFile ( ) {
SHNFile : : SHNFile ( ) {
readArr . reserve ( 64000 ) ;
readArr . reserve ( 64000 ) ;
}
}
SHNFile : : ~ SHNFile ( ) {
Cleanup ( ) ;
}
void SHNFile : : Cleanup ( ) {
if ( cryptHeader ! = nullptr ) {
delete [ ] cryptHeader ;
cryptHeader = nullptr ;
}
if ( data ! = nullptr ) {
delete [ ] data ;
data = nullptr ;
}
if ( rawData ! = nullptr ) {
delete [ ] rawData ;
rawData = nullptr ;
}
}
# endif
# endif