-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathphp_compat.h
More file actions
175 lines (142 loc) · 4.49 KB
/
Copy pathphp_compat.h
File metadata and controls
175 lines (142 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef PHP_REDIS_COMPAT_H
#define PHP_REDIS_COMPAT_H
#include <stddef.h>
#include "php.h"
#include <ext/hash/php_hash.h>
#if PHP_VERSION_ID < 80400
#include <ext/standard/php_random.h>
#include <ext/standard/php_rand.h>
#else
#include <ext/random/php_random.h>
#endif
#ifndef ZEND_UNREACHABLE
#define ZEND_UNREACHABLE() do { ZEND_ASSERT(0); ZEND_ASSUME(0); } while (0)
#endif
#if PHP_VERSION_ID < 80600
/* php_hash_bin2hex() was dropped from ext/hash in PHP 8.6; the equivalent
* routine now lives in the Zend engine as zend_bin2hex(), which has an
* identical signature (char *out, const unsigned char *in, size_t in_len). */
#define zend_bin2hex(out, in, in_len) php_hash_bin2hex((out), (in), (in_len))
#endif
#if PHP_VERSION_ID < 80600
#define zend_ini_bool_literal(name) \
zend_ini_parse_bool(zend_ini_str((name), sizeof("" name) - 1, 0))
#define zend_ini_long_literal(name) \
zend_ini_long((name), sizeof("" name) - 1, 0)
#define zend_ini_double_literal(name) \
zend_ini_double((name), sizeof("" name) - 1, 0)
#define zend_ini_str_literal(name) \
zend_ini_str((name), sizeof("" name) - 1, 0)
#define zend_ini_string_literal(name) \
zend_ini_string((name), sizeof("" name) - 1, 0)
#endif
/* Check before adding: even individually valid lengths can overflow the sum. */
static zend_always_inline size_t
redis_concat_length(size_t len1, size_t len2)
{
if (len1 > ZSTR_MAX_LEN || len2 > ZSTR_MAX_LEN - len1) {
zend_error_noreturn(E_ERROR,
"Prefixing overflows the maximum allowed key length");
}
return len1 + len2;
}
static zend_always_inline zend_string *
redis_string_concat2(const char *str1, size_t len1, const char *str2,
size_t len2)
{
#if PHP_VERSION_ID >= 80000
redis_concat_length(len1, len2);
return zend_string_concat2(str1, len1, str2, len2);
#else
size_t len = redis_concat_length(len1, len2);
zend_string *res = zend_string_alloc(len, 0);
memcpy(ZSTR_VAL(res), str1, len1);
memcpy(ZSTR_VAL(res) + len1, str2, len2);
ZSTR_VAL(res)[len] = '\0';
return res;
#endif
}
#if PHP_VERSION_ID < 80000
static zend_always_inline const php_hash_ops *
redis_hash_fetch_ops(zend_string *zstr)
{
return php_hash_fetch_ops(ZSTR_VAL(zstr), ZSTR_LEN(zstr));
}
#ifndef RETURN_THROWS
#define RETURN_THROWS() RETURN_FALSE
#endif
#ifndef ZVAL_STRINGL_FAST
#define ZVAL_STRINGL_FAST(z, s, l) ZVAL_STRINGL(z, s, l)
#endif
#ifndef RETVAL_STRINGL_FAST
#define RETVAL_STRINGL_FAST(s, l) RETVAL_STRINGL(s, l)
#endif
#ifndef Z_PARAM_ARRAY_OR_NULL
#define Z_PARAM_ARRAY_OR_NULL(dest) \
Z_PARAM_ARRAY_EX(dest, 1, 0)
#endif
#ifndef Z_PARAM_ARRAY_HT_OR_NULL
#define Z_PARAM_ARRAY_HT_OR_NULL(dest) \
Z_PARAM_ARRAY_HT_EX(dest, 1, 0)
#endif
#ifndef Z_PARAM_STRING_OR_NULL
#define Z_PARAM_STRING_OR_NULL(dest, dest_len) \
Z_PARAM_STRING_EX(dest, dest_len, 1, 0)
#endif
#ifndef Z_PARAM_STR_OR_NULL
#define Z_PARAM_STR_OR_NULL(dest) \
Z_PARAM_STR_EX(dest, 1, 0)
#endif
#ifndef Z_PARAM_LONG_OR_NULL
#define Z_PARAM_LONG_OR_NULL(dest, is_null) \
Z_PARAM_LONG_EX(dest, is_null, 1, 0)
#endif
#ifndef Z_PARAM_ZVAL_OR_NULL
#define Z_PARAM_ZVAL_OR_NULL(dest) \
Z_PARAM_ZVAL_EX(dest, 1, 0)
#endif
#ifndef Z_PARAM_BOOL_OR_NULL
#define Z_PARAM_BOOL_OR_NULL(dest, is_null) \
Z_PARAM_BOOL_EX(dest, is_null, 1, 0)
#endif
#else
static zend_always_inline const php_hash_ops *
redis_hash_fetch_ops(zend_string *zstr)
{
return php_hash_fetch_ops(zstr);
}
#endif
#if PHP_VERSION_ID < 80200
static zend_always_inline zend_bool
zend_string_starts_with_cstr(const zend_string *str, const char *prefix,
size_t prefix_length)
{
return ZSTR_LEN(str) >= prefix_length &&
!memcmp(ZSTR_VAL(str), prefix, prefix_length);
}
static zend_always_inline HashTable *
zend_array_to_list(HashTable *arr)
{
zval zret = {0}, *zv;
array_init_size(&zret, zend_hash_num_elements(arr));
ZEND_HASH_FOREACH_VAL(arr, zv) {
Z_TRY_ADDREF_P(zv);
add_next_index_zval(&zret, zv);
} ZEND_HASH_FOREACH_END();
return Z_ARRVAL(zret);
}
#endif
/* From PHP 8.1 on, the generated arginfo flags our classes with
* ZEND_ACC_NOT_SERIALIZABLE; before, handlers deny (un)serialization */
#if PHP_VERSION_ID < 80100
#include <zend_interfaces.h>
#endif
static zend_always_inline void
redis_class_deny_serialization(zend_class_entry *ce)
{
#if PHP_VERSION_ID < 80100
ce->serialize = zend_class_serialize_deny;
ce->unserialize = zend_class_unserialize_deny;
#endif
}
#endif // PHP_REDIS_COMPAT_H