]> granicus.if.org Git - php/commitdiff
- Added md5_file(), which calculaties the MD5 sum of a file.
authorDerick Rethans <derick@php.net>
Sun, 18 Nov 2001 18:48:17 +0000 (18:48 +0000)
committerDerick Rethans <derick@php.net>
Sun, 18 Nov 2001 18:48:17 +0000 (18:48 +0000)
  (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)

ext/standard/basic_functions.c
ext/standard/md5.c
ext/standard/md5.h

index 9519fd007a526bf7df50f5fcec156fa60402c560..923a18779612c90b059a20362c9ee1936faa415a 100644 (file)
@@ -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)                                                                                                                   
index 634814a74fe9b4c8a024d57afd2dd72979c3b299..641b8ac13121569e051ea6da2f4e92a4eb514b19 100644 (file)
@@ -20,6 +20,7 @@
 
 /* 
  * md5.c - Copyright 1997 Lachlan Roche 
+ * md5_file() added by Alessandro Astarita <aleast@capri.it>
  */
 
 #include <stdio.h>
 
 #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);
 }
 /* }}} */
index b4c09e498abffad73c659cea36a848230bf0a31a..ae0d67163fc9cc4e86a47acc123c064b2ce5088b 100644 (file)
@@ -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