Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/hash/hash_xxhash.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author: Anatol Belski <ab@php.net>                                   |
12
   +----------------------------------------------------------------------+
13
*/
14
15
#include "php_hash.h"
16
#include "php_hash_xxhash.h"
17
18
static hash_spec_result php_hash_xxh32_unserialize(
19
    php_hashcontext_object *hash, zend_long magic, const zval *zv);
20
static hash_spec_result php_hash_xxh64_unserialize(
21
    php_hashcontext_object *hash, zend_long magic, const zval *zv);
22
23
const php_hash_ops php_hash_xxh32_ops = {
24
  "xxh32",
25
  (php_hash_init_func_t) PHP_XXH32Init,
26
  (php_hash_update_func_t) PHP_XXH32Update,
27
  (php_hash_final_func_t) PHP_XXH32Final,
28
  (php_hash_copy_func_t) PHP_XXH32Copy,
29
  php_hash_serialize,
30
  php_hash_xxh32_unserialize,
31
  PHP_XXH32_SPEC,
32
  4,
33
  4,
34
  sizeof(PHP_XXH32_CTX),
35
  0
36
};
37
38
PHP_HASH_API void PHP_XXH32Init(PHP_XXH32_CTX *ctx, HashTable *args)
39
0
{
40
  /* XXH32_createState() is not used intentionally. */
41
0
  memset(&ctx->s, 0, sizeof ctx->s);
42
43
0
  if (args) {
44
0
    zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
45
    /* This might be a bit too restrictive, but thinking that a seed might be set
46
      once and for all, it should be done a clean way. */
47
0
    if (seed) {
48
0
      if (IS_LONG == Z_TYPE_P(seed)) {
49
0
        XXH32_reset(&ctx->s, (XXH32_hash_t)Z_LVAL_P(seed));
50
0
        return;
51
0
      } else {
52
0
        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");
53
0
      }
54
0
    }
55
0
  }
56
57
0
  XXH32_reset(&ctx->s, 0);
58
0
}
59
60
PHP_HASH_API void PHP_XXH32Update(PHP_XXH32_CTX *ctx, const unsigned char *in, size_t len)
61
0
{
62
0
  XXH32_update(&ctx->s, in, len);
63
0
}
64
65
PHP_HASH_API void PHP_XXH32Final(unsigned char digest[4], PHP_XXH32_CTX *ctx)
66
0
{
67
0
  XXH32_canonicalFromHash((XXH32_canonical_t*)digest, XXH32_digest(&ctx->s));
68
0
}
69
70
PHP_HASH_API zend_result PHP_XXH32Copy(const php_hash_ops *ops, const PHP_XXH32_CTX *orig_context, PHP_XXH32_CTX *copy_context)
71
0
{
72
0
  copy_context->s = orig_context->s;
73
0
  return SUCCESS;
74
0
}
75
76
static hash_spec_result php_hash_xxh32_unserialize(
77
    php_hashcontext_object *hash, zend_long magic, const zval *zv)
78
0
{
79
0
  PHP_XXH32_CTX *ctx = (PHP_XXH32_CTX *) hash->context;
80
0
  hash_spec_result r = HASH_SPEC_FAILURE;
81
0
  if (magic == PHP_HASH_SERIALIZE_MAGIC_SPEC
82
0
    && (r = php_hash_unserialize_spec(hash, zv, PHP_XXH32_SPEC)) == HASH_SPEC_SUCCESS
83
0
    && ctx->s.memsize < 16) {
84
0
    return HASH_SPEC_SUCCESS;
85
0
  }
86
87
0
    return r != HASH_SPEC_SUCCESS ? r : CONTEXT_VALIDATION_FAILURE;
88
0
}
89
90
const php_hash_ops php_hash_xxh64_ops = {
91
  "xxh64",
92
  (php_hash_init_func_t) PHP_XXH64Init,
93
  (php_hash_update_func_t) PHP_XXH64Update,
94
  (php_hash_final_func_t) PHP_XXH64Final,
95
  (php_hash_copy_func_t) PHP_XXH64Copy,
96
  php_hash_serialize,
97
  php_hash_xxh64_unserialize,
98
  PHP_XXH64_SPEC,
99
  8,
100
  8,
101
  sizeof(PHP_XXH64_CTX),
102
  0
103
};
104
105
PHP_HASH_API void PHP_XXH64Init(PHP_XXH64_CTX *ctx, HashTable *args)
106
0
{
107
  /* XXH64_createState() is not used intentionally. */
108
0
  memset(&ctx->s, 0, sizeof ctx->s);
109
110
0
  if (args) {
111
0
    zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
112
    /* This might be a bit too restrictive, but thinking that a seed might be set
113
      once and for all, it should be done a clean way. */
114
0
    if (seed && IS_LONG == Z_TYPE_P(seed)) {
115
0
      XXH64_reset(&ctx->s, (XXH64_hash_t)Z_LVAL_P(seed));
116
0
      return;
117
0
    } else {
118
0
      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");
119
0
    }
120
0
  }
121
122
0
  XXH64_reset(&ctx->s, 0);
123
0
}
124
125
PHP_HASH_API void PHP_XXH64Update(PHP_XXH64_CTX *ctx, const unsigned char *in, size_t len)
126
0
{
127
0
  XXH64_update(&ctx->s, in, len);
128
0
}
129
130
PHP_HASH_API void PHP_XXH64Final(unsigned char digest[8], PHP_XXH64_CTX *ctx)
131
0
{
132
0
  XXH64_canonicalFromHash((XXH64_canonical_t*)digest, XXH64_digest(&ctx->s));
133
0
}
134
135
PHP_HASH_API zend_result PHP_XXH64Copy(const php_hash_ops *ops, const PHP_XXH64_CTX *orig_context, PHP_XXH64_CTX *copy_context)
136
0
{
137
0
  copy_context->s = orig_context->s;
138
0
  return SUCCESS;
139
0
}
140
141
const php_hash_ops php_hash_xxh3_64_ops = {
142
  "xxh3",
143
  (php_hash_init_func_t) PHP_XXH3_64_Init,
144
  (php_hash_update_func_t) PHP_XXH3_64_Update,
145
  (php_hash_final_func_t) PHP_XXH3_64_Final,
146
  (php_hash_copy_func_t) PHP_XXH3_64_Copy,
147
  php_hash_serialize,
148
  php_hash_unserialize,
149
  NULL,
150
  8,
151
  8,
152
  sizeof(PHP_XXH3_64_CTX),
153
  0
154
};
155
156
typedef XXH_errorcode (*xxh3_reset_with_secret_func_t)(XXH3_state_t*, const void*, size_t);
157
typedef XXH_errorcode (*xxh3_reset_with_seed_func_t)(XXH3_state_t*, XXH64_hash_t);
158
159
static void _PHP_XXH3_Init(PHP_XXH3_64_CTX *ctx, HashTable *args,
160
    xxh3_reset_with_seed_func_t func_init_seed, xxh3_reset_with_secret_func_t func_init_secret, const char* algo_name)
161
0
{
162
0
  memset(&ctx->s, 0, sizeof ctx->s);
163
164
0
  if (args) {
165
0
    zval *_seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
166
0
    zval *_secret = zend_hash_str_find_deref(args, "secret", sizeof("secret") - 1);
167
168
0
    if (_seed && _secret) {
169
0
      zend_throw_error(NULL, "%s: Only one of seed or secret is to be passed for initialization", algo_name);
170
0
      return;
171
0
    }
172
173
0
    if (_seed && IS_LONG != Z_TYPE_P(_seed)) {
174
0
      php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is ignored");
175
0
    }
176
177
0
    if (_seed && IS_LONG == Z_TYPE_P(_seed)) {
178
      /* This might be a bit too restrictive, but thinking that a seed might be set
179
        once and for all, it should be done a clean way. */
180
0
      func_init_seed(&ctx->s, (XXH64_hash_t)Z_LVAL_P(_seed));
181
0
      return;
182
0
    } else if (_secret) {
183
0
      if (IS_STRING != Z_TYPE_P(_secret)) {
184
0
        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");
185
0
      }
186
0
      zend_string *secret_string = zval_try_get_string(_secret);
187
0
      if (UNEXPECTED(!secret_string)) {
188
0
        ZEND_ASSERT(EG(exception));
189
0
        return;
190
0
      }
191
0
      size_t len = ZSTR_LEN(secret_string);
192
0
      if (len < PHP_XXH3_SECRET_SIZE_MIN) {
193
0
        zend_string_release(secret_string);
194
0
        zend_throw_error(NULL, "%s: Secret length must be >= %u bytes, %zu bytes passed", algo_name, XXH3_SECRET_SIZE_MIN, len);
195
0
        return;
196
0
      }
197
0
      if (len > sizeof(ctx->secret)) {
198
0
        len = sizeof(ctx->secret);
199
0
        php_error_docref(NULL, E_WARNING, "%s: Secret content exceeding %zu bytes discarded", algo_name, sizeof(ctx->secret));
200
0
      }
201
0
      memcpy((unsigned char *)ctx->secret, ZSTR_VAL(secret_string), len);
202
0
      zend_string_release(secret_string);
203
0
      func_init_secret(&ctx->s, ctx->secret, len);
204
0
      return;
205
0
    }
206
0
  }
207
208
0
  func_init_seed(&ctx->s, 0);
209
0
}
210
211
PHP_HASH_API void PHP_XXH3_64_Init(PHP_XXH3_64_CTX *ctx, HashTable *args)
212
0
{
213
0
  _PHP_XXH3_Init(ctx, args, XXH3_64bits_reset_withSeed, XXH3_64bits_reset_withSecret, "xxh3");
214
0
}
215
216
PHP_HASH_API void PHP_XXH3_64_Update(PHP_XXH3_64_CTX *ctx, const unsigned char *in, size_t len)
217
0
{
218
0
  XXH3_64bits_update(&ctx->s, in, len);
219
0
}
220
221
PHP_HASH_API void PHP_XXH3_64_Final(unsigned char digest[8], PHP_XXH3_64_CTX *ctx)
222
0
{
223
0
  XXH64_canonicalFromHash((XXH64_canonical_t*)digest, XXH3_64bits_digest(&ctx->s));
224
0
}
225
226
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)
227
0
{
228
0
  copy_context->s = orig_context->s;
229
0
  return SUCCESS;
230
0
}
231
232
static hash_spec_result php_hash_xxh64_unserialize(
233
    php_hashcontext_object *hash, zend_long magic, const zval *zv)
234
0
{
235
0
  PHP_XXH64_CTX *ctx = (PHP_XXH64_CTX *) hash->context;
236
0
  hash_spec_result r = HASH_SPEC_FAILURE;
237
0
  if (magic == PHP_HASH_SERIALIZE_MAGIC_SPEC
238
0
    && (r = php_hash_unserialize_spec(hash, zv, PHP_XXH64_SPEC)) == HASH_SPEC_SUCCESS
239
0
    && ctx->s.memsize < 32) {
240
0
    return HASH_SPEC_SUCCESS;
241
0
  }
242
243
0
    return r != HASH_SPEC_SUCCESS ? r : CONTEXT_VALIDATION_FAILURE;
244
0
}
245
246
const php_hash_ops php_hash_xxh3_128_ops = {
247
  "xxh128",
248
  (php_hash_init_func_t) PHP_XXH3_128_Init,
249
  (php_hash_update_func_t) PHP_XXH3_128_Update,
250
  (php_hash_final_func_t) PHP_XXH3_128_Final,
251
  (php_hash_copy_func_t) PHP_XXH3_128_Copy,
252
  php_hash_serialize,
253
  php_hash_unserialize,
254
  NULL,
255
  16,
256
  8,
257
  sizeof(PHP_XXH3_128_CTX),
258
  0
259
};
260
261
PHP_HASH_API void PHP_XXH3_128_Init(PHP_XXH3_128_CTX *ctx, HashTable *args)
262
0
{
263
0
  _PHP_XXH3_Init(ctx, args, XXH3_128bits_reset_withSeed, XXH3_128bits_reset_withSecret, "xxh128");
264
0
}
265
266
PHP_HASH_API void PHP_XXH3_128_Update(PHP_XXH3_128_CTX *ctx, const unsigned char *in, size_t len)
267
0
{
268
0
  XXH3_128bits_update(&ctx->s, in, len);
269
0
}
270
271
PHP_HASH_API void PHP_XXH3_128_Final(unsigned char digest[16], PHP_XXH3_128_CTX *ctx)
272
0
{
273
0
  XXH128_canonicalFromHash((XXH128_canonical_t*)digest, XXH3_128bits_digest(&ctx->s));
274
0
}
275
276
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)
277
0
{
278
0
  copy_context->s = orig_context->s;
279
0
  return SUCCESS;
280
0
}
281