com php-src: Add file descriptor caching to mcrypt_create_i v(): ext/mcrypt/mcrypt.c ext/mcrypt/php_mcrypt. h
| From: | JulienPauli | Date: | Wed, 13 May 2015 12:18:32 +0000 |
| Subject: | com php-src: Add file descriptor caching to mcrypt_create_i v(): ext/mcrypt/mcrypt.c ext/mcrypt/php_mcrypt. h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-86737@lists.php.net to get a copy of this message | ||
Commit: c02c4aca007199057201b855f674b6baa9be4c6e
Author: Leigh <leigh@php.net> Fri, 27 Mar 2015 14:33:30 +0100
Committer: Julien Pauli <jpauli@php.net> Wed, 13 May 2015 14:18:32 +0200
Parents: 43c24da166b090d4745293042ee995a0361c7ff0
Branches: PHP-5.5
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=c02c4aca007199057201b855f674b6baa9be4c6e
Log:
Add file descriptor caching to mcrypt_create_iv()
This improves performance for applications that make repeated calls to
mcrypt_create_iv()
Changed paths:
M ext/mcrypt/mcrypt.c
M ext/mcrypt/php_mcrypt.h
Diff:
diff --git a/ext/mcrypt/mcrypt.c b/ext/mcrypt/mcrypt.c
index 5c9f9fc..5f14a92 100644
--- a/ext/mcrypt/mcrypt.c
+++ b/ext/mcrypt/mcrypt.c
@@ -415,7 +415,13 @@ static void php_mcrypt_module_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{
}
}
/* }}} */
-
+
+typedef enum {
+ RANDOM = 0,
+ URANDOM,
+ RAND
+} iv_source;
+
static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
{
le_mcrypt = zend_register_list_destructors_ex(php_mcrypt_module_dtor, NULL, "mcrypt",
module_number);
@@ -473,6 +479,9 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
php_stream_filter_register_factory("mcrypt.*", &php_mcrypt_filter_factory
TSRMLS_CC);
php_stream_filter_register_factory("mdecrypt.*", &php_mcrypt_filter_factory
TSRMLS_CC);
+ MCG(fd[RANDOM]) = -1;
+ MCG(fd[URANDOM]) = -1;
+
return SUCCESS;
}
/* }}} */
@@ -536,12 +545,6 @@ PHP_MINFO_FUNCTION(mcrypt) /* {{{ */
}
/* }}} */
-typedef enum {
- RANDOM = 0,
- URANDOM,
- RAND
-} iv_source;
-
/* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode,
string mode_directory)
Opens the module of the algorithm and the mode to be used */
PHP_FUNCTION(mcrypt_module_open)
@@ -1403,24 +1406,27 @@ PHP_FUNCTION(mcrypt_create_iv)
}
n = size;
#else
- int fd;
+ int *fd = &MCG(fd[source]);
size_t read_bytes = 0;
- fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY);
- if (fd < 0) {
- efree(iv);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot open source device");
- RETURN_FALSE;
+ if (*fd < 0) {
+ *fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY);
+ if (*fd < 0) {
+ efree(iv);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot open source device");
+ RETURN_FALSE;
+ }
}
+
while (read_bytes < size) {
- n = read(fd, iv + read_bytes, size - read_bytes);
+ n = read(*fd, iv + read_bytes, size - read_bytes);
if (n < 0) {
break;
}
read_bytes += n;
}
n = read_bytes;
- close(fd);
+
if (n < size) {
efree(iv);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random
data");
diff --git a/ext/mcrypt/php_mcrypt.h b/ext/mcrypt/php_mcrypt.h
index 4bc2261..9bfad99 100644
--- a/ext/mcrypt/php_mcrypt.h
+++ b/ext/mcrypt/php_mcrypt.h
@@ -77,6 +77,7 @@ ZEND_BEGIN_MODULE_GLOBALS(mcrypt)
int le_h;
char *modes_dir;
char *algorithms_dir;
+ int fd[2]; // RANDOM = 0, URANDOM = 1
ZEND_END_MODULE_GLOBALS(mcrypt)
#ifdef ZTS