php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #53709
Patch md5sha1_file.diff.base64 revision 2011-01-10 21:17 UTC by jthijssen at noxlogic dot nl
Patch md5sha1_file.diff revision 2011-01-10 21:14 UTC by jthijssen at noxlogic dot nl

Patch md5sha1_file.diff for hash related Bug #53709

Patch version 2011-01-10 21:14 UTC

Return to Bug #53709 | Download this patch
Patch Revisions:

Developer: jthijssen@noxlogic.nl

Index: ext/standard/sha1.c
===================================================================
--- ext/standard/sha1.c	(revision 307135)
+++ ext/standard/sha1.c	(working copy)
@@ -74,8 +74,9 @@
 	PHP_SHA1_CTX   context;
 	int           n;
 	php_stream    *stream;
+	long          length = -1;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl", &arg, &arg_len, &raw_output, &length) == FAILURE) {
 		return;
 	}
 	
@@ -87,7 +88,18 @@
 	PHP_SHA1Init(&context);
 
 	while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
-		PHP_SHA1Update(&context, buf, n);
+		if (length == -1) {
+			PHP_SHA1Update(&context, buf, n);
+		} else {
+			if (length < n) {
+				n = length;
+			}
+			PHP_SHA1Update(&context, buf, n);
+			length -= n;
+			if (length == 0) {
+				break;
+			}
+		}
 	}
 
 	PHP_SHA1Final(digest, &context);
Index: ext/standard/tests/strings/sha1_file.phpt
===================================================================
--- ext/standard/tests/strings/sha1_file.phpt	(revision 307135)
+++ ext/standard/tests/strings/sha1_file.phpt	(working copy)
@@ -6,16 +6,18 @@
 $path = dirname(__FILE__);
 $data_file = "$path/EmptyFile.txt";
 $data_file1 = "$path/DataFile.txt";
-if !(($fp = fopen($data_file, 'w')) || ($fp1 = fopen($data_file1, 'w'))  {
+$data_file2 = "$path/LargeDataFile.txt";
+if !(($fp = fopen($data_file, 'w')) || ($fp1 = fopen($data_file1, 'w') || ($fp2 = fopen($data_file2, 'w'))  {
   echo "File could not be created ,hence exiting from testcase due to pre-requisite failure\n";
 }
 fclose( $fp );
 fclose( $fp1 );
+fclose( $fp2 );
 
 --FILE--
 <?php
 
-/* Prototype: string sha1_file( string filename[, bool raw_output] )
+/* Prototype: string sha1_file( string filename[, bool raw_output] [,long length] )
  * Description: Calculate the sha1 hash of a file
  */
 
@@ -29,6 +31,11 @@
 if (($handle2 = fopen( "DataFile.txt", "w+")) == FALSE)
 return false;
 
+/* Creating a data file */
+if (($handle3 = fopen( "LargeDataFile.txt", "w+")) == FALSE)
+return false;
+
+
 /* Writing into file */ 
 $filename = "DataFile.txt";
 $content = b"Add this to the file\n";
@@ -39,9 +46,20 @@
   }
 }
 
+/* Writing into file */
+$filename = "LargeDataFile.txt";
+$content = str_repeat("1234567890", 10240);
+if (is_writable($filename)) {
+  if (fwrite($handle3, $content) === FALSE) {
+    echo "Cannot write to file ($filename)";
+    exit;
+  }
+}
+
 // close the files 
 fclose($handle);
 fclose($handle2);
+fclose($handle3);
 
 /* Testing error conditions */
 echo "\n*** Testing for error conditions ***\n";
@@ -61,8 +79,8 @@
 echo "\n-- Zero arguments --\n";
  var_dump ( sha1_file() );
 
-echo "\n-- More than valid number of arguments ( valid is 2) --\n";
-var_dump ( sha1_file("EmptyFile.txt", true, NULL) );
+echo "\n-- More than valid number of arguments ( valid is 3) --\n";
+var_dump ( sha1_file("EmptyFile.txt", true, 0, NULL) );
 
 echo "\n-- Hexadecimal Output for Empty file as Argument --\n";
 var_dump( sha1_file("EmptyFile.txt") );
@@ -70,18 +88,85 @@
 echo "\n-- Raw Binary Output for Empty file as Argument --\n";
 var_dump( bin2hex(sha1_file("EmptyFile.txt", true)));
 
+/* Normal operation with hexadecimal output */
 echo "\n-- Hexadecimal Output for a valid file with some contents --\n";
 var_dump( sha1_file("DataFile.txt") );
 
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a valid file with some contents and additional length --\n";
+var_dump ( sha1_file("DataFile.txt", false, 10) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a valid file with some contents and additional length --\n";
+var_dump ( sha1_file("DataFile.txt", false, 15) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a valid file with some contents and a too large length --\n";
+var_dump ( sha1_file("DataFile.txt", false, 10000000) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a valid file with some contents and zero length --\n";
+var_dump ( sha1_file("DataFile.txt", false, 0) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a valid file with some contents and default length --\n";
+var_dump ( sha1_file("DataFile.txt", false, -1) );
+
+/* Normal operation with raw binary output */
 echo "\n-- Raw Binary Output for a valid file with some contents --\n";
-var_dump ( bin2hex(sha1_file("DataFile.txt", true)));
+var_dump ( bin2hex(sha1_file("DataFile.txt", true)) );
 
+/* Normal operation with raw binary output for only a part of the file */
+echo "\n-- Raw Binary Output for a valid file with some contents and additional length --\n";
+var_dump ( bin2hex(sha1_file("DataFile.txt", true, 10)) );
+
+/* Normal operation with raw binary output for a larger part of the file */
+echo "\n-- Raw Binary Output for a valid file with some contents and additional length --\n";
+var_dump ( bin2hex(sha1_file("DataFile.txt", true, 15)) );
+
+/* Normal operation with raw binary output for a too large length*/
+echo "\n-- Raw Binary Output for a valid file with some contents and a too large length --\n";
+var_dump ( bin2hex(sha1_file("DataFile.txt", true, 10000000)) );
+
+/* Normal operation with raw binary output */
+echo "\n-- Raw Binary Output for a valid file with some contents and zero length --\n";
+var_dump ( bin2hex(sha1_file("DataFile.txt", true, 0)) );
+
+/* Normal operation with raw binary output */
+echo "\n-- Raw Binary Output for a valid file with some contents and default length --\n";
+var_dump ( bin2hex(sha1_file("DataFile.txt", true, -1)) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a large file with some contents --\n";
+var_dump ( sha1_file("LargeDataFile.txt") );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a large file with some contents and additional length --\n";
+var_dump ( sha1_file("LargeDataFile.txt", false, 10) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a large file with some contents and additional length --\n";
+var_dump ( sha1_file("LargeDataFile.txt", false, 10617) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a large file with some contents and a too large length --\n";
+var_dump ( sha1_file("LargeDataFile.txt", false, 10000000000) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a large file with some contents and zero length --\n";
+var_dump ( sha1_file("LargeDataFile.txt", false, 0) );
+
+/* Normal operation with hexadecimal output */
+echo "\n-- Hexadecimal Output for a large file with some contents and default length --\n";
+var_dump ( sha1_file("LargeDataFile.txt", false, -1) );
+
 // remove temp files
+unlink("LargeDataFile.txt");
 unlink("DataFile.txt");
 unlink("EmptyFile.txt");
 
+echo "\nDone";
 ?>
-===DONE===
 --EXPECTF--
 *** Testing sha1_file() : basic functionality ***
 
@@ -112,9 +197,9 @@
 Warning: sha1_file() expects at least 1 parameter, 0 given in %s on line %d
 NULL
 
--- More than valid number of arguments ( valid is 2) --
+-- More than valid number of arguments ( valid is 3) --
 
-Warning: sha1_file() expects at most 2 parameters, 3 given in %s on line %d
+Warning: sha1_file() expects at most 3 parameters, 4 given in %s on line %d
 NULL
 
 -- Hexadecimal Output for Empty file as Argument --
@@ -126,6 +211,55 @@
 -- Hexadecimal Output for a valid file with some contents --
 string(40) "d16a568ab98233deff7ec8b1668eb4b3d9e53fee"
 
+-- Hexadecimal Output for a valid file with some contents and additional length --
+string(40) "eb104785c3af873dab47c5556637f2cc58ac82fd"
+
+-- Hexadecimal Output for a valid file with some contents and additional length --
+string(40) "2b88af07a168a19c2e31979e280ca37108c4396d"
+
+-- Hexadecimal Output for a valid file with some contents and a too large length --
+string(40) "d16a568ab98233deff7ec8b1668eb4b3d9e53fee"
+
+-- Hexadecimal Output for a valid file with some contents and zero length --
+string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709"
+
+-- Hexadecimal Output for a valid file with some contents and default length --
+string(40) "d16a568ab98233deff7ec8b1668eb4b3d9e53fee"
+
 -- Raw Binary Output for a valid file with some contents --
 string(40) "d16a568ab98233deff7ec8b1668eb4b3d9e53fee"
-===DONE===
+
+-- Raw Binary Output for a valid file with some contents and additional length --
+string(40) "eb104785c3af873dab47c5556637f2cc58ac82fd"
+
+-- Raw Binary Output for a valid file with some contents and additional length --
+string(40) "2b88af07a168a19c2e31979e280ca37108c4396d"
+
+-- Raw Binary Output for a valid file with some contents and a too large length --
+string(40) "d16a568ab98233deff7ec8b1668eb4b3d9e53fee"
+
+-- Raw Binary Output for a valid file with some contents and zero length --
+string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709"
+
+-- Raw Binary Output for a valid file with some contents and default length --
+string(40) "d16a568ab98233deff7ec8b1668eb4b3d9e53fee"
+
+-- Hexadecimal Output for a large file with some contents --
+string(40) "1caa7198953f108765aafb82fe708d06d0902950"
+
+-- Hexadecimal Output for a large file with some contents and additional length --
+string(40) "01b307acba4f54f55aafc33bb06bbbf6ca803e9a"
+
+-- Hexadecimal Output for a large file with some contents and additional length --
+string(40) "88761b97a028026d4dae960cab0cbdf91a28ca63"
+
+-- Hexadecimal Output for a large file with some contents and a too large length --
+string(40) "1caa7198953f108765aafb82fe708d06d0902950"
+
+-- Hexadecimal Output for a large file with some contents and zero length --
+string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709"
+
+-- Hexadecimal Output for a large file with some contents and default length --
+string(40) "1caa7198953f108765aafb82fe708d06d0902950"
+
+Done
Index: ext/standard/md5.c
===================================================================
--- ext/standard/md5.c	(revision 307135)
+++ ext/standard/md5.c	(working copy)
@@ -71,7 +71,7 @@
 }
 /* }}} */
 
-/* {{{ proto string md5_file(string filename [, bool raw_output])
+/* {{{ proto string md5_file(string filename [, bool raw_output] [, long length])
    Calculate the md5 hash of given filename */
 PHP_NAMED_FUNCTION(php_if_md5_file)
 {
@@ -84,8 +84,9 @@
 	PHP_MD5_CTX   context;
 	int           n;
 	php_stream    *stream;
+	long          length = -1;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl", &arg, &arg_len, &raw_output, &length) == FAILURE) {
 		return;
 	}
 	
@@ -97,7 +98,18 @@
 	PHP_MD5Init(&context);
 
 	while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
-		PHP_MD5Update(&context, buf, n);
+		if (length == -1) {
+			PHP_MD5Update(&context, buf, n);
+		} else {
+			if (length < n) {
+				n = length;
+			}
+			PHP_MD5Update(&context, buf, n);
+			length -= n;
+			if (length == 0) {
+				break;
+			}
+		}
 	}
 
 	PHP_MD5Final(digest, &context);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 07:01:30 2024 UTC