|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | Copyright (c) The PHP Group | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | This source file is subject to version 3.01 of the PHP license, | |
| 6 | + | that is bundled with this package in the file LICENSE, and is | |
| 7 | + | available through the world-wide-web at the following url: | |
| 8 | + | http://www.php.net/license/3_01.txt | |
| 9 | + | If you did not receive a copy of the PHP license and are unable to | |
| 10 | + | obtain it through the world-wide-web, please send a note to | |
| 11 | + | license@php.net so we can mail you a copy immediately. | |
| 12 | + +----------------------------------------------------------------------+ |
| 13 | + | Author: Anatol Belski <ab@php.net> | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "php_hash.h" |
| 18 | +#include "php_hash_murmur.h" |
| 19 | + |
| 20 | +#include "murmur/PMurHash.h" |
| 21 | +#include "murmur/PMurHash128.h" |
| 22 | + |
| 23 | + |
| 24 | +const php_hash_ops php_hash_murmur3a_ops = { |
| 25 | + "murmur3a", |
| 26 | + (php_hash_init_func_t) PHP_MURMUR3AInit, |
| 27 | + (php_hash_update_func_t) PHP_MURMUR3AUpdate, |
| 28 | + (php_hash_final_func_t) PHP_MURMUR3AFinal, |
| 29 | + (php_hash_copy_func_t) PHP_MURMUR3ACopy, |
| 30 | + php_hash_serialize, |
| 31 | + php_hash_unserialize, |
| 32 | + PHP_MURMUR3A_SPEC, |
| 33 | + 4, |
| 34 | + 4, |
| 35 | + sizeof(PHP_MURMUR3A_CTX), |
| 36 | + 0 |
| 37 | +}; |
| 38 | + |
| 39 | +PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx) |
| 40 | +{ |
| 41 | + ctx->h = 0; |
| 42 | + ctx->carry = 0; |
| 43 | + ctx->len = 0; |
| 44 | +} |
| 45 | + |
| 46 | +PHP_HASH_API void PHP_MURMUR3AUpdate(PHP_MURMUR3A_CTX *ctx, const unsigned char *in, size_t len) |
| 47 | +{ |
| 48 | + ctx->len += len; |
| 49 | + PMurHash32_Process(&ctx->h, &ctx->carry, in, len); |
| 50 | +} |
| 51 | + |
| 52 | +PHP_HASH_API void PHP_MURMUR3AFinal(unsigned char digest[4], PHP_MURMUR3A_CTX *ctx) |
| 53 | +{ |
| 54 | + ctx->h = PMurHash32_Result(ctx->h, ctx->carry, ctx->len); |
| 55 | + |
| 56 | + digest[0] = (unsigned char)((ctx->h >> 24) & 0xff); |
| 57 | + digest[1] = (unsigned char)((ctx->h >> 16) & 0xff); |
| 58 | + digest[2] = (unsigned char)((ctx->h >> 8) & 0xff); |
| 59 | + digest[3] = (unsigned char)(ctx->h & 0xff); |
| 60 | +} |
| 61 | + |
| 62 | +PHP_HASH_API int PHP_MURMUR3ACopy(const php_hash_ops *ops, PHP_MURMUR3A_CTX *orig_context, PHP_MURMUR3A_CTX *copy_context) |
| 63 | +{ |
| 64 | + copy_context->h = orig_context->h; |
| 65 | + copy_context->carry = orig_context->carry; |
| 66 | + copy_context->len = orig_context->len; |
| 67 | + return SUCCESS; |
| 68 | +} |
| 69 | + |
| 70 | +const php_hash_ops php_hash_murmur3c_ops = { |
| 71 | + "murmur3c", |
| 72 | + (php_hash_init_func_t) PHP_MURMUR3CInit, |
| 73 | + (php_hash_update_func_t) PHP_MURMUR3CUpdate, |
| 74 | + (php_hash_final_func_t) PHP_MURMUR3CFinal, |
| 75 | + (php_hash_copy_func_t) PHP_MURMUR3CCopy, |
| 76 | + php_hash_serialize, |
| 77 | + php_hash_unserialize, |
| 78 | + PHP_MURMUR3C_SPEC, |
| 79 | + 16, |
| 80 | + 4, |
| 81 | + sizeof(PHP_MURMUR3C_CTX), |
| 82 | + 0 |
| 83 | +}; |
| 84 | + |
| 85 | +PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx) |
| 86 | +{ |
| 87 | + memset(&ctx->h, 0, sizeof ctx->h); |
| 88 | + memset(&ctx->carry, 0, sizeof ctx->carry); |
| 89 | + ctx->len = 0; |
| 90 | +} |
| 91 | + |
| 92 | +PHP_HASH_API void PHP_MURMUR3CUpdate(PHP_MURMUR3C_CTX *ctx, const unsigned char *in, size_t len) |
| 93 | +{ |
| 94 | + ctx->len += len; |
| 95 | + PMurHash128x86_Process(ctx->h, ctx->carry, in, len); |
| 96 | +} |
| 97 | + |
| 98 | +PHP_HASH_API void PHP_MURMUR3CFinal(unsigned char digest[4], PHP_MURMUR3C_CTX *ctx) |
| 99 | +{ |
| 100 | + uint32_t h[4] = {0}; |
| 101 | + PMurHash128x86_Result(ctx->h, ctx->carry, ctx->len, h); |
| 102 | + |
| 103 | + digest[0] = (unsigned char)((h[0] >> 24) & 0xff); |
| 104 | + digest[1] = (unsigned char)((h[0] >> 16) & 0xff); |
| 105 | + digest[2] = (unsigned char)((h[0] >> 8) & 0xff); |
| 106 | + digest[3] = (unsigned char)(h[0] & 0xff); |
| 107 | + digest[4] = (unsigned char)((h[1] >> 24) & 0xff); |
| 108 | + digest[5] = (unsigned char)((h[1] >> 16) & 0xff); |
| 109 | + digest[6] = (unsigned char)((h[1] >> 8) & 0xff); |
| 110 | + digest[7] = (unsigned char)(h[1] & 0xff); |
| 111 | + digest[8] = (unsigned char)((h[2] >> 24) & 0xff); |
| 112 | + digest[9] = (unsigned char)((h[2] >> 16) & 0xff); |
| 113 | + digest[10] = (unsigned char)((h[2] >> 8) & 0xff); |
| 114 | + digest[11] = (unsigned char)(h[2] & 0xff); |
| 115 | + digest[12] = (unsigned char)((h[3] >> 24) & 0xff); |
| 116 | + digest[13] = (unsigned char)((h[3] >> 16) & 0xff); |
| 117 | + digest[14] = (unsigned char)((h[3] >> 8) & 0xff); |
| 118 | + digest[15] = (unsigned char)(h[3] & 0xff); |
| 119 | +} |
| 120 | + |
| 121 | +PHP_HASH_API int PHP_MURMUR3CCopy(const php_hash_ops *ops, PHP_MURMUR3C_CTX *orig_context, PHP_MURMUR3C_CTX *copy_context) |
| 122 | +{ |
| 123 | + memcpy(©_context->h, &orig_context->h, sizeof orig_context->h); |
| 124 | + memcpy(©_context->carry, &orig_context->carry, sizeof orig_context->carry); |
| 125 | + copy_context->len = orig_context->len; |
| 126 | + return SUCCESS; |
| 127 | +} |
| 128 | + |
| 129 | +const php_hash_ops php_hash_murmur3f_ops = { |
| 130 | + "murmur3f", |
| 131 | + (php_hash_init_func_t) PHP_MURMUR3FInit, |
| 132 | + (php_hash_update_func_t) PHP_MURMUR3FUpdate, |
| 133 | + (php_hash_final_func_t) PHP_MURMUR3FFinal, |
| 134 | + (php_hash_copy_func_t) PHP_MURMUR3FCopy, |
| 135 | + php_hash_serialize, |
| 136 | + php_hash_unserialize, |
| 137 | + PHP_MURMUR3F_SPEC, |
| 138 | + 16, |
| 139 | + 8, |
| 140 | + sizeof(PHP_MURMUR3F_CTX), |
| 141 | + 0 |
| 142 | +}; |
| 143 | + |
| 144 | +PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx) |
| 145 | +{ |
| 146 | + memset(&ctx->h, 0, sizeof ctx->h); |
| 147 | + memset(&ctx->carry, 0, sizeof ctx->carry); |
| 148 | + ctx->len = 0; |
| 149 | +} |
| 150 | + |
| 151 | +PHP_HASH_API void PHP_MURMUR3FUpdate(PHP_MURMUR3F_CTX *ctx, const unsigned char *in, size_t len) |
| 152 | +{ |
| 153 | + ctx->len += len; |
| 154 | + PMurHash128x64_Process(ctx->h, ctx->carry, in, len); |
| 155 | +} |
| 156 | + |
| 157 | +PHP_HASH_API void PHP_MURMUR3FFinal(unsigned char digest[4], PHP_MURMUR3F_CTX *ctx) |
| 158 | +{ |
| 159 | + uint64_t h[2] = {0}; |
| 160 | + PMurHash128x64_Result(ctx->h, ctx->carry, ctx->len, h); |
| 161 | + |
| 162 | + digest[0] = (unsigned char)((h[0] >> 56) & 0xff); |
| 163 | + digest[1] = (unsigned char)((h[0] >> 48) & 0xff); |
| 164 | + digest[2] = (unsigned char)((h[0] >> 40) & 0xff); |
| 165 | + digest[3] = (unsigned char)((h[0] >> 32) & 0xff); |
| 166 | + digest[4] = (unsigned char)((h[0] >> 24) & 0xff); |
| 167 | + digest[5] = (unsigned char)((h[0] >> 16) & 0xff); |
| 168 | + digest[6] = (unsigned char)((h[0] >> 8) & 0xff); |
| 169 | + digest[7] = (unsigned char)(h[0] & 0xff); |
| 170 | + digest[8] = (unsigned char)((h[1] >> 56) & 0xff); |
| 171 | + digest[9] = (unsigned char)((h[1] >> 48) & 0xff); |
| 172 | + digest[10] = (unsigned char)((h[1] >> 40) & 0xff); |
| 173 | + digest[11] = (unsigned char)((h[1] >> 32) & 0xff); |
| 174 | + digest[12] = (unsigned char)((h[1] >> 24) & 0xff); |
| 175 | + digest[13] = (unsigned char)((h[1] >> 16) & 0xff); |
| 176 | + digest[14] = (unsigned char)((h[1] >> 8) & 0xff); |
| 177 | + digest[15] = (unsigned char)(h[1] & 0xff); |
| 178 | +} |
| 179 | + |
| 180 | +PHP_HASH_API int PHP_MURMUR3FCopy(const php_hash_ops *ops, PHP_MURMUR3F_CTX *orig_context, PHP_MURMUR3F_CTX *copy_context) |
| 181 | +{ |
| 182 | + memcpy(©_context->h, &orig_context->h, sizeof orig_context->h); |
| 183 | + memcpy(©_context->carry, &orig_context->carry, sizeof orig_context->carry); |
| 184 | + copy_context->len = orig_context->len; |
| 185 | + return SUCCESS; |
| 186 | +} |
0 commit comments