1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2006-2017 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@php.net> |
16 | Ulf Wendel <uw@php.net> |
17 | Georg Richter <georg@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #include "php.h"
22 #include "mysqlnd.h"
23 #include "mysqlnd_priv.h"
24 #include "mysqlnd_result.h"
25 #include "mysqlnd_wireprotocol.h"
26 #include "mysqlnd_debug.h"
27 #include "ext/standard/basic_functions.h"
28
29
30 /* {{{ php_mysqlnd_free_field_metadata */
31 static void
php_mysqlnd_free_field_metadata(MYSQLND_FIELD * meta,zend_bool persistent)32 php_mysqlnd_free_field_metadata(MYSQLND_FIELD *meta, zend_bool persistent)
33 {
34 if (meta) {
35 if (meta->root) {
36 mnd_pefree(meta->root, persistent);
37 meta->root = NULL;
38 }
39 if (meta->def) {
40 mnd_pefree(meta->def, persistent);
41 meta->def = NULL;
42 }
43 if (meta->sname) {
44 zend_string_release(meta->sname);
45 }
46 }
47 }
48 /* }}} */
49
50 /* {{{ mysqlnd_res_meta::read_metadata */
51 static enum_func_status
MYSQLND_METHOD(mysqlnd_res_meta,read_metadata)52 MYSQLND_METHOD(mysqlnd_res_meta, read_metadata)(MYSQLND_RES_METADATA * const meta, MYSQLND_CONN_DATA * conn)
53 {
54 unsigned int i = 0;
55 MYSQLND_PACKET_RES_FIELD * field_packet;
56
57 DBG_ENTER("mysqlnd_res_meta::read_metadata");
58
59 field_packet = conn->protocol->m.get_result_field_packet(conn->protocol, FALSE);
60 if (!field_packet) {
61 SET_OOM_ERROR(*conn->error_info);
62 DBG_RETURN(FAIL);
63 }
64 field_packet->persistent_alloc = meta->persistent;
65 for (;i < meta->field_count; i++) {
66 zend_ulong idx;
67
68 if (meta->fields[i].root) {
69 /* We re-read metadata for PS */
70 mnd_pefree(meta->fields[i].root, meta->persistent);
71 meta->fields[i].root = NULL;
72 }
73
74 field_packet->metadata = &(meta->fields[i]);
75 if (FAIL == PACKET_READ(field_packet, conn)) {
76 PACKET_FREE(field_packet);
77 DBG_RETURN(FAIL);
78 }
79 if (field_packet->error_info.error_no) {
80 COPY_CLIENT_ERROR(*conn->error_info, field_packet->error_info);
81 /* Return back from CONN_QUERY_SENT */
82 PACKET_FREE(field_packet);
83 DBG_RETURN(FAIL);
84 }
85
86 if (field_packet->stupid_list_fields_eof == TRUE) {
87 meta->field_count = i;
88 break;
89 }
90
91 if (mysqlnd_ps_fetch_functions[meta->fields[i].type].func == NULL) {
92 DBG_ERR_FMT("Unknown type %u sent by the server. Please send a report to the developers",
93 meta->fields[i].type);
94 php_error_docref(NULL, E_WARNING,
95 "Unknown type %u sent by the server. "
96 "Please send a report to the developers",
97 meta->fields[i].type);
98 PACKET_FREE(field_packet);
99 DBG_RETURN(FAIL);
100 }
101 if (meta->fields[i].type == MYSQL_TYPE_BIT) {
102 size_t field_len;
103 DBG_INF("BIT");
104 ++meta->bit_fields_count;
105 /* .length is in bits */
106 field_len = meta->fields[i].length / 8;
107 /*
108 If there is rest, add one byte :
109 8 bits = 1 byte but 9 bits = 2 bytes
110 */
111 if (meta->fields[i].length % 8) {
112 ++field_len;
113 }
114 switch (field_len) {
115 case 8:
116 case 7:
117 case 6:
118 case 5:
119 meta->bit_fields_total_len += 20;/* 21 digis, no sign*/
120 break;
121 case 4:
122 meta->bit_fields_total_len += 10;/* 2 000 000 000*/
123 break;
124 case 3:
125 meta->bit_fields_total_len += 8;/* 12 000 000*/
126 break;
127 case 2:
128 meta->bit_fields_total_len += 5;/* 32 500 */
129 break;
130 case 1:
131 meta->bit_fields_total_len += 3;/* 120 */
132 break;
133 }
134 }
135
136 /* For BC we have to check whether the key is numeric and use it like this */
137 if ((meta->zend_hash_keys[i].is_numeric = ZEND_HANDLE_NUMERIC(field_packet->metadata->sname, idx))) {
138 meta->zend_hash_keys[i].key = idx;
139 }
140 }
141 PACKET_FREE(field_packet);
142
143 DBG_RETURN(PASS);
144 }
145 /* }}} */
146
147
148 /* {{{ mysqlnd_res_meta::free */
149 static void
MYSQLND_METHOD(mysqlnd_res_meta,free)150 MYSQLND_METHOD(mysqlnd_res_meta, free)(MYSQLND_RES_METADATA * meta)
151 {
152 int i;
153 MYSQLND_FIELD *fields;
154 DBG_ENTER("mysqlnd_res_meta::free");
155 DBG_INF_FMT("persistent=%u", meta->persistent);
156
157 if ((fields = meta->fields)) {
158 DBG_INF("Freeing fields metadata");
159 i = meta->field_count;
160 while (i--) {
161 php_mysqlnd_free_field_metadata(fields++, meta->persistent);
162 }
163 mnd_pefree(meta->fields, meta->persistent);
164 meta->fields = NULL;
165 }
166
167 if (meta->zend_hash_keys) {
168 DBG_INF("Freeing zend_hash_keys");
169 mnd_pefree(meta->zend_hash_keys, meta->persistent);
170 meta->zend_hash_keys = NULL;
171 }
172 DBG_INF("Freeing metadata structure");
173 mnd_pefree(meta, meta->persistent);
174
175 DBG_VOID_RETURN;
176 }
177 /* }}} */
178
179
180 /* {{{ mysqlnd_res::clone_metadata */
181 static MYSQLND_RES_METADATA *
MYSQLND_METHOD(mysqlnd_res_meta,clone_metadata)182 MYSQLND_METHOD(mysqlnd_res_meta, clone_metadata)(const MYSQLND_RES_METADATA * const meta, zend_bool persistent)
183 {
184 unsigned int i;
185 /* +1 is to have empty marker at the end */
186 MYSQLND_RES_METADATA * new_meta = NULL;
187 MYSQLND_FIELD * new_fields;
188 MYSQLND_FIELD * orig_fields = meta->fields;
189 size_t len = meta->field_count * sizeof(struct mysqlnd_field_hash_key);
190
191 DBG_ENTER("mysqlnd_res_meta::clone_metadata");
192 DBG_INF_FMT("persistent=%u", persistent);
193
194 new_meta = mnd_pecalloc(1, sizeof(MYSQLND_RES_METADATA), persistent);
195 if (!new_meta) {
196 goto oom;
197 }
198 new_meta->persistent = persistent;
199 new_meta->m = meta->m;
200
201 new_fields = mnd_pecalloc(meta->field_count + 1, sizeof(MYSQLND_FIELD), persistent);
202 if (!new_fields) {
203 goto oom;
204 }
205
206 new_meta->zend_hash_keys = mnd_pemalloc(len, persistent);
207 if (!new_meta->zend_hash_keys) {
208 goto oom;
209 }
210 memcpy(new_meta->zend_hash_keys, meta->zend_hash_keys, len);
211
212 /*
213 This will copy also the strings and the root, which we will have
214 to adjust in the loop
215 */
216 memcpy(new_fields, orig_fields, (meta->field_count) * sizeof(MYSQLND_FIELD));
217 for (i = 0; i < meta->field_count; i++) {
218 /* First copy the root, then field by field adjust the pointers */
219 new_fields[i].root = mnd_pemalloc(orig_fields[i].root_len, persistent);
220
221 if (!new_fields[i].root) {
222 goto oom;
223 }
224
225 memcpy(new_fields[i].root, orig_fields[i].root, new_fields[i].root_len);
226
227 if (orig_fields[i].sname) {
228 new_fields[i].sname = zend_string_copy(orig_fields[i].sname);
229 new_fields[i].name = ZSTR_VAL(new_fields[i].sname);
230 new_fields[i].name_length = ZSTR_LEN(new_fields[i].sname);
231 }
232
233 if (orig_fields[i].org_name && orig_fields[i].org_name != mysqlnd_empty_string) {
234 new_fields[i].org_name = new_fields[i].root +
235 (orig_fields[i].org_name - orig_fields[i].root);
236 }
237 if (orig_fields[i].table && orig_fields[i].table != mysqlnd_empty_string) {
238 new_fields[i].table = new_fields[i].root +
239 (orig_fields[i].table - orig_fields[i].root);
240 }
241 if (orig_fields[i].org_table && orig_fields[i].org_table != mysqlnd_empty_string) {
242 new_fields[i].org_table = new_fields[i].root +
243 (orig_fields[i].org_table - orig_fields[i].root);
244 }
245 if (orig_fields[i].db && orig_fields[i].db != mysqlnd_empty_string) {
246 new_fields[i].db = new_fields[i].root + (orig_fields[i].db - orig_fields[i].root);
247 }
248 if (orig_fields[i].catalog && orig_fields[i].catalog != mysqlnd_empty_string) {
249 new_fields[i].catalog = new_fields[i].root + (orig_fields[i].catalog - orig_fields[i].root);
250 }
251 /* def is not on the root, if allocated at all */
252 if (orig_fields[i].def) {
253 new_fields[i].def = mnd_pemalloc(orig_fields[i].def_length + 1, persistent);
254 if (!new_fields[i].def) {
255 goto oom;
256 }
257 /* copy the trailing \0 too */
258 memcpy(new_fields[i].def, orig_fields[i].def, orig_fields[i].def_length + 1);
259 }
260 }
261 new_meta->current_field = 0;
262 new_meta->field_count = meta->field_count;
263
264 new_meta->fields = new_fields;
265
266 DBG_RETURN(new_meta);
267 oom:
268 if (new_meta) {
269 new_meta->m->free_metadata(new_meta);
270 new_meta = NULL;
271 }
272 DBG_RETURN(NULL);
273 }
274 /* }}} */
275
276
277 /* {{{ mysqlnd_res_meta::fetch_field */
278 static const MYSQLND_FIELD *
MYSQLND_METHOD(mysqlnd_res_meta,fetch_field)279 MYSQLND_METHOD(mysqlnd_res_meta, fetch_field)(MYSQLND_RES_METADATA * const meta)
280 {
281 DBG_ENTER("mysqlnd_res_meta::fetch_field");
282 if (meta->current_field >= meta->field_count) {
283 DBG_INF("no more fields");
284 DBG_RETURN(NULL);
285 }
286 DBG_INF_FMT("name=%s max_length=%u",
287 meta->fields[meta->current_field].name? meta->fields[meta->current_field].name:"",
288 meta->fields[meta->current_field].max_length);
289 DBG_RETURN(&meta->fields[meta->current_field++]);
290 }
291 /* }}} */
292
293
294 /* {{{ mysqlnd_res_meta::fetch_field_direct */
295 static const MYSQLND_FIELD *
MYSQLND_METHOD(mysqlnd_res_meta,fetch_field_direct)296 MYSQLND_METHOD(mysqlnd_res_meta, fetch_field_direct)(const MYSQLND_RES_METADATA * const meta, const MYSQLND_FIELD_OFFSET fieldnr)
297 {
298 DBG_ENTER("mysqlnd_res_meta::fetch_field_direct");
299 DBG_INF_FMT("fieldnr=%u", fieldnr);
300 DBG_INF_FMT("name=%s max_length=%u",
301 meta->fields[meta->current_field].name? meta->fields[meta->current_field].name:"",
302 meta->fields[meta->current_field].max_length);
303 DBG_RETURN(&meta->fields[fieldnr]);
304 }
305 /* }}} */
306
307
308 /* {{{ mysqlnd_res_meta::fetch_fields */
309 static const MYSQLND_FIELD *
MYSQLND_METHOD(mysqlnd_res_meta,fetch_fields)310 MYSQLND_METHOD(mysqlnd_res_meta, fetch_fields)(MYSQLND_RES_METADATA * const meta)
311 {
312 DBG_ENTER("mysqlnd_res_meta::fetch_fields");
313 DBG_RETURN(meta->fields);
314 }
315 /* }}} */
316
317
318 /* {{{ mysqlnd_res_meta::field_tell */
319 static MYSQLND_FIELD_OFFSET
MYSQLND_METHOD(mysqlnd_res_meta,field_tell)320 MYSQLND_METHOD(mysqlnd_res_meta, field_tell)(const MYSQLND_RES_METADATA * const meta)
321 {
322 return meta->current_field;
323 }
324 /* }}} */
325
326
327 /* {{{ mysqlnd_res_meta::field_seek */
328 static MYSQLND_FIELD_OFFSET
MYSQLND_METHOD(mysqlnd_res_meta,field_seek)329 MYSQLND_METHOD(mysqlnd_res_meta, field_seek)(MYSQLND_RES_METADATA * const meta, const MYSQLND_FIELD_OFFSET field_offset)
330 {
331 MYSQLND_FIELD_OFFSET return_value = 0;
332 DBG_ENTER("mysqlnd_res_meta::fetch_fields");
333 return_value = meta->current_field;
334 meta->current_field = field_offset;
335 DBG_RETURN(return_value);
336 }
337 /* }}} */
338
339 static
340 MYSQLND_CLASS_METHODS_START(mysqlnd_res_meta)
341 MYSQLND_METHOD(mysqlnd_res_meta, fetch_field),
342 MYSQLND_METHOD(mysqlnd_res_meta, fetch_field_direct),
343 MYSQLND_METHOD(mysqlnd_res_meta, fetch_fields),
344 MYSQLND_METHOD(mysqlnd_res_meta, field_tell),
345 MYSQLND_METHOD(mysqlnd_res_meta, field_seek),
346 MYSQLND_METHOD(mysqlnd_res_meta, read_metadata),
347 MYSQLND_METHOD(mysqlnd_res_meta, clone_metadata),
348 MYSQLND_METHOD(mysqlnd_res_meta, free),
349 MYSQLND_CLASS_METHODS_END;
350
351
352 /* {{{ mysqlnd_result_meta_init */
353 PHPAPI MYSQLND_RES_METADATA *
mysqlnd_result_meta_init(unsigned int field_count,zend_bool persistent)354 mysqlnd_result_meta_init(unsigned int field_count, zend_bool persistent)
355 {
356 size_t alloc_size = sizeof(MYSQLND_RES_METADATA) + mysqlnd_plugin_count() * sizeof(void *);
357 MYSQLND_RES_METADATA *ret = mnd_pecalloc(1, alloc_size, persistent);
358 DBG_ENTER("mysqlnd_result_meta_init");
359 DBG_INF_FMT("persistent=%u", persistent);
360
361 do {
362 if (!ret) {
363 break;
364 }
365 ret->m = & mysqlnd_mysqlnd_res_meta_methods;
366
367 ret->persistent = persistent;
368 ret->field_count = field_count;
369 /* +1 is to have empty marker at the end */
370 ret->fields = mnd_pecalloc(field_count + 1, sizeof(MYSQLND_FIELD), ret->persistent);
371 ret->zend_hash_keys = mnd_pecalloc(field_count, sizeof(struct mysqlnd_field_hash_key), ret->persistent);
372 if (!ret->fields || !ret->zend_hash_keys) {
373 break;
374 }
375 DBG_INF_FMT("meta=%p", ret);
376 DBG_RETURN(ret);
377 } while (0);
378 if (ret) {
379 ret->m->free_metadata(ret);
380 }
381 DBG_RETURN(NULL);
382 }
383 /* }}} */
384
385
386 /* {{{ mysqlnd_res_meta_get_methods */
387 PHPAPI struct st_mysqlnd_res_meta_methods *
mysqlnd_result_metadata_get_methods()388 mysqlnd_result_metadata_get_methods()
389 {
390 return &mysqlnd_mysqlnd_res_meta_methods;
391 }
392 /* }}} */
393
394
395 /* {{{ _mysqlnd_plugin_get_plugin_result_metadata_data */
396 PHPAPI void **
_mysqlnd_plugin_get_plugin_result_metadata_data(const MYSQLND_RES_METADATA * meta,unsigned int plugin_id)397 _mysqlnd_plugin_get_plugin_result_metadata_data(const MYSQLND_RES_METADATA * meta, unsigned int plugin_id)
398 {
399 DBG_ENTER("_mysqlnd_plugin_get_plugin_result_metadata_data");
400 DBG_INF_FMT("plugin_id=%u", plugin_id);
401 if (!meta || plugin_id >= mysqlnd_plugin_count()) {
402 return NULL;
403 }
404 DBG_RETURN((void *)((char *)meta + sizeof(MYSQLND_RES_METADATA) + plugin_id * sizeof(void *)));
405 }
406 /* }}} */
407
408 /*
409 * Local variables:
410 * tab-width: 4
411 * c-basic-offset: 4
412 * End:
413 * vim600: noet sw=4 ts=4 fdm=marker
414 * vim<600: noet sw=4 ts=4
415 */
416