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