From 17157f8092704e2ba5a892fe2aa2eabe1222e091 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sat, 16 Apr 2005 09:50:13 +0000 Subject: [PATCH] MFH: use streams api for md5_file() and sha1_file() --- NEWS | 1 + ext/standard/md5.c | 27 ++++++++------------------- ext/standard/sha1.c | 28 +++++++++------------------- 3 files changed, 18 insertions(+), 38 deletions(-) diff --git a/NEWS b/NEWS index f646fca756..a7cddb800f 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2005, PHP 5.0.5 +- Changed sha1_file() / md5_file() to use streams instead of low level IO (Uwe) - Removed php_check_syntax() function, never worked properly. (Ilia) - Fixed bug #32699 (pg_affected_rows() was defined when it was not available). (Derick) diff --git a/ext/standard/md5.c b/ext/standard/md5.c index 18dcb089ab..00108c49e0 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) @@ -81,40 +78,32 @@ 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_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) { return; } - - if (PG(safe_mode) && (!php_checkuid(arg, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { - RETURN_FALSE; - } - - if (php_check_open_basedir(arg TSRMLS_CC)) { - RETURN_FALSE; - } - - if ((fd = VCWD_OPEN(arg, O_RDONLY)) == -1) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open file"); + + stream = php_stream_open_wrapper(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); - if (raw_output) { RETURN_STRINGL(digest, 16, 1); } else { diff --git a/ext/standard/sha1.c b/ext/standard/sha1.c index 75b8c5fe89..0e132100d5 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 */ @@ -69,6 +66,7 @@ PHP_FUNCTION(sha1) /* }}} */ + /* {{{ proto string sha1_file(string filename [, bool raw_output]) Calculate the sha1 hash of given filename */ PHP_FUNCTION(sha1_file) @@ -80,40 +78,32 @@ 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_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) { return; } - - if (PG(safe_mode) && (!php_checkuid(arg, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { - RETURN_FALSE; - } - - if (php_check_open_basedir(arg TSRMLS_CC)) { - RETURN_FALSE; - } - - if ((fd = VCWD_OPEN(arg, O_RDONLY)) == -1) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open file"); + + stream = php_stream_open_wrapper(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); - if (raw_output) { RETURN_STRINGL(digest, 20, 1); } else { -- 2.40.0