123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719 |
- /* Generated by re2c 1.0.3 */
- /*
- +----------------------------------------------------------------------+
- | PHP Version 7 |
- +----------------------------------------------------------------------+
- | Copyright (c) 1997-2018 The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Author: George Schlossnagle <george@omniti.com> |
- +----------------------------------------------------------------------+
- */
- #include "php.h"
- #include "php_pdo_driver.h"
- #include "php_pdo_int.h"
- #define PDO_PARSER_TEXT 1
- #define PDO_PARSER_BIND 2
- #define PDO_PARSER_BIND_POS 3
- #define PDO_PARSER_EOI 4
- #define RET(i) {s->cur = cursor; return i; }
- #define SKIP_ONE(i) {s->cur = s->tok + 1; return i; }
- #define YYCTYPE unsigned char
- #define YYCURSOR cursor
- #define YYLIMIT s->end
- #define YYMARKER s->ptr
- #define YYFILL(n) { RET(PDO_PARSER_EOI); }
- typedef struct Scanner {
- char *ptr, *cur, *tok, *end;
- } Scanner;
- static int scan(Scanner *s)
- {
- char *cursor = s->cur;
- s->tok = cursor;
-
- {
- YYCTYPE yych;
- if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
- yych = *YYCURSOR;
- switch (yych) {
- case 0x00: goto yy2;
- case '"': goto yy6;
- case '\'': goto yy8;
- case '(':
- case ')':
- case '*':
- case '+':
- case ',':
- case '.': goto yy9;
- case '-': goto yy10;
- case '/': goto yy11;
- case ':': goto yy12;
- case '?': goto yy13;
- default: goto yy3;
- }
- yy2:
- YYCURSOR = YYMARKER;
- goto yy7;
- yy3:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- switch (yych) {
- case 0x00:
- case '"':
- case '\'':
- case '(':
- case ')':
- case '*':
- case '+':
- case ',':
- case '-':
- case '.':
- case '/':
- case ':':
- case '?': goto yy5;
- default: goto yy3;
- }
- yy5:
- { RET(PDO_PARSER_TEXT); }
- yy6:
- yych = *(YYMARKER = ++YYCURSOR);
- if (yych >= 0x01) goto yy16;
- yy7:
- { SKIP_ONE(PDO_PARSER_TEXT); }
- yy8:
- yych = *(YYMARKER = ++YYCURSOR);
- if (yych <= 0x00) goto yy7;
- goto yy21;
- yy9:
- ++YYCURSOR;
- goto yy7;
- yy10:
- yych = *++YYCURSOR;
- switch (yych) {
- case '-': goto yy25;
- default: goto yy7;
- }
- yy11:
- yych = *++YYCURSOR;
- switch (yych) {
- case '*': goto yy28;
- default: goto yy7;
- }
- yy12:
- yych = *++YYCURSOR;
- switch (yych) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case 'A':
- case 'B':
- case 'C':
- case 'D':
- case 'E':
- case 'F':
- case 'G':
- case 'H':
- case 'I':
- case 'J':
- case 'K':
- case 'L':
- case 'M':
- case 'N':
- case 'O':
- case 'P':
- case 'Q':
- case 'R':
- case 'S':
- case 'T':
- case 'U':
- case 'V':
- case 'W':
- case 'X':
- case 'Y':
- case 'Z':
- case '_':
- case 'a':
- case 'b':
- case 'c':
- case 'd':
- case 'e':
- case 'f':
- case 'g':
- case 'h':
- case 'i':
- case 'j':
- case 'k':
- case 'l':
- case 'm':
- case 'n':
- case 'o':
- case 'p':
- case 'q':
- case 'r':
- case 's':
- case 't':
- case 'u':
- case 'v':
- case 'w':
- case 'x':
- case 'y':
- case 'z': goto yy30;
- case ':': goto yy33;
- default: goto yy7;
- }
- yy13:
- yych = *++YYCURSOR;
- switch (yych) {
- case '?': goto yy36;
- default: goto yy14;
- }
- yy14:
- { RET(PDO_PARSER_BIND_POS); }
- yy15:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- yy16:
- switch (yych) {
- case 0x00: goto yy2;
- case '"': goto yy17;
- case '\\': goto yy19;
- default: goto yy15;
- }
- yy17:
- ++YYCURSOR;
- { RET(PDO_PARSER_TEXT); }
- yy19:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- if (yych <= 0x00) goto yy2;
- goto yy15;
- yy20:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- yy21:
- switch (yych) {
- case 0x00: goto yy2;
- case '\'': goto yy22;
- case '\\': goto yy24;
- default: goto yy20;
- }
- yy22:
- ++YYCURSOR;
- { RET(PDO_PARSER_TEXT); }
- yy24:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- if (yych <= 0x00) goto yy2;
- goto yy20;
- yy25:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- switch (yych) {
- case '\n':
- case '\r': goto yy27;
- default: goto yy25;
- }
- yy27:
- { RET(PDO_PARSER_TEXT); }
- yy28:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- switch (yych) {
- case '*': goto yy38;
- default: goto yy28;
- }
- yy30:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- switch (yych) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case 'A':
- case 'B':
- case 'C':
- case 'D':
- case 'E':
- case 'F':
- case 'G':
- case 'H':
- case 'I':
- case 'J':
- case 'K':
- case 'L':
- case 'M':
- case 'N':
- case 'O':
- case 'P':
- case 'Q':
- case 'R':
- case 'S':
- case 'T':
- case 'U':
- case 'V':
- case 'W':
- case 'X':
- case 'Y':
- case 'Z':
- case '_':
- case 'a':
- case 'b':
- case 'c':
- case 'd':
- case 'e':
- case 'f':
- case 'g':
- case 'h':
- case 'i':
- case 'j':
- case 'k':
- case 'l':
- case 'm':
- case 'n':
- case 'o':
- case 'p':
- case 'q':
- case 'r':
- case 's':
- case 't':
- case 'u':
- case 'v':
- case 'w':
- case 'x':
- case 'y':
- case 'z': goto yy30;
- default: goto yy32;
- }
- yy32:
- { RET(PDO_PARSER_BIND); }
- yy33:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- switch (yych) {
- case ':': goto yy33;
- default: goto yy35;
- }
- yy35:
- { RET(PDO_PARSER_TEXT); }
- yy36:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- switch (yych) {
- case '?': goto yy36;
- default: goto yy35;
- }
- yy38:
- ++YYCURSOR;
- if (YYLIMIT <= YYCURSOR) YYFILL(1);
- yych = *YYCURSOR;
- switch (yych) {
- case '*': goto yy38;
- case '/': goto yy40;
- default: goto yy28;
- }
- yy40:
- ++YYCURSOR;
- goto yy27;
- }
- }
- struct placeholder {
- char *pos;
- size_t len;
- size_t qlen; /* quoted length of value */
- char *quoted; /* quoted value */
- int freeq;
- int bindno;
- struct placeholder *next;
- };
- static void free_param_name(zval *el) {
- efree(Z_PTR_P(el));
- }
- PDO_API int pdo_parse_params(pdo_stmt_t *stmt, char *inquery, size_t inquery_len,
- char **outquery, size_t *outquery_len)
- {
- Scanner s;
- char *ptr, *newbuffer;
- ptrdiff_t t;
- uint32_t bindno = 0;
- int ret = 0;
- size_t newbuffer_len;
- HashTable *params;
- struct pdo_bound_param_data *param;
- int query_type = PDO_PLACEHOLDER_NONE;
- struct placeholder *placeholders = NULL, *placetail = NULL, *plc = NULL;
- ptr = *outquery;
- s.cur = inquery;
- s.end = inquery + inquery_len + 1;
- /* phase 1: look for args */
- while((t = scan(&s)) != PDO_PARSER_EOI) {
- if (t == PDO_PARSER_BIND || t == PDO_PARSER_BIND_POS) {
- if (t == PDO_PARSER_BIND) {
- ptrdiff_t len = s.cur - s.tok;
- if ((inquery < (s.cur - len)) && isalnum(*(s.cur - len - 1))) {
- continue;
- }
- query_type |= PDO_PLACEHOLDER_NAMED;
- } else {
- query_type |= PDO_PLACEHOLDER_POSITIONAL;
- }
- plc = emalloc(sizeof(*plc));
- memset(plc, 0, sizeof(*plc));
- plc->next = NULL;
- plc->pos = s.tok;
- plc->len = s.cur - s.tok;
- plc->bindno = bindno++;
- if (placetail) {
- placetail->next = plc;
- } else {
- placeholders = plc;
- }
- placetail = plc;
- }
- }
- if (bindno == 0) {
- /* nothing to do; good! */
- return 0;
- }
- /* did the query make sense to me? */
- if (query_type == (PDO_PLACEHOLDER_NAMED|PDO_PLACEHOLDER_POSITIONAL)) {
- /* they mixed both types; punt */
- pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "mixed named and positional parameters");
- ret = -1;
- goto clean_up;
- }
- if (stmt->supports_placeholders == query_type && !stmt->named_rewrite_template) {
- /* query matches native syntax */
- ret = 0;
- goto clean_up;
- }
- if (stmt->named_rewrite_template) {
- /* magic/hack.
- * We we pretend that the query was positional even if
- * it was named so that we fall into the
- * named rewrite case below. Not too pretty,
- * but it works. */
- query_type = PDO_PLACEHOLDER_POSITIONAL;
- }
- params = stmt->bound_params;
- /* Do we have placeholders but no bound params */
- if (bindno && !params && stmt->supports_placeholders == PDO_PLACEHOLDER_NONE) {
- pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "no parameters were bound");
- ret = -1;
- goto clean_up;
- }
- if (params && bindno != zend_hash_num_elements(params) && stmt->supports_placeholders == PDO_PLACEHOLDER_NONE) {
- /* extra bit of validation for instances when same params are bound more than once */
- if (query_type != PDO_PLACEHOLDER_POSITIONAL && bindno > zend_hash_num_elements(params)) {
- int ok = 1;
- for (plc = placeholders; plc; plc = plc->next) {
- if ((param = zend_hash_str_find_ptr(params, plc->pos, plc->len)) == NULL) {
- ok = 0;
- break;
- }
- }
- if (ok) {
- goto safe;
- }
- }
- pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "number of bound variables does not match number of tokens");
- ret = -1;
- goto clean_up;
- }
- safe:
- /* what are we going to do ? */
- if (stmt->supports_placeholders == PDO_PLACEHOLDER_NONE) {
- /* query generation */
- newbuffer_len = inquery_len;
- /* let's quote all the values */
- for (plc = placeholders; plc; plc = plc->next) {
- if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
- param = zend_hash_index_find_ptr(params, plc->bindno);
- } else {
- param = zend_hash_str_find_ptr(params, plc->pos, plc->len);
- }
- if (param == NULL) {
- /* parameter was not defined */
- ret = -1;
- pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
- goto clean_up;
- }
- if (stmt->dbh->methods->quoter) {
- zval *parameter;
- if (Z_ISREF(param->parameter)) {
- parameter = Z_REFVAL(param->parameter);
- } else {
- parameter = ¶m->parameter;
- }
- if (param->param_type == PDO_PARAM_LOB && Z_TYPE_P(parameter) == IS_RESOURCE) {
- php_stream *stm;
- php_stream_from_zval_no_verify(stm, parameter);
- if (stm) {
- zend_string *buf;
- buf = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
- if (!buf) {
- buf = ZSTR_EMPTY_ALLOC();
- }
- if (!stmt->dbh->methods->quoter(stmt->dbh, ZSTR_VAL(buf), ZSTR_LEN(buf), &plc->quoted, &plc->qlen,
- param->param_type)) {
- /* bork */
- ret = -1;
- strncpy(stmt->error_code, stmt->dbh->error_code, 6);
- if (buf) {
- zend_string_release_ex(buf, 0);
- }
- goto clean_up;
- }
- if (buf) {
- zend_string_release_ex(buf, 0);
- }
- } else {
- pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
- ret = -1;
- goto clean_up;
- }
- plc->freeq = 1;
- } else {
- enum pdo_param_type param_type = param->param_type;
- zend_string *buf = NULL;
- /* assume all types are nullable */
- if (Z_TYPE_P(parameter) == IS_NULL) {
- param_type = PDO_PARAM_NULL;
- }
- switch (param_type) {
- case PDO_PARAM_BOOL:
- plc->quoted = zend_is_true(parameter) ? "1" : "0";
- plc->qlen = sizeof("1")-1;
- plc->freeq = 0;
- break;
- case PDO_PARAM_INT:
- buf = zend_long_to_str(zval_get_long(parameter));
- plc->qlen = ZSTR_LEN(buf);
- plc->quoted = estrdup(ZSTR_VAL(buf));
- plc->freeq = 1;
- break;
- case PDO_PARAM_NULL:
- plc->quoted = "NULL";
- plc->qlen = sizeof("NULL")-1;
- plc->freeq = 0;
- break;
- default:
- buf = zval_get_string(parameter);
- if (!stmt->dbh->methods->quoter(stmt->dbh, ZSTR_VAL(buf),
- ZSTR_LEN(buf), &plc->quoted, &plc->qlen,
- param_type)) {
- /* bork */
- ret = -1;
- strncpy(stmt->error_code, stmt->dbh->error_code, 6);
- if (buf) {
- zend_string_release_ex(buf, 0);
- }
- goto clean_up;
- }
- plc->freeq = 1;
- }
- if (buf) {
- zend_string_release_ex(buf, 0);
- }
- }
- } else {
- zval *parameter;
- if (Z_ISREF(param->parameter)) {
- parameter = Z_REFVAL(param->parameter);
- } else {
- parameter = ¶m->parameter;
- }
- plc->quoted = Z_STRVAL_P(parameter);
- plc->qlen = Z_STRLEN_P(parameter);
- }
- newbuffer_len += plc->qlen;
- }
- rewrite:
- /* allocate output buffer */
- newbuffer = emalloc(newbuffer_len + 1);
- *outquery = newbuffer;
- /* and build the query */
- plc = placeholders;
- ptr = inquery;
- do {
- t = plc->pos - ptr;
- if (t) {
- memcpy(newbuffer, ptr, t);
- newbuffer += t;
- }
- memcpy(newbuffer, plc->quoted, plc->qlen);
- newbuffer += plc->qlen;
- ptr = plc->pos + plc->len;
- plc = plc->next;
- } while (plc);
- t = (inquery + inquery_len) - ptr;
- if (t) {
- memcpy(newbuffer, ptr, t);
- newbuffer += t;
- }
- *newbuffer = '\0';
- *outquery_len = newbuffer - *outquery;
- ret = 1;
- goto clean_up;
- } else if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
- /* rewrite ? to :pdoX */
- char *name, *idxbuf;
- const char *tmpl = stmt->named_rewrite_template ? stmt->named_rewrite_template : ":pdo%d";
- int bind_no = 1;
- newbuffer_len = inquery_len;
- if (stmt->bound_param_map == NULL) {
- ALLOC_HASHTABLE(stmt->bound_param_map);
- zend_hash_init(stmt->bound_param_map, 13, NULL, free_param_name, 0);
- }
- for (plc = placeholders; plc; plc = plc->next) {
- int skip_map = 0;
- char *p;
- name = estrndup(plc->pos, plc->len);
- /* check if bound parameter is already available */
- if (!strcmp(name, "?") || (p = zend_hash_str_find_ptr(stmt->bound_param_map, name, plc->len)) == NULL) {
- spprintf(&idxbuf, 0, tmpl, bind_no++);
- } else {
- idxbuf = estrdup(p);
- skip_map = 1;
- }
- plc->quoted = idxbuf;
- plc->qlen = strlen(plc->quoted);
- plc->freeq = 1;
- newbuffer_len += plc->qlen;
- if (!skip_map && stmt->named_rewrite_template) {
- /* create a mapping */
- zend_hash_str_update_mem(stmt->bound_param_map, name, plc->len, idxbuf, plc->qlen + 1);
- }
- /* map number to name */
- zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, idxbuf, plc->qlen + 1);
- efree(name);
- }
- goto rewrite;
- } else {
- /* rewrite :name to ? */
- newbuffer_len = inquery_len;
- if (stmt->bound_param_map == NULL) {
- ALLOC_HASHTABLE(stmt->bound_param_map);
- zend_hash_init(stmt->bound_param_map, 13, NULL, free_param_name, 0);
- }
- for (plc = placeholders; plc; plc = plc->next) {
- char *name;
- name = estrndup(plc->pos, plc->len);
- zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, name, plc->len + 1);
- efree(name);
- plc->quoted = "?";
- plc->qlen = 1;
- }
- goto rewrite;
- }
- clean_up:
- while (placeholders) {
- plc = placeholders;
- placeholders = plc->next;
- if (plc->freeq) {
- efree(plc->quoted);
- }
- efree(plc);
- }
- return ret;
- }
- /*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- * vim600: noet sw=4 ts=4 fdm=marker ft=c
- * vim<600: noet sw=4 ts=4
- */
|