|
| 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_xxhash.h" |
| 19 | + |
| 20 | +const php_hash_ops php_hash_xxh32_ops = { |
| 21 | + "xxh32", |
| 22 | + (php_hash_init_func_t) PHP_XXH32Init, |
| 23 | + (php_hash_update_func_t) PHP_XXH32Update, |
| 24 | + (php_hash_final_func_t) PHP_XXH32Final, |
| 25 | + (php_hash_copy_func_t) PHP_XXH32Copy, |
| 26 | + php_hash_serialize, |
| 27 | + php_hash_unserialize, |
| 28 | + PHP_XXH32_SPEC, |
| 29 | + 4, |
| 30 | + 4, |
| 31 | + sizeof(PHP_XXH32_CTX), |
| 32 | + 0 |
| 33 | +}; |
| 34 | + |
| 35 | +PHP_HASH_API void PHP_XXH32Init(PHP_XXH32_CTX *ctx, HashTable *args) |
| 36 | +{ |
| 37 | + /* XXH32_createState() is not used intentionally. */ |
| 38 | + memset(&ctx->s, 0, sizeof ctx->s); |
| 39 | + |
| 40 | + if (args) { |
| 41 | + zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1); |
| 42 | + /* This might be a bit too restrictive, but thinking that a seed might be set |
| 43 | + once and for all, it should be done a clean way. */ |
| 44 | + if (seed && IS_LONG == Z_TYPE_P(seed)) { |
| 45 | + XXH32_reset(&ctx->s, (XXH32_hash_t)Z_LVAL_P(seed)); |
| 46 | + } else { |
| 47 | + XXH32_reset(&ctx->s, 0); |
| 48 | + } |
| 49 | + } else { |
| 50 | + XXH32_reset(&ctx->s, 0); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +PHP_HASH_API void PHP_XXH32Update(PHP_XXH32_CTX *ctx, const unsigned char *in, size_t len) |
| 55 | +{ |
| 56 | + XXH32_update(&ctx->s, in, len); |
| 57 | +} |
| 58 | + |
| 59 | +PHP_HASH_API void PHP_XXH32Final(unsigned char digest[4], PHP_XXH32_CTX *ctx) |
| 60 | +{ |
| 61 | + XXH32_hash_t const h = XXH32_digest(&ctx->s); |
| 62 | + |
| 63 | + digest[0] = (unsigned char)((h >> 24) & 0xff); |
| 64 | + digest[1] = (unsigned char)((h >> 16) & 0xff); |
| 65 | + digest[2] = (unsigned char)((h >> 8) & 0xff); |
| 66 | + digest[3] = (unsigned char)(h & 0xff); |
| 67 | +} |
| 68 | + |
| 69 | +PHP_HASH_API int PHP_XXH32Copy(const php_hash_ops *ops, PHP_XXH32_CTX *orig_context, PHP_XXH32_CTX *copy_context) |
| 70 | +{ |
| 71 | + copy_context->s = orig_context->s; |
| 72 | + return SUCCESS; |
| 73 | +} |
| 74 | + |
| 75 | +const php_hash_ops php_hash_xxh64_ops = { |
| 76 | + "xxh64", |
| 77 | + (php_hash_init_func_t) PHP_XXH64Init, |
| 78 | + (php_hash_update_func_t) PHP_XXH64Update, |
| 79 | + (php_hash_final_func_t) PHP_XXH64Final, |
| 80 | + (php_hash_copy_func_t) PHP_XXH64Copy, |
| 81 | + php_hash_serialize, |
| 82 | + php_hash_unserialize, |
| 83 | + PHP_XXH64_SPEC, |
| 84 | + 8, |
| 85 | + 8, |
| 86 | + sizeof(PHP_XXH64_CTX), |
| 87 | + 0 |
| 88 | +}; |
| 89 | + |
| 90 | +PHP_HASH_API void PHP_XXH64Init(PHP_XXH64_CTX *ctx, HashTable *args) |
| 91 | +{ |
| 92 | + /* XXH64_createState() is not used intentionally. */ |
| 93 | + memset(&ctx->s, 0, sizeof ctx->s); |
| 94 | + |
| 95 | + if (args) { |
| 96 | + zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1); |
| 97 | + /* This might be a bit too restrictive, but thinking that a seed might be set |
| 98 | + once and for all, it should be done a clean way. */ |
| 99 | + if (seed && IS_LONG == Z_TYPE_P(seed)) { |
| 100 | + XXH64_reset(&ctx->s, (XXH64_hash_t)Z_LVAL_P(seed)); |
| 101 | + } else { |
| 102 | + XXH64_reset(&ctx->s, 0); |
| 103 | + } |
| 104 | + } else { |
| 105 | + XXH64_reset(&ctx->s, 0); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +PHP_HASH_API void PHP_XXH64Update(PHP_XXH64_CTX *ctx, const unsigned char *in, size_t len) |
| 110 | +{ |
| 111 | + XXH64_update(&ctx->s, in, len); |
| 112 | +} |
| 113 | + |
| 114 | +PHP_HASH_API void PHP_XXH64Final(unsigned char digest[8], PHP_XXH64_CTX *ctx) |
| 115 | +{ |
| 116 | + XXH64_hash_t const h = XXH64_digest(&ctx->s); |
| 117 | + |
| 118 | + digest[0] = (unsigned char)((h >> 56) & 0xff); |
| 119 | + digest[1] = (unsigned char)((h >> 48) & 0xff); |
| 120 | + digest[2] = (unsigned char)((h >> 40) & 0xff); |
| 121 | + digest[3] = (unsigned char)((h >> 32) & 0xff); |
| 122 | + digest[4] = (unsigned char)((h >> 24) & 0xff); |
| 123 | + digest[5] = (unsigned char)((h >> 16) & 0xff); |
| 124 | + digest[6] = (unsigned char)((h >> 8) & 0xff); |
| 125 | + digest[7] = (unsigned char)(h & 0xff); |
| 126 | +} |
| 127 | + |
| 128 | +PHP_HASH_API int PHP_XXH64Copy(const php_hash_ops *ops, PHP_XXH64_CTX *orig_context, PHP_XXH64_CTX *copy_context) |
| 129 | +{ |
| 130 | + copy_context->s = orig_context->s; |
| 131 | + return SUCCESS; |
| 132 | +} |
| 133 | + |
| 134 | +const php_hash_ops php_hash_xxh3_64_ops = { |
| 135 | + "xxh3", |
| 136 | + (php_hash_init_func_t) PHP_XXH3_64_Init, |
| 137 | + (php_hash_update_func_t) PHP_XXH3_64_Update, |
| 138 | + (php_hash_final_func_t) PHP_XXH3_64_Final, |
| 139 | + (php_hash_copy_func_t) PHP_XXH3_64_Copy, |
| 140 | + php_hash_serialize, |
| 141 | + php_hash_unserialize, |
| 142 | + NULL, |
| 143 | + 8, |
| 144 | + 8, |
| 145 | + sizeof(PHP_XXH3_64_CTX), |
| 146 | + 0 |
| 147 | +}; |
| 148 | + |
| 149 | +PHP_HASH_API void PHP_XXH3_64_Init(PHP_XXH3_64_CTX *ctx, HashTable *args) |
| 150 | +{ |
| 151 | + /* TODO integrate also XXH3_64bits_reset_withSecret(). */ |
| 152 | + XXH64_hash_t seed = 0; |
| 153 | + |
| 154 | + if (args) { |
| 155 | + zval *_seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1); |
| 156 | + /* This might be a bit too restrictive, but thinking that a seed might be set |
| 157 | + once and for all, it should be done a clean way. */ |
| 158 | + if (_seed && IS_LONG == Z_TYPE_P(_seed)) { |
| 159 | + seed = (XXH64_hash_t)Z_LVAL_P(_seed); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + XXH3_64bits_reset_withSeed(&ctx->s, seed); |
| 164 | +} |
| 165 | + |
| 166 | +PHP_HASH_API void PHP_XXH3_64_Update(PHP_XXH3_64_CTX *ctx, const unsigned char *in, size_t len) |
| 167 | +{ |
| 168 | + XXH3_64bits_update(&ctx->s, in, len); |
| 169 | +} |
| 170 | + |
| 171 | +PHP_HASH_API void PHP_XXH3_64_Final(unsigned char digest[8], PHP_XXH3_64_CTX *ctx) |
| 172 | +{ |
| 173 | + XXH64_hash_t const h = XXH3_64bits_digest(&ctx->s); |
| 174 | + |
| 175 | + digest[0] = (unsigned char)((h >> 56) & 0xff); |
| 176 | + digest[1] = (unsigned char)((h >> 48) & 0xff); |
| 177 | + digest[2] = (unsigned char)((h >> 40) & 0xff); |
| 178 | + digest[3] = (unsigned char)((h >> 32) & 0xff); |
| 179 | + digest[4] = (unsigned char)((h >> 24) & 0xff); |
| 180 | + digest[5] = (unsigned char)((h >> 16) & 0xff); |
| 181 | + digest[6] = (unsigned char)((h >> 8) & 0xff); |
| 182 | + digest[7] = (unsigned char)(h & 0xff); |
| 183 | +} |
| 184 | + |
| 185 | +PHP_HASH_API int PHP_XXH3_64_Copy(const php_hash_ops *ops, PHP_XXH3_64_CTX *orig_context, PHP_XXH3_64_CTX *copy_context) |
| 186 | +{ |
| 187 | + copy_context->s = orig_context->s; |
| 188 | + return SUCCESS; |
| 189 | +} |
| 190 | + |
| 191 | +const php_hash_ops php_hash_xxh3_128_ops = { |
| 192 | + "xxh128", |
| 193 | + (php_hash_init_func_t) PHP_XXH3_128_Init, |
| 194 | + (php_hash_update_func_t) PHP_XXH3_128_Update, |
| 195 | + (php_hash_final_func_t) PHP_XXH3_128_Final, |
| 196 | + (php_hash_copy_func_t) PHP_XXH3_128_Copy, |
| 197 | + php_hash_serialize, |
| 198 | + php_hash_unserialize, |
| 199 | + NULL, |
| 200 | + 16, |
| 201 | + 8, |
| 202 | + sizeof(PHP_XXH3_128_CTX), |
| 203 | + 0 |
| 204 | +}; |
| 205 | + |
| 206 | +PHP_HASH_API void PHP_XXH3_128_Init(PHP_XXH3_128_CTX *ctx, HashTable *args) |
| 207 | +{ |
| 208 | + /* TODO integrate also XXH3_128__64bits_reset_withSecret(). */ |
| 209 | + XXH64_hash_t seed = 0; |
| 210 | + |
| 211 | + if (args) { |
| 212 | + zval *_seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1); |
| 213 | + /* This might be a bit too restrictive, but thinking that a seed might be set |
| 214 | + once and for all, it should be done a clean way. */ |
| 215 | + if (_seed && IS_LONG == Z_TYPE_P(_seed)) { |
| 216 | + seed = (XXH64_hash_t)Z_LVAL_P(_seed); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + XXH3_128bits_reset_withSeed(&ctx->s, seed); |
| 221 | +} |
| 222 | + |
| 223 | +PHP_HASH_API void PHP_XXH3_128_Update(PHP_XXH3_128_CTX *ctx, const unsigned char *in, size_t len) |
| 224 | +{ |
| 225 | + XXH3_128bits_update(&ctx->s, in, len); |
| 226 | +} |
| 227 | + |
| 228 | +PHP_HASH_API void PHP_XXH3_128_Final(unsigned char digest[16], PHP_XXH3_128_CTX *ctx) |
| 229 | +{ |
| 230 | + XXH128_hash_t const h = XXH3_128bits_digest(&ctx->s); |
| 231 | + |
| 232 | + digest[0] = (unsigned char)((h.high64 >> 56) & 0xff); |
| 233 | + digest[1] = (unsigned char)((h.high64 >> 48) & 0xff); |
| 234 | + digest[2] = (unsigned char)((h.high64 >> 40) & 0xff); |
| 235 | + digest[3] = (unsigned char)((h.high64 >> 32) & 0xff); |
| 236 | + digest[4] = (unsigned char)((h.high64 >> 24) & 0xff); |
| 237 | + digest[5] = (unsigned char)((h.high64 >> 16) & 0xff); |
| 238 | + digest[6] = (unsigned char)((h.high64 >> 8) & 0xff); |
| 239 | + digest[7] = (unsigned char)(h.high64 & 0xff); |
| 240 | + digest[8] = (unsigned char)((h.low64 >> 56) & 0xff); |
| 241 | + digest[9] = (unsigned char)((h.low64 >> 48) & 0xff); |
| 242 | + digest[10] = (unsigned char)((h.low64 >> 40) & 0xff); |
| 243 | + digest[11] = (unsigned char)((h.low64 >> 32) & 0xff); |
| 244 | + digest[12] = (unsigned char)((h.low64 >> 24) & 0xff); |
| 245 | + digest[13] = (unsigned char)((h.low64 >> 16) & 0xff); |
| 246 | + digest[14] = (unsigned char)((h.low64 >> 8) & 0xff); |
| 247 | + digest[15] = (unsigned char)(h.low64 & 0xff); |
| 248 | +} |
| 249 | + |
| 250 | +PHP_HASH_API int PHP_XXH3_128_Copy(const php_hash_ops *ops, PHP_XXH3_128_CTX *orig_context, PHP_XXH3_128_CTX *copy_context) |
| 251 | +{ |
| 252 | + copy_context->s = orig_context->s; |
| 253 | + return SUCCESS; |
| 254 | +} |
| 255 | + |
0 commit comments