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/php_hash.h
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: Sara Golemon <pollita@php.net>                               |
12
  +----------------------------------------------------------------------+
13
*/
14
15
#ifndef PHP_HASH_H
16
#define PHP_HASH_H
17
18
#include "php.h"
19
20
#define PHP_HASH_EXTNAME  "hash"
21
#define PHP_HASH_VERSION  PHP_VERSION
22
#define PHP_MHASH_VERSION PHP_VERSION
23
24
8.13k
#define PHP_HASH_HMAC   0x0001
25
26
6.55k
#define PHP_HASH_SERIALIZE_MAGIC_SPEC          2
27
28
105k
#define L64 INT64_C
29
30
typedef enum {
31
    HASH_SPEC_SUCCESS = 0,
32
    HASH_SPEC_FAILURE = -1,
33
    WRONG_CONTEXT_SIZE = -999,
34
    BYTE_OFFSET_POS_ERROR = -1000,
35
    CONTEXT_VALIDATION_FAILURE = -2000,
36
} hash_spec_result;
37
38
typedef struct _php_hashcontext_object php_hashcontext_object;
39
40
typedef void (*php_hash_init_func_t)(void *context, HashTable *args);
41
typedef void (*php_hash_update_func_t)(void *context, const unsigned char *buf, size_t count);
42
typedef void (*php_hash_final_func_t)(unsigned char *digest, void *context);
43
typedef zend_result (*php_hash_copy_func_t)(const void *ops, const void *orig_context, void *dest_context);
44
typedef hash_spec_result (*php_hash_serialize_func_t)(const php_hashcontext_object *hash, zend_long *magic, zval *zv);
45
typedef hash_spec_result (*php_hash_unserialize_func_t)(php_hashcontext_object *hash, zend_long magic, const zval *zv);
46
47
typedef struct _php_hash_ops {
48
  const char *algo;
49
  php_hash_init_func_t hash_init;
50
  php_hash_update_func_t hash_update;
51
  php_hash_final_func_t hash_final;
52
  php_hash_copy_func_t hash_copy;
53
  php_hash_serialize_func_t hash_serialize;
54
  php_hash_unserialize_func_t hash_unserialize;
55
  const char *serialize_spec;
56
57
  size_t digest_size;
58
  size_t block_size;
59
  size_t context_size;
60
  unsigned is_crypto: 1;
61
  size_t context_align;
62
} php_hash_ops;
63
64
struct _php_hashcontext_object {
65
  const php_hash_ops *ops;
66
  void *context;
67
68
  zend_long options;
69
  unsigned char *key;
70
71
  zend_object std;
72
};
73
74
35.3k
static inline php_hashcontext_object *php_hashcontext_from_object(zend_object *obj) {
75
35.3k
  return ((php_hashcontext_object*)(obj + 1)) - 1;
76
35.3k
}
Unexecuted instantiation: hash_adler32.c:php_hashcontext_from_object
Unexecuted instantiation: hash_crc32.c:php_hashcontext_from_object
Unexecuted instantiation: hash_fnv.c:php_hashcontext_from_object
Unexecuted instantiation: hash_gost.c:php_hashcontext_from_object
Unexecuted instantiation: hash_haval.c:php_hashcontext_from_object
Unexecuted instantiation: hash_joaat.c:php_hashcontext_from_object
Unexecuted instantiation: hash_md.c:php_hashcontext_from_object
Unexecuted instantiation: hash_murmur.c:php_hashcontext_from_object
Unexecuted instantiation: hash_ripemd.c:php_hashcontext_from_object
Unexecuted instantiation: hash_sha_ni.c:php_hashcontext_from_object
Unexecuted instantiation: hash_sha_sse2.c:php_hashcontext_from_object
Unexecuted instantiation: hash_sha.c:php_hashcontext_from_object
Unexecuted instantiation: hash_sha3.c:php_hashcontext_from_object
Unexecuted instantiation: hash_snefru.c:php_hashcontext_from_object
Unexecuted instantiation: hash_tiger.c:php_hashcontext_from_object
Unexecuted instantiation: hash_whirlpool.c:php_hashcontext_from_object
Unexecuted instantiation: hash_xxhash.c:php_hashcontext_from_object
hash.c:php_hashcontext_from_object
Line
Count
Source
74
35.3k
static inline php_hashcontext_object *php_hashcontext_from_object(zend_object *obj) {
75
35.3k
  return ((php_hashcontext_object*)(obj + 1)) - 1;
76
35.3k
}
Unexecuted instantiation: internal_functions_cli.c:php_hashcontext_from_object
77
78
extern const php_hash_ops php_hash_md2_ops;
79
extern const php_hash_ops php_hash_md4_ops;
80
extern const php_hash_ops php_hash_md5_ops;
81
extern const php_hash_ops php_hash_sha1_ops;
82
extern const php_hash_ops php_hash_sha224_ops;
83
extern const php_hash_ops php_hash_sha256_ops;
84
extern const php_hash_ops php_hash_sha384_ops;
85
extern const php_hash_ops php_hash_sha512_ops;
86
extern const php_hash_ops php_hash_sha512_256_ops;
87
extern const php_hash_ops php_hash_sha512_224_ops;
88
extern const php_hash_ops php_hash_sha3_224_ops;
89
extern const php_hash_ops php_hash_sha3_256_ops;
90
extern const php_hash_ops php_hash_sha3_384_ops;
91
extern const php_hash_ops php_hash_sha3_512_ops;
92
extern const php_hash_ops php_hash_ripemd128_ops;
93
extern const php_hash_ops php_hash_ripemd160_ops;
94
extern const php_hash_ops php_hash_ripemd256_ops;
95
extern const php_hash_ops php_hash_ripemd320_ops;
96
extern const php_hash_ops php_hash_whirlpool_ops;
97
extern const php_hash_ops php_hash_3tiger128_ops;
98
extern const php_hash_ops php_hash_3tiger160_ops;
99
extern const php_hash_ops php_hash_3tiger192_ops;
100
extern const php_hash_ops php_hash_4tiger128_ops;
101
extern const php_hash_ops php_hash_4tiger160_ops;
102
extern const php_hash_ops php_hash_4tiger192_ops;
103
extern const php_hash_ops php_hash_snefru_ops;
104
extern const php_hash_ops php_hash_gost_ops;
105
extern const php_hash_ops php_hash_gost_crypto_ops;
106
extern const php_hash_ops php_hash_adler32_ops;
107
extern const php_hash_ops php_hash_crc32_ops;
108
extern const php_hash_ops php_hash_crc32b_ops;
109
extern const php_hash_ops php_hash_crc32c_ops;
110
extern const php_hash_ops php_hash_fnv132_ops;
111
extern const php_hash_ops php_hash_fnv1a32_ops;
112
extern const php_hash_ops php_hash_fnv164_ops;
113
extern const php_hash_ops php_hash_fnv1a64_ops;
114
extern const php_hash_ops php_hash_joaat_ops;
115
extern const php_hash_ops php_hash_murmur3a_ops;
116
extern const php_hash_ops php_hash_murmur3c_ops;
117
extern const php_hash_ops php_hash_murmur3f_ops;
118
extern const php_hash_ops php_hash_xxh32_ops;
119
extern const php_hash_ops php_hash_xxh64_ops;
120
extern const php_hash_ops php_hash_xxh3_64_ops;
121
extern const php_hash_ops php_hash_xxh3_128_ops;
122
123
#define PHP_HASH_HAVAL_OPS(p,b) extern const php_hash_ops php_hash_##p##haval##b##_ops;
124
125
PHP_HASH_HAVAL_OPS(3,128)
126
PHP_HASH_HAVAL_OPS(3,160)
127
PHP_HASH_HAVAL_OPS(3,192)
128
PHP_HASH_HAVAL_OPS(3,224)
129
PHP_HASH_HAVAL_OPS(3,256)
130
131
PHP_HASH_HAVAL_OPS(4,128)
132
PHP_HASH_HAVAL_OPS(4,160)
133
PHP_HASH_HAVAL_OPS(4,192)
134
PHP_HASH_HAVAL_OPS(4,224)
135
PHP_HASH_HAVAL_OPS(4,256)
136
137
PHP_HASH_HAVAL_OPS(5,128)
138
PHP_HASH_HAVAL_OPS(5,160)
139
PHP_HASH_HAVAL_OPS(5,192)
140
PHP_HASH_HAVAL_OPS(5,224)
141
PHP_HASH_HAVAL_OPS(5,256)
142
143
extern zend_module_entry hash_module_entry;
144
#define phpext_hash_ptr &hash_module_entry
145
146
#ifdef PHP_WIN32
147
# define PHP_HASH_API __declspec(dllexport)
148
#elif defined(__GNUC__) && __GNUC__ >= 4
149
# define PHP_HASH_API __attribute__ ((visibility("default")))
150
#else
151
# define PHP_HASH_API
152
#endif
153
154
extern PHP_HASH_API zend_class_entry *php_hashcontext_ce;
155
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(zend_string *algo);
156
PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops);
157
PHP_HASH_API zend_result php_hash_copy(const void *ops, const void *orig_context, void *dest_context);
158
PHP_HASH_API hash_spec_result php_hash_serialize(const php_hashcontext_object *context, zend_long *magic, zval *zv);
159
PHP_HASH_API hash_spec_result php_hash_unserialize(php_hashcontext_object *context, zend_long magic, const zval *zv);
160
PHP_HASH_API hash_spec_result php_hash_serialize_spec(const php_hashcontext_object *context, zval *zv, const char *spec);
161
PHP_HASH_API hash_spec_result php_hash_unserialize_spec(php_hashcontext_object *hash, const zval *zv, const char *spec);
162
163
6.18k
static inline void *php_hash_alloc_context(const php_hash_ops *ops) {
164
  /* Zero out context memory so serialization doesn't expose internals */
165
6.18k
  if (ops->context_align > 0) {
166
0
    size_t align = ops->context_align;
167
0
    char *base = ecalloc(1, ops->context_size + align);
168
0
    size_t offset = align - ((uintptr_t)base & (align - 1));
169
0
    char *ptr = base + offset;
170
0
    ptr[-1] = (char)offset;
171
0
    return ptr;
172
0
  }
173
6.18k
  return ecalloc(1, ops->context_size);
174
6.18k
}
Unexecuted instantiation: hash_adler32.c:php_hash_alloc_context
Unexecuted instantiation: hash_crc32.c:php_hash_alloc_context
Unexecuted instantiation: hash_fnv.c:php_hash_alloc_context
Unexecuted instantiation: hash_gost.c:php_hash_alloc_context
Unexecuted instantiation: hash_haval.c:php_hash_alloc_context
Unexecuted instantiation: hash_joaat.c:php_hash_alloc_context
Unexecuted instantiation: hash_md.c:php_hash_alloc_context
Unexecuted instantiation: hash_murmur.c:php_hash_alloc_context
Unexecuted instantiation: hash_ripemd.c:php_hash_alloc_context
Unexecuted instantiation: hash_sha_ni.c:php_hash_alloc_context
Unexecuted instantiation: hash_sha_sse2.c:php_hash_alloc_context
Unexecuted instantiation: hash_sha.c:php_hash_alloc_context
Unexecuted instantiation: hash_sha3.c:php_hash_alloc_context
Unexecuted instantiation: hash_snefru.c:php_hash_alloc_context
Unexecuted instantiation: hash_tiger.c:php_hash_alloc_context
Unexecuted instantiation: hash_whirlpool.c:php_hash_alloc_context
Unexecuted instantiation: hash_xxhash.c:php_hash_alloc_context
hash.c:php_hash_alloc_context
Line
Count
Source
163
6.18k
static inline void *php_hash_alloc_context(const php_hash_ops *ops) {
164
  /* Zero out context memory so serialization doesn't expose internals */
165
6.18k
  if (ops->context_align > 0) {
166
0
    size_t align = ops->context_align;
167
0
    char *base = ecalloc(1, ops->context_size + align);
168
0
    size_t offset = align - ((uintptr_t)base & (align - 1));
169
0
    char *ptr = base + offset;
170
0
    ptr[-1] = (char)offset;
171
0
    return ptr;
172
0
  }
173
6.18k
  return ecalloc(1, ops->context_size);
174
6.18k
}
Unexecuted instantiation: internal_functions_cli.c:php_hash_alloc_context
175
176
6.18k
static inline void php_hash_free_context(const php_hash_ops *ops, void *ctx) {
177
6.18k
  if (ops->context_align > 0) {
178
0
    unsigned char offset = ((unsigned char *)ctx)[-1];
179
0
    efree((char *)ctx - offset);
180
0
    return;
181
0
  }
182
6.18k
  efree(ctx);
183
6.18k
}
Unexecuted instantiation: hash_adler32.c:php_hash_free_context
Unexecuted instantiation: hash_crc32.c:php_hash_free_context
Unexecuted instantiation: hash_fnv.c:php_hash_free_context
Unexecuted instantiation: hash_gost.c:php_hash_free_context
Unexecuted instantiation: hash_haval.c:php_hash_free_context
Unexecuted instantiation: hash_joaat.c:php_hash_free_context
Unexecuted instantiation: hash_md.c:php_hash_free_context
Unexecuted instantiation: hash_murmur.c:php_hash_free_context
Unexecuted instantiation: hash_ripemd.c:php_hash_free_context
Unexecuted instantiation: hash_sha_ni.c:php_hash_free_context
Unexecuted instantiation: hash_sha_sse2.c:php_hash_free_context
Unexecuted instantiation: hash_sha.c:php_hash_free_context
Unexecuted instantiation: hash_sha3.c:php_hash_free_context
Unexecuted instantiation: hash_snefru.c:php_hash_free_context
Unexecuted instantiation: hash_tiger.c:php_hash_free_context
Unexecuted instantiation: hash_whirlpool.c:php_hash_free_context
Unexecuted instantiation: hash_xxhash.c:php_hash_free_context
hash.c:php_hash_free_context
Line
Count
Source
176
6.18k
static inline void php_hash_free_context(const php_hash_ops *ops, void *ctx) {
177
6.18k
  if (ops->context_align > 0) {
178
0
    unsigned char offset = ((unsigned char *)ctx)[-1];
179
0
    efree((char *)ctx - offset);
180
0
    return;
181
0
  }
182
6.18k
  efree(ctx);
183
6.18k
}
Unexecuted instantiation: internal_functions_cli.c:php_hash_free_context
184
185
#endif  /* PHP_HASH_H */