Browse Source

Allow computing md5 hashes from hsStream derived classes

Adam Johnson 13 years ago
parent
commit
3508df1c16
  1. 36
      Sources/Plasma/PubUtilLib/plEncryption/plChecksum.cpp
  2. 4
      Sources/Plasma/PubUtilLib/plEncryption/plChecksum.h

36
Sources/Plasma/PubUtilLib/plEncryption/plChecksum.cpp

@ -91,31 +91,43 @@ plMD5Checksum::plMD5Checksum( const char *fileName )
CalcFromFile( fileName ); CalcFromFile( fileName );
} }
plMD5Checksum::plMD5Checksum( hsStream* stream )
{
CalcFromStream(stream);
}
void plMD5Checksum::Clear() void plMD5Checksum::Clear()
{ {
memset( fChecksum, 0, sizeof( fChecksum ) ); memset( fChecksum, 0, sizeof( fChecksum ) );
fValid = false; fValid = false;
} }
void plMD5Checksum::CalcFromFile( const char *fileName) void plMD5Checksum::CalcFromFile( const char *fileName )
{ {
FILE *fp; hsUNIXStream s;
fValid = false; fValid = false;
if( fp = fopen(fileName, "rb" ) ) if( s.Open(fileName) )
{ {
unsigned loadLen = 1024 * 1024; CalcFromStream(&s);
Start(); 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)) while(int read = stream->Read(loadLen, buf))
AddTo( read, buf ); AddTo( read, buf );
delete[] buf; delete[] buf;
Finish(); Finish();
fclose(fp); stream->SetPosition(sPos);
}
} }
void plMD5Checksum::Start( void ) void plMD5Checksum::Start( void )

4
Sources/Plasma/PubUtilLib/plEncryption/plChecksum.h

@ -58,6 +58,8 @@ public:
SumStorage GetChecksum() { return fSum; } SumStorage GetChecksum() { return fSum; }
}; };
class hsStream;
class plMD5Checksum class plMD5Checksum
{ {
protected: protected:
@ -74,11 +76,13 @@ class plMD5Checksum
plMD5Checksum(); plMD5Checksum();
plMD5Checksum( const plMD5Checksum &rhs ); plMD5Checksum( const plMD5Checksum &rhs );
plMD5Checksum( const char *fileName ); plMD5Checksum( const char *fileName );
plMD5Checksum( hsStream* stream );
hsBool IsValid( void ) const { return fValid; } hsBool IsValid( void ) const { return fValid; }
void Clear(); void Clear();
void CalcFromFile( const char *fileName ); void CalcFromFile( const char *fileName );
void CalcFromStream( hsStream* stream );
void Start( void ); void Start( void );
void AddTo( UInt32 size, const UInt8 *buffer ); void AddTo( UInt32 size, const UInt8 *buffer );

Loading…
Cancel
Save