FAQ
thetaphi Fri Apr 15 05:14:38 2005 EDT

Modified files:
/php-src/ext/standard md5.c sha1.c
Log:
fix various solaris problems by replacing stdio with posix io where possible

http://cvs.php.net/diff.php/php-src/ext/standard/md5.c?r1=1.35&r2=1.36&ty=u
Index: php-src/ext/standard/md5.c
diff -u php-src/ext/standard/md5.c:1.35 php-src/ext/standard/md5.c:1.36
--- php-src/ext/standard/md5.c:1.35 Thu Jan 8 03:17:33 2004
+++ php-src/ext/standard/md5.c Fri Apr 15 05:14:38 2005
@@ -16,16 +16,17 @@
+----------------------------------------------------------------------+
*/

-/* $Id: md5.c,v 1.35 2004/01/08 08:17:33 andi Exp $ */
+/* $Id: md5.c,v 1.36 2005/04/15 09:14:38 thetaphi Exp $ */

/*
* md5.c - Copyright 1997 Lachlan Roche
* md5_file() added by Alessandro Astarita <aleast@capri.it>
*/

-#include <stdio.h>
#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)
@@ -80,8 +81,7 @@
unsigned char buf[1024];
unsigned char digest[16];
PHP_MD5_CTX context;
- int n;
- FILE *fp;
+ int n,fd;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
return;
@@ -95,25 +95,25 @@
RETURN_FALSE;
}

- if ((fp = VCWD_FOPEN(arg, "rb")) == NULL) {
+ if ((fd = VCWD_OPEN(arg, O_RDONLY)) == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open file");
RETURN_FALSE;
}

PHP_MD5Init(&context);

- while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
+ while ((n = read(fd, buf, sizeof(buf))) > 0) {
PHP_MD5Update(&context, buf, n);
}

PHP_MD5Final(digest, &context);

- if (ferror(fp)) {
- fclose(fp);
+ if (n<0) {
+ close(fd);
RETURN_FALSE;
}

- fclose(fp);
+ close(fd);

if (raw_output) {
RETURN_STRINGL(digest, 16, 1);
http://cvs.php.net/diff.php/php-src/ext/standard/sha1.c?r1=1.9&r2=1.10&ty=u
Index: php-src/ext/standard/sha1.c
diff -u php-src/ext/standard/sha1.c:1.9 php-src/ext/standard/sha1.c:1.10
--- php-src/ext/standard/sha1.c:1.9 Thu Jan 8 03:17:34 2004
+++ php-src/ext/standard/sha1.c Fri Apr 15 05:14:38 2005
@@ -16,10 +16,12 @@
+----------------------------------------------------------------------+
*/

-/* $Id: sha1.c,v 1.9 2004/01/08 08:17:34 andi Exp $ */
+/* $Id: sha1.c,v 1.10 2005/04/15 09:14:38 thetaphi Exp $ */

-#include <stdio.h>
#include "php.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>

/* This code is heavily based on the PHP md5 implementation */

@@ -78,8 +80,7 @@
unsigned char buf[1024];
unsigned char digest[20];
PHP_SHA1_CTX context;
- int n;
- FILE *fp;
+ int n, fd;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
return;
@@ -93,25 +94,25 @@
RETURN_FALSE;
}

- if ((fp = VCWD_FOPEN(arg, "rb")) == NULL) {
+ if ((fd = VCWD_OPEN(arg, O_RDONLY)) == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open file");
RETURN_FALSE;
}

PHP_SHA1Init(&context);

- while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
+ while ((n = read(fd, buf, sizeof(buf))) > 0) {
PHP_SHA1Update(&context, buf, n);
}

PHP_SHA1Final(digest, &context);

- if (ferror(fp)) {
- fclose(fp);
+ if (n<0) {
+ close(fd);
RETURN_FALSE;
}

- fclose(fp);
+ close(fd);

if (raw_output) {
RETURN_STRINGL(digest, 20, 1);

Search Discussions

  • Uwe Schindler at Apr 15, 2005 at 2:29 pm
    thetaphi Fri Apr 15 10:29:32 2005 EDT

    Modified files:
    /php-src/ext/standard md5.c sha1.c
    Log:
    use streams api for md5_file and sha1_file. Added parameter use_include_path similar to other PHP file functions. Documentation update outstanding

    http://cvs.php.net/diff.php/php-src/ext/standard/md5.c?r1=1.36&r2=1.37&ty=u
    Index: php-src/ext/standard/md5.c
    diff -u php-src/ext/standard/md5.c:1.36 php-src/ext/standard/md5.c:1.37
    --- php-src/ext/standard/md5.c:1.36 Fri Apr 15 05:14:38 2005
    +++ php-src/ext/standard/md5.c Fri Apr 15 10:29:32 2005
    @@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
    */

    -/* $Id: md5.c,v 1.36 2005/04/15 09:14:38 thetaphi Exp $ */
    +/* $Id: md5.c,v 1.37 2005/04/15 14:29:32 thetaphi Exp $ */

    /*
    * md5.c - Copyright 1997 Lachlan Roche
    @@ -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)
    @@ -70,51 +67,45 @@
    }
    /* }}} */

    -/* {{{ proto string md5_file(string filename [, bool raw_output])
    +/* {{{ proto string md5_file(string filename [, bool raw_output [, bool use_include_path]])
    Calculate the md5 hash of given filename */
    PHP_NAMED_FUNCTION(php_if_md5_file)
    {
    char *arg;
    int arg_len;
    zend_bool raw_output = 0;
    + zend_bool use_include_path = 0;
    char md5str[33];
    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) {
    + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bb", &arg, &arg_len, &raw_output, &use_include_path) == 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",
    + (use_include_path ? USE_PATH : 0) | 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 {
    http://cvs.php.net/diff.php/php-src/ext/standard/sha1.c?r1=1.10&r2=1.11&ty=u
    Index: php-src/ext/standard/sha1.c
    diff -u php-src/ext/standard/sha1.c:1.10 php-src/ext/standard/sha1.c:1.11
    --- php-src/ext/standard/sha1.c:1.10 Fri Apr 15 05:14:38 2005
    +++ php-src/ext/standard/sha1.c Fri Apr 15 10:29:32 2005
    @@ -16,12 +16,9 @@
    +----------------------------------------------------------------------+
    */

    -/* $Id: sha1.c,v 1.10 2005/04/15 09:14:38 thetaphi Exp $ */
    +/* $Id: sha1.c,v 1.11 2005/04/15 14:29:32 thetaphi Exp $ */

    #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,51 +66,46 @@

    /* }}} */

    -/* {{{ proto string sha1_file(string filename [, bool raw_output])
    +
    +/* {{{ proto string sha1_file(string filename [, bool raw_output [, bool use_include_path]])
    Calculate the sha1 hash of given filename */
    PHP_FUNCTION(sha1_file)
    {
    char *arg;
    int arg_len;
    zend_bool raw_output = 0;
    + zend_bool use_include_path = 0;
    char sha1str[41];
    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) {
    + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bb", &arg, &arg_len, &raw_output, &use_include_path) == 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",
    + (use_include_path ? USE_PATH : 0) | 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 {
  • Andrey Hristov at Apr 15, 2005 at 2:37 pm
    Hi,
    what's the reason for looking in the include path. Usually these
    functions are used to verify the MD5/SHA1 hash of a specific file.

    Regards,
    Andrey

    Uwe Schindler wrote:
    thetaphi Fri Apr 15 10:29:32 2005 EDT

    Modified files:
    /php-src/ext/standard md5.c sha1.c
    Log:
    use streams api for md5_file and sha1_file. Added parameter use_include_path similar to other PHP file functions. Documentation update outstanding

    http://cvs.php.net/diff.php/php-src/ext/standard/md5.c?r1=1.36&r2=1.37&ty=u
    Index: php-src/ext/standard/md5.c
    diff -u php-src/ext/standard/md5.c:1.36 php-src/ext/standard/md5.c:1.37
    --- php-src/ext/standard/md5.c:1.36 Fri Apr 15 05:14:38 2005
    +++ php-src/ext/standard/md5.c Fri Apr 15 10:29:32 2005
    @@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
    */

    -/* $Id: md5.c,v 1.36 2005/04/15 09:14:38 thetaphi Exp $ */
    +/* $Id: md5.c,v 1.37 2005/04/15 14:29:32 thetaphi Exp $ */

    /*
    * md5.c - Copyright 1997 Lachlan Roche
    @@ -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)
    @@ -70,51 +67,45 @@
    }
    /* }}} */

    -/* {{{ proto string md5_file(string filename [, bool raw_output])
    +/* {{{ proto string md5_file(string filename [, bool raw_output [, bool use_include_path]])
    Calculate the md5 hash of given filename */
    PHP_NAMED_FUNCTION(php_if_md5_file)
    {
    char *arg;
    int arg_len;
    zend_bool raw_output = 0;
    + zend_bool use_include_path = 0;
    char md5str[33];
    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) {
    + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bb", &arg, &arg_len, &raw_output, &use_include_path) == 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",
    + (use_include_path ? USE_PATH : 0) | 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 {
    http://cvs.php.net/diff.php/php-src/ext/standard/sha1.c?r1=1.10&r2=1.11&ty=u
    Index: php-src/ext/standard/sha1.c
    diff -u php-src/ext/standard/sha1.c:1.10 php-src/ext/standard/sha1.c:1.11
    --- php-src/ext/standard/sha1.c:1.10 Fri Apr 15 05:14:38 2005
    +++ php-src/ext/standard/sha1.c Fri Apr 15 10:29:32 2005
    @@ -16,12 +16,9 @@
    +----------------------------------------------------------------------+
    */

    -/* $Id: sha1.c,v 1.10 2005/04/15 09:14:38 thetaphi Exp $ */
    +/* $Id: sha1.c,v 1.11 2005/04/15 14:29:32 thetaphi Exp $ */

    #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,51 +66,46 @@

    /* }}} */

    -/* {{{ proto string sha1_file(string filename [, bool raw_output])
    +
    +/* {{{ proto string sha1_file(string filename [, bool raw_output [, bool use_include_path]])
    Calculate the sha1 hash of given filename */
    PHP_FUNCTION(sha1_file)
    {
    char *arg;
    int arg_len;
    zend_bool raw_output = 0;
    + zend_bool use_include_path = 0;
    char sha1str[41];
    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) {
    + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bb", &arg, &arg_len, &raw_output, &use_include_path) == 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",
    + (use_include_path ? USE_PATH : 0) | 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 {

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-cvs @
categoriesphp
postedApr 15, '05 at 9:14a
activeApr 15, '05 at 2:37p
posts3
users2
websitephp.net

2 users in discussion

Uwe Schindler: 2 posts Andrey Hristov: 1 post

People

Translate

site design / logo © 2019 Grokbase