]> granicus.if.org Git - php/commitdiff
Allow unicode-ascii to binary conversion and do proper path conversion for file variants
authorSara Golemon <pollita@php.net>
Mon, 2 Oct 2006 00:32:13 +0000 (00:32 +0000)
committerSara Golemon <pollita@php.net>
Mon, 2 Oct 2006 00:32:13 +0000 (00:32 +0000)
ext/standard/md5.c
ext/standard/sha1.c

index 1383ae5aacd6af4f3f27af3e09a6fc2f503e2ffb..865fc527da0c33fae443257555b96a1c9cbd14d4 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "php.h"
 #include "md5.h"
+#include "ext/standard/file.h"
 
 PHPAPI void make_digest(char *md5str, unsigned char *digest)
 {
@@ -44,26 +45,38 @@ PHP_NAMED_FUNCTION(php_if_md5)
 {
        char *arg;
        int arg_len;
+       zend_uchar arg_type;
        zend_bool raw_output = 0;
        char md5str[33];
        PHP_MD5_CTX context;
        unsigned char digest[16];
        
-       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, "t|b", &arg, &arg_len, &arg_type, &raw_output) == FAILURE) {
                return;
        }
+
+       if (arg_type == IS_UNICODE) {
+               arg = zend_unicode_to_ascii((UChar*)arg, arg_len TSRMLS_CC);
+               if (!arg) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Binary or ASCII-Unicode string expected, non-ASCII-Unicode string received");
+                       RETURN_FALSE;
+               }
+       }
        
        md5str[0] = '\0';
        PHP_MD5Init(&context);
        PHP_MD5Update(&context, (unsigned char*)arg, arg_len);
        PHP_MD5Final(digest, &context);
        if (raw_output) {
-               RETURN_STRINGL((char*)digest, 16, 1);
+               RETVAL_STRINGL((char*)digest, 16, 1);
        } else {
                make_digest(md5str, digest);
                RETVAL_ASCII_STRING(md5str, ZSTR_DUPLICATE);
        }
 
+       if (arg_type == IS_UNICODE) {
+               efree(arg);
+       }
 }
 /* }}} */
 
@@ -73,6 +86,7 @@ PHP_NAMED_FUNCTION(php_if_md5_file)
 {
        char          *arg;
        int           arg_len;
+       zend_uchar    arg_type;
        zend_bool raw_output = 0;
        char          md5str[33];
        unsigned char buf[1024];
@@ -81,11 +95,20 @@ PHP_NAMED_FUNCTION(php_if_md5_file)
        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, "t|b", &arg, &arg_len, &arg_type, &raw_output) == FAILURE) {
                return;
        }
+
+       if (arg_type == IS_UNICODE) {
+               if (php_stream_path_encode(NULL, &arg, &arg_len, (UChar*)arg, arg_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
+                       RETURN_FALSE;
+               }
+       }
        
        stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
+       if (arg_type == IS_UNICODE) {
+               efree(arg);
+       }
        if (!stream) {
                RETURN_FALSE;
        }
index fdfee17e1f3a88bcdd905613f5e2f5c8193753e3..f4c3635d573c123cf19a100dbd3fc94d2dd3b8d7 100644 (file)
@@ -19,6 +19,7 @@
 /* $Id$ */
 
 #include "php.h"
+#include "ext/standard/file.h"
 
 /* This code is heavily based on the PHP md5 implementation */ 
 
@@ -42,26 +43,38 @@ PHP_FUNCTION(sha1)
 {
        char *arg;
        int arg_len;
+       zend_uchar arg_type;
        zend_bool raw_output = 0;
        char sha1str[41];
        PHP_SHA1_CTX context;
        unsigned char digest[20];
        
-       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, "t|b", &arg, &arg_len, &arg_type, &raw_output) == FAILURE) {
                return;
        }
 
+       if (arg_type == IS_UNICODE) {
+               arg = zend_unicode_to_ascii((UChar*)arg, arg_len TSRMLS_CC);
+               if (!arg) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Binary or ASCII-Unicode string expected, non-ASCII-Unicode string received");
+                       RETURN_FALSE;
+               }
+       }
+
        sha1str[0] = '\0';
        PHP_SHA1Init(&context);
        PHP_SHA1Update(&context, (unsigned char*)arg, arg_len);
        PHP_SHA1Final(digest, &context);
        if (raw_output) {
-               RETURN_STRINGL((char*)digest, 20, 1);
+               RETVAL_STRINGL((char*)digest, 20, 1);
        } else {
                make_sha1_digest(sha1str, digest);
                RETVAL_ASCII_STRING(sha1str, ZSTR_DUPLICATE);
        }
 
+       if (arg_type == IS_UNICODE) {
+               efree(arg);
+       }
 }
 
 /* }}} */
@@ -73,6 +86,7 @@ PHP_FUNCTION(sha1_file)
 {
        char          *arg;
        int           arg_len;
+       zend_uchar    arg_type;
        zend_bool raw_output = 0;
        char          sha1str[41];
        unsigned char buf[1024];
@@ -81,11 +95,20 @@ PHP_FUNCTION(sha1_file)
        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, "t|b", &arg, &arg_len, &arg_type, &raw_output) == FAILURE) {
                return;
        }
+
+       if (arg_type == IS_UNICODE) {
+               if (php_stream_path_encode(NULL, &arg, &arg_len, (UChar*)arg, arg_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
+                       RETURN_FALSE;
+               }
+       }
        
        stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
+       if (arg_type == IS_UNICODE) {
+               efree(arg);
+       }
        if (!stream) {
                RETURN_FALSE;
        }