php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
hash_xxhash.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_xxhash.h"
19
20static int php_hash_xxh32_unserialize(
22static int php_hash_xxh64_unserialize(
24
39
41{
42 /* XXH32_createState() is not used intentionally. */
43 memset(&ctx->s, 0, sizeof ctx->s);
44
45 if (args) {
46 zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
47 /* This might be a bit too restrictive, but thinking that a seed might be set
48 once and for all, it should be done a clean way. */
49 if (seed) {
50 if (IS_LONG == Z_TYPE_P(seed)) {
51 XXH32_reset(&ctx->s, (XXH32_hash_t)Z_LVAL_P(seed));
52 return;
53 } else {
54 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");
55 }
56 }
57 }
58
59 XXH32_reset(&ctx->s, 0);
60}
61
62PHP_HASH_API void PHP_XXH32Update(PHP_XXH32_CTX *ctx, const unsigned char *in, size_t len)
63{
64 XXH32_update(&ctx->s, in, len);
65}
66
67PHP_HASH_API void PHP_XXH32Final(unsigned char digest[4], PHP_XXH32_CTX *ctx)
68{
70}
71
73{
74 copy_context->s = orig_context->s;
75 return SUCCESS;
76}
77
78static int php_hash_xxh32_unserialize(
80{
81 PHP_XXH32_CTX *ctx = (PHP_XXH32_CTX *) hash->context;
82 int r = FAILURE;
85 && ctx->s.memsize < 16) {
86 return SUCCESS;
87 } else {
88 return r != SUCCESS ? r : -2000;
89 }
90}
91
106
108{
109 /* XXH64_createState() is not used intentionally. */
110 memset(&ctx->s, 0, sizeof ctx->s);
111
112 if (args) {
113 zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
114 /* This might be a bit too restrictive, but thinking that a seed might be set
115 once and for all, it should be done a clean way. */
116 if (seed && IS_LONG == Z_TYPE_P(seed)) {
117 XXH64_reset(&ctx->s, (XXH64_hash_t)Z_LVAL_P(seed));
118 return;
119 } else {
120 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");
121 }
122 }
123
124 XXH64_reset(&ctx->s, 0);
125}
126
127PHP_HASH_API void PHP_XXH64Update(PHP_XXH64_CTX *ctx, const unsigned char *in, size_t len)
128{
129 XXH64_update(&ctx->s, in, len);
130}
131
132PHP_HASH_API void PHP_XXH64Final(unsigned char digest[8], PHP_XXH64_CTX *ctx)
133{
135}
136
138{
139 copy_context->s = orig_context->s;
140 return SUCCESS;
141}
142
157
160
161zend_always_inline static void _PHP_XXH3_Init(PHP_XXH3_64_CTX *ctx, HashTable *args,
162 xxh3_reset_with_seed_func_t func_init_seed, xxh3_reset_with_secret_func_t func_init_secret, const char* algo_name)
163{
164 memset(&ctx->s, 0, sizeof ctx->s);
165
166 if (args) {
167 zval *_seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
168 zval *_secret = zend_hash_str_find_deref(args, "secret", sizeof("secret") - 1);
169
170 if (_seed && _secret) {
171 zend_throw_error(NULL, "%s: Only one of seed or secret is to be passed for initialization", algo_name);
172 return;
173 }
174
175 if (_seed && IS_LONG != Z_TYPE_P(_seed)) {
176 php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is ignored");
177 }
178
179 if (_seed && IS_LONG == Z_TYPE_P(_seed)) {
180 /* This might be a bit too restrictive, but thinking that a seed might be set
181 once and for all, it should be done a clean way. */
182 func_init_seed(&ctx->s, (XXH64_hash_t)Z_LVAL_P(_seed));
183 return;
184 } else if (_secret) {
185 if (IS_STRING != Z_TYPE_P(_secret)) {
186 php_error_docref(NULL, E_DEPRECATED, "Passing a secret of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs");
187 }
188 zend_string *secret_string = zval_try_get_string(_secret);
189 if (UNEXPECTED(!secret_string)) {
191 return;
192 }
193 size_t len = ZSTR_LEN(secret_string);
195 zend_string_release(secret_string);
196 zend_throw_error(NULL, "%s: Secret length must be >= %u bytes, %zu bytes passed", algo_name, XXH3_SECRET_SIZE_MIN, len);
197 return;
198 }
199 if (len > sizeof(ctx->secret)) {
200 len = sizeof(ctx->secret);
201 php_error_docref(NULL, E_WARNING, "%s: Secret content exceeding %zu bytes discarded", algo_name, sizeof(ctx->secret));
202 }
203 memcpy((unsigned char *)ctx->secret, ZSTR_VAL(secret_string), len);
204 zend_string_release(secret_string);
205 func_init_secret(&ctx->s, ctx->secret, len);
206 return;
207 }
208 }
209
210 func_init_seed(&ctx->s, 0);
211}
212
217
218PHP_HASH_API void PHP_XXH3_64_Update(PHP_XXH3_64_CTX *ctx, const unsigned char *in, size_t len)
219{
220 XXH3_64bits_update(&ctx->s, in, len);
221}
222
223PHP_HASH_API void PHP_XXH3_64_Final(unsigned char digest[8], PHP_XXH3_64_CTX *ctx)
224{
226}
227
229{
230 copy_context->s = orig_context->s;
231 return SUCCESS;
232}
233
234static int php_hash_xxh64_unserialize(
236{
237 PHP_XXH64_CTX *ctx = (PHP_XXH64_CTX *) hash->context;
238 int r = FAILURE;
241 && ctx->s.memsize < 32) {
242 return SUCCESS;
243 } else {
244 return r != SUCCESS ? r : -2000;
245 }
246}
247
262
267
268PHP_HASH_API void PHP_XXH3_128_Update(PHP_XXH3_128_CTX *ctx, const unsigned char *in, size_t len)
269{
270 XXH3_128bits_update(&ctx->s, in, len);
271}
272
273PHP_HASH_API void PHP_XXH3_128_Final(unsigned char digest[16], PHP_XXH3_128_CTX *ctx)
274{
276}
277
279{
280 copy_context->s = orig_context->s;
281 return SUCCESS;
282}
283
size_t len
Definition apprentice.c:174
bool exception
Definition assert.c:30
zval * zv
Definition ffi.c:3975
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
unsigned long long XXH64_hash_t
Definition xxhash.h:667
XXH_errorcode
Definition xxhash.h:343
XXH_PUBLIC_API XXH32_hash_t XXH32_digest(const XXH32_state_t *statePtr)
Returns the calculated hash value from an XXH32_state_t.
XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t *statePtr, XXH32_hash_t seed)
Resets an XXH32_state_t to begin a new hash.
XXH_PUBLIC_API XXH_errorcode XXH32_update(XXH32_state_t *statePtr, const void *input, size_t length)
Consumes a block of input to an XXH32_state_t.
XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t *dst, XXH32_hash_t hash)
Converts an XXH32_hash_t to a big endian XXH32_canonical_t.
XXH_PUBLIC_API XXH_errorcode XXH3_128bits_update(XXH3_state_t *statePtr, const void *input, size_t length)
XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSeed(XXH3_state_t *statePtr, XXH64_hash_t seed)
XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSeed(XXH3_state_t *statePtr, XXH64_hash_t seed)
XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_digest(const XXH3_state_t *statePtr)
XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSecret(XXH3_state_t *statePtr, const void *secret, size_t secretSize)
XXH_PUBLIC_API XXH_errorcode XXH3_64bits_update(XXH3_state_t *statePtr, const void *input, size_t length)
struct XXH3_state_s XXH3_state_t
The state struct for the XXH3 streaming API.
Definition xxhash.h:836
XXH_PUBLIC_API XXH128_hash_t XXH3_128bits_digest(const XXH3_state_t *statePtr)
XXH_PUBLIC_API void XXH128_canonicalFromHash(XXH128_canonical_t *dst, XXH128_hash_t hash)
#define XXH3_SECRET_SIZE_MIN
Definition xxhash.h:801
XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSecret(XXH3_state_t *statePtr, const void *secret, size_t secretSize)
XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t *statePtr, XXH64_hash_t seed)
XXH_PUBLIC_API XXH_errorcode XXH64_update(XXH64_state_t *statePtr, const void *input, size_t length)
XXH_PUBLIC_API XXH64_hash_t XXH64_digest(const XXH64_state_t *statePtr)
XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t *dst, XXH64_hash_t hash)
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_spec(php_hashcontext_object *hash, const zval *zv, const char *spec)
Definition hash.c:280
PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
Definition hash.c:345
hash(string $algo, string $data, bool $binary=false, array $options=[])
Definition hash.stub.php:12
#define SUCCESS
Definition hash_sha3.c:261
PHP_HASH_API void PHP_XXH3_128_Init(PHP_XXH3_128_CTX *ctx, HashTable *args)
PHP_HASH_API void PHP_XXH64Init(PHP_XXH64_CTX *ctx, HashTable *args)
PHP_HASH_API zend_result PHP_XXH32Copy(const php_hash_ops *ops, const PHP_XXH32_CTX *orig_context, PHP_XXH32_CTX *copy_context)
Definition hash_xxhash.c:72
PHP_HASH_API void PHP_XXH64Update(PHP_XXH64_CTX *ctx, const unsigned char *in, size_t len)
PHP_HASH_API void PHP_XXH3_128_Update(PHP_XXH3_128_CTX *ctx, const unsigned char *in, size_t len)
PHP_HASH_API zend_result PHP_XXH64Copy(const php_hash_ops *ops, const PHP_XXH64_CTX *orig_context, PHP_XXH64_CTX *copy_context)
PHP_HASH_API zend_result PHP_XXH3_128_Copy(const php_hash_ops *ops, const PHP_XXH3_128_CTX *orig_context, PHP_XXH3_128_CTX *copy_context)
XXH_errorcode(* xxh3_reset_with_secret_func_t)(XXH3_state_t *, const void *, size_t)
PHP_HASH_API void PHP_XXH3_64_Update(PHP_XXH3_64_CTX *ctx, const unsigned char *in, size_t len)
PHP_HASH_API zend_result PHP_XXH3_64_Copy(const php_hash_ops *ops, const PHP_XXH3_64_CTX *orig_context, PHP_XXH3_64_CTX *copy_context)
const php_hash_ops php_hash_xxh32_ops
Definition hash_xxhash.c:25
XXH_errorcode(* xxh3_reset_with_seed_func_t)(XXH3_state_t *, XXH64_hash_t)
const php_hash_ops php_hash_xxh3_128_ops
PHP_HASH_API void PHP_XXH64Final(unsigned char digest[8], PHP_XXH64_CTX *ctx)
const php_hash_ops php_hash_xxh3_64_ops
const php_hash_ops php_hash_xxh64_ops
Definition hash_xxhash.c:92
PHP_HASH_API void PHP_XXH32Init(PHP_XXH32_CTX *ctx, HashTable *args)
Definition hash_xxhash.c:40
PHP_HASH_API void PHP_XXH32Update(PHP_XXH32_CTX *ctx, const unsigned char *in, size_t len)
Definition hash_xxhash.c:62
PHP_HASH_API void PHP_XXH3_64_Init(PHP_XXH3_64_CTX *ctx, HashTable *args)
PHP_HASH_API void PHP_XXH3_128_Final(unsigned char digest[16], PHP_XXH3_128_CTX *ctx)
PHP_HASH_API void PHP_XXH32Final(unsigned char digest[4], PHP_XXH32_CTX *ctx)
Definition hash_xxhash.c:67
PHP_HASH_API void PHP_XXH3_64_Final(unsigned char digest[8], PHP_XXH3_64_CTX *ctx)
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
struct _php_hashcontext_object php_hashcontext_object
Definition php_hash.h:32
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_HASH_SERIALIZE_MAGIC_SPEC
Definition php_hash.h:28
PHP_XXH3_CTX PHP_XXH3_64_CTX
#define PHP_XXH64_SPEC
PHP_XXH3_CTX PHP_XXH3_128_CTX
#define PHP_XXH32_SPEC
#define PHP_XXH3_SECRET_SIZE_MIN
XXH32_state_t s
XXH3_state_t s
const unsigned char secret[PHP_XXH3_SECRET_SIZE_MAX]
XXH64_state_t s
Canonical (big endian) representation of XXH32_hash_t.
Definition xxhash.h:575
Definition file.h:202
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
struct _zval_struct zval
zval * args
#define E_WARNING
Definition zend_errors.h:24
#define E_DEPRECATED
Definition zend_errors.h:37
#define EG(v)
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
#define zend_always_inline
#define ZEND_ASSERT(c)
#define UNEXPECTED(condition)
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define IS_STRING
Definition zend_types.h:606
struct _zend_array HashTable
Definition zend_types.h:386
@ FAILURE
Definition zend_types.h:61
#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