PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/hash - hash_fnv.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 50 50 100.0 %
Date: 2022-01-29 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2018 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: Michael Maclean <mgdm@php.net>                               |
      16             :   +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : /*  Based on the public domain algorithm found at
      22             :         http://www.isthe.com/chongo/tech/comp/fnv/index.html */
      23             : 
      24             : #include "php_hash.h"
      25             : #include "php_hash_fnv.h"
      26             : 
      27             : const php_hash_ops php_hash_fnv132_ops = {
      28             :         (php_hash_init_func_t) PHP_FNV132Init,
      29             :         (php_hash_update_func_t) PHP_FNV132Update,
      30             :         (php_hash_final_func_t) PHP_FNV132Final,
      31             :         (php_hash_copy_func_t) php_hash_copy,
      32             :         4,
      33             :         4,
      34             :         sizeof(PHP_FNV132_CTX),
      35             :         0
      36             : };
      37             : 
      38             : const php_hash_ops php_hash_fnv1a32_ops = {
      39             :         (php_hash_init_func_t) PHP_FNV132Init,
      40             :         (php_hash_update_func_t) PHP_FNV1a32Update,
      41             :         (php_hash_final_func_t) PHP_FNV132Final,
      42             :         (php_hash_copy_func_t) php_hash_copy,
      43             :         4,
      44             :         4,
      45             :         sizeof(PHP_FNV132_CTX),
      46             :         0
      47             : };
      48             : 
      49             : const php_hash_ops php_hash_fnv164_ops = {
      50             :         (php_hash_init_func_t) PHP_FNV164Init,
      51             :         (php_hash_update_func_t) PHP_FNV164Update,
      52             :         (php_hash_final_func_t) PHP_FNV164Final,
      53             :         (php_hash_copy_func_t) php_hash_copy,
      54             :         8,
      55             :         4,
      56             :         sizeof(PHP_FNV164_CTX),
      57             :         0
      58             : };
      59             : 
      60             : const php_hash_ops php_hash_fnv1a64_ops = {
      61             :         (php_hash_init_func_t) PHP_FNV164Init,
      62             :         (php_hash_update_func_t) PHP_FNV1a64Update,
      63             :         (php_hash_final_func_t) PHP_FNV164Final,
      64             :         (php_hash_copy_func_t) php_hash_copy,
      65             :         8,
      66             :         4,
      67             :         sizeof(PHP_FNV164_CTX),
      68             :         0
      69             : };
      70             : 
      71             : /* {{{ PHP_FNV132Init
      72             :  * 32-bit FNV-1 hash initialisation
      73             :  */
      74         483 : PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context)
      75             : {
      76         483 :         context->state = PHP_FNV1_32_INIT;
      77         483 : }
      78             : /* }}} */
      79             : 
      80         173 : PHP_HASH_API void PHP_FNV132Update(PHP_FNV132_CTX *context, const unsigned char *input,
      81             :                 unsigned int inputLen)
      82             : {
      83         173 :         context->state = fnv_32_buf((void *)input, inputLen, context->state, 0);
      84         173 : }
      85             : 
      86         306 : PHP_HASH_API void PHP_FNV1a32Update(PHP_FNV132_CTX *context, const unsigned char *input,
      87             :                 unsigned int inputLen)
      88             : {
      89         306 :         context->state = fnv_32_buf((void *)input, inputLen, context->state, 1);
      90         306 : }
      91             : 
      92         483 : PHP_HASH_API void PHP_FNV132Final(unsigned char digest[4], PHP_FNV132_CTX * context)
      93             : {
      94             : #ifdef WORDS_BIGENDIAN
      95             :         memcpy(digest, &context->state, 4);
      96             : #else
      97         483 :         int i = 0;
      98         483 :         unsigned char *c = (unsigned char *) &context->state;
      99             : 
     100        2415 :         for (i = 0; i < 4; i++) {
     101        1932 :                 digest[i] = c[3 - i];
     102             :         }
     103             : #endif
     104         483 : }
     105             : 
     106             : /* {{{ PHP_FNV164Init
     107             :  * 64-bit FNV-1 hash initialisation
     108             :  */
     109         483 : PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context)
     110             : {
     111         483 :         context->state = PHP_FNV1_64_INIT;
     112         483 : }
     113             : /* }}} */
     114             : 
     115         173 : PHP_HASH_API void PHP_FNV164Update(PHP_FNV164_CTX *context, const unsigned char *input,
     116             :                 unsigned int inputLen)
     117             : {
     118         173 :         context->state = fnv_64_buf((void *)input, inputLen, context->state, 0);
     119         173 : }
     120             : 
     121         306 : PHP_HASH_API void PHP_FNV1a64Update(PHP_FNV164_CTX *context, const unsigned char *input,
     122             :                 unsigned int inputLen)
     123             : {
     124         306 :         context->state = fnv_64_buf((void *)input, inputLen, context->state, 1);
     125         306 : }
     126             : 
     127         483 : PHP_HASH_API void PHP_FNV164Final(unsigned char digest[8], PHP_FNV164_CTX * context)
     128             : {
     129             : #ifdef WORDS_BIGENDIAN
     130             :         memcpy(digest, &context->state, 8);
     131             : #else
     132         483 :         int i = 0;
     133         483 :         unsigned char *c = (unsigned char *) &context->state;
     134             : 
     135        4347 :         for (i = 0; i < 8; i++) {
     136        3864 :                 digest[i] = c[7 - i];
     137             :         }
     138             : #endif
     139         483 : }
     140             : 
     141             : 
     142             : /*
     143             :  * fnv_32_buf - perform a 32 bit Fowler/Noll/Vo hash on a buffer
     144             :  *
     145             :  * input:
     146             :  *  buf - start of buffer to hash
     147             :  *  len - length of buffer in octets
     148             :  *  hval        - previous hash value or 0 if first call
     149             :  *  alternate - if > 0 use the alternate version
     150             :  *
     151             :  * returns:
     152             :  *  32 bit hash as a static hash type
     153             :  */
     154             : static uint32_t
     155         479 : fnv_32_buf(void *buf, size_t len, uint32_t hval, int alternate)
     156             : {
     157         479 :         unsigned char *bp = (unsigned char *)buf;   /* start of buffer */
     158         479 :         unsigned char *be = bp + len;      /* beyond end of buffer */
     159             : 
     160             :         /*
     161             :          * FNV-1 hash each octet in the buffer
     162             :          */
     163       17234 :         while (bp < be) {
     164             : 
     165       16276 :                 if (alternate == 0) {
     166             :                         /* multiply by the 32 bit FNV magic prime mod 2^32 */
     167        6952 :                         hval *= PHP_FNV_32_PRIME;
     168             : 
     169             :                         /* xor the bottom with the current octet */
     170        6952 :                         hval ^= (uint32_t)*bp++;
     171             :                 } else {
     172             :                         /* xor the bottom with the current octet */
     173        9324 :                         hval ^= (uint32_t)*bp++;
     174             : 
     175             :                         /* multiply by the 32 bit FNV magic prime mod 2^32 */
     176        9324 :                         hval *= PHP_FNV_32_PRIME;
     177             :                 }
     178             :         }
     179             : 
     180             :         /* return our new hash value */
     181         479 :         return hval;
     182             : }
     183             : 
     184             : /*
     185             :  * fnv_64_buf - perform a 64 bit Fowler/Noll/Vo hash on a buffer
     186             :  *
     187             :  * input:
     188             :  *  buf - start of buffer to hash
     189             :  *  len - length of buffer in octets
     190             :  *  hval        - previous hash value or 0 if first call
     191             :  *  alternate - if > 0 use the alternate version
     192             :  *
     193             :  * returns:
     194             :  *  64 bit hash as a static hash type
     195             :  */
     196             : static uint64_t
     197         479 : fnv_64_buf(void *buf, size_t len, uint64_t hval, int alternate)
     198             : {
     199         479 :         unsigned char *bp = (unsigned char *)buf;   /* start of buffer */
     200         479 :         unsigned char *be = bp + len;      /* beyond end of buffer */
     201             : 
     202             :         /*
     203             :          * FNV-1 hash each octet of the buffer
     204             :          */
     205       17234 :         while (bp < be) {
     206             : 
     207       16276 :                 if (alternate == 0) {
     208             :                         /* multiply by the 64 bit FNV magic prime mod 2^64 */
     209        6952 :                         hval *= PHP_FNV_64_PRIME;
     210             : 
     211             :                         /* xor the bottom with the current octet */
     212        6952 :                         hval ^= (uint64_t)*bp++;
     213             :                  } else {
     214             :                         /* xor the bottom with the current octet */
     215        9324 :                         hval ^= (uint64_t)*bp++;
     216             : 
     217             :                         /* multiply by the 64 bit FNV magic prime mod 2^64 */
     218        9324 :                         hval *= PHP_FNV_64_PRIME;
     219             :                  }
     220             :         }
     221             : 
     222             :         /* return our new hash value */
     223         479 :         return hval;
     224             : }
     225             : 
     226             : /*
     227             :  * Local variables:
     228             :  * tab-width: 4
     229             :  * c-basic-offset: 4
     230             :  * End:
     231             :  * vim600: noet sw=4 ts=4 fdm=marker
     232             :  * vim<600: noet sw=4 ts=4
     233             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Jan 2022 07:14:32 +0000 (13 minutes ago)

Copyright © 2005-2022 The PHP Group
All rights reserved.