xref: /PHP-5.6/ext/mysqlnd/mysqlnd_priv.h (revision 49493a2d)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006-2016 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: Andrey Hristov <andrey@mysql.com>                           |
16   |          Ulf Wendel <uwendel@mysql.com>                              |
17   |          Georg Richter <georg@mysql.com>                             |
18   +----------------------------------------------------------------------+
19 */
20 
21 /* $Id$ */
22 
23 #ifndef MYSQLND_PRIV_H
24 #define MYSQLND_PRIV_H
25 
26 #ifndef Z_ADDREF_P
27 /* PHP 5.2, old GC */
28 #define Z_ADDREF_P(pz)				(++(pz)->refcount)
29 #define Z_DELREF_P(pz)				(--(pz)->refcount)
30 #define Z_REFCOUNT_P(pz)			((pz)->refcount)
31 #define Z_SET_REFCOUNT_P(pz, rc)	((pz)->refcount = rc)
32 #define Z_REFCOUNT_PP(ppz)			Z_REFCOUNT_P(*(ppz))
33 #define Z_DELREF_PP(ppz)			Z_DELREF_P(*(ppz))
34 #endif
35 
36 #ifdef ZTS
37 #include "TSRM.h"
38 #endif
39 
40 #ifndef pestrndup
41 #define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
42 #endif
43 
44 #define mysqlnd_array_init(arg, field_count) \
45 { \
46 	ALLOC_HASHTABLE_REL(Z_ARRVAL_P(arg));\
47 	zend_hash_init(Z_ARRVAL_P(arg), (field_count), NULL, ZVAL_PTR_DTOR, 0); \
48 	Z_TYPE_P(arg) = IS_ARRAY;\
49 }
50 
51 #define MYSQLND_STR_W_LEN(str)  str, (sizeof(str) - 1)
52 
53 #define MYSQLND_DEBUG_DUMP_TIME				1
54 #define MYSQLND_DEBUG_DUMP_TRACE			2
55 #define MYSQLND_DEBUG_DUMP_PID				4
56 #define MYSQLND_DEBUG_DUMP_LINE				8
57 #define MYSQLND_DEBUG_DUMP_FILE				16
58 #define MYSQLND_DEBUG_DUMP_LEVEL			32
59 #define MYSQLND_DEBUG_APPEND				64
60 #define MYSQLND_DEBUG_FLUSH					128
61 #define MYSQLND_DEBUG_TRACE_MEMORY_CALLS	256
62 #define MYSQLND_DEBUG_PROFILE_CALLS			512
63 
64 
65 /* Client Error codes */
66 #define CR_UNKNOWN_ERROR		2000
67 #define CR_CONNECTION_ERROR		2002
68 #define CR_SERVER_GONE_ERROR	2006
69 #define CR_OUT_OF_MEMORY		2008
70 #define CR_SERVER_LOST			2013
71 #define CR_COMMANDS_OUT_OF_SYNC	2014
72 #define CR_CANT_FIND_CHARSET	2019
73 #define CR_MALFORMED_PACKET		2027
74 #define CR_NOT_IMPLEMENTED		2054
75 #define CR_NO_PREPARE_STMT		2030
76 #define CR_PARAMS_NOT_BOUND		2031
77 #define CR_INVALID_PARAMETER_NO	2034
78 #define CR_INVALID_BUFFER_USE	2035
79 
80 #define MYSQLND_EE_FILENOTFOUND	 7890
81 
82 #define UNKNOWN_SQLSTATE		"HY000"
83 
84 #define MAX_CHARSET_LEN			32
85 
86 
87 #define SET_ERROR_AFF_ROWS(s)	(s)->upsert_status->affected_rows = (uint64_t) ~0
88 
89 /* Error handling */
90 #define SET_NEW_MESSAGE(buf, buf_len, message, len, persistent) \
91 	{\
92 		if ((buf)) { \
93 			mnd_pefree((buf), (persistent)); \
94 		} \
95 		if ((message)) { \
96 			(buf) = mnd_pestrndup((message), (len), (persistent)); \
97 		} else { \
98 			(buf) = NULL; \
99 		} \
100 		(buf_len) = (len); \
101 	}
102 
103 #define SET_EMPTY_MESSAGE(buf, buf_len, persistent) \
104 	{\
105 		if ((buf)) { \
106 			mnd_pefree((buf), (persistent)); \
107 			(buf) = NULL; \
108 		} \
109 		(buf_len) = 0; \
110 	}
111 
112 
113 #define SET_EMPTY_ERROR(error_info) \
114 	{ \
115 		(error_info).error_no = 0; \
116 		(error_info).error[0] = '\0'; \
117 		strlcpy((error_info).sqlstate, "00000", sizeof((error_info).sqlstate)); \
118 		if ((error_info).error_list) { \
119 			zend_llist_clean((error_info).error_list); \
120 		} \
121 	}
122 
123 
124 #define SET_CLIENT_ERROR(error_info, a, b, c) \
125 { \
126 	if (0 == (a)) { \
127 		SET_EMPTY_ERROR((error_info)); \
128 	} else { \
129 		(error_info).error_no = (a); \
130 		strlcpy((error_info).sqlstate, (b), sizeof((error_info).sqlstate)); \
131 		strlcpy((error_info).error, (c), sizeof((error_info).error)); \
132 		if ((error_info).error_list) {\
133 			MYSQLND_ERROR_LIST_ELEMENT error_for_the_list = {0}; \
134 																	\
135 			error_for_the_list.error_no = (a); \
136 			strlcpy(error_for_the_list.sqlstate, (b), sizeof(error_for_the_list.sqlstate)); \
137 			error_for_the_list.error = mnd_pestrdup((c), TRUE); \
138 			if (error_for_the_list.error) { \
139 				DBG_INF_FMT("adding error [%s] to the list", error_for_the_list.error); \
140 				zend_llist_add_element((error_info).error_list, &error_for_the_list); \
141 			} \
142 		} \
143 	} \
144 }
145 
146 
147 #define COPY_CLIENT_ERROR(error_info_to, error_info_from) \
148 	{ \
149 		SET_CLIENT_ERROR((error_info_to), (error_info_from).error_no, (error_info_from).sqlstate, (error_info_from).error); \
150 	}
151 
152 
153 #define SET_OOM_ERROR(error_info) SET_CLIENT_ERROR((error_info), CR_OUT_OF_MEMORY, UNKNOWN_SQLSTATE, mysqlnd_out_of_memory)
154 
155 
156 #define SET_STMT_ERROR(stmt, a, b, c)	SET_CLIENT_ERROR(*(stmt)->error_info, a, b, c)
157 
158 #define CONN_GET_STATE(c)		(c)->m->get_state((c) TSRMLS_CC)
159 #define CONN_SET_STATE(c, s)	(c)->m->set_state((c), (s) TSRMLS_CC)
160 
161 /* PS stuff */
162 typedef void (*ps_field_fetch_func)(zval * zv, const MYSQLND_FIELD * const field, unsigned int pack_len, zend_uchar ** row TSRMLS_DC);
163 struct st_mysqlnd_perm_bind {
164 	ps_field_fetch_func func;
165 	/* should be signed int */
166 	int					pack_len;
167 	unsigned int		php_type;
168 	zend_bool			is_possibly_blob;
169 	zend_bool			can_ret_as_str_in_uni;
170 };
171 
172 extern struct st_mysqlnd_perm_bind mysqlnd_ps_fetch_functions[MYSQL_TYPE_LAST + 1];
173 
174 enum_func_status mysqlnd_stmt_fetch_row_buffered(MYSQLND_RES * result, void * param, unsigned int flags, zend_bool * fetched_anything TSRMLS_DC);
175 enum_func_status mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES * result, void * param, unsigned int flags, zend_bool * fetched_anything TSRMLS_DC);
176 
177 
178 PHPAPI extern const char * const mysqlnd_old_passwd;
179 PHPAPI extern const char * const mysqlnd_out_of_sync;
180 PHPAPI extern const char * const mysqlnd_server_gone;
181 PHPAPI extern const char * const mysqlnd_out_of_memory;
182 
183 PHPAPI extern MYSQLND_CLASS_METHOD_TABLE_NAME_FORWARD(mysqlnd_object_factory);
184 PHPAPI extern MYSQLND_CLASS_METHOD_TABLE_NAME_FORWARD(mysqlnd_conn);
185 PHPAPI extern MYSQLND_CLASS_METHOD_TABLE_NAME_FORWARD(mysqlnd_conn_data);
186 PHPAPI extern MYSQLND_CLASS_METHOD_TABLE_NAME_FORWARD(mysqlnd_res);
187 PHPAPI extern MYSQLND_CLASS_METHOD_TABLE_NAME_FORWARD(mysqlnd_result_unbuffered);
188 PHPAPI extern MYSQLND_CLASS_METHOD_TABLE_NAME_FORWARD(mysqlnd_result_buffered);
189 PHPAPI extern MYSQLND_CLASS_METHOD_TABLE_NAME_FORWARD(mysqlnd_protocol);
190 PHPAPI extern MYSQLND_CLASS_METHOD_TABLE_NAME_FORWARD(mysqlnd_net);
191 
192 enum_func_status mysqlnd_handle_local_infile(MYSQLND_CONN_DATA * conn, const char * filename, zend_bool * is_warning TSRMLS_DC);
193 
194 
195 
196 void _mysqlnd_init_ps_subsystem();/* This one is private, mysqlnd_library_init() will call it */
197 void _mysqlnd_init_ps_fetch_subsystem();
198 
199 void ps_fetch_from_1_to_8_bytes(zval * zv, const MYSQLND_FIELD * const field, unsigned int pack_len, zend_uchar ** row, unsigned int byte_count TSRMLS_DC);
200 
201 void mysqlnd_plugin_subsystem_init(TSRMLS_D);
202 void mysqlnd_plugin_subsystem_end(TSRMLS_D);
203 
204 void mysqlnd_register_builtin_authentication_plugins(TSRMLS_D);
205 
206 void mysqlnd_example_plugin_register(TSRMLS_D);
207 
208 struct st_mysqlnd_packet_greet;
209 struct st_mysqlnd_authentication_plugin;
210 
211 enum_func_status
212 mysqlnd_auth_handshake(MYSQLND_CONN_DATA * conn,
213 						const char * const user,
214 						const char * const passwd,
215 						const size_t passwd_len,
216 						const char * const db,
217 						const size_t db_len,
218 						const MYSQLND_OPTIONS * const options,
219 						unsigned long mysql_flags,
220 						unsigned int server_charset_no,
221 						zend_bool use_full_blown_auth_packet,
222 						const char * const auth_protocol,
223 						const zend_uchar * const auth_plugin_data,
224 						const size_t auth_plugin_data_len,
225 						char ** switch_to_auth_protocol,
226 						size_t * switch_to_auth_protocol_len,
227 						zend_uchar ** switch_to_auth_protocol_data,
228 						size_t * switch_to_auth_protocol_data_len
229 						TSRMLS_DC);
230 
231 enum_func_status
232 mysqlnd_auth_change_user(MYSQLND_CONN_DATA * const conn,
233 								const char * const user,
234 								const size_t user_len,
235 								const char * const passwd,
236 								const size_t passwd_len,
237 								const char * const db,
238 								const size_t db_len,
239 								const zend_bool silent,
240 								zend_bool use_full_blown_auth_packet,
241 								const char * const auth_protocol,
242 								zend_uchar * auth_plugin_data,
243 								size_t auth_plugin_data_len,
244 								char ** switch_to_auth_protocol,
245 								size_t * switch_to_auth_protocol_len,
246 								zend_uchar ** switch_to_auth_protocol_data,
247 								size_t * switch_to_auth_protocol_data_len
248 								TSRMLS_DC);
249 
250 #endif	/* MYSQLND_PRIV_H */
251 
252 
253 /*
254  * Local variables:
255  * tab-width: 4
256  * c-basic-offset: 4
257  * End:
258  * vim600: noet sw=4 ts=4 fdm=marker
259  * vim<600: noet sw=4 ts=4
260  */
261