-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add an OOP API to HashContext #6347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -595,7 +595,7 @@ PHP_FUNCTION(hash_hmac_file) | |||||
/* }}} */ | ||||||
|
||||||
/* {{{ Initialize a hashing context */ | ||||||
PHP_FUNCTION(hash_init) | ||||||
static void do_hash_init(INTERNAL_FUNCTION_PARAMETERS, zval *thisObj) | ||||||
{ | ||||||
zend_string *algo, *key = NULL; | ||||||
zend_long options = 0; | ||||||
|
@@ -625,8 +625,12 @@ PHP_FUNCTION(hash_init) | |||||
} | ||||||
} | ||||||
|
||||||
object_init_ex(return_value, php_hashcontext_ce); | ||||||
hash = php_hashcontext_from_object(Z_OBJ_P(return_value)); | ||||||
if (!thisObj) { | ||||||
/* Called as hash_init(), create a new object */ | ||||||
object_init_ex(return_value, php_hashcontext_ce); | ||||||
thisObj = return_value; | ||||||
} /* otherwise, called as new HashContext() */ | ||||||
hash = php_hashcontext_from_object(Z_OBJ_P(thisObj)); | ||||||
|
||||||
context = php_hash_alloc_context(ops); | ||||||
ops->hash_init(context); | ||||||
|
@@ -663,47 +667,10 @@ PHP_FUNCTION(hash_init) | |||||
} | ||||||
/* }}} */ | ||||||
|
||||||
#define PHP_HASHCONTEXT_VERIFY(hash) { \ | ||||||
if (!hash->context) { \ | ||||||
zend_argument_type_error(1, "must be a valid Hash Context resource"); \ | ||||||
RETURN_THROWS(); \ | ||||||
} \ | ||||||
} | ||||||
|
||||||
/* {{{ Pump data into the hashing algorithm */ | ||||||
PHP_FUNCTION(hash_update) | ||||||
{ | ||||||
zval *zhash; | ||||||
php_hashcontext_object *hash; | ||||||
zend_string *data; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zhash, php_hashcontext_ce, &data) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data)); | ||||||
|
||||||
RETURN_TRUE; | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ Pump data into the hashing algorithm from an open stream */ | ||||||
PHP_FUNCTION(hash_update_stream) | ||||||
static zend_long do_update_stream(php_hashcontext_object *hash, php_stream *stream, zend_long length, zend_bool throwOnError) | ||||||
{ | ||||||
zval *zhash, *zstream; | ||||||
php_hashcontext_object *hash; | ||||||
php_stream *stream = NULL; | ||||||
zend_long length = -1, didread = 0; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Or|l", &zhash, php_hashcontext_ce, &zstream, &length) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
php_stream_from_zval(stream, zstream); | ||||||
zend_long didread = 0; | ||||||
|
||||||
while (length) { | ||||||
char buf[1024]; | ||||||
|
@@ -715,69 +682,54 @@ PHP_FUNCTION(hash_update_stream) | |||||
} | ||||||
|
||||||
if ((n = php_stream_read(stream, buf, toread)) <= 0) { | ||||||
RETURN_LONG(didread); | ||||||
if (throwOnError && (n < 0) && !EG(exception)) { | ||||||
zend_throw_exception_ex(NULL, 0, "Stream read encountered error, %zd bytes copied", didread); | ||||||
} | ||||||
return didread; | ||||||
} | ||||||
hash->ops->hash_update(hash->context, (unsigned char *) buf, n); | ||||||
length -= n; | ||||||
didread += n; | ||||||
} | ||||||
|
||||||
RETURN_LONG(didread); | ||||||
return didread; | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ Pump data into the hashing algorithm from a file */ | ||||||
PHP_FUNCTION(hash_update_file) | ||||||
static zend_bool do_update_file(php_hashcontext_object *hash, zend_string *filename, php_stream_context *context, zend_bool throwOnError) | ||||||
{ | ||||||
zval *zhash, *zcontext = NULL; | ||||||
php_hashcontext_object *hash; | ||||||
php_stream_context *context = NULL; | ||||||
php_stream *stream; | ||||||
zend_string *filename; | ||||||
php_stream *stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", throwOnError ? 0 : REPORT_ERRORS, NULL, context); | ||||||
char buf[1024]; | ||||||
ssize_t n; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r!", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
ssize_t n, didcopy = 0; | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
context = php_stream_context_from_zval(zcontext, 0); | ||||||
|
||||||
stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context); | ||||||
if (!stream) { | ||||||
/* Stream will report errors opening file */ | ||||||
RETURN_FALSE; | ||||||
if (throwOnError && !EG(exception)) { | ||||||
zend_throw_exception_ex(NULL, 0, "Error opening '%s'", ZSTR_VAL(filename)); | ||||||
} | ||||||
return false; | ||||||
} | ||||||
|
||||||
while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) { | ||||||
hash->ops->hash_update(hash->context, (unsigned char *) buf, n); | ||||||
didcopy += n; | ||||||
} | ||||||
php_stream_close(stream); | ||||||
|
||||||
RETURN_BOOL(n >= 0); | ||||||
if (throwOnError && (n < 0) && !EG(exception)) { | ||||||
zend_throw_exception_ex(NULL, 0, "Error while reading file, %zd bytes copied", didcopy); | ||||||
} | ||||||
return (n >= 0); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ Output resulting digest */ | ||||||
PHP_FUNCTION(hash_final) | ||||||
static void do_hash_final(zval *return_value, php_hashcontext_object *hash, zend_bool raw_output) | ||||||
{ | ||||||
zval *zhash; | ||||||
php_hashcontext_object *hash; | ||||||
zend_bool raw_output = 0; | ||||||
zend_string *digest; | ||||||
size_t digest_len; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
size_t digest_len = hash->ops->digest_size; | ||||||
zend_string *digest = zend_string_alloc(digest_len, 0); | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
|
||||||
digest_len = hash->ops->digest_size; | ||||||
digest = zend_string_alloc(digest_len, 0); | ||||||
hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context); | ||||||
if (hash->options & PHP_HASH_HMAC) { | ||||||
size_t i, block_size; | ||||||
|
@@ -818,6 +770,97 @@ PHP_FUNCTION(hash_final) | |||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ Initialize a hashing context */ | ||||||
PHP_FUNCTION(hash_init) | ||||||
{ | ||||||
do_hash_init(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL); | ||||||
} | ||||||
/* {{{ */ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
#define PHP_HASHCONTEXT_VERIFY(hash) { \ | ||||||
if (!hash->context) { \ | ||||||
zend_argument_type_error(1, "must be a valid Hash Context resource"); \ | ||||||
RETURN_THROWS(); \ | ||||||
} \ | ||||||
} | ||||||
|
||||||
/* {{{ Pump data into the hashing algorithm */ | ||||||
PHP_FUNCTION(hash_update) | ||||||
{ | ||||||
zval *zhash; | ||||||
php_hashcontext_object *hash; | ||||||
zend_string *data; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zhash, php_hashcontext_ce, &data) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data)); | ||||||
|
||||||
RETURN_TRUE; | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ Pump data into the hashing algorithm from an open stream */ | ||||||
PHP_FUNCTION(hash_update_stream) | ||||||
{ | ||||||
zval *zhash, *zstream; | ||||||
php_hashcontext_object *hash; | ||||||
php_stream *stream = NULL; | ||||||
zend_long length = -1; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Or|l", &zhash, php_hashcontext_ce, &zstream, &length) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
php_stream_from_zval(stream, zstream); | ||||||
|
||||||
RETURN_LONG(do_update_stream(hash, stream, length, 0)); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ Pump data into the hashing algorithm from a file */ | ||||||
PHP_FUNCTION(hash_update_file) | ||||||
{ | ||||||
zval *zhash, *zcontext = NULL; | ||||||
php_hashcontext_object *hash; | ||||||
php_stream_context *context = NULL; | ||||||
zend_string *filename; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r!", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
context = php_stream_context_from_zval(zcontext, 0); | ||||||
|
||||||
RETURN_BOOL(do_update_file(hash, filename, context, 0)); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ Output resulting digest */ | ||||||
PHP_FUNCTION(hash_final) | ||||||
{ | ||||||
zval *zhash; | ||||||
php_hashcontext_object *hash; | ||||||
zend_bool raw_output = 0; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(zhash)); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
|
||||||
do_hash_final(return_value, hash, raw_output); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ Copy hash object */ | ||||||
PHP_FUNCTION(hash_copy) | ||||||
{ | ||||||
|
@@ -1129,10 +1172,100 @@ PHP_FUNCTION(hash_equals) | |||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ */ | ||||||
PHP_METHOD(HashContext, __construct) { | ||||||
/* Normally unreachable as private/final */ | ||||||
zend_throw_exception(zend_ce_error, "Illegal call to private/final constructor", 0); | ||||||
/* {{{ HashContext::__construct() */ | ||||||
PHP_METHOD(HashContext, __construct) | ||||||
{ | ||||||
do_hash_init(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis()); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ HashContext::update() */ | ||||||
PHP_METHOD(HashContext, update) | ||||||
{ | ||||||
php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(getThis())); | ||||||
zend_string *data; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &data) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data)); | ||||||
|
||||||
RETURN_ZVAL(getThis(), 1, 0); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ HashContext::updateStream() */ | ||||||
PHP_METHOD(HashContext, updateStream) | ||||||
{ | ||||||
php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(getThis())); | ||||||
zval *zstream; | ||||||
php_stream *stream = NULL; | ||||||
zend_long length = -1; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zstream, &length) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
php_stream_from_zval(stream, zstream); | ||||||
|
||||||
do_update_stream(hash, stream, length, 1); | ||||||
RETURN_ZVAL(getThis(), 1, 0); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ HashContext::updateFile() */ | ||||||
PHP_METHOD(HashContext, updateFile) | ||||||
{ | ||||||
php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(getThis())); | ||||||
zval *zcontext = NULL; | ||||||
php_stream_context *context = NULL; | ||||||
zend_string *filename; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|r!", &filename, &zcontext) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
context = php_stream_context_from_zval(zcontext, 0); | ||||||
|
||||||
do_update_file(hash, filename, context, 1); | ||||||
RETURN_ZVAL(getThis(), 1, 0); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ HashContext::final() */ | ||||||
PHP_METHOD(HashContext, final) | ||||||
{ | ||||||
php_hashcontext_object *hash; | ||||||
zend_bool raw_output = 0; | ||||||
|
||||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &raw_output) == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(getThis())); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
|
||||||
do_hash_final(return_value, hash, raw_output); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
/* {{{ HashContext::getAlgo() */ | ||||||
PHP_METHOD(HashContext, getAlgo) | ||||||
{ | ||||||
php_hashcontext_object *hash; | ||||||
|
||||||
if (zend_parse_parameters_none() == FAILURE) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
|
||||||
hash = php_hashcontext_from_object(Z_OBJ_P(getThis())); | ||||||
PHP_HASHCONTEXT_VERIFY(hash); | ||||||
|
||||||
RETURN_STRING(hash->ops->algo); | ||||||
} | ||||||
/* }}} */ | ||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try
(something about 32bit zend_long printf format, check line 57 & 86 here here https://github.com/php/php-src/blob/master/Zend/zend_long.h#L57 )