From: Uwe Schindler Date: Sat, 16 Apr 2005 09:49:53 +0000 (+0000) Subject: MFH: use streams api for md5_file() and sha1_file() X-Git-Tag: php-4.4.0RC1~124 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bd36484d85ea341f99541785fcfe01db79db5940;p=php MFH: use streams api for md5_file() and sha1_file() --- diff --git a/NEWS b/NEWS index b072cae931..dae06e58cd 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ PHP 4 NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 20??, Version 4.?.? +- Changed sha1_file() / md5_file() to use streams instead of low level IO (Uwe) - Added the sorting flag SORT_LOCALE_STRING to the sort() functions which makes them sort based on the current locale. (Derick) - Fixed bug #32699 (pg_affected_rows() was defined when it was not available). diff --git a/ext/standard/md5.c b/ext/standard/md5.c index 34b7976a98..dbf780bad5 100644 --- a/ext/standard/md5.c +++ b/ext/standard/md5.c @@ -24,9 +24,6 @@ */ #include "php.h" -#include -#include -#include #include "md5.h" PHPAPI void make_digest(char *md5str, unsigned char *digest) @@ -73,7 +70,8 @@ PHP_NAMED_FUNCTION(php_if_md5_file) unsigned char buf[1024]; unsigned char digest[16]; PHP_MD5_CTX context; - int n,fd; + int n; + php_stream *stream; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; @@ -81,34 +79,25 @@ PHP_NAMED_FUNCTION(php_if_md5_file) 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 ((fd = VCWD_OPEN(Z_STRVAL_PP(arg), O_RDONLY)) == -1) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open file"); + stream = php_stream_open_wrapper(Z_STRVAL_PP(arg), "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL); + if (!stream) { RETURN_FALSE; } PHP_MD5Init(&context); - while ((n = read(fd, buf, sizeof(buf))) > 0) { + while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) { PHP_MD5Update(&context, buf, n); } PHP_MD5Final(digest, &context); + php_stream_close(stream); + if (n<0) { - close(fd); RETURN_FALSE; } - close(fd); - make_digest(md5str, digest); RETVAL_STRING(md5str, 1); diff --git a/ext/standard/sha1.c b/ext/standard/sha1.c index 8734083fcc..ec489dd31b 100644 --- a/ext/standard/sha1.c +++ b/ext/standard/sha1.c @@ -19,9 +19,6 @@ /* $Id$ */ #include "php.h" -#include -#include -#include /* This code is heavily based on the PHP md5 implementation */ @@ -72,7 +69,8 @@ PHP_FUNCTION(sha1_file) unsigned char buf[1024]; unsigned char digest[20]; PHP_SHA1_CTX context; - int n, fd; + int n; + php_stream *stream; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; @@ -80,34 +78,25 @@ PHP_FUNCTION(sha1_file) 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 ((fd = VCWD_OPEN(Z_STRVAL_PP(arg), O_RDONLY)) == -1) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open file"); + stream = php_stream_open_wrapper(Z_STRVAL_PP(arg), "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL); + if (!stream) { RETURN_FALSE; } PHP_SHA1Init(&context); - while ((n = read(fd, buf, sizeof(buf))) > 0) { + while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) { PHP_SHA1Update(&context, buf, n); } PHP_SHA1Final(digest, &context); + php_stream_close(stream); + if (n<0) { - close(fd); RETURN_FALSE; } - close(fd); - make_sha1_digest(sha1str, digest); RETVAL_STRING(sha1str, 1);