From e86e5c8bddd004a7d2b3238a7120eb7abbff228c Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Fri, 4 Oct 2019 11:07:44 -0400 Subject: [PATCH] Allow computing md5 hashes from hsStream derived classes (cherry picked from commit e43cb685365bfc4498b681168bab0b1357ae208c) --- .../PubUtilLib/plEncryption/plChecksum.cpp | 36 ++++++++++++------- .../PubUtilLib/plEncryption/plChecksum.h | 4 +++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/PubUtilLib/plEncryption/plChecksum.cpp b/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/PubUtilLib/plEncryption/plChecksum.cpp index e9d21798..16a53a49 100644 --- a/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/PubUtilLib/plEncryption/plChecksum.cpp +++ b/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/PubUtilLib/plEncryption/plChecksum.cpp @@ -90,31 +90,43 @@ plMD5Checksum::plMD5Checksum( const char *fileName ) CalcFromFile( fileName ); } +plMD5Checksum::plMD5Checksum( hsStream* stream ) +{ + CalcFromStream(stream); +} + void plMD5Checksum::Clear() { memset( fChecksum, 0, sizeof( fChecksum ) ); fValid = false; } -void plMD5Checksum::CalcFromFile( const char *fileName) +void plMD5Checksum::CalcFromFile( const char *fileName ) { - FILE *fp; + hsUNIXStream s; fValid = false; - if( fp = fopen(fileName, "rb" ) ) + if( s.Open(fileName) ) { - unsigned loadLen = 1024 * 1024; - Start(); + CalcFromStream(&s); + s.Close(); + } +} - UInt8 *buf = TRACKED_NEW UInt8[loadLen]; +void plMD5Checksum::CalcFromStream( hsStream* stream ) +{ + UInt32 sPos = stream->GetPosition(); + unsigned loadLen = 1024 * 1024; + Start(); + + UInt8 *buf = TRACKED_NEW UInt8[loadLen]; - while(int read = fread(buf, sizeof(UInt8), loadLen, fp)) - AddTo( read, buf ); - delete[] buf; + while(int read = stream->Read(loadLen, buf)) + AddTo( read, buf ); + delete[] buf; - Finish(); - fclose(fp); - } + Finish(); + stream->SetPosition(sPos); } void plMD5Checksum::Start( void ) diff --git a/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/PubUtilLib/plEncryption/plChecksum.h b/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/PubUtilLib/plEncryption/plChecksum.h index a6ce748f..a1f6213a 100644 --- a/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/PubUtilLib/plEncryption/plChecksum.h +++ b/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/PubUtilLib/plEncryption/plChecksum.h @@ -58,6 +58,8 @@ public: SumStorage GetChecksum() { return fSum; } }; +class hsStream; + class plMD5Checksum { protected: @@ -74,11 +76,13 @@ class plMD5Checksum plMD5Checksum(); plMD5Checksum( const plMD5Checksum &rhs ); plMD5Checksum( const char *fileName ); + plMD5Checksum( hsStream* stream ); hsBool IsValid( void ) const { return fValid; } void Clear(); void CalcFromFile( const char *fileName ); + void CalcFromStream( hsStream* stream ); void Start( void ); void AddTo( UInt32 size, const UInt8 *buffer );