diff options
author | Derick Rethans <derick@php.net> | 2001-11-18 18:48:17 +0000 |
---|---|---|
committer | Derick Rethans <derick@php.net> | 2001-11-18 18:48:17 +0000 |
commit | f17f3371becb689a3a3751a733c991dd2569211a (patch) | |
tree | eca04335fa024c5cbf7ad8d7aced891569783a62 /ext | |
parent | c1f93729b83141e8ec71a33f25e019f3c2c62b47 (diff) | |
download | php-git-f17f3371becb689a3a3751a733c991dd2569211a.tar.gz |
- Added md5_file(), which calculaties the MD5 sum of a file.
(patch by: Alessandro Astarita <aleast@capri.it>) (Derick)
@- Added md5_file(), which calculaties the MD5 sum of a file.
@ (patch by: Alessandro Astarita <aleast@capri.it>) (Derick)
Diffstat (limited to 'ext')
-rw-r--r-- | ext/standard/basic_functions.c | 1 | ||||
-rw-r--r-- | ext/standard/md5.c | 72 | ||||
-rw-r--r-- | ext/standard/md5.h | 1 |
3 files changed, 68 insertions, 6 deletions
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 9519fd007a..923a187796 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -282,6 +282,7 @@ function_entry basic_functions[] = { PHP_FE(htmlentities, NULL) PHP_FE(get_html_translation_table, NULL) PHP_NAMED_FE(md5,php_if_md5, NULL) + PHP_NAMED_FE(md5_file,php_if_md5_file, NULL) PHP_NAMED_FE(crc32,php_if_crc32, NULL) PHP_FE(iptcparse, NULL) diff --git a/ext/standard/md5.c b/ext/standard/md5.c index 634814a74f..641b8ac131 100644 --- a/ext/standard/md5.c +++ b/ext/standard/md5.c @@ -20,6 +20,7 @@ /* * md5.c - Copyright 1997 Lachlan Roche + * md5_file() added by Alessandro Astarita <aleast@capri.it> */ #include <stdio.h> @@ -27,16 +28,26 @@ #include "md5.h" +static void make_digest(char *md5str, unsigned char *digest) +{ + int i; + + for (i = 0; i < 16; i++) { + sprintf(md5str, "%02x", digest[i]); + md5str += 2; + } + + *md5str = '\0'; +} + /* {{{ proto string md5(string str) Calculate the md5 hash of a string */ PHP_NAMED_FUNCTION(php_if_md5) { - pval **arg; + zval **arg; char md5str[33]; PHP_MD5_CTX context; unsigned char digest[16]; - int i; - char *r; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; @@ -47,10 +58,59 @@ PHP_NAMED_FUNCTION(php_if_md5) PHP_MD5Init(&context); PHP_MD5Update(&context, Z_STRVAL_PP(arg), Z_STRLEN_PP(arg)); PHP_MD5Final(digest, &context); - for (i = 0, r = md5str; i < 16; i++, r += 2) { - sprintf(r, "%02x", digest[i]); + make_digest(md5str, digest); + RETVAL_STRING(md5str, 1); +} +/* }}} */ + +/* {{{ proto string md5sum(string filename) + Calculate the md5 hash of given filename */ +PHP_NAMED_FUNCTION(php_if_md5_file) +{ + zval **arg; + char md5str[33]; + unsigned char buf[1024]; + unsigned char digest[16]; + PHP_MD5_CTX context; + int n; + FILE *fp; + + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { + WRONG_PARAM_COUNT; + } + + 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 ((fp = VCWD_FOPEN(Z_STRVAL_PP(arg), "rb")) == NULL) { + php_error(E_WARNING, "md5_file(): Unable to open file"); + RETURN_FALSE; + } + + PHP_MD5Init(&context); + + while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { + PHP_MD5Update(&context, buf, n); + } + + PHP_MD5Final(digest, &context); + + if (ferror(fp)) { + fclose(fp); + RETURN_FALSE; } - *r = '\0'; + + fclose(fp); + + make_digest(md5str, digest); + RETVAL_STRING(md5str, 1); } /* }}} */ diff --git a/ext/standard/md5.h b/ext/standard/md5.h index b4c09e498a..ae0d67163f 100644 --- a/ext/standard/md5.h +++ b/ext/standard/md5.h @@ -59,5 +59,6 @@ void PHP_MD5Update(PHP_MD5_CTX *, const unsigned char *, unsigned int); void PHP_MD5Final(unsigned char[16], PHP_MD5_CTX *); PHP_NAMED_FUNCTION(php_if_md5); +PHP_NAMED_FUNCTION(php_if_md5_file); #endif |