cvs: php4 /ext/standard basic_functions.c md5.c md5.h
| From: | Derick Rethans | Date: | Sun, 18 Nov 2001 18:48:17 +0000 |
| Subject: | cvs: php4 /ext/standard basic_functions.c md5.c md5.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-8245@lists.php.net to get a copy of this message | ||
derick Sun Nov 18 13:48:17 2001 EDT
Modified files:
/php4/ext/standard basic_functions.c md5.c md5.h
Log:
- 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)
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.421 php4/ext/standard/basic_functions.c:1.422
--- php4/ext/standard/basic_functions.c:1.421 Thu Nov 15 04:53:40 2001
+++ php4/ext/standard/basic_functions.c Sun Nov 18 13:48:17 2001
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.421 2001/11/15 09:53:40 stas Exp $ */
+/* $Id: basic_functions.c,v 1.422 2001/11/18 18:48:17 derick Exp $ */
#include "php.h"
#include "php_main.h"
@@ -282,6 +282,7 @@
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: php4/ext/standard/md5.c
diff -u php4/ext/standard/md5.c:1.21 php4/ext/standard/md5.c:1.22
--- php4/ext/standard/md5.c:1.21 Tue Sep 25 17:58:23 2001
+++ php4/ext/standard/md5.c Sun Nov 18 13:48:17 2001
@@ -16,10 +16,11 @@
+----------------------------------------------------------------------+
*/
-/* $Id: md5.c,v 1.21 2001/09/25 21:58:23 jeroen Exp $ */
+/* $Id: md5.c,v 1.22 2001/11/18 18:48:17 derick Exp $ */
/*
* 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_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: php4/ext/standard/md5.h
diff -u php4/ext/standard/md5.h:1.9 php4/ext/standard/md5.h:1.10
--- php4/ext/standard/md5.h:1.9 Mon Feb 26 01:07:23 2001
+++ php4/ext/standard/md5.h Sun Nov 18 13:48:17 2001
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: md5.h,v 1.9 2001/02/26 06:07:23 andi Exp $ */
+/* $Id: md5.h,v 1.10 2001/11/18 18:48:17 derick Exp $ */
#ifndef MD5_H
#define MD5_H
@@ -59,5 +59,6 @@
void PHP_MD5Final(unsigned char[16], PHP_MD5_CTX *);
PHP_NAMED_FUNCTION(php_if_md5);
+PHP_NAMED_FUNCTION(php_if_md5_file);
#endif