pdo_sql_parser.re 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. | Author: George Schlossnagle <george@omniti.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include "php_pdo_driver.h"
  18. #include "php_pdo_int.h"
  19. #define PDO_PARSER_TEXT 1
  20. #define PDO_PARSER_BIND 2
  21. #define PDO_PARSER_BIND_POS 3
  22. #define PDO_PARSER_ESCAPED_QUESTION 4
  23. #define PDO_PARSER_EOI 5
  24. #define PDO_PARSER_BINDNO_ESCAPED_CHAR -1
  25. #define RET(i) {s->cur = cursor; return i; }
  26. #define SKIP_ONE(i) {s->cur = s->tok + 1; return i; }
  27. #define YYCTYPE unsigned char
  28. #define YYCURSOR cursor
  29. #define YYLIMIT s->end
  30. #define YYMARKER s->ptr
  31. #define YYFILL(n) { RET(PDO_PARSER_EOI); }
  32. typedef struct Scanner {
  33. const char *ptr, *cur, *tok, *end;
  34. } Scanner;
  35. static int scan(Scanner *s)
  36. {
  37. const char *cursor = s->cur;
  38. s->tok = cursor;
  39. /*!re2c
  40. BINDCHR = [:][a-zA-Z0-9_]+;
  41. QUESTION = [?];
  42. ESCQUESTION = [?][?];
  43. COMMENTS = ("/*"([^*]+|[*]+[^/*])*[*]*"*/"|"--"[^\r\n]*);
  44. SPECIALS = [:?"'-/];
  45. MULTICHAR = [:]{2,};
  46. ANYNOEOF = [\001-\377];
  47. */
  48. /*!re2c
  49. (["](([\\]ANYNOEOF)|ANYNOEOF\["\\])*["]) { RET(PDO_PARSER_TEXT); }
  50. (['](([\\]ANYNOEOF)|ANYNOEOF\['\\])*[']) { RET(PDO_PARSER_TEXT); }
  51. MULTICHAR { RET(PDO_PARSER_TEXT); }
  52. ESCQUESTION { RET(PDO_PARSER_ESCAPED_QUESTION); }
  53. BINDCHR { RET(PDO_PARSER_BIND); }
  54. QUESTION { RET(PDO_PARSER_BIND_POS); }
  55. SPECIALS { SKIP_ONE(PDO_PARSER_TEXT); }
  56. COMMENTS { RET(PDO_PARSER_TEXT); }
  57. (ANYNOEOF\SPECIALS)+ { RET(PDO_PARSER_TEXT); }
  58. */
  59. }
  60. struct placeholder {
  61. const char *pos;
  62. size_t len;
  63. zend_string *quoted; /* quoted value */
  64. int bindno;
  65. struct placeholder *next;
  66. };
  67. static void free_param_name(zval *el) {
  68. zend_string_release(Z_PTR_P(el));
  69. }
  70. PDO_API int pdo_parse_params(pdo_stmt_t *stmt, zend_string *inquery, zend_string **outquery)
  71. {
  72. Scanner s;
  73. char *newbuffer;
  74. ptrdiff_t t;
  75. uint32_t bindno = 0;
  76. int ret = 0, escapes = 0;
  77. size_t newbuffer_len;
  78. HashTable *params;
  79. struct pdo_bound_param_data *param;
  80. int query_type = PDO_PLACEHOLDER_NONE;
  81. struct placeholder *placeholders = NULL, *placetail = NULL, *plc = NULL;
  82. s.cur = ZSTR_VAL(inquery);
  83. s.end = s.cur + ZSTR_LEN(inquery) + 1;
  84. /* phase 1: look for args */
  85. while((t = scan(&s)) != PDO_PARSER_EOI) {
  86. if (t == PDO_PARSER_BIND || t == PDO_PARSER_BIND_POS || t == PDO_PARSER_ESCAPED_QUESTION) {
  87. if (t == PDO_PARSER_ESCAPED_QUESTION && stmt->supports_placeholders == PDO_PLACEHOLDER_POSITIONAL) {
  88. /* escaped question marks unsupported, treat as text */
  89. continue;
  90. }
  91. if (t == PDO_PARSER_BIND) {
  92. ptrdiff_t len = s.cur - s.tok;
  93. if ((ZSTR_VAL(inquery) < (s.cur - len)) && isalnum(*(s.cur - len - 1))) {
  94. continue;
  95. }
  96. query_type |= PDO_PLACEHOLDER_NAMED;
  97. } else if (t == PDO_PARSER_BIND_POS) {
  98. query_type |= PDO_PLACEHOLDER_POSITIONAL;
  99. }
  100. plc = emalloc(sizeof(*plc));
  101. memset(plc, 0, sizeof(*plc));
  102. plc->next = NULL;
  103. plc->pos = s.tok;
  104. plc->len = s.cur - s.tok;
  105. if (t == PDO_PARSER_ESCAPED_QUESTION) {
  106. plc->bindno = PDO_PARSER_BINDNO_ESCAPED_CHAR;
  107. plc->quoted = ZSTR_CHAR('?');
  108. escapes++;
  109. } else {
  110. plc->bindno = bindno++;
  111. }
  112. if (placetail) {
  113. placetail->next = plc;
  114. } else {
  115. placeholders = plc;
  116. }
  117. placetail = plc;
  118. }
  119. }
  120. /* did the query make sense to me? */
  121. if (query_type == (PDO_PLACEHOLDER_NAMED|PDO_PLACEHOLDER_POSITIONAL)) {
  122. /* they mixed both types; punt */
  123. pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "mixed named and positional parameters");
  124. ret = -1;
  125. goto clean_up;
  126. }
  127. params = stmt->bound_params;
  128. if (stmt->supports_placeholders == PDO_PLACEHOLDER_NONE && params && bindno != zend_hash_num_elements(params)) {
  129. /* extra bit of validation for instances when same params are bound more than once */
  130. if (query_type != PDO_PLACEHOLDER_POSITIONAL && bindno > zend_hash_num_elements(params)) {
  131. int ok = 1;
  132. for (plc = placeholders; plc; plc = plc->next) {
  133. if ((param = zend_hash_str_find_ptr(params, plc->pos, plc->len)) == NULL) {
  134. ok = 0;
  135. break;
  136. }
  137. }
  138. if (ok) {
  139. goto safe;
  140. }
  141. }
  142. pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "number of bound variables does not match number of tokens");
  143. ret = -1;
  144. goto clean_up;
  145. }
  146. if (!placeholders) {
  147. /* nothing to do; good! */
  148. return 0;
  149. }
  150. if (stmt->supports_placeholders == query_type && !stmt->named_rewrite_template) {
  151. /* query matches native syntax */
  152. if (escapes) {
  153. newbuffer_len = ZSTR_LEN(inquery);
  154. goto rewrite;
  155. }
  156. ret = 0;
  157. goto clean_up;
  158. }
  159. if (query_type == PDO_PLACEHOLDER_NAMED && stmt->named_rewrite_template) {
  160. /* magic/hack.
  161. * We we pretend that the query was positional even if
  162. * it was named so that we fall into the
  163. * named rewrite case below. Not too pretty,
  164. * but it works. */
  165. query_type = PDO_PLACEHOLDER_POSITIONAL;
  166. }
  167. safe:
  168. /* what are we going to do ? */
  169. if (stmt->supports_placeholders == PDO_PLACEHOLDER_NONE) {
  170. /* query generation */
  171. newbuffer_len = ZSTR_LEN(inquery);
  172. /* let's quote all the values */
  173. for (plc = placeholders; plc && params; plc = plc->next) {
  174. if (plc->bindno == PDO_PARSER_BINDNO_ESCAPED_CHAR) {
  175. /* escaped character */
  176. continue;
  177. }
  178. if (query_type == PDO_PLACEHOLDER_NONE) {
  179. continue;
  180. }
  181. if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
  182. param = zend_hash_index_find_ptr(params, plc->bindno);
  183. } else {
  184. param = zend_hash_str_find_ptr(params, plc->pos, plc->len);
  185. }
  186. if (param == NULL) {
  187. /* parameter was not defined */
  188. ret = -1;
  189. pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
  190. goto clean_up;
  191. }
  192. if (stmt->dbh->methods->quoter) {
  193. zval *parameter;
  194. if (Z_ISREF(param->parameter)) {
  195. parameter = Z_REFVAL(param->parameter);
  196. } else {
  197. parameter = &param->parameter;
  198. }
  199. if (param->param_type == PDO_PARAM_LOB && Z_TYPE_P(parameter) == IS_RESOURCE) {
  200. php_stream *stm;
  201. php_stream_from_zval_no_verify(stm, parameter);
  202. if (stm) {
  203. zend_string *buf;
  204. buf = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
  205. if (!buf) {
  206. buf = ZSTR_EMPTY_ALLOC();
  207. }
  208. plc->quoted = stmt->dbh->methods->quoter(stmt->dbh, buf, param->param_type);
  209. if (buf) {
  210. zend_string_release_ex(buf, 0);
  211. }
  212. } else {
  213. pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
  214. ret = -1;
  215. goto clean_up;
  216. }
  217. } else {
  218. enum pdo_param_type param_type = param->param_type;
  219. zend_string *buf = NULL;
  220. /* assume all types are nullable */
  221. if (Z_TYPE_P(parameter) == IS_NULL) {
  222. param_type = PDO_PARAM_NULL;
  223. }
  224. switch (param_type) {
  225. case PDO_PARAM_BOOL:
  226. plc->quoted = zend_is_true(parameter) ? ZSTR_CHAR('1') : ZSTR_CHAR('0');
  227. break;
  228. case PDO_PARAM_INT:
  229. plc->quoted = zend_long_to_str(zval_get_long(parameter));
  230. break;
  231. case PDO_PARAM_NULL:
  232. plc->quoted = ZSTR_KNOWN(ZEND_STR_NULL);
  233. break;
  234. default: {
  235. buf = zval_try_get_string(parameter);
  236. /* parameter does not have a string representation, buf == NULL */
  237. if (EG(exception)) {
  238. /* bork */
  239. ret = -1;
  240. strncpy(stmt->error_code, stmt->dbh->error_code, 6);
  241. goto clean_up;
  242. }
  243. plc->quoted = stmt->dbh->methods->quoter(stmt->dbh, buf, param_type);
  244. }
  245. }
  246. if (buf) {
  247. zend_string_release_ex(buf, 0);
  248. }
  249. }
  250. } else {
  251. zval *parameter;
  252. if (Z_ISREF(param->parameter)) {
  253. parameter = Z_REFVAL(param->parameter);
  254. } else {
  255. parameter = &param->parameter;
  256. }
  257. plc->quoted = zend_string_copy(Z_STR_P(parameter));
  258. }
  259. newbuffer_len += ZSTR_LEN(plc->quoted);
  260. }
  261. rewrite:
  262. /* allocate output buffer */
  263. *outquery = zend_string_alloc(newbuffer_len, 0);
  264. newbuffer = ZSTR_VAL(*outquery);
  265. /* and build the query */
  266. const char *ptr = ZSTR_VAL(inquery);
  267. plc = placeholders;
  268. do {
  269. t = plc->pos - ptr;
  270. if (t) {
  271. memcpy(newbuffer, ptr, t);
  272. newbuffer += t;
  273. }
  274. if (plc->quoted) {
  275. memcpy(newbuffer, ZSTR_VAL(plc->quoted), ZSTR_LEN(plc->quoted));
  276. newbuffer += ZSTR_LEN(plc->quoted);
  277. } else {
  278. memcpy(newbuffer, plc->pos, plc->len);
  279. newbuffer += plc->len;
  280. }
  281. ptr = plc->pos + plc->len;
  282. plc = plc->next;
  283. } while (plc);
  284. t = ZSTR_VAL(inquery) + ZSTR_LEN(inquery) - ptr;
  285. if (t) {
  286. memcpy(newbuffer, ptr, t);
  287. newbuffer += t;
  288. }
  289. *newbuffer = '\0';
  290. ZSTR_LEN(*outquery) = newbuffer - ZSTR_VAL(*outquery);
  291. ret = 1;
  292. goto clean_up;
  293. } else if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
  294. /* rewrite ? to :pdoX */
  295. const char *tmpl = stmt->named_rewrite_template ? stmt->named_rewrite_template : ":pdo%d";
  296. int bind_no = 1;
  297. newbuffer_len = ZSTR_LEN(inquery);
  298. if (stmt->bound_param_map == NULL) {
  299. ALLOC_HASHTABLE(stmt->bound_param_map);
  300. zend_hash_init(stmt->bound_param_map, 13, NULL, free_param_name, 0);
  301. }
  302. for (plc = placeholders; plc; plc = plc->next) {
  303. int skip_map = 0;
  304. zend_string *p;
  305. zend_string *idxbuf;
  306. if (plc->bindno == PDO_PARSER_BINDNO_ESCAPED_CHAR) {
  307. continue;
  308. }
  309. zend_string *name = zend_string_init(plc->pos, plc->len, 0);
  310. /* check if bound parameter is already available */
  311. if (zend_string_equals_literal(name, "?") || (p = zend_hash_find_ptr(stmt->bound_param_map, name)) == NULL) {
  312. idxbuf = zend_strpprintf(0, tmpl, bind_no++);
  313. } else {
  314. idxbuf = zend_string_copy(p);
  315. skip_map = 1;
  316. }
  317. plc->quoted = idxbuf;
  318. newbuffer_len += ZSTR_LEN(plc->quoted);
  319. if (!skip_map && stmt->named_rewrite_template) {
  320. /* create a mapping */
  321. zend_hash_update_ptr(stmt->bound_param_map, name, zend_string_copy(plc->quoted));
  322. }
  323. /* map number to name */
  324. zend_hash_index_update_ptr(stmt->bound_param_map, plc->bindno, zend_string_copy(plc->quoted));
  325. zend_string_release(name);
  326. }
  327. goto rewrite;
  328. } else {
  329. /* rewrite :name to ? */
  330. newbuffer_len = ZSTR_LEN(inquery);
  331. if (stmt->bound_param_map == NULL) {
  332. ALLOC_HASHTABLE(stmt->bound_param_map);
  333. zend_hash_init(stmt->bound_param_map, 13, NULL, free_param_name, 0);
  334. }
  335. for (plc = placeholders; plc; plc = plc->next) {
  336. zend_string *name = zend_string_init(plc->pos, plc->len, 0);
  337. zend_hash_index_update_ptr(stmt->bound_param_map, plc->bindno, name);
  338. plc->quoted = ZSTR_CHAR('?');
  339. newbuffer_len -= plc->len - 1;
  340. }
  341. goto rewrite;
  342. }
  343. clean_up:
  344. while (placeholders) {
  345. plc = placeholders;
  346. placeholders = plc->next;
  347. if (plc->quoted) {
  348. zend_string_release_ex(plc->quoted, 0);
  349. }
  350. efree(plc);
  351. }
  352. return ret;
  353. }