mysqlnd_ps_codec.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Andrey Hristov <andrey@php.net> |
  14. | Ulf Wendel <uw@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include <math.h>
  18. #include "php.h"
  19. #include "mysqlnd.h"
  20. #include "mysqlnd_wireprotocol.h"
  21. #include "mysqlnd_connection.h"
  22. #include "mysqlnd_ps.h"
  23. #include "mysqlnd_priv.h"
  24. #include "mysqlnd_debug.h"
  25. #include "mysql_float_to_double.h"
  26. enum mysqlnd_timestamp_type
  27. {
  28. MYSQLND_TIMESTAMP_NONE= -2,
  29. MYSQLND_TIMESTAMP_ERROR= -1,
  30. MYSQLND_TIMESTAMP_DATE= 0,
  31. MYSQLND_TIMESTAMP_DATETIME= 1,
  32. MYSQLND_TIMESTAMP_TIME= 2
  33. };
  34. struct st_mysqlnd_time
  35. {
  36. unsigned int year, month, day, hour, minute, second;
  37. zend_ulong second_part;
  38. bool neg;
  39. enum mysqlnd_timestamp_type time_type;
  40. };
  41. struct st_mysqlnd_perm_bind mysqlnd_ps_fetch_functions[MYSQL_TYPE_LAST + 1];
  42. #define MYSQLND_PS_SKIP_RESULT_W_LEN -1
  43. #define MYSQLND_PS_SKIP_RESULT_STR -2
  44. /* {{{ ps_fetch_from_1_to_8_bytes */
  45. void
  46. ps_fetch_from_1_to_8_bytes(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len,
  47. const zend_uchar ** row, unsigned int byte_count)
  48. {
  49. bool is_bit = field->type == MYSQL_TYPE_BIT;
  50. DBG_ENTER("ps_fetch_from_1_to_8_bytes");
  51. DBG_INF_FMT("zv=%p byte_count=%u", zv, byte_count);
  52. if (field->flags & UNSIGNED_FLAG) {
  53. uint64_t uval = 0;
  54. switch (byte_count) {
  55. case 8:uval = is_bit? (uint64_t) bit_uint8korr(*row):(uint64_t) uint8korr(*row);break;
  56. case 7:uval = bit_uint7korr(*row);break;
  57. case 6:uval = bit_uint6korr(*row);break;
  58. case 5:uval = bit_uint5korr(*row);break;
  59. case 4:uval = is_bit? (uint64_t) bit_uint4korr(*row):(uint64_t) uint4korr(*row);break;
  60. case 3:uval = is_bit? (uint64_t) bit_uint3korr(*row):(uint64_t) uint3korr(*row);break;
  61. case 2:uval = is_bit? (uint64_t) bit_uint2korr(*row):(uint64_t) uint2korr(*row);break;
  62. case 1:uval = (uint64_t) uint1korr(*row);break;
  63. }
  64. if (field->flags & ZEROFILL_FLAG) {
  65. DBG_INF("stringify due to zerofill");
  66. ZVAL_STR(zv, zend_strpprintf(0, "%0*" PRIu64, (int) field->length, uval));
  67. } else
  68. #if SIZEOF_ZEND_LONG==4
  69. if (uval > INT_MAX) {
  70. DBG_INF("stringify");
  71. ZVAL_STR(zv, zend_u64_to_str(uval));
  72. } else
  73. #endif /* #if SIZEOF_LONG==4 */
  74. {
  75. if (byte_count < 8 || uval <= L64(9223372036854775807)) {
  76. ZVAL_LONG(zv, (zend_long) uval); /* the cast is safe, we are in the range */
  77. } else {
  78. DBG_INF("stringify");
  79. ZVAL_STR(zv, zend_u64_to_str(uval));
  80. }
  81. }
  82. } else {
  83. /* SIGNED */
  84. int64_t lval = 0;
  85. switch (byte_count) {
  86. case 8:lval = (int64_t) sint8korr(*row);break;
  87. /*
  88. 7, 6 and 5 are not possible.
  89. BIT is only unsigned, thus only uint5|6|7 macros exist
  90. */
  91. case 4:lval = (int64_t) sint4korr(*row);break;
  92. case 3:lval = (int64_t) sint3korr(*row);break;
  93. case 2:lval = (int64_t) sint2korr(*row);break;
  94. case 1:lval = (int64_t) *(int8_t*)*row;break;
  95. }
  96. #if SIZEOF_ZEND_LONG==4
  97. if ((L64(2147483647) < (int64_t) lval) || (L64(-2147483648) > (int64_t) lval)) {
  98. DBG_INF("stringify");
  99. ZVAL_STR(zv, zend_i64_to_str(lval));
  100. } else
  101. #endif /* SIZEOF */
  102. {
  103. ZVAL_LONG(zv, (zend_long) lval); /* the cast is safe, we are in the range */
  104. }
  105. }
  106. (*row)+= byte_count;
  107. DBG_VOID_RETURN;
  108. }
  109. /* }}} */
  110. /* {{{ ps_fetch_null */
  111. static void
  112. ps_fetch_null(zval *zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  113. {
  114. ZVAL_NULL(zv);
  115. }
  116. /* }}} */
  117. /* {{{ ps_fetch_int8 */
  118. static void
  119. ps_fetch_int8(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  120. {
  121. ps_fetch_from_1_to_8_bytes(zv, field, pack_len, row, 1);
  122. }
  123. /* }}} */
  124. /* {{{ ps_fetch_int16 */
  125. static void
  126. ps_fetch_int16(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  127. {
  128. ps_fetch_from_1_to_8_bytes(zv, field, pack_len, row, 2);
  129. }
  130. /* }}} */
  131. /* {{{ ps_fetch_int32 */
  132. static void
  133. ps_fetch_int32(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  134. {
  135. ps_fetch_from_1_to_8_bytes(zv, field, pack_len, row, 4);
  136. }
  137. /* }}} */
  138. /* {{{ ps_fetch_int64 */
  139. static void
  140. ps_fetch_int64(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  141. {
  142. ps_fetch_from_1_to_8_bytes(zv, field, pack_len, row, 8);
  143. }
  144. /* }}} */
  145. /* {{{ ps_fetch_float */
  146. static void
  147. ps_fetch_float(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  148. {
  149. float fval;
  150. double dval;
  151. DBG_ENTER("ps_fetch_float");
  152. float4get(fval, *row);
  153. (*row)+= 4;
  154. DBG_INF_FMT("value=%f", fval);
  155. #ifndef NOT_FIXED_DEC
  156. # define NOT_FIXED_DEC 31
  157. #endif
  158. dval = mysql_float_to_double(fval, (field->decimals >= NOT_FIXED_DEC) ? -1 : (int)field->decimals);
  159. ZVAL_DOUBLE(zv, dval);
  160. DBG_VOID_RETURN;
  161. }
  162. /* }}} */
  163. /* {{{ ps_fetch_double */
  164. static void
  165. ps_fetch_double(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  166. {
  167. double value;
  168. DBG_ENTER("ps_fetch_double");
  169. float8get(value, *row);
  170. ZVAL_DOUBLE(zv, value);
  171. (*row)+= 8;
  172. DBG_INF_FMT("value=%f", value);
  173. DBG_VOID_RETURN;
  174. }
  175. /* }}} */
  176. /* {{{ ps_fetch_time */
  177. static void
  178. ps_fetch_time(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  179. {
  180. struct st_mysqlnd_time t;
  181. zend_ulong length; /* First byte encodes the length */
  182. DBG_ENTER("ps_fetch_time");
  183. if ((length = php_mysqlnd_net_field_length(row))) {
  184. const zend_uchar * to = *row;
  185. t.time_type = MYSQLND_TIMESTAMP_TIME;
  186. t.neg = (bool) to[0];
  187. t.day = (zend_ulong) sint4korr(to+1);
  188. t.hour = (unsigned int) to[5];
  189. t.minute = (unsigned int) to[6];
  190. t.second = (unsigned int) to[7];
  191. t.second_part = (length > 8) ? (zend_ulong) sint4korr(to+8) : 0;
  192. t.year = t.month= 0;
  193. if (t.day) {
  194. /* Convert days to hours at once */
  195. t.hour += t.day*24;
  196. t.day = 0;
  197. }
  198. (*row) += length;
  199. } else {
  200. memset(&t, 0, sizeof(t));
  201. t.time_type = MYSQLND_TIMESTAMP_TIME;
  202. }
  203. if (field->decimals > 0 && field->decimals < 7) {
  204. ZVAL_STR(zv, zend_strpprintf(0, "%s%02u:%02u:%02u.%0*u",
  205. (t.neg ? "-" : ""), t.hour, t.minute, t.second, field->decimals,
  206. (uint32_t) (t.second_part / pow(10, 6 - field->decimals))));
  207. } else {
  208. ZVAL_STR(zv, zend_strpprintf(0, "%s%02u:%02u:%02u",
  209. (t.neg ? "-" : ""), t.hour, t.minute, t.second));
  210. }
  211. DBG_VOID_RETURN;
  212. }
  213. /* }}} */
  214. /* {{{ ps_fetch_date */
  215. static void
  216. ps_fetch_date(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  217. {
  218. struct st_mysqlnd_time t = {0};
  219. zend_ulong length; /* First byte encodes the length*/
  220. DBG_ENTER("ps_fetch_date");
  221. if ((length = php_mysqlnd_net_field_length(row))) {
  222. const zend_uchar * to = *row;
  223. t.time_type = MYSQLND_TIMESTAMP_DATE;
  224. t.neg = 0;
  225. t.second_part = t.hour = t.minute = t.second = 0;
  226. t.year = (unsigned int) sint2korr(to);
  227. t.month = (unsigned int) to[2];
  228. t.day = (unsigned int) to[3];
  229. (*row)+= length;
  230. } else {
  231. memset(&t, 0, sizeof(t));
  232. t.time_type = MYSQLND_TIMESTAMP_DATE;
  233. }
  234. ZVAL_STR(zv, zend_strpprintf(0, "%04u-%02u-%02u", t.year, t.month, t.day));
  235. DBG_VOID_RETURN;
  236. }
  237. /* }}} */
  238. /* {{{ ps_fetch_datetime */
  239. static void
  240. ps_fetch_datetime(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  241. {
  242. struct st_mysqlnd_time t;
  243. zend_ulong length; /* First byte encodes the length*/
  244. DBG_ENTER("ps_fetch_datetime");
  245. if ((length = php_mysqlnd_net_field_length(row))) {
  246. const zend_uchar * to = *row;
  247. t.time_type = MYSQLND_TIMESTAMP_DATETIME;
  248. t.neg = 0;
  249. t.year = (unsigned int) sint2korr(to);
  250. t.month = (unsigned int) to[2];
  251. t.day = (unsigned int) to[3];
  252. if (length > 4) {
  253. t.hour = (unsigned int) to[4];
  254. t.minute = (unsigned int) to[5];
  255. t.second = (unsigned int) to[6];
  256. } else {
  257. t.hour = t.minute = t.second= 0;
  258. }
  259. t.second_part = (length > 7) ? (zend_ulong) sint4korr(to+7) : 0;
  260. (*row)+= length;
  261. } else {
  262. memset(&t, 0, sizeof(t));
  263. t.time_type = MYSQLND_TIMESTAMP_DATETIME;
  264. }
  265. if (field->decimals > 0 && field->decimals < 7) {
  266. ZVAL_STR(zv, zend_strpprintf(0, "%04u-%02u-%02u %02u:%02u:%02u.%0*u",
  267. t.year, t.month, t.day, t.hour, t.minute, t.second, field->decimals,
  268. (uint32_t) (t.second_part / pow(10, 6 - field->decimals))));
  269. } else {
  270. ZVAL_STR(zv, zend_strpprintf(0, "%04u-%02u-%02u %02u:%02u:%02u",
  271. t.year, t.month, t.day, t.hour, t.minute, t.second));
  272. }
  273. DBG_VOID_RETURN;
  274. }
  275. /* }}} */
  276. /* {{{ ps_fetch_string */
  277. static void
  278. ps_fetch_string(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  279. {
  280. const zend_ulong length = php_mysqlnd_net_field_length(row);
  281. DBG_ENTER("ps_fetch_string");
  282. DBG_INF_FMT("len = " ZEND_ULONG_FMT, length);
  283. DBG_INF("copying from the row buffer");
  284. ZVAL_STRINGL_FAST(zv, (char *)*row, length);
  285. (*row) += length;
  286. DBG_VOID_RETURN;
  287. }
  288. /* }}} */
  289. /* {{{ ps_fetch_bit */
  290. static void
  291. ps_fetch_bit(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
  292. {
  293. const zend_ulong length = php_mysqlnd_net_field_length(row);
  294. ps_fetch_from_1_to_8_bytes(zv, field, pack_len, row, length);
  295. }
  296. /* }}} */
  297. /* {{{ _mysqlnd_init_ps_fetch_subsystem */
  298. void _mysqlnd_init_ps_fetch_subsystem()
  299. {
  300. memset(mysqlnd_ps_fetch_functions, 0, sizeof(mysqlnd_ps_fetch_functions));
  301. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NULL].func = ps_fetch_null;
  302. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NULL].pack_len = 0;
  303. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NULL].php_type = IS_NULL;
  304. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TINY].func = ps_fetch_int8;
  305. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TINY].pack_len = 1;
  306. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TINY].php_type = IS_LONG;
  307. mysqlnd_ps_fetch_functions[MYSQL_TYPE_SHORT].func = ps_fetch_int16;
  308. mysqlnd_ps_fetch_functions[MYSQL_TYPE_SHORT].pack_len = 2;
  309. mysqlnd_ps_fetch_functions[MYSQL_TYPE_SHORT].php_type = IS_LONG;
  310. mysqlnd_ps_fetch_functions[MYSQL_TYPE_YEAR].func = ps_fetch_int16;
  311. mysqlnd_ps_fetch_functions[MYSQL_TYPE_YEAR].pack_len = 2;
  312. mysqlnd_ps_fetch_functions[MYSQL_TYPE_YEAR].php_type = IS_LONG;
  313. mysqlnd_ps_fetch_functions[MYSQL_TYPE_INT24].func = ps_fetch_int32;
  314. mysqlnd_ps_fetch_functions[MYSQL_TYPE_INT24].pack_len = 4;
  315. mysqlnd_ps_fetch_functions[MYSQL_TYPE_INT24].php_type = IS_LONG;
  316. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONG].func = ps_fetch_int32;
  317. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONG].pack_len = 4;
  318. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONG].php_type = IS_LONG;
  319. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONGLONG].func = ps_fetch_int64;
  320. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONGLONG].pack_len= 8;
  321. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONGLONG].php_type= IS_LONG;
  322. mysqlnd_ps_fetch_functions[MYSQL_TYPE_FLOAT].func = ps_fetch_float;
  323. mysqlnd_ps_fetch_functions[MYSQL_TYPE_FLOAT].pack_len = 4;
  324. mysqlnd_ps_fetch_functions[MYSQL_TYPE_FLOAT].php_type = IS_DOUBLE;
  325. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DOUBLE].func = ps_fetch_double;
  326. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DOUBLE].pack_len = 8;
  327. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DOUBLE].php_type = IS_DOUBLE;
  328. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TIME].func = ps_fetch_time;
  329. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TIME].pack_len = MYSQLND_PS_SKIP_RESULT_W_LEN;
  330. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TIME].php_type = IS_STRING;
  331. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DATE].func = ps_fetch_date;
  332. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DATE].pack_len = MYSQLND_PS_SKIP_RESULT_W_LEN;
  333. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DATE].php_type = IS_STRING;
  334. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NEWDATE].func = ps_fetch_string;
  335. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NEWDATE].pack_len = MYSQLND_PS_SKIP_RESULT_W_LEN;
  336. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NEWDATE].php_type = IS_STRING;
  337. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DATETIME].func = ps_fetch_datetime;
  338. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DATETIME].pack_len= MYSQLND_PS_SKIP_RESULT_W_LEN;
  339. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DATETIME].php_type= IS_STRING;
  340. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TIMESTAMP].func = ps_fetch_datetime;
  341. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TIMESTAMP].pack_len= MYSQLND_PS_SKIP_RESULT_W_LEN;
  342. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TIMESTAMP].php_type= IS_STRING;
  343. mysqlnd_ps_fetch_functions[MYSQL_TYPE_JSON].func = ps_fetch_string;
  344. mysqlnd_ps_fetch_functions[MYSQL_TYPE_JSON].pack_len= MYSQLND_PS_SKIP_RESULT_STR;
  345. mysqlnd_ps_fetch_functions[MYSQL_TYPE_JSON].php_type = IS_STRING;
  346. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TINY_BLOB].func = ps_fetch_string;
  347. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TINY_BLOB].pack_len= MYSQLND_PS_SKIP_RESULT_STR;
  348. mysqlnd_ps_fetch_functions[MYSQL_TYPE_TINY_BLOB].php_type = IS_STRING;
  349. mysqlnd_ps_fetch_functions[MYSQL_TYPE_BLOB].func = ps_fetch_string;
  350. mysqlnd_ps_fetch_functions[MYSQL_TYPE_BLOB].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  351. mysqlnd_ps_fetch_functions[MYSQL_TYPE_BLOB].php_type = IS_STRING;
  352. mysqlnd_ps_fetch_functions[MYSQL_TYPE_MEDIUM_BLOB].func = ps_fetch_string;
  353. mysqlnd_ps_fetch_functions[MYSQL_TYPE_MEDIUM_BLOB].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  354. mysqlnd_ps_fetch_functions[MYSQL_TYPE_MEDIUM_BLOB].php_type = IS_STRING;
  355. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONG_BLOB].func = ps_fetch_string;
  356. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONG_BLOB].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  357. mysqlnd_ps_fetch_functions[MYSQL_TYPE_LONG_BLOB].php_type = IS_STRING;
  358. mysqlnd_ps_fetch_functions[MYSQL_TYPE_BIT].func = ps_fetch_bit;
  359. mysqlnd_ps_fetch_functions[MYSQL_TYPE_BIT].pack_len = 8;
  360. mysqlnd_ps_fetch_functions[MYSQL_TYPE_BIT].php_type = IS_LONG;
  361. mysqlnd_ps_fetch_functions[MYSQL_TYPE_VAR_STRING].func = ps_fetch_string;
  362. mysqlnd_ps_fetch_functions[MYSQL_TYPE_VAR_STRING].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  363. mysqlnd_ps_fetch_functions[MYSQL_TYPE_VAR_STRING].php_type = IS_STRING;
  364. mysqlnd_ps_fetch_functions[MYSQL_TYPE_VARCHAR].func = ps_fetch_string;
  365. mysqlnd_ps_fetch_functions[MYSQL_TYPE_VARCHAR].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  366. mysqlnd_ps_fetch_functions[MYSQL_TYPE_VARCHAR].php_type = IS_STRING;
  367. mysqlnd_ps_fetch_functions[MYSQL_TYPE_STRING].func = ps_fetch_string;
  368. mysqlnd_ps_fetch_functions[MYSQL_TYPE_STRING].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  369. mysqlnd_ps_fetch_functions[MYSQL_TYPE_STRING].php_type = IS_STRING;
  370. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DECIMAL].func = ps_fetch_string;
  371. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DECIMAL].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  372. mysqlnd_ps_fetch_functions[MYSQL_TYPE_DECIMAL].php_type = IS_STRING;
  373. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NEWDECIMAL].func = ps_fetch_string;
  374. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NEWDECIMAL].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  375. mysqlnd_ps_fetch_functions[MYSQL_TYPE_NEWDECIMAL].php_type = IS_STRING;
  376. mysqlnd_ps_fetch_functions[MYSQL_TYPE_ENUM].func = ps_fetch_string;
  377. mysqlnd_ps_fetch_functions[MYSQL_TYPE_ENUM].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  378. mysqlnd_ps_fetch_functions[MYSQL_TYPE_ENUM].php_type = IS_STRING;
  379. mysqlnd_ps_fetch_functions[MYSQL_TYPE_SET].func = ps_fetch_string;
  380. mysqlnd_ps_fetch_functions[MYSQL_TYPE_SET].pack_len = MYSQLND_PS_SKIP_RESULT_STR;
  381. mysqlnd_ps_fetch_functions[MYSQL_TYPE_SET].php_type = IS_STRING;
  382. mysqlnd_ps_fetch_functions[MYSQL_TYPE_GEOMETRY].func = ps_fetch_string;
  383. mysqlnd_ps_fetch_functions[MYSQL_TYPE_GEOMETRY].pack_len= MYSQLND_PS_SKIP_RESULT_STR;
  384. mysqlnd_ps_fetch_functions[MYSQL_TYPE_GEOMETRY].php_type= IS_STRING;
  385. }
  386. /* }}} */
  387. /* {{{ mysqlnd_stmt_copy_it */
  388. static enum_func_status
  389. mysqlnd_stmt_copy_it(zval ** copies, zval * original, unsigned int param_count, unsigned int current)
  390. {
  391. if (!*copies) {
  392. *copies = mnd_ecalloc(param_count, sizeof(zval));
  393. }
  394. if (*copies) {
  395. ZVAL_COPY(&(*copies)[current], original);
  396. return PASS;
  397. }
  398. return FAIL;
  399. }
  400. /* }}} */
  401. /* {{{ mysqlnd_stmt_free_copies */
  402. static void
  403. mysqlnd_stmt_free_copies(MYSQLND_STMT_DATA * stmt, zval *copies)
  404. {
  405. if (copies) {
  406. unsigned int i;
  407. for (i = 0; i < stmt->param_count; i++) {
  408. zval_ptr_dtor(&copies[i]);
  409. }
  410. mnd_efree(copies);
  411. }
  412. }
  413. /* }}} */
  414. /* {{{ mysqlnd_stmt_execute_check_n_enlarge_buffer */
  415. static enum_func_status
  416. mysqlnd_stmt_execute_check_n_enlarge_buffer(zend_uchar **buf, zend_uchar **p, size_t * buf_len, zend_uchar * const provided_buffer, size_t needed_bytes)
  417. {
  418. const size_t overalloc = 5;
  419. size_t left = (*buf_len - (*p - *buf));
  420. if (left < (needed_bytes + overalloc)) {
  421. const size_t offset = *p - *buf;
  422. zend_uchar *tmp_buf;
  423. *buf_len = offset + needed_bytes + overalloc;
  424. tmp_buf = mnd_emalloc(*buf_len);
  425. if (!tmp_buf) {
  426. return FAIL;
  427. }
  428. memcpy(tmp_buf, *buf, offset);
  429. if (*buf != provided_buffer) {
  430. mnd_efree(*buf);
  431. }
  432. *buf = tmp_buf;
  433. /* Update our pos pointer */
  434. *p = *buf + offset;
  435. }
  436. return PASS;
  437. }
  438. /* }}} */
  439. /* {{{ mysqlnd_stmt_execute_prepare_param_types */
  440. static enum_func_status
  441. mysqlnd_stmt_execute_prepare_param_types(MYSQLND_STMT_DATA * stmt, zval ** copies_param, int * resend_types_next_time)
  442. {
  443. unsigned int i;
  444. DBG_ENTER("mysqlnd_stmt_execute_prepare_param_types");
  445. for (i = 0; i < stmt->param_count; i++) {
  446. const short current_type = stmt->param_bind[i].type;
  447. zval *parameter = &stmt->param_bind[i].zv;
  448. ZVAL_DEREF(parameter);
  449. if (!Z_ISNULL_P(parameter) && (current_type == MYSQL_TYPE_LONG || current_type == MYSQL_TYPE_LONGLONG || current_type == MYSQL_TYPE_TINY)) {
  450. /* always copy the var, because we do many conversions */
  451. if (Z_TYPE_P(parameter) != IS_LONG &&
  452. PASS != mysqlnd_stmt_copy_it(copies_param, parameter, stmt->param_count, i))
  453. {
  454. SET_OOM_ERROR(stmt->error_info);
  455. goto end;
  456. }
  457. /*
  458. if it doesn't fit in a long send it as a string.
  459. Check bug #52891 : Wrong data inserted with mysqli/mysqlnd when using bind_param, value > LONG_MAX
  460. */
  461. if (Z_TYPE_P(parameter) != IS_LONG) {
  462. zval *tmp_data = (*copies_param && !Z_ISUNDEF((*copies_param)[i]))? &(*copies_param)[i]: parameter;
  463. /*
  464. Because converting to double and back to long can lead
  465. to losing precision we need second variable. Conversion to double is to see if
  466. value is too big for a long. As said, precision could be lost.
  467. */
  468. double d = zval_get_double(tmp_data);
  469. /*
  470. if it doesn't fit in a long send it as a string.
  471. Check bug #52891 : Wrong data inserted with mysqli/mysqlnd when using bind_param, value > LONG_MAX
  472. We do transformation here, which will be used later when sending types. The code later relies on this.
  473. */
  474. if (d >= (double) ZEND_LONG_MAX || d < (double) ZEND_LONG_MIN) {
  475. stmt->send_types_to_server = *resend_types_next_time = 1;
  476. convert_to_string(tmp_data);
  477. } else {
  478. convert_to_long(tmp_data);
  479. }
  480. }
  481. }
  482. }
  483. DBG_RETURN(PASS);
  484. end:
  485. DBG_RETURN(FAIL);
  486. }
  487. /* }}} */
  488. /* {{{ mysqlnd_stmt_execute_store_types */
  489. static void
  490. mysqlnd_stmt_execute_store_types(MYSQLND_STMT_DATA * stmt, zval * copies, zend_uchar ** p)
  491. {
  492. unsigned int i;
  493. for (i = 0; i < stmt->param_count; i++) {
  494. short current_type = stmt->param_bind[i].type;
  495. zval *parameter = &stmt->param_bind[i].zv;
  496. /* our types are not unsigned */
  497. #if SIZEOF_ZEND_LONG==8
  498. if (current_type == MYSQL_TYPE_LONG) {
  499. current_type = MYSQL_TYPE_LONGLONG;
  500. }
  501. #endif
  502. ZVAL_DEREF(parameter);
  503. if (!Z_ISNULL_P(parameter) && (current_type == MYSQL_TYPE_LONG || current_type == MYSQL_TYPE_LONGLONG)) {
  504. /*
  505. if it doesn't fit in a long send it as a string.
  506. Check bug #52891 : Wrong data inserted with mysqli/mysqlnd when using bind_param, value > LONG_MAX
  507. */
  508. if (Z_TYPE_P(parameter) != IS_LONG) {
  509. const zval *tmp_data = (copies && !Z_ISUNDEF(copies[i]))? &copies[i] : parameter;
  510. /*
  511. In case of IS_LONG we do nothing, it is ok, in case of string, we just need to set current_type.
  512. The actual transformation has been performed several dozens line above.
  513. */
  514. if (Z_TYPE_P(tmp_data) == IS_STRING) {
  515. current_type = MYSQL_TYPE_VAR_STRING;
  516. /*
  517. don't change stmt->param_bind[i].type to MYSQL_TYPE_VAR_STRING
  518. we force convert_to_long in all cases, thus the type will be right in the next switch.
  519. if the type is however not long, then we will do a goto in the next switch.
  520. We want to preserve the original bind type given by the user. Thus, we do these hacks.
  521. */
  522. }
  523. }
  524. }
  525. int2store(*p, current_type);
  526. *p+= 2;
  527. }
  528. }
  529. /* }}} */
  530. /* {{{ mysqlnd_stmt_execute_calculate_param_values_size */
  531. static enum_func_status
  532. mysqlnd_stmt_execute_calculate_param_values_size(MYSQLND_STMT_DATA * stmt, zval ** copies_param, size_t * data_size)
  533. {
  534. unsigned int i;
  535. DBG_ENTER("mysqlnd_stmt_execute_calculate_param_values_size");
  536. for (i = 0; i < stmt->param_count; i++) {
  537. unsigned short is_longlong = 0;
  538. unsigned int j;
  539. zval *bind_var, *the_var = &stmt->param_bind[i].zv;
  540. bind_var = the_var;
  541. ZVAL_DEREF(the_var);
  542. if ((stmt->param_bind[i].type != MYSQL_TYPE_LONG_BLOB && Z_TYPE_P(the_var) == IS_NULL)) {
  543. continue;
  544. }
  545. if (Z_ISREF_P(bind_var)) {
  546. for (j = i + 1; j < stmt->param_count; j++) {
  547. if (Z_ISREF(stmt->param_bind[j].zv) && Z_REFVAL(stmt->param_bind[j].zv) == the_var) {
  548. /* Double binding of the same zval, make a copy */
  549. if (!*copies_param || Z_ISUNDEF((*copies_param)[i])) {
  550. if (PASS != mysqlnd_stmt_copy_it(copies_param, the_var, stmt->param_count, i)) {
  551. SET_OOM_ERROR(stmt->error_info);
  552. goto end;
  553. }
  554. }
  555. break;
  556. }
  557. }
  558. }
  559. switch (stmt->param_bind[i].type) {
  560. case MYSQL_TYPE_DOUBLE:
  561. *data_size += 8;
  562. if (Z_TYPE_P(the_var) != IS_DOUBLE) {
  563. if (!*copies_param || Z_ISUNDEF((*copies_param)[i])) {
  564. if (PASS != mysqlnd_stmt_copy_it(copies_param, the_var, stmt->param_count, i)) {
  565. SET_OOM_ERROR(stmt->error_info);
  566. goto end;
  567. }
  568. }
  569. }
  570. break;
  571. case MYSQL_TYPE_LONGLONG:
  572. is_longlong = 4;
  573. ZEND_FALLTHROUGH;
  574. case MYSQL_TYPE_LONG:
  575. {
  576. zval *tmp_data = (*copies_param && !Z_ISUNDEF((*copies_param)[i]))? &(*copies_param)[i]: the_var;
  577. if (Z_TYPE_P(tmp_data) == IS_STRING) {
  578. goto use_string;
  579. }
  580. convert_to_long(tmp_data);
  581. }
  582. *data_size += 4 + is_longlong;
  583. break;
  584. case MYSQL_TYPE_LONG_BLOB:
  585. if (!(stmt->param_bind[i].flags & MYSQLND_PARAM_BIND_BLOB_USED)) {
  586. /*
  587. User hasn't sent anything, we will send empty string.
  588. Empty string has length of 0, encoded in 1 byte. No real
  589. data will follows after it.
  590. */
  591. (*data_size)++;
  592. }
  593. break;
  594. case MYSQL_TYPE_VAR_STRING:
  595. use_string:
  596. *data_size += 8; /* max 8 bytes for size */
  597. if (Z_TYPE_P(the_var) != IS_STRING) {
  598. if (!*copies_param || Z_ISUNDEF((*copies_param)[i])) {
  599. if (PASS != mysqlnd_stmt_copy_it(copies_param, the_var, stmt->param_count, i)) {
  600. SET_OOM_ERROR(stmt->error_info);
  601. goto end;
  602. }
  603. }
  604. the_var = &((*copies_param)[i]);
  605. }
  606. if (!try_convert_to_string(the_var)) {
  607. goto end;
  608. }
  609. *data_size += Z_STRLEN_P(the_var);
  610. break;
  611. }
  612. }
  613. DBG_RETURN(PASS);
  614. end:
  615. DBG_RETURN(FAIL);
  616. }
  617. /* }}} */
  618. /* {{{ mysqlnd_stmt_execute_store_param_values */
  619. static void
  620. mysqlnd_stmt_execute_store_param_values(MYSQLND_STMT_DATA * stmt, zval * copies, zend_uchar * buf, zend_uchar ** p, size_t null_byte_offset)
  621. {
  622. unsigned int i;
  623. for (i = 0; i < stmt->param_count; i++) {
  624. zval *data, *parameter = &stmt->param_bind[i].zv;
  625. ZVAL_DEREF(parameter);
  626. data = (copies && !Z_ISUNDEF(copies[i]))? &copies[i]: parameter;
  627. /* Handle long data */
  628. if (!Z_ISUNDEF_P(parameter) && Z_TYPE_P(data) == IS_NULL) {
  629. (buf + null_byte_offset)[i/8] |= (zend_uchar) (1 << (i & 7));
  630. } else {
  631. switch (stmt->param_bind[i].type) {
  632. case MYSQL_TYPE_DOUBLE:
  633. convert_to_double(data);
  634. float8store(*p, Z_DVAL_P(data));
  635. (*p) += 8;
  636. break;
  637. case MYSQL_TYPE_LONGLONG:
  638. if (Z_TYPE_P(data) == IS_STRING) {
  639. goto send_string;
  640. }
  641. /* data has alreade been converted to long */
  642. int8store(*p, Z_LVAL_P(data));
  643. (*p) += 8;
  644. break;
  645. case MYSQL_TYPE_LONG:
  646. if (Z_TYPE_P(data) == IS_STRING) {
  647. goto send_string;
  648. }
  649. /* data has alreade been converted to long */
  650. int4store(*p, Z_LVAL_P(data));
  651. (*p) += 4;
  652. break;
  653. case MYSQL_TYPE_TINY:
  654. if (Z_TYPE_P(data) == IS_STRING) {
  655. goto send_string;
  656. }
  657. int1store(*p, Z_LVAL_P(data));
  658. (*p)++;
  659. break;
  660. case MYSQL_TYPE_LONG_BLOB:
  661. if (stmt->param_bind[i].flags & MYSQLND_PARAM_BIND_BLOB_USED) {
  662. stmt->param_bind[i].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
  663. } else {
  664. /* send_long_data() not called, send empty string */
  665. *p = php_mysqlnd_net_store_length(*p, 0);
  666. }
  667. break;
  668. case MYSQL_TYPE_VAR_STRING:
  669. send_string:
  670. {
  671. const size_t len = Z_STRLEN_P(data);
  672. /* to is after p. The latter hasn't been moved */
  673. *p = php_mysqlnd_net_store_length(*p, len);
  674. memcpy(*p, Z_STRVAL_P(data), len);
  675. (*p) += len;
  676. }
  677. break;
  678. default:
  679. /* Won't happen, but set to NULL */
  680. (buf + null_byte_offset)[i/8] |= (zend_uchar) (1 << (i & 7));
  681. break;
  682. }
  683. }
  684. }
  685. }
  686. /* }}} */
  687. /* {{{ mysqlnd_stmt_execute_store_params */
  688. static enum_func_status
  689. mysqlnd_stmt_execute_store_params(MYSQLND_STMT * s, zend_uchar **buf, zend_uchar **p, size_t *buf_len )
  690. {
  691. MYSQLND_STMT_DATA * stmt = s->data;
  692. zend_uchar * provided_buffer = *buf;
  693. size_t data_size = 0;
  694. zval *copies = NULL;/* if there are different types */
  695. enum_func_status ret = FAIL;
  696. int resend_types_next_time = 0;
  697. size_t null_byte_offset;
  698. DBG_ENTER("mysqlnd_stmt_execute_store_params");
  699. {
  700. unsigned int null_count = (stmt->param_count + 7) / 8;
  701. if (FAIL == mysqlnd_stmt_execute_check_n_enlarge_buffer(buf, p, buf_len, provided_buffer, null_count)) {
  702. SET_OOM_ERROR(stmt->error_info);
  703. goto end;
  704. }
  705. /* put `null` bytes */
  706. null_byte_offset = *p - *buf;
  707. memset(*p, 0, null_count);
  708. *p += null_count;
  709. }
  710. /* 1. Store type information */
  711. /*
  712. check if need to send the types even if stmt->send_types_to_server is 0. This is because
  713. if we send "i" (42) then the type will be int and the server will expect int. However, if next
  714. time we try to send > LONG_MAX, the conversion to string will send a string and the server
  715. won't expect it and interpret the value as 0. Thus we need to resend the types, if any such values
  716. occur, and force resend for the next execution.
  717. */
  718. if (FAIL == mysqlnd_stmt_execute_prepare_param_types(stmt, &copies, &resend_types_next_time)) {
  719. goto end;
  720. }
  721. int1store(*p, stmt->send_types_to_server);
  722. (*p)++;
  723. if (stmt->send_types_to_server) {
  724. if (FAIL == mysqlnd_stmt_execute_check_n_enlarge_buffer(buf, p, buf_len, provided_buffer, stmt->param_count * 2)) {
  725. SET_OOM_ERROR(stmt->error_info);
  726. goto end;
  727. }
  728. mysqlnd_stmt_execute_store_types(stmt, copies, p);
  729. }
  730. stmt->send_types_to_server = resend_types_next_time;
  731. /* 2. Store data */
  732. /* 2.1 Calculate how much space we need */
  733. if (FAIL == mysqlnd_stmt_execute_calculate_param_values_size(stmt, &copies, &data_size)) {
  734. goto end;
  735. }
  736. /* 2.2 Enlarge the buffer, if needed */
  737. if (FAIL == mysqlnd_stmt_execute_check_n_enlarge_buffer(buf, p, buf_len, provided_buffer, data_size)) {
  738. SET_OOM_ERROR(stmt->error_info);
  739. goto end;
  740. }
  741. /* 2.3 Store the actual data */
  742. mysqlnd_stmt_execute_store_param_values(stmt, copies, *buf, p, null_byte_offset);
  743. ret = PASS;
  744. end:
  745. mysqlnd_stmt_free_copies(stmt, copies);
  746. DBG_INF_FMT("ret=%s", ret == PASS? "PASS":"FAIL");
  747. DBG_RETURN(ret);
  748. }
  749. /* }}} */
  750. /* {{{ mysqlnd_stmt_execute_generate_request */
  751. enum_func_status
  752. mysqlnd_stmt_execute_generate_request(MYSQLND_STMT * const s, zend_uchar ** request, size_t *request_len, bool * free_buffer)
  753. {
  754. MYSQLND_STMT_DATA * stmt = s->data;
  755. zend_uchar *p = stmt->execute_cmd_buffer.buffer,
  756. *cmd_buffer = stmt->execute_cmd_buffer.buffer;
  757. size_t cmd_buffer_length = stmt->execute_cmd_buffer.length;
  758. enum_func_status ret = PASS;
  759. DBG_ENTER("mysqlnd_stmt_execute_generate_request");
  760. int4store(p, stmt->stmt_id);
  761. p += 4;
  762. /* flags is 4 bytes, we store just 1 */
  763. int1store(p, (zend_uchar) stmt->flags);
  764. p++;
  765. /* Make it all zero */
  766. int4store(p, 0);
  767. int1store(p, 1); /* and send 1 for iteration count */
  768. p+= 4;
  769. if (stmt->param_count != 0) {
  770. ret = mysqlnd_stmt_execute_store_params(s, &cmd_buffer, &p, &cmd_buffer_length);
  771. }
  772. *free_buffer = (cmd_buffer != stmt->execute_cmd_buffer.buffer);
  773. *request_len = (p - cmd_buffer);
  774. *request = cmd_buffer;
  775. DBG_INF_FMT("ret=%s", ret == PASS? "PASS":"FAIL");
  776. DBG_RETURN(ret);
  777. }
  778. /* }}} */