1 /*
2   +----------------------------------------------------------------------+
3   | Compatibility between PHP versions                                   |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2015 The PHP Group                                     |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt.                                 |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Francois Laupretre <francois@tekwire.net>                    |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifndef __PECL_COMPAT_MISC_H
20 #define __PECL_COMPAT_MISC_H 1
21 
22 #ifdef PHP_7
23 /*============================================================================*/
24 
25 typedef zend_string * OPENED_PATH_PTR; /* Type of stream opened_path argument */
26 typedef size_t        COMPAT_ARG_SIZE_T; /* Size of string arguments */
27 typedef zend_long     COMPAT_ARG_LONG_T; /* Type of long (integer) arguments */
28 
29 #define compat_zval_ptr_dtor(zp)	zval_ptr_dtor(zp)
30 
31 #include <Zend/zend_smart_str.h>
32 
33 #else
34 /*== PHP 5 ===================================================================*/
35 
36 typedef char *    OPENED_PATH_PTR;
37 typedef off_t     zend_off_t;
38 typedef int       COMPAT_ARG_SIZE_T;
39 typedef long      COMPAT_ARG_LONG_T;
40 typedef long      zend_long;
41 
42 #define compat_zval_ptr_dtor(zp)	zval_dtor(zp)
43 
44 #endif
45 /*============================================================================*/
46 
47 #ifndef MIN
48 #	define MIN(a,b) (((a) < (b)) ? (a) : (b))
49 #endif
50 
51 #ifndef MAX
52 #	define MAX(a,b) (((a) > (b)) ? (a) : (b))
53 #endif
54 
55 /*---------------------------------------------------------------*/
56 /* (Taken from pcre/pcrelib/internal.h) */
57 /* To cope with SunOS4 and other systems that lack memmove() but have bcopy(),
58 define a macro for memmove() if HAVE_MEMMOVE is false, provided that HAVE_BCOPY
59 is set. Otherwise, include an emulating function for those systems that have
60 neither (there are some non-Unix environments where this is the case). This
61 assumes that all calls to memmove are moving strings upwards in store,
62 which is the case in this extension. */
63 
64 #if ! HAVE_MEMMOVE
65 #	ifdef memmove
66 #		undef  memmove					/* some systems may have a macro */
67 #	endif
68 #	if HAVE_BCOPY
69 #		define memmove(a, b, c) bcopy(b, a, c)
70 #	else
my_memmove(unsigned char * dest,const unsigned char * src,size_t n)71 		static void *my_memmove(unsigned char *dest, const unsigned char *src,
72 								size_t n)
73 		{
74 			int i;
75 
76 			dest += n;
77 			src += n;
78 			for (i = 0; i < n; ++i)
79 				*(--dest) = *(--src);
80 		}
81 #		define memmove(a, b, c) my_memmove(a, b, c)
82 #	endif	/* not HAVE_BCOPY */
83 #endif		/* not HAVE_MEMMOVE */
84 
85 #ifdef _AIX
86 #	undef PHP_SHLIB_SUFFIX
87 #	define PHP_SHLIB_SUFFIX "a"
88 #endif
89 
90 #ifndef ZVAL_IS_ARRAY
91 #define ZVAL_IS_ARRAY(zp)	(Z_TYPE_P((zp))==IS_ARRAY)
92 #endif
93 
94 #ifndef ZVAL_IS_STRING
95 #define ZVAL_IS_STRING(zp)	(Z_TYPE_P((zp))==IS_STRING)
96 #endif
97 
98 #ifndef ZVAL_IS_LONG
99 #define ZVAL_IS_LONG(zp)	(Z_TYPE_P((zp))==IS_LONG)
100 #endif
101 
102 #ifndef ZVAL_IS_BOOL
103 #define ZVAL_IS_BOOL(zp)	(Z_TYPE_P((zp))==IS_BOOL)
104 #endif
105 
106 #ifndef INIT_ZVAL
107 #define INIT_ZVAL(z) memset(&z, 0, sizeof(z))
108 #endif
109 
110 #ifndef ZVAL_UNDEF
111 #define ZVAL_UNDEF(z) INIT_ZVAL(*(z))
112 #endif
113 
114 #ifndef MAKE_STD_ZVAL
115 #define MAKE_STD_ZVAL(zp) { zp = emalloc(sizeof(zval)); INIT_ZVAL(*zp); }
116 #endif
117 
118 #ifndef ALLOC_INIT_ZVAL
119 #define ALLOC_INIT_ZVAL(zp) MAKE_STD_ZVAL(zp)
120 #endif
121 
122 #ifndef ZEND_ASSUME
123 #if defined(ZEND_WIN32) && !defined(__clang__)
124 # define ZEND_ASSUME(c) __assume(c)
125 #else
126 # define ZEND_ASSUME(c)
127 #endif
128 #endif
129 
130 #ifndef ZEND_ASSERT
131 #if ZEND_DEBUG
132 # define ZEND_ASSERT(c) assert(c)
133 #else
134 # define ZEND_ASSERT(c) ZEND_ASSUME(c)
135 #endif
136 #endif
137 
138 #ifndef EMPTY_SWITCH_DEFAULT_CASE
139 /* Only use this macro if you know for sure that all of the switches values
140    are covered by its case statements */
141 #if ZEND_DEBUG
142 # define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSERT(0); break;
143 #else
144 # define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSUME(0); break;
145 #endif
146 #endif
147 
148 #ifndef ZEND_IGNORE_VALUE
149 #if defined(__GNUC__) && __GNUC__ >= 4
150 # define ZEND_IGNORE_VALUE(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
151 #else
152 # define ZEND_IGNORE_VALUE(x) ((void) (x))
153 #endif
154 #endif
155 
156 #if PHP_API_VERSION >= 20100412
157 	typedef size_t PHP_ESCAPE_HTML_ENTITIES_SIZE;
158 #else
159 	typedef int PHP_ESCAPE_HTML_ENTITIES_SIZE;
160 #endif
161 
162 /* Avoid a warning when compiling stream wrapper declarations for
163    openfile/opendir/url_stat */
164 
165 #if ZEND_EXTENSION_API_NO >= PHP_5_6_X_API_NO
166 #	define COMPAT_STREAM_CONST_DECL const
167 #else
168 #	define COMPAT_STREAM_CONST_DECL
169 #endif
170 
171 #ifndef ZEND_MODULE_GLOBALS_ACCESSOR
172 #	ifdef ZTS
173 #		define ZEND_MODULE_GLOBALS_ACCESSOR(module_name, v) \
174 			TSRMG(module_name##_globals_id, zend_##module_name##_globals *, v)
175 #	else
176 #		define ZEND_MODULE_GLOBALS_ACCESSOR(module_name, v) \
177 			(module_name##_globals.v)
178 #	endif
179 #endif
180 
181 #ifndef ZEND_MODULE_GLOBALS_BULK
182 #	ifdef ZTS
183 #		define ZEND_MODULE_GLOBALS_BULK(module_name) \
184 			((zend_##module_name##_globals *) \
185 				(*((void ***) tsrm_ls))[module_name##_globals_id - 1])
186 #	else
187 #		define ZEND_MODULE_GLOBALS_BULK(module_name) \
188 			(&module_name##_globals)
189 #	endif
190 #endif
191 
192 #ifndef ZEND_TSRMLS_CACHE_DEFINE
193 #	define ZEND_TSRMLS_CACHE_DEFINE()
194 #endif
195 
196 #ifndef ZEND_TSRMLS_CACHE_UPDATE
197 #	define ZEND_TSRMLS_CACHE_UPDATE()
198 #endif
199 
200 #ifndef PHP_FE_END
201 # define PHP_FE_END { NULL, NULL, NULL }
202 #endif
203 
204 #ifndef ZEND_MOD_END
205 	/* for php < 5.3.7 */
206 #	define ZEND_MOD_END {NULL, NULL, NULL}
207 #endif
208 
209 /*============================================================================*/
210 /* Duplicate a memory buffer */
211 /* Supports zero size (allocates 1 byte) */
212 
_compat_dup(const void * ptr,size_t size,int persistent)213 static zend_always_inline void *_compat_dup(const void *ptr, size_t size, int persistent)
214 {
215 	char *p;
216 
217 	if (!ptr) return NULL;
218 	if (size) {
219 		p = pemalloc(size, persistent);
220 		memmove(p, ptr, size);
221 	} else {
222 		p = pemalloc(1,persistent);
223 		(*p) = '\0'; /* Ensures deterministic behavior */
224 	}
225 
226 	return p;
227 }
228 
229 /*---------------------------------------------------------------*/
230 /* Duplicate a string and set terminating null.
231    Input string does not have to be null-terminated */
232 
_compat_dup_str(const void * ptr,size_t size,int persistent)233 static zend_always_inline void *_compat_dup_str(const void *ptr, size_t size, int persistent)
234 {
235 	char *p;
236 
237 	if (!ptr) return NULL;
238 
239 	p = pemalloc(size + 1, persistent);
240 	if (size) memmove(p, ptr, size);
241 	p[size] = '\0';
242 	return p;
243 }
244 
245 /*-----------------------------------------------------*/
246 /* Fatal error - display message and abort process */
247 
compat_unsupported(char * msg)248 static zend_always_inline void compat_unsupported(char *msg)
249 {
250 	php_error(E_CORE_ERROR, "This feature is not supported in this environment : %s", msg);
251 	exit(1);
252 }
253 
254 /*============================================================================*/
255 #endif	/* __PECL_COMPAT_MISC_H */
256