]> 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:49:53 +0000 (09:49 +0000)
committerUwe Schindler <thetaphi@php.net>
Sat, 16 Apr 2005 09:49:53 +0000 (09:49 +0000)
NEWS
ext/standard/md5.c
ext/standard/sha1.c

diff --git a/NEWS b/NEWS
index b072cae93179acc01ead519d3342a45683eb5e85..dae06e58cd9f4c0ed62b2cffa2523f89111b0e7f 100644 (file)
--- 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).
index 34b7976a98bd19ff0a0132f81cd7cc26b59aba52..dbf780bad5250f8a4f5042367275047f04fdc666 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)
@@ -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);
index 8734083fcc83e14f2b68d5abcc64330fa743b109..ec489dd31b09b090cbd5280a682cc44af4c00676 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 */ 
 
@@ -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);