[Scummvm-cvs-logs] SF.net SVN: scummvm: [25592] scummvm/trunk/common

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Wed Feb 14 22:43:21 CET 2007


Revision: 25592
          http://scummvm.svn.sourceforge.net/scummvm/?rev=25592&view=rev
Author:   fingolfin
Date:     2007-02-14 13:43:21 -0800 (Wed, 14 Feb 2007)

Log Message:
-----------
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)

Modified Paths:
--------------
    scummvm/trunk/common/md5.cpp
    scummvm/trunk/common/md5.h

Modified: scummvm/trunk/common/md5.cpp
===================================================================
--- scummvm/trunk/common/md5.cpp	2007-02-14 21:17:01 UTC (rev 25591)
+++ scummvm/trunk/common/md5.cpp	2007-02-14 21:43:21 UTC (rev 25592)
@@ -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,7 +264,13 @@
 		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);
 #else
@@ -261,7 +278,7 @@
 	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 @@
 
 	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 @@
 	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

Modified: scummvm/trunk/common/md5.h
===================================================================
--- scummvm/trunk/common/md5.h	2007-02-14 21:17:01 UTC (rev 25591)
+++ scummvm/trunk/common/md5.h	2007-02-14 21:43:21 UTC (rev 25592)
@@ -24,22 +24,22 @@
 
 #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
 
 #endif


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list