1
2/*
3 +------------------------------------------------------------------------+
4 | Phalcon Framework                                                      |
5 +------------------------------------------------------------------------+
6 | Copyright (c) 2011-2017 Phalcon Team (https://phalconphp.com)          |
7 +------------------------------------------------------------------------+
8 | This source file is subject to the New BSD License that is bundled     |
9 | with this package in the file LICENSE.txt.                             |
10 |                                                                        |
11 | If you did not receive a copy of the license and are unable to         |
12 | obtain it through the world-wide-web, please send an email             |
13 | to license@phalconphp.com so we can send you a copy immediately.       |
14 +------------------------------------------------------------------------+
15 | Authors: Andres Gutierrez <andres@phalconphp.com>                      |
16 |          Eduar Carvajal <eduar@phalconphp.com>                         |
17 +------------------------------------------------------------------------+
18 */
19
20namespace Phalcon;
21
22/**
23 * Phalcon\Kernel
24 *
25 * This class allows to change the internal behavior of the framework in runtime
26 */
27class Kernel
28{
29	/**
30	 * Produces a pre-computed hash key based on a string. This function
31	 * produces different numbers in 32bit/64bit processors
32	 *
33	 * @param string key
34	 * @return string
35	 */
36	public static function preComputeHashKey(string! key)
37	{
38		%{
39
40		{
41
42#if PHP_VERSION_ID < 70000
43		char *arKey = Z_STRVAL_P(key), *strKey;
44		int nKeyLength = strlen(arKey);
45		register ulong hash = 5381;
46
47		nKeyLength++;
48
49		/* variant with the hash unrolled eight times */
50		for (; nKeyLength >= 8; nKeyLength -= 8) {
51			hash = ((hash << 5) + hash) + *arKey++;
52			hash = ((hash << 5) + hash) + *arKey++;
53			hash = ((hash << 5) + hash) + *arKey++;
54			hash = ((hash << 5) + hash) + *arKey++;
55			hash = ((hash << 5) + hash) + *arKey++;
56			hash = ((hash << 5) + hash) + *arKey++;
57			hash = ((hash << 5) + hash) + *arKey++;
58			hash = ((hash << 5) + hash) + *arKey++;
59		}
60
61		switch (nKeyLength) {
62			case 7: hash = ((hash << 5) + hash) + *arKey++;
63			/* no break */
64			case 6: hash = ((hash << 5) + hash) + *arKey++;
65			/* no break */
66			case 5: hash = ((hash << 5) + hash) + *arKey++;
67			/* no break */
68			case 4: hash = ((hash << 5) + hash) + *arKey++;
69			/* no break */
70			case 3: hash = ((hash << 5) + hash) + *arKey++;
71			/* no break */
72			case 2: hash = ((hash << 5) + hash) + *arKey++;
73			/* no break */
74			case 1: hash = ((hash << 5) + hash) + *arKey++; break;
75		}
76
77		strKey = emalloc(24);
78		snprintf(strKey, 24, "%lu", hash);
79
80		RETURN_MM_STRING(strKey, 0);
81#else
82		RETURN_MM_NULL();
83#endif
84
85		}
86
87		}%
88	}
89}
90