I have added md5sum() function to the standard extension. It returns the 
md5 hash of given filename.

I think this is useful. Can you merge it in the main distribution?

-- 
Alessandro 'Asterix75' Astarita <[EMAIL PROTECTED]>
Web site: http://asterix75.capri.it
E-commerce software: http://www.alfacomm.it/
diff -urN php4-200111121500.orig/ext/standard/basic_functions.c php4-200111121500/ext/standard/basic_functions.c
--- php4-200111121500.orig/ext/standard/basic_functions.c	Thu Nov 15 15:59:16 2001
+++ php4-200111121500/ext/standard/basic_functions.c	Thu Nov 15 16:00:38 2001
@@ -282,6 +282,7 @@
 	PHP_FE(htmlentities,													NULL)
 	PHP_FE(get_html_translation_table,										NULL)
 	PHP_NAMED_FE(md5,php_if_md5,											NULL)
+	PHP_NAMED_FE(md5sum,php_if_md5sum,										NULL)
 	PHP_NAMED_FE(crc32,php_if_crc32,										NULL)
 
 	PHP_FE(iptcparse,														NULL)															
diff -urN php4-200111121500.orig/ext/standard/md5.c php4-200111121500/ext/standard/md5.c
--- php4-200111121500.orig/ext/standard/md5.c	Thu Nov 15 15:59:17 2001
+++ php4-200111121500/ext/standard/md5.c	Thu Nov 15 16:00:39 2001
@@ -20,6 +20,7 @@
 
 /* 
  * md5.c - Copyright 1997 Lachlan Roche 
+ * md5sum() added by Alessandro Astarita <[EMAIL PROTECTED]>
  */
 
 #include <stdio.h>
@@ -35,8 +36,6 @@
 	char md5str[33];
 	PHP_MD5_CTX context;
 	unsigned char digest[16];
-	int i;
-	char *r;
 	
 	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
 		WRONG_PARAM_COUNT;
@@ -47,13 +46,74 @@
 	PHP_MD5Init(&context);
 	PHP_MD5Update(&context, Z_STRVAL_PP(arg), Z_STRLEN_PP(arg));
 	PHP_MD5Final(digest, &context);
+	make_digest(md5str, digest);
+	RETVAL_STRING(md5str, 1);
+}
+/* }}} */
+
+/* {{{ proto string md5sum(string filename)
+   Calculate the md5 hash of given filename */
+PHP_NAMED_FUNCTION(php_if_md5sum)
+{
+	zval **arg;
+	char md5str[33];
+	unsigned char buf[1024];
+	unsigned char digest[16];
+	PHP_MD5_CTX context;
+	int n;
+	FILE *fp;
+
+	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
+		WRONG_PARAM_COUNT;
+	}
+
+	convert_to_string_ex(arg);
+
+	if (PG(safe_mode) && (!php_checkuid(Z_STRVAL_PP(arg), NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
+		RETURN_FALSE;
+	}
+
+	if (php_check_open_basedir(Z_STRVAL_PP(arg) TSRMLS_CC)) {
+		RETURN_FALSE;
+	}
+
+	if ((fp = VCWD_FOPEN(Z_STRVAL_PP(arg), "rb")) == NULL) {
+		php_error(E_WARNING, "md5sum(): Unable to open file");
+		RETURN_FALSE;
+	}
+
+	PHP_MD5Init(&context);
+
+	while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
+		PHP_MD5Update(&context, buf, n);
+	}
+
+	PHP_MD5Final(digest, &context);
+
+	if (ferror(fp)) {
+		fclose(fp);
+		RETURN_FALSE;
+	}
+
+	fclose(fp);
+
+	make_digest(md5str, digest);
+
+	RETVAL_STRING(md5str, 1);
+}
+/* }}} */
+
+static void make_digest(char *md5str, unsigned char *digest)
+{
+	int i;
+	char *r;
+
 	for (i = 0, r = md5str; i < 16; i++, r += 2) {
 		sprintf(r, "%02x", digest[i]);
 	}
+
 	*r = '\0';
-	RETVAL_STRING(md5str, 1);
 }
-/* }}} */
 
 /*
  * The remaining code is the reference MD5 code (md5c.c) from rfc1321
diff -urN php4-200111121500.orig/ext/standard/md5.h php4-200111121500/ext/standard/md5.h
--- php4-200111121500.orig/ext/standard/md5.h	Thu Nov 15 15:59:17 2001
+++ php4-200111121500/ext/standard/md5.h	Thu Nov 15 16:00:41 2001
@@ -57,7 +57,9 @@
 void PHP_MD5Init(PHP_MD5_CTX *);
 void PHP_MD5Update(PHP_MD5_CTX *, const unsigned char *, unsigned int);
 void PHP_MD5Final(unsigned char[16], PHP_MD5_CTX *);
+static void make_digest(char *md5str, unsigned char *digest);
 
 PHP_NAMED_FUNCTION(php_if_md5);
+PHP_NAMED_FUNCTION(php_if_md5sum);
 
 #endif

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to