]> granicus.if.org Git - php/commitdiff
MFH: use streams api for md5_file() and sha1_file()
authorUwe Schindler <thetaphi@php.net>
Sat, 16 Apr 2005 09:50:13 +0000 (09:50 +0000)
committerUwe Schindler <thetaphi@php.net>
Sat, 16 Apr 2005 09:50:13 +0000 (09:50 +0000)
NEWS
ext/standard/md5.c
ext/standard/sha1.c

diff --git a/NEWS b/NEWS
index f646fca756e45808bbafe995375a61cf9a167954..a7cddb800fd311c2b50d73e9e810fc5178ba0272 100644 (file)
--- 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)
index 18dcb089ab0c86b7be4a6c7e08c086a50736dd7b..00108c49e038b2e563a6fc570d9ccb817de48d77 100644 (file)
@@ -24,9 +24,6 @@
  */
 
 #include "php.h"
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 #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 {
index 75b8c5fe89d8612ac1535014693da9cd968995db..0e132100d58a6ffbc51a1b7cbaa4f37fbff1206b 100644 (file)
@@ -19,9 +19,6 @@
 /* $Id$ */
 
 #include "php.h"
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 
 /* 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 {