php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
hash_murmur.c
Go to the documentation of this file.
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 | https://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
38
40{
41 if (args) {
42 zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
43 /* This might be a bit too restrictive, but thinking that a seed might be set
44 once and for all, it should be done a clean way. */
45 if (seed) {
46 if (IS_LONG == Z_TYPE_P(seed)) {
47 ctx->h = (uint32_t) Z_LVAL_P(seed);
48 } else {
49 php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0");
50 ctx->h = 0;
51 }
52 } else {
53 ctx->h = 0;
54 }
55 } else {
56 ctx->h = 0;
57 }
58 ctx->carry = 0;
59 ctx->len = 0;
60}
61
62PHP_HASH_API void PHP_MURMUR3AUpdate(PHP_MURMUR3A_CTX *ctx, const unsigned char *in, size_t len)
63{
64 ctx->len += len;
65 PMurHash32_Process(&ctx->h, &ctx->carry, in, len);
66}
67
68PHP_HASH_API void PHP_MURMUR3AFinal(unsigned char digest[4], PHP_MURMUR3A_CTX *ctx)
69{
70 ctx->h = PMurHash32_Result(ctx->h, ctx->carry, ctx->len);
71
72 digest[0] = (unsigned char)((ctx->h >> 24) & 0xff);
73 digest[1] = (unsigned char)((ctx->h >> 16) & 0xff);
74 digest[2] = (unsigned char)((ctx->h >> 8) & 0xff);
75 digest[3] = (unsigned char)(ctx->h & 0xff);
76}
77
79{
80 copy_context->h = orig_context->h;
81 copy_context->carry = orig_context->carry;
82 copy_context->len = orig_context->len;
83 return SUCCESS;
84}
85
100
102{
103 if (args) {
104 zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
105 /* This might be a bit too restrictive, but thinking that a seed might be set
106 once and for all, it should be done a clean way. */
107 if (seed) {
108 if (IS_LONG == Z_TYPE_P(seed)) {
109 uint32_t _seed = (uint32_t)Z_LVAL_P(seed);
110 ctx->h[0] = _seed;
111 ctx->h[1] = _seed;
112 ctx->h[2] = _seed;
113 ctx->h[3] = _seed;
114 } else {
115 php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0");
116 memset(&ctx->h, 0, sizeof ctx->h);
117 }
118 } else {
119 memset(&ctx->h, 0, sizeof ctx->h);
120 }
121 } else {
122 memset(&ctx->h, 0, sizeof ctx->h);
123 }
124 memset(&ctx->carry, 0, sizeof ctx->carry);
125 ctx->len = 0;
126}
127
128PHP_HASH_API void PHP_MURMUR3CUpdate(PHP_MURMUR3C_CTX *ctx, const unsigned char *in, size_t len)
129{
130 ctx->len += len;
131 PMurHash128x86_Process(ctx->h, ctx->carry, in, len);
132}
133
134PHP_HASH_API void PHP_MURMUR3CFinal(unsigned char digest[16], PHP_MURMUR3C_CTX *ctx)
135{
136 uint32_t h[4] = {0, 0, 0, 0};
137 PMurHash128x86_Result(ctx->h, ctx->carry, ctx->len, h);
138
139 digest[0] = (unsigned char)((h[0] >> 24) & 0xff);
140 digest[1] = (unsigned char)((h[0] >> 16) & 0xff);
141 digest[2] = (unsigned char)((h[0] >> 8) & 0xff);
142 digest[3] = (unsigned char)(h[0] & 0xff);
143 digest[4] = (unsigned char)((h[1] >> 24) & 0xff);
144 digest[5] = (unsigned char)((h[1] >> 16) & 0xff);
145 digest[6] = (unsigned char)((h[1] >> 8) & 0xff);
146 digest[7] = (unsigned char)(h[1] & 0xff);
147 digest[8] = (unsigned char)((h[2] >> 24) & 0xff);
148 digest[9] = (unsigned char)((h[2] >> 16) & 0xff);
149 digest[10] = (unsigned char)((h[2] >> 8) & 0xff);
150 digest[11] = (unsigned char)(h[2] & 0xff);
151 digest[12] = (unsigned char)((h[3] >> 24) & 0xff);
152 digest[13] = (unsigned char)((h[3] >> 16) & 0xff);
153 digest[14] = (unsigned char)((h[3] >> 8) & 0xff);
154 digest[15] = (unsigned char)(h[3] & 0xff);
155}
156
158{
159 memcpy(&copy_context->h, &orig_context->h, sizeof orig_context->h);
160 memcpy(&copy_context->carry, &orig_context->carry, sizeof orig_context->carry);
161 copy_context->len = orig_context->len;
162 return SUCCESS;
163}
164
179
181{
182 if (args) {
183 zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
184 /* This might be a bit too restrictive, but thinking that a seed might be set
185 once and for all, it should be done a clean way. */
186 if (seed) {
187 if (IS_LONG == Z_TYPE_P(seed)) {
188 uint64_t _seed = (uint64_t) Z_LVAL_P(seed);
189 ctx->h[0] = _seed;
190 ctx->h[1] = _seed;
191 } else {
192 php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0");
193 memset(&ctx->h, 0, sizeof ctx->h);
194 }
195 } else {
196 memset(&ctx->h, 0, sizeof ctx->h);
197 }
198 } else {
199 memset(&ctx->h, 0, sizeof ctx->h);
200 }
201 memset(&ctx->carry, 0, sizeof ctx->carry);
202 ctx->len = 0;
203}
204
205PHP_HASH_API void PHP_MURMUR3FUpdate(PHP_MURMUR3F_CTX *ctx, const unsigned char *in, size_t len)
206{
207 ctx->len += len;
208 PMurHash128x64_Process(ctx->h, ctx->carry, in, len);
209}
210
211PHP_HASH_API void PHP_MURMUR3FFinal(unsigned char digest[16], PHP_MURMUR3F_CTX *ctx)
212{
213 uint64_t h[2] = {0, 0};
214 PMurHash128x64_Result(ctx->h, ctx->carry, ctx->len, h);
215
216 digest[0] = (unsigned char)((h[0] >> 56) & 0xff);
217 digest[1] = (unsigned char)((h[0] >> 48) & 0xff);
218 digest[2] = (unsigned char)((h[0] >> 40) & 0xff);
219 digest[3] = (unsigned char)((h[0] >> 32) & 0xff);
220 digest[4] = (unsigned char)((h[0] >> 24) & 0xff);
221 digest[5] = (unsigned char)((h[0] >> 16) & 0xff);
222 digest[6] = (unsigned char)((h[0] >> 8) & 0xff);
223 digest[7] = (unsigned char)(h[0] & 0xff);
224 digest[8] = (unsigned char)((h[1] >> 56) & 0xff);
225 digest[9] = (unsigned char)((h[1] >> 48) & 0xff);
226 digest[10] = (unsigned char)((h[1] >> 40) & 0xff);
227 digest[11] = (unsigned char)((h[1] >> 32) & 0xff);
228 digest[12] = (unsigned char)((h[1] >> 24) & 0xff);
229 digest[13] = (unsigned char)((h[1] >> 16) & 0xff);
230 digest[14] = (unsigned char)((h[1] >> 8) & 0xff);
231 digest[15] = (unsigned char)(h[1] & 0xff);
232}
233
235{
236 memcpy(&copy_context->h, &orig_context->h, sizeof orig_context->h);
237 memcpy(&copy_context->carry, &orig_context->carry, sizeof orig_context->carry);
238 copy_context->len = orig_context->len;
239 return SUCCESS;
240}
void PMurHash128x86_Result(const uint32_t ph[4], const uint32_t pcarry[4], uint32_t total_length, uint32_t out[4])
void PMurHash128x64_Process(uint64_t ph[2], uint64_t pcarry[2], const void *const key, int len)
void PMurHash128x64_Result(const uint64_t ph[2], const uint64_t pcarry[2], const uint32_t total_length, uint64_t out[2])
void PMurHash128x86_Process(uint32_t ph[4], uint32_t pcarry[4], const void *const key, int len)
uint32_t PMurHash32_Result(uint32_t h, uint32_t carry, uint32_t total_length)
Definition PMurHash.c:208
void PMurHash32_Process(uint32_t *ph1, uint32_t *pcarry, const void *key, int len)
Definition PMurHash.c:123
size_t len
Definition apprentice.c:174
memcpy(ptr1, ptr2, size)
memset(ptr, 0, type->size)
const php_stream_filter_ops * ops
Definition filters.c:1899
#define NULL
Definition gdcache.h:45
PHP_HASH_API zend_result php_hash_serialize(const php_hashcontext_object *hash, zend_long *magic, zval *zv)
Definition hash.c:334
PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
Definition hash.c:345
const php_hash_ops php_hash_murmur3c_ops
Definition hash_murmur.c:86
PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx, HashTable *args)
const php_hash_ops php_hash_murmur3f_ops
PHP_HASH_API void PHP_MURMUR3FFinal(unsigned char digest[16], PHP_MURMUR3F_CTX *ctx)
PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx, HashTable *args)
PHP_HASH_API void PHP_MURMUR3AFinal(unsigned char digest[4], PHP_MURMUR3A_CTX *ctx)
Definition hash_murmur.c:68
PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx, HashTable *args)
Definition hash_murmur.c:39
const php_hash_ops php_hash_murmur3a_ops
Definition hash_murmur.c:24
PHP_HASH_API zend_result PHP_MURMUR3CCopy(const php_hash_ops *ops, const PHP_MURMUR3C_CTX *orig_context, PHP_MURMUR3C_CTX *copy_context)
PHP_HASH_API zend_result PHP_MURMUR3ACopy(const php_hash_ops *ops, const PHP_MURMUR3A_CTX *orig_context, PHP_MURMUR3A_CTX *copy_context)
Definition hash_murmur.c:78
PHP_HASH_API void PHP_MURMUR3CUpdate(PHP_MURMUR3C_CTX *ctx, const unsigned char *in, size_t len)
PHP_HASH_API void PHP_MURMUR3FUpdate(PHP_MURMUR3F_CTX *ctx, const unsigned char *in, size_t len)
PHP_HASH_API void PHP_MURMUR3CFinal(unsigned char digest[16], PHP_MURMUR3C_CTX *ctx)
PHP_HASH_API zend_result PHP_MURMUR3FCopy(const php_hash_ops *ops, const PHP_MURMUR3F_CTX *orig_context, PHP_MURMUR3F_CTX *copy_context)
PHP_HASH_API void PHP_MURMUR3AUpdate(PHP_MURMUR3A_CTX *ctx, const unsigned char *in, size_t len)
Definition hash_murmur.c:62
#define SUCCESS
Definition hash_sha3.c:261
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
void(* php_hash_final_func_t)(unsigned char *digest, void *context)
Definition php_hash.h:36
zend_result(* php_hash_copy_func_t)(const void *ops, const void *orig_context, void *dest_context)
Definition php_hash.h:37
void(* php_hash_init_func_t)(void *context, HashTable *args)
Definition php_hash.h:34
void(* php_hash_update_func_t)(void *context, const unsigned char *buf, size_t count)
Definition php_hash.h:35
#define PHP_HASH_API
Definition php_hash.h:144
struct _php_hash_ops php_hash_ops
#define PHP_MURMUR3C_SPEC
#define PHP_MURMUR3A_SPEC
#define PHP_MURMUR3F_SPEC
struct _zval_struct zval
zval * args
#define E_DEPRECATED
Definition zend_errors.h:37
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
struct _zend_array HashTable
Definition zend_types.h:386
#define IS_LONG
Definition zend_types.h:604
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966