static PHP_METHOD(SQLite, sqliteCreateAggregate) { struct pdo_sqlite_func *func; zval *step_callback, *fini_callback; char *func_name; size_t func_name_len; zend_long argc = -1; zend_string *cbname = NULL; pdo_dbh_t *dbh; pdo_sqlite_db_handle *H; int ret; ZEND_PARSE_PARAMETERS_START(3, 4) Z_PARAM_STRING(func_name, func_name_len) Z_PARAM_ZVAL_DEREF(step_callback) Z_PARAM_ZVAL_DEREF(fini_callback) Z_PARAM_OPTIONAL Z_PARAM_LONG(argc) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); dbh = Z_PDO_DBH_P(getThis()); PDO_CONSTRUCT_CHECK; if (!zend_is_callable(step_callback, 0, &cbname)) { php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname)); zend_string_release(cbname); RETURN_FALSE; } zend_string_release(cbname); if (!zend_is_callable(fini_callback, 0, &cbname)) { php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname)); zend_string_release(cbname); RETURN_FALSE; } zend_string_release(cbname); H = (pdo_sqlite_db_handle *)dbh->driver_data; func = (struct pdo_sqlite_func*)ecalloc(1, sizeof(*func)); ret = sqlite3_create_function(H->db, func_name, argc, SQLITE_UTF8, func, NULL, php_sqlite3_func_step_callback, php_sqlite3_func_final_callback); if (ret == SQLITE_OK) { func->funcname = estrdup(func_name); ZVAL_COPY(&func->step, step_callback); ZVAL_COPY(&func->fini, fini_callback); func->argc = argc; func->next = H->funcs; H->funcs = func; RETURN_TRUE; } efree(func); RETURN_FALSE; }
static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type) { zval *arg; #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) { RETURN_FALSE; } ZVAL_DEREF(arg); #else ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ZVAL_DEREF(arg) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #endif if (Z_TYPE_P(arg) == type) { if (type == IS_OBJECT) { zend_class_entry *ce = Z_OBJCE_P(arg); if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1 && !memcmp(ce->name->val, INCOMPLETE_CLASS, sizeof(INCOMPLETE_CLASS) - 1)) { RETURN_FALSE; } } else if (type == IS_RESOURCE) { const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg)); if (!type_name) { RETURN_FALSE; } } RETURN_TRUE; } else { RETURN_FALSE; } }
/* {{{ proto string json_encode(mixed data [, int options[, int depth]]) Returns the JSON representation of a value */ static PHP_FUNCTION(json_encode) { zval *parameter; php_json_encoder encoder; smart_str buf = {0}; zend_long options = 0; zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH; ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_ZVAL_DEREF(parameter) Z_PARAM_OPTIONAL Z_PARAM_LONG(options) Z_PARAM_LONG(depth) ZEND_PARSE_PARAMETERS_END(); php_json_encode_init(&encoder); encoder.max_depth = (int)depth; encoder.error_code = PHP_JSON_ERROR_NONE; php_json_encode_zval(&buf, parameter, (int)options, &encoder); JSON_G(error_code) = encoder.error_code; if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { smart_str_free(&buf); RETURN_FALSE; } smart_str_0(&buf); /* copy? */ if (buf.s) { RETURN_NEW_STR(buf.s); } RETURN_EMPTY_STRING(); }
static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */ { char *cmd; size_t cmd_len; zval *ret_code=NULL, *ret_array=NULL; int ret; ZEND_PARSE_PARAMETERS_START(1, (mode ? 2 : 3)) Z_PARAM_STRING(cmd, cmd_len) Z_PARAM_OPTIONAL if (!mode) { Z_PARAM_ZVAL_DEREF(ret_array) } Z_PARAM_ZVAL_DEREF(ret_code) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); if (!cmd_len) { php_error_docref(NULL, E_WARNING, "Cannot execute a blank command"); RETURN_FALSE; } if (strlen(cmd) != cmd_len) { php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack"); RETURN_FALSE; } if (!ret_array) { ret = php_exec(mode, cmd, NULL, return_value); } else { if (Z_TYPE_P(ret_array) != IS_ARRAY) { zval_ptr_dtor(ret_array); array_init(ret_array); } else if (Z_REFCOUNT_P(ret_array) > 1) { zval_ptr_dtor(ret_array); ZVAL_ARR(ret_array, zend_array_dup(Z_ARR_P(ret_array))); } ret = php_exec(2, cmd, ret_array, return_value); } if (ret_code) { zval_ptr_dtor(ret_code); ZVAL_LONG(ret_code, ret); } }
/* {{{ bool SQLite::sqliteCreateCollation(string name, mixed callback) Registers a collation with the sqlite db handle */ static PHP_METHOD(SQLite, sqliteCreateCollation) { struct pdo_sqlite_collation *collation; zval *callback; char *collation_name; size_t collation_name_len; zend_string *cbname = NULL; pdo_dbh_t *dbh; pdo_sqlite_db_handle *H; int ret; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STRING(collation_name, collation_name_len) Z_PARAM_ZVAL_DEREF(callback) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); dbh = Z_PDO_DBH_P(getThis()); PDO_CONSTRUCT_CHECK; if (!zend_is_callable(callback, 0, &cbname)) { php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname)); zend_string_release(cbname); RETURN_FALSE; } zend_string_release(cbname); H = (pdo_sqlite_db_handle *)dbh->driver_data; collation = (struct pdo_sqlite_collation*)ecalloc(1, sizeof(*collation)); ret = sqlite3_create_collation(H->db, collation_name, SQLITE_UTF8, collation, php_sqlite3_collation_callback); if (ret == SQLITE_OK) { collation->name = estrdup(collation_name); ZVAL_COPY(&collation->callback, callback); collation->next = H->collations; H->collations = collation; RETURN_TRUE; } efree(collation); RETURN_FALSE; }
static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type) { zval *arg; #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) { RETURN_FALSE; } ZVAL_DEREF(arg); #else ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ZVAL_DEREF(arg) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #endif if (Z_TYPE_P(arg) == type) { if (type == IS_OBJECT) { zend_class_entry *ce; if (Z_OBJ_HT_P(arg)->get_class_entry == NULL) { /* if there's no get_class_entry it's not a PHP object, so it can't be INCOMPLETE_CLASS */ RETURN_TRUE; } ce = Z_OBJCE_P(arg); if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1 && !strncmp(ce->name->val, INCOMPLETE_CLASS, ce->name->len)) { RETURN_FALSE; } } else if (type == IS_RESOURCE) { const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg) TSRMLS_CC); if (!type_name) { RETURN_FALSE; } } RETURN_TRUE; } else { RETURN_FALSE; } }
static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent) { char *host; size_t host_len; zend_long port = -1; zval *zerrno = NULL, *zerrstr = NULL; double timeout = (double)FG(default_socket_timeout); #ifndef PHP_WIN32 time_t conv; #else long conv; #endif struct timeval tv; char *hashkey = NULL; php_stream *stream = NULL; int err; char *hostname = NULL; size_t hostname_len; zend_string *errstr = NULL; RETVAL_FALSE; ZEND_PARSE_PARAMETERS_START(1, 5) Z_PARAM_STRING(host, host_len) Z_PARAM_OPTIONAL Z_PARAM_LONG(port) Z_PARAM_ZVAL_DEREF(zerrno) Z_PARAM_ZVAL_DEREF(zerrstr) Z_PARAM_DOUBLE(timeout) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); if (persistent) { spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port); } if (port > 0) { hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port); } else { hostname_len = host_len; hostname = host; } /* prepare the timeout value for use */ #ifndef PHP_WIN32 conv = (time_t) (timeout * 1000000.0); tv.tv_sec = conv / 1000000; #else conv = (long) (timeout * 1000000.0); tv.tv_sec = conv / 1000000; #endif tv.tv_usec = conv % 1000000; if (zerrno) { zval_ptr_dtor(zerrno); ZVAL_LONG(zerrno, 0); } if (zerrstr) { zval_ptr_dtor(zerrstr); ZVAL_EMPTY_STRING(zerrstr); } stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err); if (port > 0) { efree(hostname); } if (stream == NULL) { php_error_docref(NULL, E_WARNING, "unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr)); } if (hashkey) { efree(hashkey); } if (stream == NULL) { if (zerrno) { zval_ptr_dtor(zerrno); ZVAL_LONG(zerrno, err); } if (zerrstr && errstr) { /* no need to dup; we need to efree buf anyway */ zval_ptr_dtor(zerrstr); ZVAL_STR(zerrstr, errstr); } else if (!zerrstr && errstr) { zend_string_release_ex(errstr, 0); } RETURN_FALSE; } if (errstr) { zend_string_release_ex(errstr, 0); } php_stream_to_zval(stream, return_value); }