diff options
author | Max Horn | 2007-02-14 21:43:21 +0000 |
---|---|---|
committer | Max Horn | 2007-02-14 21:43:21 +0000 |
commit | 80f799a2b25a5ff353dd572c5aeeb469688102c2 (patch) | |
tree | fdd92f2e9d7213f1c52d76f4f779a012205cf2ec /common | |
parent | 8bd1bd53b9ef0b16367a9fe8a986a5ffef17dfe3 (diff) | |
download | scummvm-rg350-80f799a2b25a5ff353dd572c5aeeb469688102c2.tar.gz scummvm-rg350-80f799a2b25a5ff353dd572c5aeeb469688102c2.tar.bz2 scummvm-rg350-80f799a2b25a5ff353dd572c5aeeb469688102c2.zip |
Extended MD5 API a bit: added a variant of md5_file which takes an arbitrary ReadStream; and added md5_file_string methods which directly produce a human readable md5 string (instead of a binary digest)
svn-id: r25592
Diffstat (limited to 'common')
-rw-r--r-- | common/md5.cpp | 57 | ||||
-rw-r--r-- | common/md5.h | 20 |
2 files changed, 65 insertions, 12 deletions
diff --git a/common/md5.cpp b/common/md5.cpp index 287e14107a..1296fe08b9 100644 --- a/common/md5.cpp +++ b/common/md5.cpp @@ -34,6 +34,17 @@ namespace Common { +typedef struct { + uint32 total[2]; + uint32 state[4]; + uint8 buffer[64]; +} md5_context; + +void md5_starts(md5_context *ctx); +void md5_update(md5_context *ctx, const uint8 *input, uint32 length); +void md5_finish(md5_context *ctx, uint8 digest[16]); + + #define GET_UINT32(n, b, i) (n) = READ_LE_UINT32(b + i) #define PUT_UINT32(n, b, i) WRITE_LE_UINT32(b + i, n) @@ -253,6 +264,12 @@ bool md5_file(const char *name, uint8 digest[16], uint32 length) { warning("md5_file couldn't open '%s'", name); return false; } + + return md5_file(f, digest, length); +} + + +bool md5_file(ReadStream &stream, uint8 digest[16], uint32 length) { #ifdef DISABLE_MD5 memset(digest, 0, 16); @@ -261,7 +278,7 @@ bool md5_file(const char *name, uint8 digest[16], uint32 length) { int i; unsigned char buf[1000]; bool restricted = (length != 0); - int readlen; + uint32 readlen; if (!restricted || sizeof(buf) <= length) readlen = sizeof(buf); @@ -270,7 +287,7 @@ bool md5_file(const char *name, uint8 digest[16], uint32 length) { md5_starts(&ctx); - while ((i = f.read(buf, readlen)) > 0) { + while ((i = stream.read(buf, readlen)) > 0) { md5_update(&ctx, buf, i); length -= i; @@ -286,6 +303,42 @@ bool md5_file(const char *name, uint8 digest[16], uint32 length) { return true; } +bool md5_file_string(const FilesystemNode &file, char md5str[32+1], uint32 length) { + uint8 digest[16]; + if (!md5_file(file, digest, length)) + return false; + + for (int i = 0; i < 16; i++) { + sprintf(md5str + i*2, "%02x", (int)digest[i]); + } + + return true; +} + +bool md5_file_string(const char *name, char md5str[32+1], uint32 length) { + uint8 digest[16]; + if (!md5_file(name, digest, length)) + return false; + + for (int i = 0; i < 16; i++) { + sprintf(md5str + i*2, "%02x", (int)digest[i]); + } + + return true; +} + +bool md5_file_string(ReadStream &stream, char md5str[32+1], uint32 length) { + uint8 digest[16]; + if (!md5_file(stream, digest, length)) + return false; + + for (int i = 0; i < 16; i++) { + sprintf(md5str + i*2, "%02x", (int)digest[i]); + } + + return true; +} + } // End of namespace Common #ifdef TEST diff --git a/common/md5.h b/common/md5.h index b4b852b5c6..6700ff355f 100644 --- a/common/md5.h +++ b/common/md5.h @@ -24,21 +24,21 @@ #include "common/scummsys.h" #include "common/fs.h" +#include "common/stream.h" namespace Common { -typedef struct { - uint32 total[2]; - uint32 state[4]; - uint8 buffer[64]; -} md5_context; - -void md5_starts(md5_context *ctx); -void md5_update(md5_context *ctx, const uint8 *input, uint32 length); -void md5_finish(md5_context *ctx, uint8 digest[16]); - bool md5_file(const char *name, uint8 digest[16], uint32 length = 0); bool md5_file(const FilesystemNode &file, uint8 digest[16], uint32 length = 0); +bool md5_file(ReadStream &stream, uint8 digest[16], uint32 length = 0); + +// The following two methods work similar to the above two, but +// instead of computing the binary MD5 digest, they produce +// a human readable lowercase hexstring representing the digest. +bool md5_file_string(const char *name, char md5str[32+1], uint32 length = 0); +bool md5_file_string(const FilesystemNode &file, char md5str[32+1], uint32 length = 0); +bool md5_file_string(ReadStream &stream, char md5str[32+1], uint32 length = 0); + } // End of namespace Common |