From a6a92fc4586717e06e01d5bf1198a4e9da116403 Mon Sep 17 00:00:00 2001 From: Sara Golemon Date: Mon, 2 Oct 2006 00:32:13 +0000 Subject: [PATCH] Allow unicode-ascii to binary conversion and do proper path conversion for file variants --- ext/standard/md5.c | 29 ++++++++++++++++++++++++++--- ext/standard/sha1.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/ext/standard/md5.c b/ext/standard/md5.c index 1383ae5aac..865fc527da 100644 --- a/ext/standard/md5.c +++ b/ext/standard/md5.c @@ -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; } diff --git a/ext/standard/sha1.c b/ext/standard/sha1.c index fdfee17e1f..f4c3635d57 100644 --- a/ext/standard/sha1.c +++ b/ext/standard/sha1.c @@ -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; } -- 2.40.0