xref: /PHP-7.3/ext/mysqli/php_mysqli_structs.h (revision c9abb0c0)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 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   | Authors: Georg Richter <georg@php.net>                               |
16   |          Andrey Hristov <andrey@php.net>                             |
17   |          Ulf Wendel <uw@php.net>                                     |
18   +----------------------------------------------------------------------+
19 */
20 
21 #ifndef PHP_MYSQLI_STRUCTS_H
22 #define PHP_MYSQLI_STRUCTS_H
23 
24 /* A little hack to prevent build break, when mysql is used together with
25  * c-client, which also defines LIST.
26  */
27 #ifdef LIST
28 #undef LIST
29 #endif
30 
31 #ifndef TRUE
32 #define TRUE 1
33 #endif
34 
35 #ifndef FALSE
36 #define FALSE 0
37 #endif
38 
39 #ifdef MYSQLI_USE_MYSQLND
40 #include "ext/mysqlnd/mysqlnd.h"
41 #include "mysqli_mysqlnd.h"
42 #else
43 
44 /*
45   The libmysql headers (a PITA) also define it and there will be an warning.
46   Undef it and later we might need to define it again.
47 */
48 #ifdef HAVE_MBRLEN
49 #undef HAVE_MBRLEN
50 #define WE_HAD_MBRLEN
51 #endif
52 #ifdef HAVE_MBSTATE_T
53 #undef HAVE_MBSTATE_T
54 #define WE_HAD_MBSTATE_T
55 #endif
56 
57 #if defined(ulong) && !defined(HAVE_ULONG)
58 #define HAVE_ULONG
59 #endif
60 
61 #if !defined(HAVE_MBRLEN) && defined(WE_HAD_MBRLEN)
62 #define HAVE_MBRLEN 1
63 #endif
64 
65 #if !defined(HAVE_MBSTATE_T) && defined(WE_HAD_MBSTATE_T)
66 #define HAVE_MBSTATE_T 1
67 #endif
68 
69 #include <mysql.h>
70 #if MYSQL_VERSION_ID >= 80000 &&  MYSQL_VERSION_ID < 100000
71 typedef _Bool		my_bool;
72 #endif
73 #include <errmsg.h>
74 #include <mysqld_error.h>
75 #include "mysqli_libmysql.h"
76 #endif /* MYSQLI_USE_MYSQLND */
77 
78 
79 #define MYSQLI_VERSION_ID		101009
80 
81 enum mysqli_status {
82 	MYSQLI_STATUS_UNKNOWN=0,
83 	MYSQLI_STATUS_CLEARED,
84 	MYSQLI_STATUS_INITIALIZED,
85 	MYSQLI_STATUS_VALID
86 };
87 
88 typedef struct {
89 	char		*val;
90 	zend_ulong		buflen;
91 	zend_ulong		output_len;
92 	zend_ulong		type;
93 } VAR_BUFFER;
94 
95 typedef struct {
96 	unsigned int	var_cnt;
97 	VAR_BUFFER		*buf;
98 	zval			*vars;
99 	char			*is_null;
100 } BIND_BUFFER;
101 
102 typedef struct {
103 	MYSQL_STMT	*stmt;
104 	BIND_BUFFER	param;
105 	BIND_BUFFER	result;
106 	char		*query;
107 #ifndef MYSQLI_USE_MYSQLND
108 	/* used to manage refcount with libmysql (already implement in mysqlnd) */
109 	zval		link_handle;
110 #endif
111 } MY_STMT;
112 
113 typedef struct {
114 	MYSQL			*mysql;
115 	zend_string		*hash_key;
116 	zval			li_read;
117 	php_stream		*li_stream;
118 	unsigned int 	multi_query;
119 	zend_bool		persistent;
120 #if defined(MYSQLI_USE_MYSQLND)
121 	int				async_result_fetch_type;
122 #endif
123 } MY_MYSQL;
124 
125 typedef struct {
126 	void				*ptr;		/* resource: (mysql, result, stmt)   */
127 	void				*info;		/* additional buffer				 */
128 	enum mysqli_status	status;		/* object status */
129 } MYSQLI_RESOURCE;
130 
131 typedef struct _mysqli_object {
132 	void 				*ptr;
133 	HashTable 			*prop_handler;
134 	zend_object 		zo;
135 } mysqli_object; /* extends zend_object */
136 
php_mysqli_fetch_object(zend_object * obj)137 static inline mysqli_object *php_mysqli_fetch_object(zend_object *obj) {
138 	return (mysqli_object *)((char*)(obj) - XtOffsetOf(mysqli_object, zo));
139 }
140 
141 #define Z_MYSQLI_P(zv) php_mysqli_fetch_object(Z_OBJ_P((zv)))
142 
143 typedef struct st_mysqli_warning MYSQLI_WARNING;
144 
145 struct st_mysqli_warning {
146 	zval	reason;
147 	zval	sqlstate;
148 	int		errorno;
149    	MYSQLI_WARNING	*next;
150 };
151 
152 typedef struct _mysqli_property_entry {
153 	const char *pname;
154 	size_t pname_length;
155 	zval *(*r_func)(mysqli_object *obj, zval *retval);
156 	int (*w_func)(mysqli_object *obj, zval *value);
157 } mysqli_property_entry;
158 
159 typedef struct {
160 	zend_ptr_stack free_links;
161 } mysqli_plist_entry;
162 
163 #ifdef PHP_WIN32
164 #define PHP_MYSQLI_API __declspec(dllexport)
165 #define MYSQLI_LLU_SPEC "%I64u"
166 #define MYSQLI_LL_SPEC "%I64d"
167 #ifndef L64
168 #define L64(x) x##i64
169 #endif
170 typedef __int64 my_longlong;
171 #else
172 # if defined(__GNUC__) && __GNUC__ >= 4
173 #  define PHP_MYSQLI_API __attribute__ ((visibility("default")))
174 # else
175 #  define PHP_MYSQLI_API
176 # endif
177 /* we need this for PRIu64 and PRId64 */
178 #include <inttypes.h>
179 #define MYSQLI_LLU_SPEC "%" PRIu64
180 #define MYSQLI_LL_SPEC "%" PRId64
181 #ifndef L64
182 #define L64(x) x##LL
183 #endif
184 typedef int64_t my_longlong;
185 #endif
186 
187 #ifdef ZTS
188 #include "TSRM.h"
189 #endif
190 
191 extern zend_class_entry *mysqli_link_class_entry;
192 extern zend_class_entry *mysqli_stmt_class_entry;
193 extern zend_class_entry *mysqli_result_class_entry;
194 extern zend_class_entry *mysqli_driver_class_entry;
195 extern zend_class_entry *mysqli_warning_class_entry;
196 extern zend_class_entry *mysqli_exception_class_entry;
197 extern int php_le_pmysqli(void);
198 extern void php_mysqli_dtor_p_elements(void *data);
199 
200 extern void php_mysqli_close(MY_MYSQL * mysql, int close_type, int resource_status);
201 
202 extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
203 extern zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
204 
205 extern void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * result, zend_long fetchtype);
206 
207 #define MYSQLI_DISABLE_MQ if (mysql->multi_query) { \
208 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
209 	mysql->multi_query = 0; \
210 }
211 
212 #define MYSQLI_ENABLE_MQ if (!mysql->multi_query) { \
213 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); \
214 	mysql->multi_query = 1; \
215 }
216 
217 #define REGISTER_MYSQLI_CLASS_ENTRY(name, mysqli_entry, class_functions) { \
218 	zend_class_entry ce; \
219 	INIT_CLASS_ENTRY(ce, name,class_functions); \
220 	ce.create_object = mysqli_objects_new; \
221 	mysqli_entry = zend_register_internal_class(&ce); \
222 } \
223 
224 #define MYSQLI_REGISTER_RESOURCE_EX(__ptr, __zval)  \
225 	(Z_MYSQLI_P(__zval))->ptr = __ptr;
226 
227 #define MYSQLI_RETURN_RESOURCE(__ptr, __ce) \
228 	RETVAL_OBJ(mysqli_objects_new(__ce)); \
229 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, return_value)
230 
231 #define MYSQLI_REGISTER_RESOURCE(__ptr, __ce) \
232 {\
233 	zval *object = getThis(); \
234 	if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry)) { \
235 		object = return_value; \
236 		ZVAL_OBJ(object, mysqli_objects_new(__ce)); \
237 	} \
238 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, object)\
239 }
240 
241 #define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name, __check) \
242 { \
243 	MYSQLI_RESOURCE *my_res; \
244 	mysqli_object *intern = Z_MYSQLI_P(__id); \
245 	if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
246   		php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
247 		RETURN_FALSE;\
248   	}\
249 	__ptr = (__type)my_res->ptr; \
250 	if (__check && my_res->status < __check) { \
251 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
252 		RETURN_FALSE;\
253 	}\
254 }
255 
256 #define MYSQLI_FETCH_RESOURCE_BY_OBJ(__ptr, __type, __obj, __name, __check) \
257 { \
258 	MYSQLI_RESOURCE *my_res; \
259 	if (!(my_res = (MYSQLI_RESOURCE *)(__obj->ptr))) {\
260   		php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
261   		return;\
262   	}\
263 	__ptr = (__type)my_res->ptr; \
264 	if (__check && my_res->status < __check) { \
265 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
266 		return;\
267 	}\
268 }
269 
270 #define MYSQLI_FETCH_RESOURCE_CONN(__ptr, __id, __check) \
271 { \
272 	MYSQLI_FETCH_RESOURCE((__ptr), MY_MYSQL *, (__id), "mysqli_link", (__check)); \
273 	if (!(__ptr)->mysql) { \
274 		mysqli_object *intern = Z_MYSQLI_P(__id); \
275 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
276 		RETURN_NULL(); \
277 	} \
278 }
279 
280 #define MYSQLI_FETCH_RESOURCE_STMT(__ptr, __id, __check) \
281 { \
282 	MYSQLI_FETCH_RESOURCE((__ptr), MY_STMT *, (__id), "mysqli_stmt", (__check)); \
283 	if (!(__ptr)->stmt) { \
284 		mysqli_object *intern = Z_MYSQLI_P(__id); \
285 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
286 		RETURN_NULL();\
287 	} \
288 }
289 
290 #define MYSQLI_SET_STATUS(__id, __value) \
291 { \
292 	mysqli_object *intern = Z_MYSQLI_P(__id); \
293 	((MYSQLI_RESOURCE *)intern->ptr)->status = __value; \
294 } \
295 
296 #define MYSQLI_CLEAR_RESOURCE(__id) \
297 { \
298 	mysqli_object *intern = Z_MYSQLI_P(__id); \
299 	efree(intern->ptr); \
300 	intern->ptr = NULL; \
301 }
302 
303 
304 ZEND_BEGIN_MODULE_GLOBALS(mysqli)
305 	zend_long			default_link;
306 	zend_long			num_links;
307 	zend_long			max_links;
308 	zend_long 			num_active_persistent;
309 	zend_long 			num_inactive_persistent;
310 	zend_long			max_persistent;
311 	zend_long			allow_persistent;
312 	zend_ulong	default_port;
313 	char			*default_host;
314 	char			*default_user;
315 	char			*default_socket;
316 	char			*default_pw;
317 	zend_long			reconnect;
318 	zend_long			allow_local_infile;
319 	zend_long			strict;
320 	zend_long			error_no;
321 	char			*error_msg;
322 	zend_long			report_mode;
323 	HashTable		*report_ht;
324 	zend_ulong	multi_query;
325 	zend_ulong	embedded;
326 	zend_bool 		rollback_on_cached_plink;
327 ZEND_END_MODULE_GLOBALS(mysqli)
328 
329 #define MyG(v) ZEND_MODULE_GLOBALS_ACCESSOR(mysqli, v)
330 
331 #if defined(ZTS) && defined(COMPILE_DL_MYSQLI)
332 ZEND_TSRMLS_CACHE_EXTERN()
333 #endif
334 
335 #define my_estrdup(x) (x) ? estrdup(x) : NULL
336 #define my_efree(x) if (x) efree(x)
337 
338 ZEND_EXTERN_MODULE_GLOBALS(mysqli)
339 
340 #endif	/* PHP_MYSQLI_STRUCTS.H */
341 
342 
343 /*
344  * Local variables:
345  * tab-width: 4
346  * c-basic-offset: 4
347  * indent-tabs-mode: t
348  * End:
349  * vim600: noet sw=4 ts=4 fdm=marker
350  * vim<600: noet sw=4 ts=4
351  */
352