@@ -345,23 +345,14 @@ PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long ma
345345
346346/* Userspace */
347347
348- static void php_hash_do_hash (INTERNAL_FUNCTION_PARAMETERS , int isfilename , zend_bool raw_output_default ) /* {{{ */
349- {
350- zend_string * digest , * algo ;
351- char * data ;
352- size_t data_len ;
353- zend_bool raw_output = raw_output_default ;
348+ static void php_hash_do_hash (
349+ zval * return_value , zend_string * algo , char * data , size_t data_len , zend_bool raw_output , bool isfilename
350+ ) /* {{{ */ {
351+ zend_string * digest ;
354352 const php_hash_ops * ops ;
355353 void * context ;
356354 php_stream * stream = NULL ;
357355
358- ZEND_PARSE_PARAMETERS_START (2 , 3 )
359- Z_PARAM_STR (algo )
360- Z_PARAM_STRING (data , data_len )
361- Z_PARAM_OPTIONAL
362- Z_PARAM_BOOL (raw_output )
363- ZEND_PARSE_PARAMETERS_END ();
364-
365356 ops = php_hash_fetch_ops (algo );
366357 if (!ops ) {
367358 zend_argument_value_error (1 , "must be a valid hashing algorithm" );
@@ -420,15 +411,39 @@ static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_
420411Returns lowercase hexits by default */
421412PHP_FUNCTION (hash )
422413{
423- php_hash_do_hash (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 , 0 );
414+ zend_string * algo ;
415+ char * data ;
416+ size_t data_len ;
417+ zend_bool raw_output = 0 ;
418+
419+ ZEND_PARSE_PARAMETERS_START (2 , 3 )
420+ Z_PARAM_STR (algo )
421+ Z_PARAM_STRING (data , data_len )
422+ Z_PARAM_OPTIONAL
423+ Z_PARAM_BOOL (raw_output )
424+ ZEND_PARSE_PARAMETERS_END ();
425+
426+ php_hash_do_hash (return_value , algo , data , data_len , raw_output , 0 );
424427}
425428/* }}} */
426429
427430/* {{{ Generate a hash of a given file
428431Returns lowercase hexits by default */
429432PHP_FUNCTION (hash_file )
430433{
431- php_hash_do_hash (INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 , 0 );
434+ zend_string * algo ;
435+ char * data ;
436+ size_t data_len ;
437+ zend_bool raw_output = 0 ;
438+
439+ ZEND_PARSE_PARAMETERS_START (2 , 3 )
440+ Z_PARAM_STR (algo )
441+ Z_PARAM_STRING (data , data_len )
442+ Z_PARAM_OPTIONAL
443+ Z_PARAM_BOOL (raw_output )
444+ ZEND_PARSE_PARAMETERS_END ();
445+
446+ php_hash_do_hash (return_value , algo , data , data_len , raw_output , 1 );
432447}
433448/* }}} */
434449
@@ -467,22 +482,15 @@ static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops
467482 ops -> hash_final (final , context );
468483}
469484
470- static void php_hash_do_hash_hmac (INTERNAL_FUNCTION_PARAMETERS , int isfilename , zend_bool raw_output_default ) /* {{{ */
471- {
472- zend_string * digest , * algo ;
473- char * data , * key ;
485+ static void php_hash_do_hash_hmac (
486+ zval * return_value , zend_string * algo , char * data , size_t data_len , char * key , size_t key_len , zend_bool raw_output , bool isfilename
487+ ) /* {{{ */ {
488+ zend_string * digest ;
474489 unsigned char * K ;
475- size_t data_len , key_len ;
476- zend_bool raw_output = raw_output_default ;
477490 const php_hash_ops * ops ;
478491 void * context ;
479492 php_stream * stream = NULL ;
480493
481- if (zend_parse_parameters (ZEND_NUM_ARGS (), "Sss|b" , & algo , & data , & data_len ,
482- & key , & key_len , & raw_output ) == FAILURE ) {
483- RETURN_THROWS ();
484- }
485-
486494 ops = php_hash_fetch_ops (algo );
487495 if (!ops || !ops -> is_crypto ) {
488496 zend_argument_value_error (1 , "must be a valid cryptographic hashing algorithm" );
@@ -556,15 +564,33 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename,
556564Returns lowercase hexits by default */
557565PHP_FUNCTION (hash_hmac )
558566{
559- php_hash_do_hash_hmac (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 , 0 );
567+ zend_string * algo ;
568+ char * data , * key ;
569+ size_t data_len , key_len ;
570+ zend_bool raw_output = 0 ;
571+
572+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "Sss|b" , & algo , & data , & data_len , & key , & key_len , & raw_output ) == FAILURE ) {
573+ RETURN_THROWS ();
574+ }
575+
576+ php_hash_do_hash_hmac (return_value , algo , data , data_len , key , key_len , raw_output , 0 );
560577}
561578/* }}} */
562579
563580/* {{{ Generate a hash of a given file with a key using HMAC
564581Returns lowercase hexits by default */
565582PHP_FUNCTION (hash_hmac_file )
566583{
567- php_hash_do_hash_hmac (INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 , 0 );
584+ zend_string * algo ;
585+ char * data , * key ;
586+ size_t data_len , key_len ;
587+ zend_bool raw_output = 0 ;
588+
589+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "Sss|b" , & algo , & data , & data_len , & key , & key_len , & raw_output ) == FAILURE ) {
590+ RETURN_THROWS ();
591+ }
592+
593+ php_hash_do_hash_hmac (return_value , algo , data , data_len , key , key_len , raw_output , 1 );
568594}
569595/* }}} */
570596
@@ -1163,29 +1189,27 @@ static void mhash_init(INIT_FUNC_ARGS)
11631189/* {{{ Hash data with hash */
11641190PHP_FUNCTION (mhash )
11651191{
1166- zval * z_algorithm ;
11671192 zend_long algorithm ;
1193+ zend_string * algo = NULL ;
1194+ char * data , * key = NULL ;
1195+ size_t data_len , key_len = 0 ;
11681196
1169- if (zend_parse_parameters (1 , "z " , & z_algorithm ) == FAILURE ) {
1197+ if (zend_parse_parameters (ZEND_NUM_ARGS () , "ls|s! " , & algorithm , & data , & data_len , & key , & key_len ) == FAILURE ) {
11701198 RETURN_THROWS ();
11711199 }
11721200
1173- algorithm = zval_get_long (z_algorithm );
1174-
11751201 /* need to convert the first parameter from int constant to string algorithm name */
11761202 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS ) {
11771203 struct mhash_bc_entry algorithm_lookup = mhash_to_hash [algorithm ];
11781204 if (algorithm_lookup .hash_name ) {
1179- ZVAL_STRING ( z_algorithm , algorithm_lookup .hash_name );
1205+ algo = zend_string_init ( algorithm_lookup . hash_name , strlen ( algorithm_lookup .hash_name ), 1 );
11801206 }
11811207 }
11821208
1183- if (ZEND_NUM_ARGS () == 3 ) {
1184- php_hash_do_hash_hmac (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 , 1 );
1185- } else if (ZEND_NUM_ARGS () == 2 ) {
1186- php_hash_do_hash (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 , 1 );
1209+ if (key ) {
1210+ php_hash_do_hash_hmac (return_value , algo , data , data_len , key , key_len , 1 , 0 );
11871211 } else {
1188- WRONG_PARAM_COUNT ;
1212+ php_hash_do_hash ( return_value , algo , data , data_len , 1 , 0 ) ;
11891213 }
11901214}
11911215/* }}} */
0 commit comments