1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Dmitry Stogov <dmitry@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifndef ZEND_STRING_H
20 #define ZEND_STRING_H
21
22 #include "zend.h"
23
24 BEGIN_EXTERN_C()
25
26 typedef void (*zend_string_copy_storage_func_t)(void);
27 typedef zend_string *(ZEND_FASTCALL *zend_new_interned_string_func_t)(zend_string *str);
28 typedef zend_string *(ZEND_FASTCALL *zend_string_init_interned_func_t)(const char *str, size_t size, bool permanent);
29 typedef zend_string *(ZEND_FASTCALL *zend_string_init_existing_interned_func_t)(const char *str, size_t size, bool permanent);
30
31 ZEND_API extern zend_new_interned_string_func_t zend_new_interned_string;
32 ZEND_API extern zend_string_init_interned_func_t zend_string_init_interned;
33 /* Init an interned string if it already exists, but do not create a new one if it does not. */
34 ZEND_API extern zend_string_init_existing_interned_func_t zend_string_init_existing_interned;
35
36 ZEND_API zend_ulong ZEND_FASTCALL zend_string_hash_func(zend_string *str);
37 ZEND_API zend_ulong ZEND_FASTCALL zend_hash_func(const char *str, size_t len);
38 ZEND_API zend_string* ZEND_FASTCALL zend_interned_string_find_permanent(zend_string *str);
39
40 ZEND_API zend_string *zend_string_concat2(
41 const char *str1, size_t str1_len,
42 const char *str2, size_t str2_len);
43 ZEND_API zend_string *zend_string_concat3(
44 const char *str1, size_t str1_len,
45 const char *str2, size_t str2_len,
46 const char *str3, size_t str3_len);
47
48 ZEND_API void zend_interned_strings_init(void);
49 ZEND_API void zend_interned_strings_dtor(void);
50 ZEND_API void zend_interned_strings_activate(void);
51 ZEND_API void zend_interned_strings_deactivate(void);
52 ZEND_API void zend_interned_strings_set_request_storage_handlers(
53 zend_new_interned_string_func_t handler,
54 zend_string_init_interned_func_t init_handler,
55 zend_string_init_existing_interned_func_t init_existing_handler);
56 ZEND_API void zend_interned_strings_switch_storage(bool request);
57
58 ZEND_API extern zend_string *zend_empty_string;
59 ZEND_API extern zend_string *zend_one_char_string[256];
60 ZEND_API extern zend_string **zend_known_strings;
61
END_EXTERN_C()62 END_EXTERN_C()
63
64 /* Shortcuts */
65
66 #define ZSTR_VAL(zstr) (zstr)->val
67 #define ZSTR_LEN(zstr) (zstr)->len
68 #define ZSTR_H(zstr) (zstr)->h
69 #define ZSTR_HASH(zstr) zend_string_hash_val(zstr)
70
71 /* Compatibility macros */
72
73 #define IS_INTERNED(s) ZSTR_IS_INTERNED(s)
74 #define STR_EMPTY_ALLOC() ZSTR_EMPTY_ALLOC()
75 #define _STR_HEADER_SIZE _ZSTR_HEADER_SIZE
76 #define STR_ALLOCA_ALLOC(str, _len, use_heap) ZSTR_ALLOCA_ALLOC(str, _len, use_heap)
77 #define STR_ALLOCA_INIT(str, s, len, use_heap) ZSTR_ALLOCA_INIT(str, s, len, use_heap)
78 #define STR_ALLOCA_FREE(str, use_heap) ZSTR_ALLOCA_FREE(str, use_heap)
79
80 /*---*/
81
82 #define ZSTR_IS_INTERNED(s) (GC_FLAGS(s) & IS_STR_INTERNED)
83 #define ZSTR_IS_VALID_UTF8(s) (GC_FLAGS(s) & IS_STR_VALID_UTF8)
84
85 /* These are properties, encoded as flags, that will hold on the resulting string
86 * after concatenating two strings that have these property.
87 * Example: concatenating two UTF-8 strings yields another UTF-8 string. */
88 #define ZSTR_COPYABLE_CONCAT_PROPERTIES (IS_STR_VALID_UTF8)
89
90 #define ZSTR_GET_COPYABLE_CONCAT_PROPERTIES(s) (GC_FLAGS(s) & ZSTR_COPYABLE_CONCAT_PROPERTIES)
91 /* This macro returns the copyable concat properties which hold on both strings. */
92 #define ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH(s1, s2) (GC_FLAGS(s1) & GC_FLAGS(s2) & ZSTR_COPYABLE_CONCAT_PROPERTIES)
93
94 #define ZSTR_COPY_CONCAT_PROPERTIES(out, in) do { \
95 zend_string *_out = (out); \
96 uint32_t properties = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES((in)); \
97 GC_ADD_FLAGS(_out, properties); \
98 } while (0)
99
100 #define ZSTR_COPY_CONCAT_PROPERTIES_BOTH(out, in1, in2) do { \
101 zend_string *_out = (out); \
102 uint32_t properties = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH((in1), (in2)); \
103 GC_ADD_FLAGS(_out, properties); \
104 } while (0)
105
106 #define ZSTR_EMPTY_ALLOC() zend_empty_string
107 #define ZSTR_CHAR(c) zend_one_char_string[c]
108 #define ZSTR_KNOWN(idx) zend_known_strings[idx]
109
110 #define _ZSTR_HEADER_SIZE XtOffsetOf(zend_string, val)
111
112 #define _ZSTR_STRUCT_SIZE(len) (_ZSTR_HEADER_SIZE + len + 1)
113
114 #define ZSTR_MAX_OVERHEAD (ZEND_MM_ALIGNED_SIZE(_ZSTR_HEADER_SIZE + 1))
115 #define ZSTR_MAX_LEN (SIZE_MAX - ZSTR_MAX_OVERHEAD)
116
117 #define ZSTR_ALLOCA_ALLOC(str, _len, use_heap) do { \
118 (str) = (zend_string *)do_alloca(ZEND_MM_ALIGNED_SIZE_EX(_ZSTR_STRUCT_SIZE(_len), 8), (use_heap)); \
119 GC_SET_REFCOUNT(str, 1); \
120 GC_TYPE_INFO(str) = GC_STRING; \
121 ZSTR_H(str) = 0; \
122 ZSTR_LEN(str) = _len; \
123 } while (0)
124
125 #define ZSTR_ALLOCA_INIT(str, s, len, use_heap) do { \
126 ZSTR_ALLOCA_ALLOC(str, len, use_heap); \
127 memcpy(ZSTR_VAL(str), (s), (len)); \
128 ZSTR_VAL(str)[(len)] = '\0'; \
129 } while (0)
130
131 #define ZSTR_ALLOCA_FREE(str, use_heap) free_alloca(str, use_heap)
132
133 #define ZSTR_INIT_LITERAL(s, persistent) (zend_string_init((s), strlen(s), (persistent)))
134
135 /*---*/
136
137 static zend_always_inline zend_ulong zend_string_hash_val(zend_string *s)
138 {
139 return ZSTR_H(s) ? ZSTR_H(s) : zend_string_hash_func(s);
140 }
141
zend_string_forget_hash_val(zend_string * s)142 static zend_always_inline void zend_string_forget_hash_val(zend_string *s)
143 {
144 ZSTR_H(s) = 0;
145 GC_DEL_FLAGS(s, IS_STR_VALID_UTF8);
146 }
147
zend_string_refcount(const zend_string * s)148 static zend_always_inline uint32_t zend_string_refcount(const zend_string *s)
149 {
150 if (!ZSTR_IS_INTERNED(s)) {
151 return GC_REFCOUNT(s);
152 }
153 return 1;
154 }
155
zend_string_addref(zend_string * s)156 static zend_always_inline uint32_t zend_string_addref(zend_string *s)
157 {
158 if (!ZSTR_IS_INTERNED(s)) {
159 return GC_ADDREF(s);
160 }
161 return 1;
162 }
163
zend_string_delref(zend_string * s)164 static zend_always_inline uint32_t zend_string_delref(zend_string *s)
165 {
166 if (!ZSTR_IS_INTERNED(s)) {
167 return GC_DELREF(s);
168 }
169 return 1;
170 }
171
zend_string_alloc(size_t len,bool persistent)172 static zend_always_inline zend_string *zend_string_alloc(size_t len, bool persistent)
173 {
174 zend_string *ret = (zend_string *)pemalloc(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
175
176 GC_SET_REFCOUNT(ret, 1);
177 GC_TYPE_INFO(ret) = GC_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << GC_FLAGS_SHIFT);
178 ZSTR_H(ret) = 0;
179 ZSTR_LEN(ret) = len;
180 return ret;
181 }
182
zend_string_safe_alloc(size_t n,size_t m,size_t l,bool persistent)183 static zend_always_inline zend_string *zend_string_safe_alloc(size_t n, size_t m, size_t l, bool persistent)
184 {
185 zend_string *ret = (zend_string *)safe_pemalloc(n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
186
187 GC_SET_REFCOUNT(ret, 1);
188 GC_TYPE_INFO(ret) = GC_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << GC_FLAGS_SHIFT);
189 ZSTR_H(ret) = 0;
190 ZSTR_LEN(ret) = (n * m) + l;
191 return ret;
192 }
193
zend_string_init(const char * str,size_t len,bool persistent)194 static zend_always_inline zend_string *zend_string_init(const char *str, size_t len, bool persistent)
195 {
196 zend_string *ret = zend_string_alloc(len, persistent);
197
198 memcpy(ZSTR_VAL(ret), str, len);
199 ZSTR_VAL(ret)[len] = '\0';
200 return ret;
201 }
202
zend_string_init_fast(const char * str,size_t len)203 static zend_always_inline zend_string *zend_string_init_fast(const char *str, size_t len)
204 {
205 if (len > 1) {
206 return zend_string_init(str, len, 0);
207 } else if (len == 0) {
208 return zend_empty_string;
209 } else /* if (len == 1) */ {
210 return ZSTR_CHAR((zend_uchar) *str);
211 }
212 }
213
zend_string_copy(zend_string * s)214 static zend_always_inline zend_string *zend_string_copy(zend_string *s)
215 {
216 if (!ZSTR_IS_INTERNED(s)) {
217 GC_ADDREF(s);
218 }
219 return s;
220 }
221
zend_string_dup(zend_string * s,bool persistent)222 static zend_always_inline zend_string *zend_string_dup(zend_string *s, bool persistent)
223 {
224 if (ZSTR_IS_INTERNED(s)) {
225 return s;
226 } else {
227 return zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
228 }
229 }
230
zend_string_separate(zend_string * s,bool persistent)231 static zend_always_inline zend_string *zend_string_separate(zend_string *s, bool persistent)
232 {
233 if (ZSTR_IS_INTERNED(s) || GC_REFCOUNT(s) > 1) {
234 if (!ZSTR_IS_INTERNED(s)) {
235 GC_DELREF(s);
236 }
237 return zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
238 }
239
240 zend_string_forget_hash_val(s);
241 return s;
242 }
243
zend_string_realloc(zend_string * s,size_t len,bool persistent)244 static zend_always_inline zend_string *zend_string_realloc(zend_string *s, size_t len, bool persistent)
245 {
246 zend_string *ret;
247
248 if (!ZSTR_IS_INTERNED(s)) {
249 if (EXPECTED(GC_REFCOUNT(s) == 1)) {
250 ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
251 ZSTR_LEN(ret) = len;
252 zend_string_forget_hash_val(ret);
253 return ret;
254 }
255 }
256 ret = zend_string_alloc(len, persistent);
257 memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), MIN(len, ZSTR_LEN(s)) + 1);
258 if (!ZSTR_IS_INTERNED(s)) {
259 GC_DELREF(s);
260 }
261 return ret;
262 }
263
zend_string_extend(zend_string * s,size_t len,bool persistent)264 static zend_always_inline zend_string *zend_string_extend(zend_string *s, size_t len, bool persistent)
265 {
266 zend_string *ret;
267
268 ZEND_ASSERT(len >= ZSTR_LEN(s));
269 if (!ZSTR_IS_INTERNED(s)) {
270 if (EXPECTED(GC_REFCOUNT(s) == 1)) {
271 ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
272 ZSTR_LEN(ret) = len;
273 zend_string_forget_hash_val(ret);
274 return ret;
275 }
276 }
277 ret = zend_string_alloc(len, persistent);
278 memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), ZSTR_LEN(s) + 1);
279 if (!ZSTR_IS_INTERNED(s)) {
280 GC_DELREF(s);
281 }
282 return ret;
283 }
284
zend_string_truncate(zend_string * s,size_t len,bool persistent)285 static zend_always_inline zend_string *zend_string_truncate(zend_string *s, size_t len, bool persistent)
286 {
287 zend_string *ret;
288
289 ZEND_ASSERT(len <= ZSTR_LEN(s));
290 if (!ZSTR_IS_INTERNED(s)) {
291 if (EXPECTED(GC_REFCOUNT(s) == 1)) {
292 ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
293 ZSTR_LEN(ret) = len;
294 zend_string_forget_hash_val(ret);
295 return ret;
296 }
297 }
298 ret = zend_string_alloc(len, persistent);
299 memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), len + 1);
300 if (!ZSTR_IS_INTERNED(s)) {
301 GC_DELREF(s);
302 }
303 return ret;
304 }
305
zend_string_safe_realloc(zend_string * s,size_t n,size_t m,size_t l,bool persistent)306 static zend_always_inline zend_string *zend_string_safe_realloc(zend_string *s, size_t n, size_t m, size_t l, bool persistent)
307 {
308 zend_string *ret;
309
310 if (!ZSTR_IS_INTERNED(s)) {
311 if (GC_REFCOUNT(s) == 1) {
312 ret = (zend_string *)safe_perealloc(s, n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
313 ZSTR_LEN(ret) = (n * m) + l;
314 zend_string_forget_hash_val(ret);
315 return ret;
316 }
317 }
318 ret = zend_string_safe_alloc(n, m, l, persistent);
319 memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), MIN((n * m) + l, ZSTR_LEN(s)) + 1);
320 if (!ZSTR_IS_INTERNED(s)) {
321 GC_DELREF(s);
322 }
323 return ret;
324 }
325
zend_string_free(zend_string * s)326 static zend_always_inline void zend_string_free(zend_string *s)
327 {
328 if (!ZSTR_IS_INTERNED(s)) {
329 ZEND_ASSERT(GC_REFCOUNT(s) <= 1);
330 pefree(s, GC_FLAGS(s) & IS_STR_PERSISTENT);
331 }
332 }
333
zend_string_efree(zend_string * s)334 static zend_always_inline void zend_string_efree(zend_string *s)
335 {
336 ZEND_ASSERT(!ZSTR_IS_INTERNED(s));
337 ZEND_ASSERT(GC_REFCOUNT(s) <= 1);
338 ZEND_ASSERT(!(GC_FLAGS(s) & IS_STR_PERSISTENT));
339 efree(s);
340 }
341
zend_string_release(zend_string * s)342 static zend_always_inline void zend_string_release(zend_string *s)
343 {
344 if (!ZSTR_IS_INTERNED(s)) {
345 if (GC_DELREF(s) == 0) {
346 pefree(s, GC_FLAGS(s) & IS_STR_PERSISTENT);
347 }
348 }
349 }
350
zend_string_release_ex(zend_string * s,bool persistent)351 static zend_always_inline void zend_string_release_ex(zend_string *s, bool persistent)
352 {
353 if (!ZSTR_IS_INTERNED(s)) {
354 if (GC_DELREF(s) == 0) {
355 if (persistent) {
356 ZEND_ASSERT(GC_FLAGS(s) & IS_STR_PERSISTENT);
357 free(s);
358 } else {
359 ZEND_ASSERT(!(GC_FLAGS(s) & IS_STR_PERSISTENT));
360 efree(s);
361 }
362 }
363 }
364 }
365
zend_string_equals_cstr(const zend_string * s1,const char * s2,size_t s2_length)366 static zend_always_inline bool zend_string_equals_cstr(const zend_string *s1, const char *s2, size_t s2_length)
367 {
368 return ZSTR_LEN(s1) == s2_length && !memcmp(ZSTR_VAL(s1), s2, s2_length);
369 }
370
371 #if defined(__GNUC__) && (defined(__i386__) || (defined(__x86_64__) && !defined(__ILP32__)))
372 BEGIN_EXTERN_C()
373 ZEND_API bool ZEND_FASTCALL zend_string_equal_val(const zend_string *s1, const zend_string *s2);
END_EXTERN_C()374 END_EXTERN_C()
375 #else
376 static zend_always_inline bool zend_string_equal_val(const zend_string *s1, const zend_string *s2)
377 {
378 return !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1));
379 }
380 #endif
381
382 static zend_always_inline bool zend_string_equal_content(const zend_string *s1, const zend_string *s2)
383 {
384 return ZSTR_LEN(s1) == ZSTR_LEN(s2) && zend_string_equal_val(s1, s2);
385 }
386
zend_string_equals(const zend_string * s1,const zend_string * s2)387 static zend_always_inline bool zend_string_equals(const zend_string *s1, const zend_string *s2)
388 {
389 return s1 == s2 || zend_string_equal_content(s1, s2);
390 }
391
392 #define zend_string_equals_ci(s1, s2) \
393 (ZSTR_LEN(s1) == ZSTR_LEN(s2) && !zend_binary_strcasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)))
394
395 #define zend_string_equals_literal_ci(str, c) \
396 (ZSTR_LEN(str) == sizeof("" c) - 1 && !zend_binary_strcasecmp(ZSTR_VAL(str), ZSTR_LEN(str), (c), sizeof(c) - 1))
397
398 #define zend_string_equals_literal(str, literal) \
399 zend_string_equals_cstr(str, "" literal, sizeof(literal) - 1)
400
zend_string_starts_with_cstr(const zend_string * str,const char * prefix,size_t prefix_length)401 static zend_always_inline bool zend_string_starts_with_cstr(const zend_string *str, const char *prefix, size_t prefix_length)
402 {
403 return ZSTR_LEN(str) >= prefix_length && !memcmp(ZSTR_VAL(str), prefix, prefix_length);
404 }
405
zend_string_starts_with(const zend_string * str,const zend_string * prefix)406 static zend_always_inline bool zend_string_starts_with(const zend_string *str, const zend_string *prefix)
407 {
408 return zend_string_starts_with_cstr(str, ZSTR_VAL(prefix), ZSTR_LEN(prefix));
409 }
410
411 #define zend_string_starts_with_literal(str, prefix) \
412 zend_string_starts_with_cstr(str, prefix, strlen(prefix))
413
zend_string_starts_with_cstr_ci(const zend_string * str,const char * prefix,size_t prefix_length)414 static zend_always_inline bool zend_string_starts_with_cstr_ci(const zend_string *str, const char *prefix, size_t prefix_length)
415 {
416 return ZSTR_LEN(str) >= prefix_length && !strncasecmp(ZSTR_VAL(str), prefix, prefix_length);
417 }
418
zend_string_starts_with_ci(const zend_string * str,const zend_string * prefix)419 static zend_always_inline bool zend_string_starts_with_ci(const zend_string *str, const zend_string *prefix)
420 {
421 return zend_string_starts_with_cstr_ci(str, ZSTR_VAL(prefix), ZSTR_LEN(prefix));
422 }
423
424 #define zend_string_starts_with_literal_ci(str, prefix) \
425 zend_string_starts_with_cstr_ci(str, prefix, strlen(prefix))
426
427 /*
428 * DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
429 *
430 * This is Daniel J. Bernstein's popular `times 33' hash function as
431 * posted by him years ago on comp.lang.c. It basically uses a function
432 * like ``hash(i) = hash(i-1) * 33 + str[i]''. This is one of the best
433 * known hash functions for strings. Because it is both computed very
434 * fast and distributes very well.
435 *
436 * The magic of number 33, i.e. why it works better than many other
437 * constants, prime or not, has never been adequately explained by
438 * anyone. So I try an explanation: if one experimentally tests all
439 * multipliers between 1 and 256 (as RSE did now) one detects that even
440 * numbers are not usable at all. The remaining 128 odd numbers
441 * (except for the number 1) work more or less all equally well. They
442 * all distribute in an acceptable way and this way fill a hash table
443 * with an average percent of approx. 86%.
444 *
445 * If one compares the Chi^2 values of the variants, the number 33 not
446 * even has the best value. But the number 33 and a few other equally
447 * good numbers like 17, 31, 63, 127 and 129 have nevertheless a great
448 * advantage to the remaining numbers in the large set of possible
449 * multipliers: their multiply operation can be replaced by a faster
450 * operation based on just one shift plus either a single addition
451 * or subtraction operation. And because a hash function has to both
452 * distribute good _and_ has to be very fast to compute, those few
453 * numbers should be preferred and seems to be the reason why Daniel J.
454 * Bernstein also preferred it.
455 *
456 *
457 * -- Ralf S. Engelschall <rse@engelschall.com>
458 */
459
zend_inline_hash_func(const char * str,size_t len)460 static zend_always_inline zend_ulong zend_inline_hash_func(const char *str, size_t len)
461 {
462 zend_ulong hash = Z_UL(5381);
463
464 #if defined(_WIN32) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
465 /* Version with multiplication works better on modern CPU */
466 for (; len >= 8; len -= 8, str += 8) {
467 # if defined(__aarch64__) && !defined(WORDS_BIGENDIAN)
468 /* On some architectures it is beneficial to load 8 bytes at a
469 time and extract each byte with a bit field extract instr. */
470 uint64_t chunk;
471
472 memcpy(&chunk, str, sizeof(chunk));
473 hash =
474 hash * 33 * 33 * 33 * 33 +
475 ((chunk >> (8 * 0)) & 0xff) * 33 * 33 * 33 +
476 ((chunk >> (8 * 1)) & 0xff) * 33 * 33 +
477 ((chunk >> (8 * 2)) & 0xff) * 33 +
478 ((chunk >> (8 * 3)) & 0xff);
479 hash =
480 hash * 33 * 33 * 33 * 33 +
481 ((chunk >> (8 * 4)) & 0xff) * 33 * 33 * 33 +
482 ((chunk >> (8 * 5)) & 0xff) * 33 * 33 +
483 ((chunk >> (8 * 6)) & 0xff) * 33 +
484 ((chunk >> (8 * 7)) & 0xff);
485 # else
486 hash =
487 hash * Z_L(33 * 33 * 33 * 33) +
488 str[0] * Z_L(33 * 33 * 33) +
489 str[1] * Z_L(33 * 33) +
490 str[2] * Z_L(33) +
491 str[3];
492 hash =
493 hash * Z_L(33 * 33 * 33 * 33) +
494 str[4] * Z_L(33 * 33 * 33) +
495 str[5] * Z_L(33 * 33) +
496 str[6] * Z_L(33) +
497 str[7];
498 # endif
499 }
500 if (len >= 4) {
501 hash =
502 hash * Z_L(33 * 33 * 33 * 33) +
503 str[0] * Z_L(33 * 33 * 33) +
504 str[1] * Z_L(33 * 33) +
505 str[2] * Z_L(33) +
506 str[3];
507 len -= 4;
508 str += 4;
509 }
510 if (len >= 2) {
511 if (len > 2) {
512 hash =
513 hash * Z_L(33 * 33 * 33) +
514 str[0] * Z_L(33 * 33) +
515 str[1] * Z_L(33) +
516 str[2];
517 } else {
518 hash =
519 hash * Z_L(33 * 33) +
520 str[0] * Z_L(33) +
521 str[1];
522 }
523 } else if (len != 0) {
524 hash = hash * Z_L(33) + *str;
525 }
526 #else
527 /* variant with the hash unrolled eight times */
528 for (; len >= 8; len -= 8) {
529 hash = ((hash << 5) + hash) + *str++;
530 hash = ((hash << 5) + hash) + *str++;
531 hash = ((hash << 5) + hash) + *str++;
532 hash = ((hash << 5) + hash) + *str++;
533 hash = ((hash << 5) + hash) + *str++;
534 hash = ((hash << 5) + hash) + *str++;
535 hash = ((hash << 5) + hash) + *str++;
536 hash = ((hash << 5) + hash) + *str++;
537 }
538 switch (len) {
539 case 7: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
540 case 6: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
541 case 5: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
542 case 4: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
543 case 3: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
544 case 2: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
545 case 1: hash = ((hash << 5) + hash) + *str++; break;
546 case 0: break;
547 EMPTY_SWITCH_DEFAULT_CASE()
548 }
549 #endif
550
551 /* Hash value can't be zero, so we always set the high bit */
552 #if SIZEOF_ZEND_LONG == 8
553 return hash | Z_UL(0x8000000000000000);
554 #elif SIZEOF_ZEND_LONG == 4
555 return hash | Z_UL(0x80000000);
556 #else
557 # error "Unknown SIZEOF_ZEND_LONG"
558 #endif
559 }
560
561 #define ZEND_KNOWN_STRINGS(_) \
562 _(ZEND_STR_FILE, "file") \
563 _(ZEND_STR_LINE, "line") \
564 _(ZEND_STR_FUNCTION, "function") \
565 _(ZEND_STR_CLASS, "class") \
566 _(ZEND_STR_OBJECT, "object") \
567 _(ZEND_STR_TYPE, "type") \
568 _(ZEND_STR_OBJECT_OPERATOR, "->") \
569 _(ZEND_STR_PAAMAYIM_NEKUDOTAYIM, "::") \
570 _(ZEND_STR_ARGS, "args") \
571 _(ZEND_STR_UNKNOWN, "unknown") \
572 _(ZEND_STR_UNKNOWN_CAPITALIZED, "Unknown") \
573 _(ZEND_STR_EVAL, "eval") \
574 _(ZEND_STR_INCLUDE, "include") \
575 _(ZEND_STR_REQUIRE, "require") \
576 _(ZEND_STR_INCLUDE_ONCE, "include_once") \
577 _(ZEND_STR_REQUIRE_ONCE, "require_once") \
578 _(ZEND_STR_SCALAR, "scalar") \
579 _(ZEND_STR_ERROR_REPORTING, "error_reporting") \
580 _(ZEND_STR_STATIC, "static") \
581 _(ZEND_STR_THIS, "this") \
582 _(ZEND_STR_VALUE, "value") \
583 _(ZEND_STR_KEY, "key") \
584 _(ZEND_STR_MAGIC_INVOKE, "__invoke") \
585 _(ZEND_STR_PREVIOUS, "previous") \
586 _(ZEND_STR_CODE, "code") \
587 _(ZEND_STR_MESSAGE, "message") \
588 _(ZEND_STR_SEVERITY, "severity") \
589 _(ZEND_STR_STRING, "string") \
590 _(ZEND_STR_TRACE, "trace") \
591 _(ZEND_STR_SCHEME, "scheme") \
592 _(ZEND_STR_HOST, "host") \
593 _(ZEND_STR_PORT, "port") \
594 _(ZEND_STR_USER, "user") \
595 _(ZEND_STR_PASS, "pass") \
596 _(ZEND_STR_PATH, "path") \
597 _(ZEND_STR_QUERY, "query") \
598 _(ZEND_STR_FRAGMENT, "fragment") \
599 _(ZEND_STR_NULL, "NULL") \
600 _(ZEND_STR_BOOLEAN, "boolean") \
601 _(ZEND_STR_INTEGER, "integer") \
602 _(ZEND_STR_DOUBLE, "double") \
603 _(ZEND_STR_ARRAY, "array") \
604 _(ZEND_STR_RESOURCE, "resource") \
605 _(ZEND_STR_CLOSED_RESOURCE, "resource (closed)") \
606 _(ZEND_STR_NAME, "name") \
607 _(ZEND_STR_ARGV, "argv") \
608 _(ZEND_STR_ARGC, "argc") \
609 _(ZEND_STR_ARRAY_CAPITALIZED, "Array") \
610 _(ZEND_STR_BOOL, "bool") \
611 _(ZEND_STR_INT, "int") \
612 _(ZEND_STR_FLOAT, "float") \
613 _(ZEND_STR_CALLABLE, "callable") \
614 _(ZEND_STR_ITERABLE, "iterable") \
615 _(ZEND_STR_VOID, "void") \
616 _(ZEND_STR_NEVER, "never") \
617 _(ZEND_STR_FALSE, "false") \
618 _(ZEND_STR_TRUE, "true") \
619 _(ZEND_STR_NULL_LOWERCASE, "null") \
620 _(ZEND_STR_MIXED, "mixed") \
621 _(ZEND_STR_TRAVERSABLE, "Traversable") \
622 _(ZEND_STR_SLEEP, "__sleep") \
623 _(ZEND_STR_WAKEUP, "__wakeup") \
624 _(ZEND_STR_CASES, "cases") \
625 _(ZEND_STR_FROM, "from") \
626 _(ZEND_STR_TRYFROM, "tryFrom") \
627 _(ZEND_STR_TRYFROM_LOWERCASE, "tryfrom") \
628 _(ZEND_STR_AUTOGLOBAL_SERVER, "_SERVER") \
629 _(ZEND_STR_AUTOGLOBAL_ENV, "_ENV") \
630 _(ZEND_STR_AUTOGLOBAL_REQUEST, "_REQUEST") \
631 _(ZEND_STR_COUNT, "count") \
632 _(ZEND_STR_SENSITIVEPARAMETER, "SensitiveParameter") \
633 _(ZEND_STR_CONST_EXPR_PLACEHOLDER, "[constant expression]") \
634
635
636 typedef enum _zend_known_string_id {
637 #define _ZEND_STR_ID(id, str) id,
638 ZEND_KNOWN_STRINGS(_ZEND_STR_ID)
639 #undef _ZEND_STR_ID
640 ZEND_STR_LAST_KNOWN
641 } zend_known_string_id;
642
643 #endif /* ZEND_STRING_H */
644