LTP GCOV extension - code coverage report
Current view: directory - ext/hash - hash.c
Test: PHP Code Coverage
Date: 2007-04-10 Instrumented lines: 283
Code covered: 21.2 % Executed lines: 60
Legend: not executed executed

       1                 : /*
       2                 :   +----------------------------------------------------------------------+
       3                 :   | PHP Version 5                                                        |
       4                 :   +----------------------------------------------------------------------+
       5                 :   | Copyright (c) 1997-2007 The PHP Group                                |
       6                 :   +----------------------------------------------------------------------+
       7                 :   | This source file is subject to version 3.01 of the PHP license,      |
       8                 :   | that is bundled with this package in the file LICENSE, and is        |
       9                 :   | available through the world-wide-web at the following url:           |
      10                 :   | http://www.php.net/license/3_01.txt                                  |
      11                 :   | If you did not receive a copy of the PHP license and are unable to   |
      12                 :   | obtain it through the world-wide-web, please send a note to          |
      13                 :   | license@php.net so we can mail you a copy immediately.               |
      14                 :   +----------------------------------------------------------------------+
      15                 :   | Author: Sara Golemon <pollita@php.net>                               |
      16                 :   +----------------------------------------------------------------------+
      17                 : */
      18                 : 
      19                 : /* $Id: hash.c,v 1.18.2.5.2.7 2007/02/27 03:28:16 iliaa Exp $ */
      20                 : 
      21                 : #ifdef HAVE_CONFIG_H
      22                 : #include "config.h"
      23                 : #endif
      24                 : 
      25                 : #include "php_hash.h"
      26                 : #include "ext/standard/info.h"
      27                 : #include "ext/standard/file.h"
      28                 : 
      29                 : static int php_hash_le_hash;
      30                 : HashTable php_hash_hashtable;
      31                 : 
      32                 : #if (PHP_MAJOR_VERSION >= 5)
      33                 : # define DEFAULT_CONTEXT FG(default_context)
      34                 : #else
      35                 : # define DEFAULT_CONTEXT NULL
      36                 : #endif
      37                 : 
      38                 : /* Hash Registry Access */
      39                 : 
      40                 : PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, int algo_len)
      41              36 : {
      42                 :         php_hash_ops *ops;
      43              36 :         char *lower = estrndup(algo, algo_len);
      44                 : 
      45              36 :         zend_str_tolower(lower, algo_len);
      46              36 :         if (SUCCESS != zend_hash_find(&php_hash_hashtable, lower, algo_len + 1, (void*)&ops)) {
      47               3 :                 ops = NULL;
      48                 :         }
      49              36 :         efree(lower);
      50                 : 
      51              36 :         return ops;
      52                 : }
      53                 : 
      54                 : PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops)
      55            8360 : {
      56            8360 :         int algo_len = strlen(algo);
      57            8360 :         char *lower = estrndup(algo, algo_len);
      58                 :         
      59            8360 :         zend_str_tolower(lower, algo_len);
      60            8360 :         zend_hash_add(&php_hash_hashtable, lower, algo_len + 1, (void*)ops, sizeof(php_hash_ops), NULL);
      61            8360 :         efree(lower);
      62            8360 : }
      63                 : 
      64                 : /* Userspace */
      65                 : 
      66                 : static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename)
      67               0 : {
      68                 :         char *algo, *data, *digest;
      69                 :         int algo_len, data_len;
      70               0 :         zend_bool raw_output = 0;
      71                 :         const php_hash_ops *ops;
      72                 :         void *context;
      73               0 :         php_stream *stream = NULL;
      74                 : 
      75               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &algo, &algo_len, &data, &data_len, &raw_output) == FAILURE) {
      76               0 :                 return;
      77                 :         }
      78                 : 
      79               0 :         ops = php_hash_fetch_ops(algo, algo_len);
      80               0 :         if (!ops) {
      81               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
      82               0 :                 RETURN_FALSE;
      83                 :         }
      84               0 :         if (isfilename) {
      85               0 :                 stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL, DEFAULT_CONTEXT);
      86               0 :                 if (!stream) {
      87                 :                         /* Stream will report errors opening file */
      88               0 :                         RETURN_FALSE;
      89                 :                 }
      90                 :         }
      91                 : 
      92               0 :         context = emalloc(ops->context_size);
      93               0 :         ops->hash_init(context);
      94                 : 
      95               0 :         if (isfilename) {
      96                 :                 char buf[1024];
      97                 :                 int n;
      98                 : 
      99               0 :                 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
     100               0 :                         ops->hash_update(context, (unsigned char *) buf, n);
     101                 :                 }
     102               0 :                 php_stream_close(stream);
     103                 :         } else {
     104               0 :                 ops->hash_update(context, (unsigned char *) data, data_len);
     105                 :         }
     106                 : 
     107               0 :         digest = emalloc(ops->digest_size + 1);
     108               0 :         ops->hash_final((unsigned char *) digest, context);
     109               0 :         efree(context);
     110                 : 
     111               0 :         if (raw_output) {
     112               0 :                 digest[ops->digest_size] = 0;
     113               0 :                 RETURN_STRINGL(digest, ops->digest_size, 0);
     114                 :         } else {
     115               0 :                 char *hex_digest = safe_emalloc(ops->digest_size, 2, 1);
     116                 : 
     117               0 :                 php_hash_bin2hex(hex_digest, (unsigned char *) digest, ops->digest_size);
     118               0 :                 hex_digest[2 * ops->digest_size] = 0;
     119               0 :                 efree(digest);
     120               0 :                 RETURN_STRINGL(hex_digest, 2 * ops->digest_size, 0);
     121                 :         }
     122                 : }
     123                 : 
     124                 : /* {{{ proto string hash(string algo, string data[, bool raw_output = false])
     125                 : Generate a hash of a given input string
     126                 : Returns lowercase hexits by default */
     127               0 : PHP_FUNCTION(hash) {
     128               0 :         php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     129               0 : }
     130                 : /* }}} */
     131                 : 
     132                 : /* {{{ proto string hash_file(string algo, string filename[, bool raw_output = false])
     133                 : Generate a hash of a given file
     134                 : Returns lowercase hexits by default */
     135               0 : PHP_FUNCTION(hash_file) {
     136               0 :         php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     137               0 : }
     138                 : /* }}} */
     139                 : 
     140                 : static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename)
     141               0 : {
     142                 :         char *algo, *data, *digest, *key, *K;
     143                 :         int algo_len, data_len, key_len, i;
     144               0 :         zend_bool raw_output = 0;
     145                 :         const php_hash_ops *ops;
     146                 :         void *context;
     147               0 :         php_stream *stream = NULL;
     148                 : 
     149               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|b", &algo, &algo_len, &data, &data_len, 
     150                 :                                                                                                                                   &key, &key_len, &raw_output) == FAILURE) {
     151               0 :                 return;
     152                 :         }
     153                 : 
     154               0 :         ops = php_hash_fetch_ops(algo, algo_len);
     155               0 :         if (!ops) {
     156               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
     157               0 :                 RETURN_FALSE;
     158                 :         }
     159               0 :         if (isfilename) {
     160               0 :                 stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL, DEFAULT_CONTEXT);
     161               0 :                 if (!stream) {
     162                 :                         /* Stream will report errors opening file */
     163               0 :                         RETURN_FALSE;
     164                 :                 }
     165                 :         }
     166                 : 
     167               0 :         context = emalloc(ops->context_size);
     168               0 :         ops->hash_init(context);
     169                 : 
     170               0 :         K = emalloc(ops->block_size);
     171               0 :         memset(K, 0, ops->block_size);
     172                 : 
     173               0 :         if (key_len > ops->block_size) {
     174                 :                 /* Reduce the key first */
     175               0 :                 ops->hash_update(context, (unsigned char *) key, key_len);
     176               0 :                 ops->hash_final((unsigned char *) K, context);
     177                 :                 /* Make the context ready to start over */
     178               0 :                 ops->hash_init(context);
     179                 :         } else {
     180               0 :                 memcpy(K, key, key_len);
     181                 :         }
     182                 :                         
     183                 :         /* XOR ipad */
     184               0 :         for(i=0; i < ops->block_size; i++) {
     185               0 :                 K[i] ^= 0x36;
     186                 :         }
     187               0 :         ops->hash_update(context, (unsigned char *) K, ops->block_size);
     188                 : 
     189               0 :         if (isfilename) {
     190                 :                 char buf[1024];
     191                 :                 int n;
     192                 : 
     193               0 :                 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
     194               0 :                         ops->hash_update(context, (unsigned char *) buf, n);
     195                 :                 }
     196               0 :                 php_stream_close(stream);
     197                 :         } else {
     198               0 :                 ops->hash_update(context, (unsigned char *) data, data_len);
     199                 :         }
     200                 : 
     201               0 :         digest = emalloc(ops->digest_size + 1);
     202               0 :         ops->hash_final((unsigned char *) digest, context);
     203                 : 
     204                 :         /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
     205               0 :         for(i=0; i < ops->block_size; i++) {
     206               0 :                 K[i] ^= 0x6A;
     207                 :         }
     208                 : 
     209                 :         /* Feed this result into the outter hash */
     210               0 :         ops->hash_init(context);
     211               0 :         ops->hash_update(context, (unsigned char *) K, ops->block_size);
     212               0 :         ops->hash_update(context, (unsigned char *) digest, ops->digest_size);
     213               0 :         ops->hash_final((unsigned char *) digest, context);
     214                 : 
     215                 :         /* Zero the key */
     216               0 :         memset(K, 0, ops->block_size);
     217               0 :         efree(K);
     218               0 :         efree(context);
     219                 : 
     220               0 :         if (raw_output) {
     221               0 :                 digest[ops->digest_size] = 0;
     222               0 :                 RETURN_STRINGL(digest, ops->digest_size, 0);
     223                 :         } else {
     224               0 :                 char *hex_digest = safe_emalloc(ops->digest_size, 2, 1);
     225                 : 
     226               0 :                 php_hash_bin2hex(hex_digest, (unsigned char *) digest, ops->digest_size);
     227               0 :                 hex_digest[2 * ops->digest_size] = 0;
     228               0 :                 efree(digest);
     229               0 :                 RETURN_STRINGL(hex_digest, 2 * ops->digest_size, 0);
     230                 :         }
     231                 : }
     232                 : 
     233                 : /* {{{ proto string hash_hmac(string algo, string data, string key[, bool raw_output = false])
     234                 : Generate a hash of a given input string with a key using HMAC
     235                 : Returns lowercase hexits by default */
     236               0 : PHP_FUNCTION(hash_hmac) {
     237               0 :         php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     238               0 : }
     239                 : /* }}} */
     240                 : 
     241                 : /* {{{ proto string hash_hmac_file(string algo, string filename, string key[, bool raw_output = false])
     242                 : Generate a hash of a given file with a key using HMAC
     243                 : Returns lowercase hexits by default */
     244               0 : PHP_FUNCTION(hash_hmac_file) {
     245               0 :         php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     246               0 : }
     247                 : /* }}} */
     248                 : 
     249                 : 
     250                 : /* {{{ proto resource hash_init(string algo[, int options, string key])
     251                 : Initialize a hashing context */
     252                 : PHP_FUNCTION(hash_init)
     253               0 : {
     254               0 :         char *algo, *key = NULL;
     255               0 :         int algo_len, key_len = 0, argc = ZEND_NUM_ARGS();
     256               0 :         long options = 0;
     257                 :         void *context;
     258                 :         const php_hash_ops *ops;
     259                 :         php_hash_data *hash;
     260                 : 
     261               0 :         if (zend_parse_parameters(argc TSRMLS_CC, "s|ls", &algo, &algo_len, &options, &key, &key_len) == FAILURE) {
     262               0 :                 return;
     263                 :         }
     264                 : 
     265               0 :         ops = php_hash_fetch_ops(algo, algo_len);
     266               0 :         if (!ops) {
     267               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
     268               0 :                 RETURN_FALSE;
     269                 :         }
     270                 : 
     271               0 :         if (options & PHP_HASH_HMAC &&
     272                 :                 key_len <= 0) {
     273                 :                 /* Note: a zero length key is no key at all */
     274               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "HMAC requested without a key");
     275               0 :                 RETURN_FALSE;
     276                 :         }
     277                 : 
     278               0 :         context = emalloc(ops->context_size);
     279               0 :         ops->hash_init(context);
     280                 : 
     281               0 :         hash = emalloc(sizeof(php_hash_data));
     282               0 :         hash->ops = ops;
     283               0 :         hash->context = context;
     284               0 :         hash->options = options;
     285               0 :         hash->key = NULL;
     286                 : 
     287               0 :         if (options & PHP_HASH_HMAC) {
     288               0 :                 char *K = emalloc(ops->block_size);
     289                 :                 int i;
     290                 : 
     291               0 :                 memset(K, 0, ops->block_size);
     292                 : 
     293               0 :                 if (key_len > ops->block_size) {
     294                 :                         /* Reduce the key first */
     295               0 :                         ops->hash_update(context, (unsigned char *) key, key_len);
     296               0 :                         ops->hash_final((unsigned char *) K, context);
     297                 :                         /* Make the context ready to start over */
     298               0 :                         ops->hash_init(context);
     299                 :                 } else {
     300               0 :                         memcpy(K, key, key_len);
     301                 :                 }
     302                 :                         
     303                 :                 /* XOR ipad */
     304               0 :                 for(i=0; i < ops->block_size; i++) {
     305               0 :                         K[i] ^= 0x36;
     306                 :                 }
     307               0 :                 ops->hash_update(context, (unsigned char *) K, ops->block_size);
     308               0 :                 hash->key = (unsigned char *) K;
     309                 :         }
     310                 : 
     311               0 :         ZEND_REGISTER_RESOURCE(return_value, hash, php_hash_le_hash);
     312                 : }
     313                 : /* }}} */
     314                 : 
     315                 : /* {{{ proto bool hash_update(resource context, string data)
     316                 : Pump data into the hashing algorithm */
     317                 : PHP_FUNCTION(hash_update)
     318               0 : {
     319                 :         zval *zhash;
     320                 :         php_hash_data *hash;
     321                 :         char *data;
     322                 :         int data_len;
     323                 : 
     324               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zhash, &data, &data_len) == FAILURE) {
     325               0 :                 return;
     326                 :         }
     327                 : 
     328               0 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, &zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     329                 : 
     330               0 :         hash->ops->hash_update(hash->context, (unsigned char *) data, data_len);
     331                 : 
     332               0 :         RETURN_TRUE;
     333                 : }
     334                 : /* }}} */
     335                 : 
     336                 : /* {{{ proto int hash_update_stream(resource context, resource handle[, integer length])
     337                 : Pump data into the hashing algorithm from an open stream */
     338                 : PHP_FUNCTION(hash_update_stream)
     339               0 : {
     340                 :         zval *zhash, *zstream;
     341                 :         php_hash_data *hash;
     342               0 :         php_stream *stream = NULL;
     343               0 :         long length = -1, didread = 0;
     344                 : 
     345               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l", &zhash, &zstream, &length) == FAILURE) {
     346               0 :                 return;
     347                 :         }
     348                 : 
     349               0 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, &zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     350               0 :         php_stream_from_zval(stream, &zstream);
     351                 : 
     352               0 :         while (length) {
     353                 :                 char buf[1024];
     354               0 :                 long n, toread = 1024;
     355                 : 
     356               0 :                 if (length > 0 && toread > length) {
     357               0 :                         toread = length;
     358                 :                 }
     359                 : 
     360               0 :                 if ((n = php_stream_read(stream, buf, toread)) <= 0) {
     361                 :                         /* Nada mas */
     362               0 :                         RETURN_LONG(didread);
     363                 :                 }
     364               0 :                 hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
     365               0 :                 length -= n;
     366               0 :                 didread += n;
     367                 :         } 
     368                 : 
     369               0 :         RETURN_LONG(didread);
     370                 : }
     371                 : /* }}} */
     372                 : 
     373                 : /* {{{ proto bool hash_update_file(resource context, string filename[, resource context])
     374                 : Pump data into the hashing algorithm from a file */
     375                 : PHP_FUNCTION(hash_update_file)
     376               0 : {
     377               0 :         zval *zhash, *zcontext = NULL;
     378                 :         php_hash_data *hash;
     379                 :         php_stream_context *context;
     380                 :         php_stream *stream;
     381                 :         char *filename, buf[1024];
     382                 :         int filename_len, n;
     383                 : 
     384               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|r", &zhash, &filename, &filename_len, &zcontext) == FAILURE) {
     385               0 :                 return;
     386                 :         }
     387                 : 
     388               0 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, &zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     389               0 :         context = php_stream_context_from_zval(zcontext, 0);
     390                 : 
     391               0 :         stream = php_stream_open_wrapper_ex(filename, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL, context);
     392               0 :         if (!stream) {
     393                 :                 /* Stream will report errors opening file */
     394               0 :                 RETURN_FALSE;
     395                 :         }
     396                 : 
     397               0 :         while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
     398               0 :                 hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
     399                 :         }
     400               0 :         php_stream_close(stream);
     401                 : 
     402               0 :         RETURN_TRUE;
     403                 : }
     404                 : /* }}} */
     405                 : 
     406                 : /* {{{ proto string hash_final(resource context[, bool raw_output=false])
     407                 : Output resulting digest */
     408                 : PHP_FUNCTION(hash_final)
     409               0 : {
     410                 :         zval *zhash;
     411                 :         php_hash_data *hash;
     412               0 :         zend_bool raw_output = 0;
     413                 :         zend_rsrc_list_entry *le;
     414                 :         char *digest;
     415                 :         int digest_len;
     416                 : 
     417               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|b", &zhash, &raw_output) == FAILURE) {
     418               0 :                 return;
     419                 :         }
     420                 : 
     421               0 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, &zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     422                 : 
     423               0 :         digest_len = hash->ops->digest_size;
     424               0 :         digest = emalloc(digest_len + 1);
     425               0 :         hash->ops->hash_final((unsigned char *) digest, hash->context);
     426               0 :         if (hash->options & PHP_HASH_HMAC) {
     427                 :                 int i;
     428                 : 
     429                 :                 /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
     430               0 :                 for(i=0; i < hash->ops->block_size; i++) {
     431               0 :                         hash->key[i] ^= 0x6A;
     432                 :                 }
     433                 : 
     434                 :                 /* Feed this result into the outter hash */
     435               0 :                 hash->ops->hash_init(hash->context);
     436               0 :                 hash->ops->hash_update(hash->context, (unsigned char *) hash->key, hash->ops->block_size);
     437               0 :                 hash->ops->hash_update(hash->context, (unsigned char *) digest, hash->ops->digest_size);
     438               0 :                 hash->ops->hash_final((unsigned char *) digest, hash->context);
     439                 : 
     440                 :                 /* Zero the key */
     441               0 :                 memset(hash->key, 0, hash->ops->block_size);
     442               0 :                 efree(hash->key);
     443               0 :                 hash->key = NULL;
     444                 :         }
     445               0 :         digest[digest_len] = 0;
     446               0 :         efree(hash->context);
     447               0 :         hash->context = NULL;
     448                 : 
     449                 :         /* zend_list_REAL_delete() */
     450               0 :         if (zend_hash_index_find(&EG(regular_list), Z_RESVAL_P(zhash), (void *) &le)==SUCCESS) {
     451                 :                 /* This is a hack to avoid letting the resource hide elsewhere (like in separated vars)
     452                 :                         FETCH_RESOURCE is intelligent enough to handle dealing with any issues this causes */
     453               0 :                 le->refcount = 1;
     454                 :         } /* FAILURE is not an option */
     455               0 :         zend_list_delete(Z_RESVAL_P(zhash));
     456                 : 
     457               0 :         if (raw_output) {
     458               0 :                 RETURN_STRINGL(digest, digest_len, 0);
     459                 :         } else {
     460               0 :                 char *hex_digest = safe_emalloc(digest_len,2,1);
     461                 : 
     462               0 :                 php_hash_bin2hex(hex_digest, (unsigned char *) digest, digest_len);
     463               0 :                 hex_digest[2 * digest_len] = 0;
     464               0 :                 efree(digest);
     465               0 :                 RETURN_STRINGL(hex_digest, 2 * digest_len, 0);          
     466                 :         }
     467                 : }
     468                 : /* }}} */
     469                 : 
     470                 : /* {{{ proto array hash_algos(void)
     471                 : Return a list of registered hashing algorithms */
     472                 : PHP_FUNCTION(hash_algos)
     473               0 : {
     474                 :         HashPosition pos;
     475                 :         char *str;
     476                 :         uint str_len;
     477                 :         long type;
     478                 :         ulong idx;
     479                 : 
     480               0 :         array_init(return_value);
     481               0 :         for(zend_hash_internal_pointer_reset_ex(&php_hash_hashtable, &pos);
     482               0 :                 (type = zend_hash_get_current_key_ex(&php_hash_hashtable, &str, &str_len, &idx, 0, &pos)) != HASH_KEY_NON_EXISTANT;
     483               0 :                 zend_hash_move_forward_ex(&php_hash_hashtable, &pos)) {
     484               0 :                 add_next_index_stringl(return_value, str, str_len-1, 1);
     485                 :         }
     486               0 : }
     487                 : /* }}} */
     488                 : 
     489                 : /* Module Housekeeping */
     490                 : 
     491                 : static void php_hash_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     492               0 : {
     493               0 :         php_hash_data *hash = (php_hash_data*)rsrc->ptr;
     494                 : 
     495                 :         /* Just in case the algo has internally allocated resources */
     496               0 :         if (hash->context) {
     497               0 :                 unsigned char *dummy = emalloc(hash->ops->digest_size);
     498               0 :                 hash->ops->hash_final(dummy, hash->context);
     499               0 :                 efree(dummy);
     500               0 :                 efree(hash->context);
     501                 :         }
     502                 : 
     503               0 :         if (hash->key) {
     504               0 :                 memset(hash->key, 0, hash->ops->block_size);
     505               0 :                 efree(hash->key);
     506                 :         }
     507               0 :         efree(hash);
     508               0 : }
     509                 : 
     510                 : #define PHP_HASH_HAVAL_REGISTER(p,b)    php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
     511                 : 
     512                 : /* {{{ PHP_MINIT_FUNCTION
     513                 :  */
     514                 : PHP_MINIT_FUNCTION(hash)
     515             220 : {
     516             220 :         php_hash_le_hash = zend_register_list_destructors_ex(php_hash_dtor, NULL, PHP_HASH_RESNAME, module_number);
     517                 : 
     518             220 :         zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
     519                 : 
     520             220 :         php_hash_register_algo("md2",                 &php_hash_md2_ops);
     521             220 :         php_hash_register_algo("md4",                 &php_hash_md4_ops);
     522             220 :         php_hash_register_algo("md5",                 &php_hash_md5_ops);
     523             220 :         php_hash_register_algo("sha1",                        &php_hash_sha1_ops);
     524             220 :         php_hash_register_algo("sha256",              &php_hash_sha256_ops);
     525             220 :         php_hash_register_algo("sha384",              &php_hash_sha384_ops);
     526             220 :         php_hash_register_algo("sha512",              &php_hash_sha512_ops);
     527             220 :         php_hash_register_algo("ripemd128",           &php_hash_ripemd128_ops);
     528             220 :         php_hash_register_algo("ripemd160",           &php_hash_ripemd160_ops);
     529             220 :         php_hash_register_algo("ripemd256",           &php_hash_ripemd256_ops);
     530             220 :         php_hash_register_algo("ripemd320",           &php_hash_ripemd320_ops);
     531             220 :         php_hash_register_algo("whirlpool",           &php_hash_whirlpool_ops);
     532             220 :         php_hash_register_algo("tiger128,3",  &php_hash_3tiger128_ops);
     533             220 :         php_hash_register_algo("tiger160,3",  &php_hash_3tiger160_ops);
     534             220 :         php_hash_register_algo("tiger192,3",  &php_hash_3tiger192_ops);
     535             220 :         php_hash_register_algo("tiger128,4",  &php_hash_4tiger128_ops);
     536             220 :         php_hash_register_algo("tiger160,4",  &php_hash_4tiger160_ops);
     537             220 :         php_hash_register_algo("tiger192,4",  &php_hash_4tiger192_ops);
     538             220 :         php_hash_register_algo("snefru",              &php_hash_snefru_ops);
     539             220 :         php_hash_register_algo("gost",                        &php_hash_gost_ops);
     540             220 :         php_hash_register_algo("adler32",             &php_hash_adler32_ops);
     541             220 :         php_hash_register_algo("crc32",                       &php_hash_crc32_ops);
     542             220 :         php_hash_register_algo("crc32b",              &php_hash_crc32b_ops);
     543                 : 
     544             220 :         PHP_HASH_HAVAL_REGISTER(3,128);
     545             220 :         PHP_HASH_HAVAL_REGISTER(3,160);
     546             220 :         PHP_HASH_HAVAL_REGISTER(3,192);
     547             220 :         PHP_HASH_HAVAL_REGISTER(3,224);
     548             220 :         PHP_HASH_HAVAL_REGISTER(3,256);
     549                 : 
     550             220 :         PHP_HASH_HAVAL_REGISTER(4,128);
     551             220 :         PHP_HASH_HAVAL_REGISTER(4,160);
     552             220 :         PHP_HASH_HAVAL_REGISTER(4,192);
     553             220 :         PHP_HASH_HAVAL_REGISTER(4,224);
     554             220 :         PHP_HASH_HAVAL_REGISTER(4,256);
     555                 : 
     556             220 :         PHP_HASH_HAVAL_REGISTER(5,128);
     557             220 :         PHP_HASH_HAVAL_REGISTER(5,160);
     558             220 :         PHP_HASH_HAVAL_REGISTER(5,192);
     559             220 :         PHP_HASH_HAVAL_REGISTER(5,224);
     560             220 :         PHP_HASH_HAVAL_REGISTER(5,256);
     561                 : 
     562             220 :         REGISTER_LONG_CONSTANT("HASH_HMAC",           PHP_HASH_HMAC,  CONST_CS | CONST_PERSISTENT);
     563                 : 
     564             220 :         return SUCCESS;
     565                 : }
     566                 : /* }}} */
     567                 : 
     568                 : /* {{{ PHP_MSHUTDOWN_FUNCTION
     569                 :  */
     570                 : PHP_MSHUTDOWN_FUNCTION(hash)
     571             219 : {
     572             219 :         zend_hash_destroy(&php_hash_hashtable);
     573                 : 
     574             219 :         return SUCCESS;
     575                 : }
     576                 : /* }}} */
     577                 : 
     578                 : /* {{{ PHP_MINFO_FUNCTION
     579                 :  */
     580                 : PHP_MINFO_FUNCTION(hash)
     581               0 : {
     582                 :         HashPosition pos;
     583                 :         char buffer[2048];
     584               0 :         char *s = buffer, *e = s + sizeof(buffer), *str;
     585                 :         ulong idx;
     586                 :         long type;
     587                 : 
     588               0 :         for(zend_hash_internal_pointer_reset_ex(&php_hash_hashtable, &pos);
     589               0 :                 (type = zend_hash_get_current_key_ex(&php_hash_hashtable, &str, NULL, &idx, 0, &pos)) != HASH_KEY_NON_EXISTANT;
     590               0 :                 zend_hash_move_forward_ex(&php_hash_hashtable, &pos)) {
     591               0 :                 s += slprintf(s, e - s, "%s ", str);
     592                 :         }
     593               0 :         *s = 0;
     594                 : 
     595               0 :         php_info_print_table_start();
     596               0 :         php_info_print_table_row(2, "hash support", "enabled");
     597               0 :         php_info_print_table_row(2, "Hashing Engines", buffer);
     598               0 :         php_info_print_table_end();
     599               0 : }
     600                 : /* }}} */
     601                 : 
     602                 : /* {{{ arginfo */
     603                 : #ifdef PHP_HASH_MD5_NOT_IN_CORE
     604                 : static
     605                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5, 0, 0, 1)
     606                 :         ZEND_ARG_INFO(0, str)
     607                 :         ZEND_ARG_INFO(0, raw_output)
     608                 : ZEND_END_ARG_INFO()
     609                 : 
     610                 : static
     611                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5_file, 0, 0, 1)
     612                 :         ZEND_ARG_INFO(0, filename)
     613                 :         ZEND_ARG_INFO(0, raw_output)
     614                 : ZEND_END_ARG_INFO()
     615                 : #endif
     616                 : 
     617                 : #ifdef PHP_HASH_SHA1_NOT_IN_CORE
     618                 : static
     619                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1, 0, 0, 1)
     620                 :         ZEND_ARG_INFO(0, str)
     621                 :         ZEND_ARG_INFO(0, raw_output)
     622                 : ZEND_END_ARG_INFO()
     623                 : 
     624                 : static
     625                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1_file, 0, 0, 1)
     626                 :         ZEND_ARG_INFO(0, filename)
     627                 :         ZEND_ARG_INFO(0, raw_output)
     628                 : ZEND_END_ARG_INFO()
     629                 : #endif
     630                 : 
     631                 : static
     632                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash, 0, 0, 2)
     633                 :         ZEND_ARG_INFO(0, algo)
     634                 :         ZEND_ARG_INFO(0, data)
     635                 :         ZEND_ARG_INFO(0, raw_output)
     636                 : ZEND_END_ARG_INFO()
     637                 : 
     638                 : static
     639                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_file, 0, 0, 2)
     640                 :         ZEND_ARG_INFO(0, algo)
     641                 :         ZEND_ARG_INFO(0, filename)
     642                 :         ZEND_ARG_INFO(0, raw_output)
     643                 : ZEND_END_ARG_INFO()
     644                 : 
     645                 : static
     646                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac, 0, 0, 3)
     647                 :         ZEND_ARG_INFO(0, algo)
     648                 :         ZEND_ARG_INFO(0, data)
     649                 :         ZEND_ARG_INFO(0, key)
     650                 :         ZEND_ARG_INFO(0, raw_output)
     651                 : ZEND_END_ARG_INFO()
     652                 : 
     653                 : static
     654                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac_file, 0, 0, 3)
     655                 :         ZEND_ARG_INFO(0, algo)
     656                 :         ZEND_ARG_INFO(0, filename)
     657                 :         ZEND_ARG_INFO(0, key)
     658                 :         ZEND_ARG_INFO(0, raw_output)
     659                 : ZEND_END_ARG_INFO()
     660                 : 
     661                 : static
     662                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_init, 0, 0, 1)
     663                 :         ZEND_ARG_INFO(0, algo)
     664                 :         ZEND_ARG_INFO(0, options)
     665                 :         ZEND_ARG_INFO(0, key)
     666                 : ZEND_END_ARG_INFO()
     667                 : 
     668                 : static
     669                 : ZEND_BEGIN_ARG_INFO(arginfo_hash_update, 0)
     670                 :         ZEND_ARG_INFO(0, context)
     671                 :         ZEND_ARG_INFO(0, data)
     672                 : ZEND_END_ARG_INFO()
     673                 : 
     674                 : static
     675                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_stream, 0, 0, 2)
     676                 :         ZEND_ARG_INFO(0, context)
     677                 :         ZEND_ARG_INFO(0, handle)
     678                 :         ZEND_ARG_INFO(0, length)
     679                 : ZEND_END_ARG_INFO()
     680                 : 
     681                 : static
     682                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_file, 0, 0, 2)
     683                 :         ZEND_ARG_INFO(0, context)
     684                 :         ZEND_ARG_INFO(0, filename)
     685                 :         ZEND_ARG_INFO(0, context)
     686                 : ZEND_END_ARG_INFO()
     687                 : 
     688                 : static
     689                 : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_final, 0, 0, 1)
     690                 :         ZEND_ARG_INFO(0, context)
     691                 :         ZEND_ARG_INFO(0, raw_output)
     692                 : ZEND_END_ARG_INFO()
     693                 : 
     694                 : static
     695                 : ZEND_BEGIN_ARG_INFO(arginfo_hash_algos, 0)
     696                 : ZEND_END_ARG_INFO()
     697                 : 
     698                 : /* }}} */
     699                 : 
     700                 : /* {{{ hash_functions[]
     701                 :  */
     702                 : zend_function_entry hash_functions[] = {
     703                 :         PHP_FE(hash,                                                                    arginfo_hash)
     704                 :         PHP_FE(hash_file,                                                               arginfo_hash_file)
     705                 : 
     706                 :         PHP_FE(hash_hmac,                                                               arginfo_hash_hmac)
     707                 :         PHP_FE(hash_hmac_file,                                                  arginfo_hash_hmac_file)
     708                 : 
     709                 :         PHP_FE(hash_init,                                                               arginfo_hash_init)
     710                 :         PHP_FE(hash_update,                                                             arginfo_hash_update)
     711                 :         PHP_FE(hash_update_stream,                                              arginfo_hash_update_stream)
     712                 :         PHP_FE(hash_update_file,                                                arginfo_hash_update_file)
     713                 :         PHP_FE(hash_final,                                                              arginfo_hash_final)
     714                 : 
     715                 :         PHP_FE(hash_algos,                                                              arginfo_hash_algos)
     716                 : 
     717                 :         /* BC Land */
     718                 : #ifdef PHP_HASH_MD5_NOT_IN_CORE
     719                 :         PHP_NAMED_FE(md5, php_if_md5,                                   arginfo_hash_md5)
     720                 :         PHP_NAMED_FE(md5_file, php_if_md5_file,                 arginfo_hash_md5_file)
     721                 : #endif /* PHP_HASH_MD5_NOT_IN_CORE */
     722                 : 
     723                 : #ifdef PHP_HASH_SHA1_NOT_IN_CORE
     724                 :         PHP_NAMED_FE(sha1, php_if_sha1,                                 arginfo_hash_sha1)
     725                 :         PHP_NAMED_FE(sha1_file, php_if_sha1_file,               arginfo_hash_sha1_file)
     726                 : #endif /* PHP_HASH_SHA1_NOT_IN_CORE */
     727                 : 
     728                 :         {NULL, NULL, NULL}
     729                 : };
     730                 : /* }}} */
     731                 : 
     732                 : /* {{{ hash_module_entry
     733                 :  */
     734                 : zend_module_entry hash_module_entry = {
     735                 : #if ZEND_MODULE_API_NO >= 20010901
     736                 :         STANDARD_MODULE_HEADER,
     737                 : #endif
     738                 :         PHP_HASH_EXTNAME,
     739                 :         hash_functions,
     740                 :         PHP_MINIT(hash),
     741                 :         PHP_MSHUTDOWN(hash),
     742                 :         NULL, /* RINIT */
     743                 :         NULL, /* RSHUTDOWN */
     744                 :         PHP_MINFO(hash),
     745                 : #if ZEND_MODULE_API_NO >= 20010901
     746                 :         PHP_HASH_EXTVER, /* Replace with version number for your extension */
     747                 : #endif
     748                 :         STANDARD_MODULE_PROPERTIES
     749                 : };
     750                 : /* }}} */
     751                 : 
     752                 : #ifdef COMPILE_DL_HASH
     753                 : ZEND_GET_MODULE(hash)
     754                 : #endif
     755                 : 
     756                 : /*
     757                 :  * Local variables:
     758                 :  * tab-width: 4
     759                 :  * c-basic-offset: 4
     760                 :  * End:
     761                 :  * vim600: noet sw=4 ts=4 fdm=marker
     762                 :  * vim<600: noet sw=4 ts=4
     763                 :  */

Generated by: LTP GCOV extension version 1.5