Coverage Report

Created: 2026-07-25 06:39

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