/** * @brief Checks whether @a index exists in array @a arr * @param arr Array * @param index Index * @return isset($arr[$index]) * @retval 0 Not exists, @a arr is not an array or @a index is of not supported type * @retval 1 Exists * @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer * @throw E_WARNING if @a offset is not a scalar */ int ZEPHIR_FASTCALL zephir_array_isset(const zval *arr, zval *index) { HashTable *h; if (Z_TYPE_P(arr) != IS_ARRAY) { return 0; } h = Z_ARRVAL_P(arr); switch (Z_TYPE_P(index)) { case IS_NULL: return zephir_hash_exists(h, ZEND_STRS("")); case IS_DOUBLE: return zend_hash_index_exists(h, (ulong)Z_DVAL_P(index)); case IS_BOOL: case IS_LONG: case IS_RESOURCE: return zend_hash_index_exists(h, Z_LVAL_P(index)); case IS_STRING: return zend_symtable_exists(h, Z_STRVAL_P(index), Z_STRLEN_P(index)+1); default: zend_error(E_WARNING, "Illegal offset type"); return 0; } }
/** * @brief Unsets @a index from array @a arr * @param[in,out] arr Array * @param index Index * @param flags Flags (@c PH_SEPARATE: separate array if its reference count is greater than 1; @c arr will contain the separated array) * @return Whether the operation succeeded * @retval @c FAILURE Failure, @a arr is not an array or @a index is of not supported type * @retval @c SUCCESS Success * @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer * @throw @c E_WARNING if @a offset is not a scalar */ int ZEPHIR_FASTCALL zephir_array_unset(zval **arr, zval *index, int flags) { HashTable *ht; if (Z_TYPE_PP(arr) != IS_ARRAY) { return FAILURE; } if ((flags & PH_SEPARATE) == PH_SEPARATE) { SEPARATE_ZVAL(arr); } ht = Z_ARRVAL_PP(arr); switch (Z_TYPE_P(index)) { case IS_NULL: return (zend_hash_del(ht, "", 1) == SUCCESS); case IS_DOUBLE: return (zend_hash_index_del(ht, (ulong)Z_DVAL_P(index)) == SUCCESS); case IS_LONG: case IS_BOOL: case IS_RESOURCE: return (zend_hash_index_del(ht, Z_LVAL_P(index)) == SUCCESS); case IS_STRING: return (zend_symtable_del(ht, Z_STRVAL_P(index), Z_STRLEN_P(index)+1) == SUCCESS); default: zend_error(E_WARNING, "Illegal offset type"); return 0; } }
static VALUE get(VALUE self, VALUE key) { zval **value; if(zend_hash_find(EG(active_symbol_table), StringValuePtr(key), RSTRING_LEN(key) + 1, (void **)&value) == SUCCESS) { switch(Z_TYPE_P(*value)) { case IS_NULL: return Qnil; break; case IS_BOOL: if(Z_BVAL_P(*value)) return Qtrue; else return Qfalse; break; case IS_LONG: return INT2NUM(Z_LVAL_P(*value)); break; case IS_DOUBLE: return rb_float_new(Z_DVAL_P(*value)); break; case IS_STRING: return rb_str_new(Z_STRVAL_P(*value), Z_STRLEN_P(*value)); break; default: rb_raise(rb_eRuntimeError, "Whoa, I don't know how to convert that"); } } return Qnil; }
static void zend_dump_const(const zval *zv) { switch (Z_TYPE_P(zv)) { case IS_NULL: fprintf(stderr, " null"); break; case IS_FALSE: fprintf(stderr, " bool(false)"); break; case IS_TRUE: fprintf(stderr, " bool(true)"); break; case IS_LONG: fprintf(stderr, " int(" ZEND_LONG_FMT ")", Z_LVAL_P(zv)); break; case IS_DOUBLE: fprintf(stderr, " float(%g)", Z_DVAL_P(zv)); break; case IS_STRING: fprintf(stderr, " string(\"%s\")", Z_STRVAL_P(zv)); break; case IS_ARRAY: fprintf(stderr, " array(...)"); break; default: fprintf(stderr, " zval(type=%d)", Z_TYPE_P(zv)); break; } }
/* * Convert a zval to a string containing a base(2-36) representation of * the number. */ PHPAPI char * _php_math_zvaltobase(zval *arg, int base TSRMLS_DC) { static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; if ((Z_TYPE_P(arg) != IS_LONG && Z_TYPE_P(arg) != IS_DOUBLE) || base < 2 || base > 36) { return STR_EMPTY_ALLOC(); } if (Z_TYPE_P(arg) == IS_DOUBLE) { double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */ char *ptr, *end; char buf[(sizeof(double) << 3) + 1]; /* Don't try to convert +/- infinity */ if (fvalue == HUGE_VAL || fvalue == -HUGE_VAL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number too large"); return STR_EMPTY_ALLOC(); } end = ptr = buf + sizeof(buf) - 1; *ptr = '\0'; do { *--ptr = digits[(int) fmod(fvalue, base)]; fvalue /= base; } while (ptr > buf && fabs(fvalue) >= 1); return estrndup(ptr, end - ptr); } return _php_math_longtobase(arg, base); }
inline void get_item_helper(zval* zvar, CSimpleTypeVar* var) { if(var) { switch(Z_TYPE_P(zvar)) { case IS_STRING: var->type = CSimpleTypeVar::VAR_STRING; var->sval.str = Z_STRVAL_P(zvar); var->sval.str_l = Z_STRLEN_P(zvar); break; case IS_DOUBLE: var->type = CSimpleTypeVar::VAR_FLOAT; var->fval = Z_DVAL_P(zvar); break; case IS_LONG: var->type = CSimpleTypeVar::VAR_LONG; var->lval = Z_LVAL_P(zvar); break; case IS_ARRAY: var->type = CSimpleTypeVar::VAR_ARRAY; var->aval = new CPHPArray(zvar); break; default: var->type = CSimpleTypeVar::VAR_NULL; }; }; };
BaseLib::PVariable PhpVariableConverter::getVariable(zval* value) { try { BaseLib::PVariable variable; if(!value) return variable; if(Z_TYPE_P(value) == IS_LONG) { variable.reset(new BaseLib::Variable((int32_t)Z_LVAL_P(value))); } else if(Z_TYPE_P(value) == IS_DOUBLE) { variable.reset(new BaseLib::Variable((double)Z_DVAL_P(value))); } else if(Z_TYPE_P(value) == IS_TRUE) { variable.reset(new BaseLib::Variable(true)); } else if(Z_TYPE_P(value) == IS_FALSE) { variable.reset(new BaseLib::Variable(false)); } else if(Z_TYPE_P(value) == IS_STRING) { if(Z_STRLEN_P(value) > 0) variable.reset(new BaseLib::Variable(std::string(Z_STRVAL_P(value), Z_STRLEN_P(value)))); else variable.reset(new BaseLib::Variable(std::string(""))); } else if(Z_TYPE_P(value) == IS_ARRAY) { zval* element = nullptr; HashTable* ht = Z_ARRVAL_P(value); zend_string* key = nullptr; ulong keyIndex = 0; ZEND_HASH_FOREACH_KEY_VAL(ht, keyIndex, key, element) { if(!element) continue; if(!variable) { if(key) variable.reset(new BaseLib::Variable(BaseLib::VariableType::tStruct)); else variable.reset(new BaseLib::Variable(BaseLib::VariableType::tArray)); } BaseLib::PVariable arrayElement = getVariable(element); if(!arrayElement) continue; if(key) { std::string keyName; keyName = std::string(key->val, key->len); variable->structValue->insert(BaseLib::StructElement(keyName, arrayElement)); } else variable->arrayValue->push_back(arrayElement); } ZEND_HASH_FOREACH_END(); } else { variable.reset(new BaseLib::Variable(std::string(""))); } return variable; }
static PHP_METHOD(swoole_table, set) { zval *array; char *key; zend_size_t keylen; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &key, &keylen, &array) == FAILURE) { RETURN_FALSE; } swTable *table = swoole_get_object(getThis()); if (!table->memory) { swoole_php_fatal_error(E_ERROR, "Must create table first."); RETURN_FALSE; } sw_atomic_t *_lock = NULL; swTableRow *row = swTableRow_set(table, key, keylen, &_lock); if (!row) { sw_spinlock_release(_lock); swoole_php_error(E_WARNING, "Unable to allocate memory."); RETURN_FALSE; } swTableColumn *col; zval *v; char *k; uint32_t klen; int ktype; HashTable *_ht = Z_ARRVAL_P(array); SW_HASHTABLE_FOREACH_START2(_ht, k, klen, ktype, v) { col = swTableColumn_get(table, k, klen); if (k == NULL || col == NULL) { continue; } else if (col->type == SW_TABLE_STRING) { convert_to_string(v); swTableRow_set_value(row, col, Z_STRVAL_P(v), Z_STRLEN_P(v)); } else if (col->type == SW_TABLE_FLOAT) { convert_to_double(v); swTableRow_set_value(row, col, &Z_DVAL_P(v), 0); } else { convert_to_long(v); swTableRow_set_value(row, col, &Z_LVAL_P(v), 0); } }
void inspect(zval* value) { switch (Z_TYPE_P(value)) { case IS_NULL: php_printf("The variable passed is of type IS_NULL\n"); break; case IS_LONG: php_printf("The variable passed is of type IS_LONG\n"); php_printf("The content is %ld\n", Z_LVAL_P(value)); break; case IS_DOUBLE: php_printf("The variable passed is of type IS_DOUBLE\n"); php_printf("The content is %f\n", Z_DVAL_P(value)); break; #if PHP_VERSION_ID >= 70000 case IS_TRUE: php_printf("The variable passed is of type IS_TRUE\n"); break; case IS_FALSE: php_printf("The variable passed is of type IS_FALSE\n"); break; #else case IS_BOOL: php_printf("The variable passed is of type IS_BOOL\n"); break; #endif case IS_OBJECT: php_printf("The variable passed is of type IS_OBJECT\n"); break; case IS_STRING: php_printf("The variable passed is of type IS_STRING\n"); php_printf("The content is \"%s\"\n", Z_STRVAL_P(value)); php_printf("The content reversed is \""); char *p = Z_STRVAL_P(value) + Z_STRLEN_P(value); while (--p >= Z_STRVAL_P(value)) php_printf("%c", *p); php_printf("\"\n"); break; case IS_RESOURCE: php_printf("The variable is of type IS_RESOURCE\n"); php_printf("The number of resource is: %ld\n", #if PHP_VERSION_ID >= 70000 Z_RES_HANDLE_P(value) #else Z_RESVAL_P(value) #endif ); break; case IS_ARRAY: php_printf("The variable passed is type of IS_ARRAY\n"); php_printf("There is %ld direct elements in the array\n", zend_hash_num_elements(Z_ARRVAL_P(value))); break; default: php_printf("unkown type"); break; } }
/** * @brief Fetches @a index if it exists from the array @a arr * @param[out] fetched <code>&$arr[$index]</code>; @a fetched is modified only when the function returns 1 * @param arr Array * @param index Index * @return isset($arr[$index]) * @retval 0 Not exists, @a arr is not an array or @a index is of not supported type * @retval 1 Exists * @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer * @note $arr[$index] is returned as is: no copying occurs, reference copunt is not updated * @throw E_WARNING if @a offset is not a scalar */ int zephir_array_isset_fetch(zval **fetched, const zval *arr, zval *index, int readonly TSRMLS_DC) { HashTable *h; zval **val; int result; if (Z_TYPE_P(arr) != IS_ARRAY) { *fetched = ZEPHIR_GLOBAL(global_null); if (!readonly) { Z_ADDREF_P(*fetched); } return 0; } h = Z_ARRVAL_P(arr); switch (Z_TYPE_P(index)) { case IS_NULL: result = zephir_hash_find(h, SS(""), (void**)&val); break; case IS_DOUBLE: result = zend_hash_index_find(h, (ulong)Z_DVAL_P(index), (void**)&val); break; case IS_LONG: case IS_BOOL: case IS_RESOURCE: result = zend_hash_index_find(h, Z_LVAL_P(index), (void**)&val); break; case IS_STRING: result = zend_symtable_find(h, (Z_STRLEN_P(index) ? Z_STRVAL_P(index) : ""), Z_STRLEN_P(index)+1, (void**)&val); break; default: zend_error(E_WARNING, "Illegal offset type"); *fetched = ZEPHIR_GLOBAL(global_null); if (!readonly) { Z_ADDREF_P(*fetched); } return 0; } if (result == SUCCESS) { *fetched = *val; if (!readonly) { Z_ADDREF_P(*fetched); } return 1; } *fetched = ZEPHIR_GLOBAL(global_null); if (!readonly) { Z_ADDREF_P(*fetched); } return 0; }
static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */ { /* the trivial way would be to do * convert_to_string_ex(arg); * append it and kill the now tmp arg. * but that could cause some E_NOTICE and also damn long lines. */ ZVAL_DEREF(arg); switch (Z_TYPE_P(arg)) { case IS_NULL: smart_str_appends(str, "NULL, "); break; case IS_STRING: smart_str_appendc(str, '\''); smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), 15)); if (Z_STRLEN_P(arg) > 15) { smart_str_appends(str, "...', "); } else { smart_str_appends(str, "', "); } break; case IS_FALSE: smart_str_appends(str, "false, "); break; case IS_TRUE: smart_str_appends(str, "true, "); break; case IS_RESOURCE: smart_str_appends(str, "Resource id #"); smart_str_append_long(str, Z_RES_HANDLE_P(arg)); smart_str_appends(str, ", "); break; case IS_LONG: smart_str_append_long(str, Z_LVAL_P(arg)); smart_str_appends(str, ", "); break; case IS_DOUBLE: { double dval = Z_DVAL_P(arg); char *s_tmp = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1); int l_tmp = zend_sprintf(s_tmp, "%.*G", (int) EG(precision), dval); /* SAFE */ smart_str_appendl(str, s_tmp, l_tmp); smart_str_appends(str, ", "); efree(s_tmp); break; } case IS_ARRAY: smart_str_appends(str, "Array, "); break; case IS_OBJECT: smart_str_appends(str, "Object("); smart_str_appends(str, ZSTR_VAL(Z_OBJCE_P(arg)->name)); smart_str_appends(str, "), "); break; } }
VALUE php_value_to_float(VALUE self) { zval *zv = get_zval(self); if (zv) { convert_to_double(zv); return rb_float_new(Z_DVAL_P(zv)); } return Qnil; }
// extern void TF_SetAttrFloatList(TF_OperationDescription* desc, // const char* attr_name, const float* values, // int num_values); static PHP_METHOD(TensorFlow_OperationDescription, setAttrFloatList) { zend_string *name; zval* values; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(name) Z_PARAM_ARRAY(values) ZEND_PARSE_PARAMETERS_END(); float* tf_values = NULL; int tf_num_values = 0; HashTable *values_table = Z_ARRVAL_P(values); tf_num_values = zend_hash_num_elements(values_table); // count of array if (tf_num_values > 0) { tf_values = (float*)emalloc(sizeof(float) * tf_num_values); HashPosition pos; zval* element; int index = 0; zend_hash_internal_pointer_reset_ex(values_table, &pos); while (zend_hash_has_more_elements_ex(values_table, &pos) == SUCCESS) { if (!(element = zend_hash_get_current_data_ex(values_table, &pos))) { zend_throw_exception(spl_ce_InvalidArgumentException, "values something wrong", 0); return; } if (zval_get_type(element) != IS_DOUBLE) { zend_throw_exception(spl_ce_InvalidArgumentException, "values must be array of float", 0); return; } // insert tf_values tf_values[index] = Z_DVAL_P(element); // php_printf("%d \n", element->value.lval); zend_hash_move_forward_ex(values_table, &pos); index++; } } // int i; // for (i = 0; i < tf_num_values; i++) { // php_printf("values[%f] ? %d\n", i, tf_values[i]); // } // php_printf("tf_num_values ? %d\n", tf_num_values); // this t_tf_operation_description_object* intern = TF_OPERATION_DESCRIPTION_P_ZV(getThis()); t_tf_operation_description* node = intern->ptr; TF_SetAttrFloatList(node->src, name->val, tf_values, tf_num_values); }
static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */ { /* the trivial way would be to do * convert_to_string_ex(arg); * append it and kill the now tmp arg. * but that could cause some E_NOTICE and also damn long lines. */ ZVAL_DEREF(arg); switch (Z_TYPE_P(arg)) { case IS_NULL: smart_str_appends(str, "NULL, "); break; case IS_STRING: smart_str_appendc(str, '\''); smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), 15)); if (Z_STRLEN_P(arg) > 15) { smart_str_appends(str, "...', "); } else { smart_str_appends(str, "', "); } break; case IS_FALSE: smart_str_appends(str, "false, "); break; case IS_TRUE: smart_str_appends(str, "true, "); break; case IS_RESOURCE: smart_str_appends(str, "Resource id #"); smart_str_append_long(str, Z_RES_HANDLE_P(arg)); smart_str_appends(str, ", "); break; case IS_LONG: smart_str_append_long(str, Z_LVAL_P(arg)); smart_str_appends(str, ", "); break; case IS_DOUBLE: { smart_str_append_printf(str, "%.*G", (int) EG(precision), Z_DVAL_P(arg)); smart_str_appends(str, ", "); break; } case IS_ARRAY: smart_str_appends(str, "Array, "); break; case IS_OBJECT: { zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg)); smart_str_appends(str, "Object("); smart_str_appends(str, ZSTR_VAL(class_name)); smart_str_appends(str, "), "); zend_string_release_ex(class_name, 0); break; } } }
/** * @brief Updates value in @a arr at position @a index with @a value * @param[in,out] arr Array * @param index Index * @param[in,out] value Value * @param flags Flags * @return Whether the operation succeeded * @retval @c FAILURE Failure, @a arr is not an array or @a index is of not supported type * @retval @c SUCCESS Success * @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer * @throw @c E_WARNING if @a offset is not a scalar or @c arr is not an array * * Equivalent to <tt>$arr[$index] = $value</tt> in PHP. * Flags may be a bitwise OR of the following values: * @arg @c PH_CTOR: create a copy of @a value and work with that copy; @c *value will be updated with the newly constructed value * @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version * @arg @c PH_COPY: increment the reference count on @c **value */ int zephir_array_update_zval(zval **arr, zval *index, zval **value, int flags) { HashTable *ht; if (Z_TYPE_PP(arr) != IS_ARRAY) { zend_error(E_WARNING, "Cannot use a scalar value as an array (2)"); return FAILURE; } if ((flags & PH_CTOR) == PH_CTOR) { zval *new_zv; Z_DELREF_PP(value); ALLOC_ZVAL(new_zv); INIT_PZVAL_COPY(new_zv, *value); *value = new_zv; zval_copy_ctor(new_zv); } if ((flags & PH_SEPARATE) == PH_SEPARATE) { SEPARATE_ZVAL_IF_NOT_REF(arr); } if ((flags & PH_COPY) == PH_COPY) { Z_ADDREF_PP(value); } ht = Z_ARRVAL_PP(arr); switch (Z_TYPE_P(index)) { case IS_NULL: return zend_symtable_update(ht, "", 1, value, sizeof(zval*), NULL); case IS_DOUBLE: return zend_hash_index_update(ht, (ulong)Z_DVAL_P(index), value, sizeof(zval*), NULL); case IS_LONG: case IS_BOOL: case IS_RESOURCE: return zend_hash_index_update(ht, Z_LVAL_P(index), value, sizeof(zval*), NULL); case IS_STRING: return zend_symtable_update(ht, Z_STRVAL_P(index), Z_STRLEN_P(index)+1, value, sizeof(zval*), NULL); default: zend_error(E_WARNING, "Illegal offset type"); return FAILURE; } }
static int /* O - Number of options */ cups_convert_options( zval *optionsobj, /* I - Options array object */ cups_option_t **options) /* O - Options */ { int num_options; /* Number of options */ HashTable *ht; /* Option array hash table */ Bucket *current; /* Current element in array */ zval *value; /* Current value in array */ char temp[255]; /* String value for numbers */ ht = Z_ARRVAL_P(optionsobj); num_options = 0; for (current = ht->pListHead; current; current = current->pListNext) { value = (zval *)current->pDataPtr; switch (Z_TYPE_P(value)) { case IS_LONG : sprintf(temp, "%ld", Z_LVAL_P(value)); num_options = cupsAddOption(current->arKey, temp, num_options, options); break; case IS_DOUBLE : sprintf(temp, "%g", Z_DVAL_P(value)); num_options = cupsAddOption(current->arKey, temp, num_options, options); break; case IS_BOOL : num_options = cupsAddOption(current->arKey, Z_BVAL_P(value) ? "true" : "false", num_options, options); break; case IS_STRING : num_options = cupsAddOption(current->arKey, Z_STRVAL_P(value), num_options, options); break; } } return (num_options); }
PHP_METHOD(Test_Oo_OoParams, setStrictAverage) { zval *average_param = NULL; double average; zephir_fetch_params(0, 1, 0, &average_param); if (unlikely(Z_TYPE_P(average_param) != IS_DOUBLE)) { zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'average' must be a double") TSRMLS_CC); RETURN_NULL(); } average = Z_DVAL_P(average_param); RETURN_DOUBLE(average); }
static PHP_FUNCTION(params_dump) { zval *arg;//定义参数值,zval可以表示所有的参数的值 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) { return; } switch(Z_TYPE_P(arg)) { case IS_NULL://arg is null php_printf("NULL\n"); break; case IS_BOOL://arg is true, false php_printf("bool(%s)\n", Z_BVAL_P(arg)?"true":"false"); break; case IS_LONG: php_printf("int(%d)\n", Z_LVAL_P(arg)); break; case IS_DOUBLE: php_printf("float(%f)\n", Z_DVAL_P(arg)); break; case IS_STRING: php_printf("string(%d)\n", Z_STRLEN_P(arg)); PHPWRITE(Z_STRVAL_P(arg), Z_STRLEN_P(arg)); php_printf("\n"); break; case IS_ARRAY: php_printf("array(%d){...}\n", zend_hash_num_elements(Z_ARRVAL_P(arg))); break; case IS_RESOURCE:{ const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RESVAL_P(arg) TSRMLS_CC); php_printf("resource#%ld(%s)\n", Z_RESVAL_P(arg), type_name?type_name:"Unknown"); break; } case IS_OBJECT:{ const zend_class_entry *ce = Z_OBJCE_P(arg); php_printf("object#%u(%s)\n", Z_OBJ_HANDLE_P(arg), (ce && ce->name)?ce->name:"Unknown"); break; } default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown type: %d\n", Z_TYPE_P(arg)); } }
ZEND_METHOD(Vedis, decrby) { char *key; int key_len; zval *zv; VEDIS_ARGS_PARAM(DECRBY, 6, 2); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &key, &key_len, &zv) == FAILURE) { return; } VEDIS_SELF(intern); VEDIS_ARGS_STRING(key, key_len); switch (Z_TYPE_P(zv)) { case IS_LONG: VEDIS_ARGS_INT(Z_LVAL_P(zv)); break; case IS_DOUBLE: VEDIS_ARGS_DOUBLE(Z_DVAL_P(zv)); break; case IS_STRING: if (is_numeric_string(Z_STRVAL_P(zv), Z_STRLEN_P(zv), NULL, NULL, 0)) { VEDIS_ARGS_STRING(Z_STRVAL_P(zv), Z_STRLEN_P(zv)); } else { RETURN_FALSE; } break; default: RETURN_FALSE; break; } VEDIS_ARGS_EXEC(RETURN_FALSE); VEDIS_RETURN_LONG(); }
static double microtime(TSRMLS_D) { zval *t; zval *func,*return_as_double; zval **args[1]; double dt; MAKE_STD_ZVAL(func); ZVAL_STRING(func, "microtime", 0); MAKE_STD_ZVAL(return_as_double); ZVAL_BOOL(return_as_double, 1); args[0]=&return_as_double; if(call_user_function_ex(EG(function_table), NULL, func, &t, 1, args, 0,NULL TSRMLS_CC) != SUCCESS) { dt=0; } else { dt=Z_DVAL_P(t); } FREE_ZVAL(func); FREE_ZVAL(t); FREE_ZVAL(return_as_double); return dt; }
PHPAPI zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */ { zend_ulong idx; switch (Z_TYPE_P(offset)) { case IS_STRING: if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), idx)) { return idx; } break; case IS_DOUBLE: return (zend_long)Z_DVAL_P(offset); case IS_LONG: return Z_LVAL_P(offset); case IS_FALSE: return 0; case IS_TRUE: return 1; case IS_RESOURCE: return Z_RES_HANDLE_P(offset); } return -1; }
PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */ { HashTable *myht; zend_string *class_name; int is_temp; int is_ref = 0; zend_ulong num; zend_string *key; zval *val; uint32_t count; if (level > 1) { php_printf("%*c", level - 1, ' '); } again: switch (Z_TYPE_P(struc)) { case IS_FALSE: php_printf("%sbool(false)\n", COMMON); break; case IS_TRUE: php_printf("%sbool(true)\n", COMMON); break; case IS_NULL: php_printf("%sNULL\n", COMMON); break; case IS_LONG: php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc)); break; case IS_DOUBLE: php_printf("%sfloat(%.*G)\n", COMMON, (int) EG(precision), Z_DVAL_P(struc)); break; case IS_STRING: php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc)); PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc)); PUTS("\"\n"); break; case IS_ARRAY: myht = Z_ARRVAL_P(struc); if (level > 1 && ZEND_HASH_APPLY_PROTECTION(myht) && ++myht->u.v.nApplyCount > 1) { PUTS("*RECURSION*\n"); --myht->u.v.nApplyCount; return; } count = zend_array_count(myht); php_printf("%sarray(%d) {\n", COMMON, count); is_temp = 0; ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) { php_array_element_dump(val, num, key, level); } ZEND_HASH_FOREACH_END(); if (level > 1 && ZEND_HASH_APPLY_PROTECTION(myht)) { --myht->u.v.nApplyCount; } if (is_temp) { zend_hash_destroy(myht); efree(myht); } if (level > 1) { php_printf("%*c", level-1, ' '); } PUTS("}\n"); break; case IS_OBJECT: if (Z_OBJ_APPLY_COUNT_P(struc) > 0) { PUTS("*RECURSION*\n"); return; } Z_OBJ_INC_APPLY_COUNT_P(struc); myht = Z_OBJDEBUG_P(struc, is_temp); class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc)); php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0); zend_string_release(class_name); if (myht) { zend_ulong num; zend_string *key; zval *val; ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) { php_object_property_dump(val, num, key, level); } ZEND_HASH_FOREACH_END(); if (is_temp) { zend_hash_destroy(myht); efree(myht); } }
static int yac_add_impl(char *prefix, uint prefix_len, char *key, uint len, zval *value, int ttl, int add TSRMLS_DC) /* {{{ */ { int ret = 0, flag = Z_TYPE_P(value); char *msg, buf[YAC_STORAGE_MAX_KEY_LEN]; if ((len + prefix_len) > YAC_STORAGE_MAX_KEY_LEN) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key%s can not be longer than %d bytes", prefix_len? "(include prefix)" : "", YAC_STORAGE_MAX_KEY_LEN); return ret; } if (prefix_len) { len = snprintf(buf, sizeof(buf), "%s%s", prefix, key); key = (char *)buf; } switch (Z_TYPE_P(value)) { case IS_NULL: ret = yac_storage_update(key, len, (char *)&flag, sizeof(int), flag, add, ttl); break; case IS_BOOL: case IS_LONG: ret = yac_storage_update(key, len, (char *)&Z_LVAL_P(value), sizeof(long), flag, add, ttl); break; case IS_DOUBLE: ret = yac_storage_update(key, len, (char *)&Z_DVAL_P(value), sizeof(double), flag, add, ttl); break; case IS_STRING: case IS_CONSTANT: { if (Z_STRLEN_P(value) > YAC_G(compress_threshold) || Z_STRLEN_P(value) > YAC_STORAGE_MAX_ENTRY_LEN) { int compressed_len; char *compressed; /* if longer than this, then we can not stored the length in flag */ if (Z_STRLEN_P(value) > YAC_ENTRY_MAX_ORIG_LEN) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Value is too long(%d bytes) to be stored", Z_STRLEN_P(value)); return ret; } compressed = emalloc(Z_STRLEN_P(value) * 1.05); compressed_len = fastlz_compress(Z_STRVAL_P(value), Z_STRLEN_P(value), compressed); if (!compressed_len || compressed_len > Z_STRLEN_P(value)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Compression failed"); efree(compressed); return ret; } if (compressed_len > YAC_G(compress_threshold) || compressed_len > YAC_STORAGE_MAX_ENTRY_LEN) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Value is too long(%d bytes) to be stored", Z_STRLEN_P(value)); efree(compressed); return ret; } flag |= YAC_ENTRY_COMPRESSED; flag |= (Z_STRLEN_P(value) << YAC_ENTRY_ORIG_LEN_SHIT); ret = yac_storage_update(key, len, compressed, compressed_len, flag, ttl, add); efree(compressed); } else { ret = yac_storage_update(key, len, Z_STRVAL_P(value), Z_STRLEN_P(value), flag, ttl, add); } } break; case IS_ARRAY: case IS_CONSTANT_ARRAY: case IS_OBJECT: { smart_str buf = {0}; if (yac_serializer_php_pack(value, &buf, &msg TSRMLS_CC)) { if (buf.len > YAC_G(compress_threshold) || buf.len > YAC_STORAGE_MAX_ENTRY_LEN) { int compressed_len; char *compressed; if (buf.len > YAC_ENTRY_MAX_ORIG_LEN) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Value is too big to be stored"); return ret; } compressed = emalloc(buf.len * 1.05); compressed_len = fastlz_compress(buf.c, buf.len, compressed); if (!compressed_len || compressed_len > buf.len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Compression failed"); efree(compressed); return ret; } if (compressed_len > YAC_G(compress_threshold) || compressed_len > YAC_STORAGE_MAX_ENTRY_LEN) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Value is too big to be stored"); efree(compressed); return ret; } flag |= YAC_ENTRY_COMPRESSED; flag |= (buf.len << YAC_ENTRY_ORIG_LEN_SHIT); ret = yac_storage_update(key, len, compressed, compressed_len, flag, ttl, add); efree(compressed); } else { ret = yac_storage_update(key, len, buf.c, buf.len, flag, ttl, add); } smart_str_free(&buf); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Serialization failed"); smart_str_free(&buf); } } break; case IS_RESOURCE: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type 'IS_RESOURCE' cannot be stored"); break; default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported valued type to be stored '%d'", flag); break; } return ret; }
/* {{{ mysqlnd_stmt_execute_store_params */ static void mysqlnd_stmt_execute_store_params(MYSQLND_STMT * s, zend_uchar **buf, zend_uchar **p, size_t *buf_len, unsigned int null_byte_offset TSRMLS_DC) { MYSQLND_STMT_DATA * stmt = s->data; unsigned int i = 0; size_t left = (*buf_len - (*p - *buf)); size_t data_size = 0; zval **copies = NULL;/* if there are different types */ /* 1. Store type information */ if (stmt->send_types_to_server) { /* 2 bytes per type, and leave 20 bytes for future use */ if (left < ((stmt->param_count * 2) + 20)) { unsigned int offset = *p - *buf; zend_uchar *tmp_buf; *buf_len = offset + stmt->param_count * 2 + 20; tmp_buf = mnd_emalloc(*buf_len); memcpy(tmp_buf, *buf, offset); *buf = tmp_buf; /* Update our pos pointer */ *p = *buf + offset; } for (i = 0; i < stmt->param_count; i++) { /* our types are not unsigned */ #if SIZEOF_LONG==8 if (stmt->param_bind[i].type == MYSQL_TYPE_LONG) { stmt->param_bind[i].type = MYSQL_TYPE_LONGLONG; } #endif int2store(*p, stmt->param_bind[i].type); *p+= 2; } } /* 2. Store data */ /* 2.1 Calculate how much space we need */ for (i = 0; i < stmt->param_count; i++) { unsigned int j; zval *the_var = stmt->param_bind[i].zv; if (!the_var || (stmt->param_bind[i].type != MYSQL_TYPE_LONG_BLOB && Z_TYPE_P(the_var) == IS_NULL)) { continue; } for (j = i + 1; j < stmt->param_count; j++) { if (stmt->param_bind[j].zv == the_var) { /* Double binding of the same zval, make a copy */ mysqlnd_stmt_copy_it(&copies, the_var, stmt->param_count, i TSRMLS_CC); break; } } switch (stmt->param_bind[i].type) { case MYSQL_TYPE_DOUBLE: data_size += 8; if (Z_TYPE_P(the_var) != IS_DOUBLE) { if (!copies || !copies[i]) { mysqlnd_stmt_copy_it(&copies, the_var, stmt->param_count, i TSRMLS_CC); } } break; #if SIZEOF_LONG==8 case MYSQL_TYPE_LONGLONG: data_size += 8; #elif SIZEOF_LONG==4 case MYSQL_TYPE_LONG: data_size += 4; #else #error "Should not happen" #endif if (Z_TYPE_P(the_var) != IS_LONG) { if (!copies || !copies[i]) { mysqlnd_stmt_copy_it(&copies, the_var, stmt->param_count, i TSRMLS_CC); } } break; case MYSQL_TYPE_LONG_BLOB: if (!(stmt->param_bind[i].flags & MYSQLND_PARAM_BIND_BLOB_USED)) { /* User hasn't sent anything, we will send empty string. Empty string has length of 0, encoded in 1 byte. No real data will follows after it. */ data_size++; } break; case MYSQL_TYPE_VAR_STRING: data_size += 8; /* max 8 bytes for size */ #if PHP_MAJOR_VERSION < 6 if (Z_TYPE_P(the_var) != IS_STRING) #elif PHP_MAJOR_VERSION >= 6 if (Z_TYPE_P(the_var) != IS_STRING || Z_TYPE_P(the_var) == IS_UNICODE) #endif { if (!copies || !copies[i]) { mysqlnd_stmt_copy_it(&copies, the_var, stmt->param_count, i TSRMLS_CC); } the_var = copies[i]; #if PHP_MAJOR_VERSION >= 6 if (Z_TYPE_P(the_var) == IS_UNICODE) { zval_unicode_to_string_ex(the_var, UG(utf8_conv) TSRMLS_CC); } #endif } convert_to_string_ex(&the_var); data_size += Z_STRLEN_P(the_var); break; } } /* 2.2 Enlarge the buffer, if needed */ left = (*buf_len - (*p - *buf)); if (left < data_size) { unsigned int offset = *p - *buf; zend_uchar *tmp_buf; *buf_len = offset + data_size + 10; /* Allocate + 10 for safety */ tmp_buf = mnd_emalloc(*buf_len); memcpy(tmp_buf, *buf, offset); *buf = tmp_buf; /* Update our pos pointer */ *p = *buf + offset; } /* 2.3 Store the actual data */ for (i = 0; i < stmt->param_count; i++) { zval *data = copies && copies[i]? copies[i]: stmt->param_bind[i].zv; /* Handle long data */ if (stmt->param_bind[i].zv && Z_TYPE_P(data) == IS_NULL) { (*buf + null_byte_offset)[i/8] |= (zend_uchar) (1 << (i & 7)); } else { switch (stmt->param_bind[i].type) { case MYSQL_TYPE_DOUBLE: convert_to_double_ex(&data); float8store(*p, Z_DVAL_P(data)); (*p) += 8; break; #if SIZEOF_LONG==8 case MYSQL_TYPE_LONGLONG: convert_to_long_ex(&data); int8store(*p, Z_LVAL_P(data)); (*p) += 8; break; #elif SIZEOF_LONG==4 case MYSQL_TYPE_LONG: convert_to_long_ex(&data); int4store(*p, Z_LVAL_P(data)); (*p) += 4; break; #else #error "Should not happen" #endif case MYSQL_TYPE_LONG_BLOB: if (stmt->param_bind[i].flags & MYSQLND_PARAM_BIND_BLOB_USED) { stmt->param_bind[i].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED; } else { /* send_long_data() not called, send empty string */ *p = php_mysqlnd_net_store_length(*p, 0); } break; case MYSQL_TYPE_VAR_STRING:{ unsigned int len = Z_STRLEN_P(data); /* to is after p. The latter hasn't been moved */ *p = php_mysqlnd_net_store_length(*p, len); memcpy(*p, Z_STRVAL_P(data), len); (*p) += len; } break; default: /* Won't happen, but set to NULL */ (*buf + null_byte_offset)[i/8] |= (zend_uchar) (1 << (i & 7)); break; } } } if (copies) { for (i = 0; i < stmt->param_count; i++) { if (copies[i]) { zval_ptr_dtor(&copies[i]); } } mnd_efree(copies); } }
static PHP_METHOD(swoole_redis, __construct) { zval *zset = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &zset) == FAILURE) { return; } swRedisClient *redis = emalloc(sizeof(swRedisClient)); bzero(redis, sizeof(swRedisClient)); redis->object = getThis(); redis->timeout = SW_REDIS_CONNECT_TIMEOUT; redis->database = -1; if (zset && !ZVAL_IS_NULL(zset)) { php_swoole_array_separate(zset); zend_update_property(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("setting"), zset TSRMLS_CC); sw_zval_ptr_dtor(&zset); HashTable *vht; zval *ztmp; vht = Z_ARRVAL_P(zset); /** * timeout */ if (php_swoole_array_get_value(vht, "timeout", ztmp)) { convert_to_double(ztmp); redis->timeout = (double) Z_DVAL_P(ztmp); } /** * password */ if (php_swoole_array_get_value(vht, "password", ztmp)) { convert_to_string(ztmp); if (Z_STRLEN_P(ztmp) >= 1 << 8) { swoole_php_fatal_error(E_WARNING, "redis password is too long."); } else if (Z_STRLEN_P(ztmp) > 0) { redis->password = estrdup(Z_STRVAL_P(ztmp)); redis->password_len = Z_STRLEN_P(ztmp); } } /** * database */ if (php_swoole_array_get_value(vht, "database", ztmp)) { convert_to_long(ztmp); if (Z_LVAL_P(ztmp) > 1 << 8) { swoole_php_fatal_error(E_WARNING, "redis database number is too big."); } else { redis->database = (int8_t) Z_LVAL_P(ztmp); } } } sw_copy_to_stack(redis->object, redis->_object); swoole_set_object(getThis(), redis); }
/* php_formatted_print() {{{ * New sprintf implementation for PHP. * * Modifiers: * * " " pad integers with spaces * "-" left adjusted field * n field size * "."n precision (floats only) * "+" Always place a sign (+ or -) in front of a number * * Type specifiers: * * "%" literal "%", modifiers are ignored. * "b" integer argument is printed as binary * "c" integer argument is printed as a single character * "d" argument is an integer * "f" the argument is a float * "o" integer argument is printed as octal * "s" argument is a string * "x" integer argument is printed as lowercase hexadecimal * "X" integer argument is printed as uppercase hexadecimal * */ static char * php_formatted_print(int ht, int *len, int use_array, int format_offset TSRMLS_DC) { zval ***args, **z_format; int argc, size = 240, inpos = 0, outpos = 0, temppos; int alignment, currarg, adjusting, argnum, width, precision; char *format, *result, padding; int always_sign; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return NULL; } /* verify the number of args */ if ((use_array && argc != (2 + format_offset)) || (!use_array && argc < (1 + format_offset))) { efree(args); WRONG_PARAM_COUNT_WITH_RETVAL(NULL); } if (use_array) { int i = 1; zval ***newargs; zval **array; z_format = args[format_offset]; array = args[1 + format_offset]; SEPARATE_ZVAL(array); convert_to_array_ex(array); argc = 1 + zend_hash_num_elements(Z_ARRVAL_PP(array)); newargs = (zval ***)safe_emalloc(argc, sizeof(zval *), 0); newargs[0] = z_format; for (zend_hash_internal_pointer_reset(Z_ARRVAL_PP(array)); zend_hash_get_current_data(Z_ARRVAL_PP(array), (void **)&newargs[i++]) == SUCCESS; zend_hash_move_forward(Z_ARRVAL_PP(array))); efree(args); args = newargs; format_offset = 0; } convert_to_string_ex(args[format_offset]); format = Z_STRVAL_PP(args[format_offset]); result = emalloc(size); currarg = 1; while (inpos<Z_STRLEN_PP(args[format_offset])) { int expprec = 0, multiuse = 0; zval *tmp; PRINTF_DEBUG(("sprintf: format[%d]='%c'\n", inpos, format[inpos])); PRINTF_DEBUG(("sprintf: outpos=%d\n", outpos)); if (format[inpos] != '%') { php_sprintf_appendchar(&result, &outpos, &size, format[inpos++] TSRMLS_CC); } else if (format[inpos + 1] == '%') { php_sprintf_appendchar(&result, &outpos, &size, '%' TSRMLS_CC); inpos += 2; } else { /* starting a new format specifier, reset variables */ alignment = ALIGN_RIGHT; adjusting = 0; padding = ' '; always_sign = 0; inpos++; /* skip the '%' */ PRINTF_DEBUG(("sprintf: first looking at '%c', inpos=%d\n", format[inpos], inpos)); if (isascii((int)format[inpos]) && !isalpha((int)format[inpos])) { /* first look for argnum */ temppos = inpos; while (isdigit((int)format[temppos])) temppos++; if (format[temppos] == '$') { argnum = php_sprintf_getnumber(format, &inpos); if (argnum <= 0) { efree(result); efree(args); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument number must be greater than zero"); return NULL; } multiuse = 1; inpos++; /* skip the '$' */ } else { argnum = currarg++; } argnum += format_offset; /* after argnum comes modifiers */ PRINTF_DEBUG(("sprintf: looking for modifiers\n" "sprintf: now looking at '%c', inpos=%d\n", format[inpos], inpos)); for (;; inpos++) { if (format[inpos] == ' ' || format[inpos] == '0') { padding = format[inpos]; } else if (format[inpos] == '-') { alignment = ALIGN_LEFT; /* space padding, the default */ } else if (format[inpos] == '+') { always_sign = 1; } else if (format[inpos] == '\'') { padding = format[++inpos]; } else { PRINTF_DEBUG(("sprintf: end of modifiers\n")); break; } } PRINTF_DEBUG(("sprintf: padding='%c'\n", padding)); PRINTF_DEBUG(("sprintf: alignment=%s\n", (alignment == ALIGN_LEFT) ? "left" : "right")); /* after modifiers comes width */ if (isdigit((int)format[inpos])) { PRINTF_DEBUG(("sprintf: getting width\n")); if ((width = php_sprintf_getnumber(format, &inpos)) < 0) { efree(result); efree(args); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Width must be greater than zero and less than %d", INT_MAX); return NULL; } adjusting |= ADJ_WIDTH; } else { width = 0; } PRINTF_DEBUG(("sprintf: width=%d\n", width)); /* after width and argnum comes precision */ if (format[inpos] == '.') { inpos++; PRINTF_DEBUG(("sprintf: getting precision\n")); if (isdigit((int)format[inpos])) { if ((precision = php_sprintf_getnumber(format, &inpos)) < 0) { efree(result); efree(args); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Precision must be greater than zero and less than %d", INT_MAX); return NULL; } adjusting |= ADJ_PRECISION; expprec = 1; } else { precision = 0; } } else { precision = 0; } PRINTF_DEBUG(("sprintf: precision=%d\n", precision)); } else { width = precision = 0; argnum = currarg++ + format_offset; } if (argnum >= argc) { efree(result); efree(args); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too few arguments"); return NULL; } if (format[inpos] == 'l') { inpos++; } PRINTF_DEBUG(("sprintf: format character='%c'\n", format[inpos])); /* now we expect to find a type specifier */ if (multiuse) { MAKE_STD_ZVAL(tmp); *tmp = **(args[argnum]); INIT_PZVAL(tmp); zval_copy_ctor(tmp); } else { SEPARATE_ZVAL(args[argnum]); tmp = *(args[argnum]); } switch (format[inpos]) { case 's': { zval *var, var_copy; int use_copy; zend_make_printable_zval(tmp, &var_copy, &use_copy); if (use_copy) { var = &var_copy; } else { var = tmp; } php_sprintf_appendstring(&result, &outpos, &size, Z_STRVAL_P(var), width, precision, padding, alignment, Z_STRLEN_P(var), 0, expprec, 0); if (use_copy) { zval_dtor(&var_copy); } break; } case 'd': convert_to_long(tmp); php_sprintf_appendint(&result, &outpos, &size, Z_LVAL_P(tmp), width, padding, alignment, always_sign); break; case 'u': convert_to_long(tmp); php_sprintf_appenduint(&result, &outpos, &size, Z_LVAL_P(tmp), width, padding, alignment); break; case 'g': case 'G': case 'e': case 'E': case 'f': case 'F': convert_to_double(tmp); php_sprintf_appenddouble(&result, &outpos, &size, Z_DVAL_P(tmp), width, padding, alignment, precision, adjusting, format[inpos], always_sign TSRMLS_CC); break; case 'c': convert_to_long(tmp); php_sprintf_appendchar(&result, &outpos, &size, (char) Z_LVAL_P(tmp) TSRMLS_CC); break; case 'o': convert_to_long(tmp); php_sprintf_append2n(&result, &outpos, &size, Z_LVAL_P(tmp), width, padding, alignment, 3, hexchars, expprec); break; case 'x': convert_to_long(tmp); php_sprintf_append2n(&result, &outpos, &size, Z_LVAL_P(tmp), width, padding, alignment, 4, hexchars, expprec); break; case 'X': convert_to_long(tmp); php_sprintf_append2n(&result, &outpos, &size, Z_LVAL_P(tmp), width, padding, alignment, 4, HEXCHARS, expprec); break; case 'b': convert_to_long(tmp); php_sprintf_append2n(&result, &outpos, &size, Z_LVAL_P(tmp), width, padding, alignment, 1, hexchars, expprec); break; case '%': php_sprintf_appendchar(&result, &outpos, &size, '%' TSRMLS_CC); break; default: break; } if (multiuse) { zval_ptr_dtor(&tmp); } inpos++; } } efree(args); /* possibly, we have to make sure we have room for the terminating null? */ result[outpos]=0; *len = outpos; return result; }
/* laod array from INI settings */ RedisArray *ra_load_array(const char *name) { zval z_params_hosts, *z_hosts; zval z_params_prev, *z_prev; zval z_params_funs, *z_data_p, z_fun, z_dist; zval z_params_index; zval z_params_autorehash; zval z_params_retry_interval; zval z_params_pconnect; zval z_params_connect_timeout; zval z_params_lazy_connect; RedisArray *ra = NULL; zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0; long l_retry_interval = 0; zend_bool b_lazy_connect = 0; double d_connect_timeout = 0; HashTable *hHosts = NULL, *hPrev = NULL; /* find entry */ if (!ra_find_name(name)) { return ra; } /* find hosts */ array_init(&z_params_hosts); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.hosts")), &z_params_hosts); if ((z_hosts = zend_hash_str_find(Z_ARRVAL(z_params_hosts), name, strlen(name))) != NULL ) { hHosts = Z_ARRVAL_P(z_hosts); } /* find previous hosts */ array_init(&z_params_prev); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.previous")), &z_params_prev); if ((z_prev = zend_hash_str_find(Z_ARRVAL(z_params_prev), name, strlen(name))) != NULL ) { hPrev = Z_ARRVAL_P(z_prev); } /* find function */ array_init(&z_params_funs); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.functions")), &z_params_funs); if ((z_data_p = zend_hash_str_find(Z_ARRVAL(z_params_funs), name, strlen(name))) != NULL ) { ZVAL_DUP(&z_fun, z_data_p); } /* find distributor */ array_init(&z_params_funs); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.distributor")), &z_params_funs); if ((z_data_p = zend_hash_str_find(Z_ARRVAL(z_params_funs), name, strlen(name))) != NULL ) { ZVAL_DUP(&z_dist, z_data_p); } /* find index option */ array_init(&z_params_index); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.index")), &z_params_index); if ((z_data_p = zend_hash_str_find(Z_ARRVAL(z_params_index), name, strlen(name))) != NULL ) { if (Z_TYPE_P(z_data_p) == IS_STRING && strncmp(Z_STRVAL_P(z_data_p), "1", 1) == 0) { b_index = 1; } } /* find autorehash option */ array_init(&z_params_autorehash); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.autorehash")), &z_params_autorehash); if ((z_data_p = zend_hash_str_find(Z_ARRVAL(z_params_autorehash), name, strlen(name))) != NULL ) { if (Z_TYPE_P(z_data_p) == IS_STRING && strncmp(Z_STRVAL_P(z_data_p), "1", 1) == 0) { b_autorehash = 1; } } /* find retry interval option */ array_init(&z_params_retry_interval); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.retryinterval")), &z_params_retry_interval); if ((z_data_p = zend_hash_str_find(Z_ARRVAL(z_params_retry_interval), name, strlen(name))) != NULL ) { if (Z_TYPE_P(z_data_p) == IS_LONG || Z_TYPE_P(z_data_p) == IS_STRING) { if (Z_TYPE_P(z_data_p) == IS_LONG) { l_retry_interval = Z_LVAL_P(z_data_p); } else { l_retry_interval = atol(Z_STRVAL_P(z_data_p)); } } } /* find pconnect option */ array_init(&z_params_pconnect); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.pconnect")), &z_params_pconnect); if ((z_data_p = zend_hash_str_find(Z_ARRVAL(z_params_pconnect), name, strlen(name))) != NULL ) { if (Z_TYPE_P(z_data_p) == IS_STRING && strncmp(Z_STRVAL_P(z_data_p), "1", 1) == 0) { b_pconnect = 1; } } /* find lazy connect option */ array_init(&z_params_lazy_connect); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.lazyconnect")), &z_params_lazy_connect); if ((z_data_p = zend_hash_str_find(Z_ARRVAL(z_params_lazy_connect), name, strlen(name))) != NULL ) { if (Z_TYPE_P(z_data_p) == IS_STRING && strncmp(Z_STRVAL_P(z_data_p), "1", 1) == 0) { b_lazy_connect = 1; } } /* find connect timeout option */ array_init(&z_params_connect_timeout); sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.connecttimeout")), &z_params_connect_timeout); if ((z_data_p = zend_hash_str_find(Z_ARRVAL(z_params_connect_timeout), name, strlen(name))) != NULL ) { if (Z_TYPE_P(z_data_p) == IS_DOUBLE || Z_TYPE_P(z_data_p) == IS_STRING) { if (Z_TYPE_P(z_data_p) == IS_DOUBLE) { d_connect_timeout = Z_DVAL_P(z_data_p); } else { d_connect_timeout = atof(Z_STRVAL_P(z_data_p)); } } } /* create RedisArray object */ ra = ra_make_array(hHosts, &z_fun, &z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout); ra->auto_rehash = b_autorehash; if (ra->prev) ra->prev->auto_rehash = b_autorehash; /* cleanup */ zval_dtor(&z_params_hosts); efree(&z_params_hosts); zval_dtor(&z_params_prev); efree(&z_params_prev); zval_dtor(&z_params_funs); efree(&z_params_funs); zval_dtor(&z_params_index); efree(&z_params_index); zval_dtor(&z_params_autorehash); efree(&z_params_autorehash); zval_dtor(&z_params_retry_interval); efree(&z_params_retry_interval); zval_dtor(&z_params_pconnect); efree(&z_params_pconnect); zval_dtor(&z_params_connect_timeout); efree(&z_params_connect_timeout); zval_dtor(&z_params_lazy_connect); efree(&z_params_lazy_connect); return ra; }
static PHP_METHOD(swoole_mysql, connect) { zval *server_info; zval *callback; char buf[2048]; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "az", &server_info, &callback) == FAILURE) { RETURN_FALSE; } HashTable *_ht = Z_ARRVAL_P(server_info); zval *value; mysql_client *client = swoole_get_object(getThis()); mysql_connector *connector = &client->connector; if (php_swoole_array_get_value(_ht, "host", value)) { convert_to_string(value); connector->host = Z_STRVAL_P(value); connector->host_len = Z_STRLEN_P(value); } else { zend_throw_exception(swoole_mysql_exception_class_entry, "HOST parameter is required.", 11 TSRMLS_CC); RETURN_FALSE; } if (php_swoole_array_get_value(_ht, "port", value)) { convert_to_long(value); connector->port = Z_LVAL_P(value); } else { connector->port = SW_MYSQL_DEFAULT_PORT; } if (php_swoole_array_get_value(_ht, "user", value)) { convert_to_string(value); connector->user = Z_STRVAL_P(value); connector->user_len = Z_STRLEN_P(value); } else { zend_throw_exception(swoole_mysql_exception_class_entry, "USER parameter is required.", 11 TSRMLS_CC); RETURN_FALSE; } if (php_swoole_array_get_value(_ht, "password", value)) { convert_to_string(value); connector->password = Z_STRVAL_P(value); connector->password_len = Z_STRLEN_P(value); } else { zend_throw_exception(swoole_mysql_exception_class_entry, "PASSWORD parameter is required.", 11 TSRMLS_CC); RETURN_FALSE; } if (php_swoole_array_get_value(_ht, "database", value)) { convert_to_string(value); connector->database = Z_STRVAL_P(value); connector->database_len = Z_STRLEN_P(value); } else { zend_throw_exception(swoole_mysql_exception_class_entry, "DATABASE parameter is required.", 11 TSRMLS_CC); RETURN_FALSE; } if (php_swoole_array_get_value(_ht, "timeout", value)) { convert_to_double(value); connector->timeout = Z_DVAL_P(value); } else { connector->timeout = SW_MYSQL_CONNECT_TIMEOUT; } if (php_swoole_array_get_value(_ht, "charset", value)) { convert_to_string(value); connector->character_set = mysql_get_charset(Z_STRVAL_P(value)); if (connector->character_set < 0) { snprintf(buf, sizeof(buf), "unknown charset [%s].", Z_STRVAL_P(value)); zend_throw_exception(swoole_mysql_exception_class_entry, buf, 11 TSRMLS_CC); RETURN_FALSE; } } else { connector->character_set = SW_MYSQL_DEFAULT_CHARSET; } swClient *cli = emalloc(sizeof(swClient)); int type = SW_SOCK_TCP; if (strncasecmp(connector->host, ZEND_STRL("unix:/")) == 0) { connector->host = connector->host + 5; connector->host_len = connector->host_len - 5; type = SW_SOCK_UNIX_STREAM; } else if (strchr(connector->host, ':')) { type = SW_SOCK_TCP6; } php_swoole_check_reactor(); if (!isset_event_callback) { SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_MYSQL | SW_EVENT_READ, swoole_mysql_onRead); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_MYSQL | SW_EVENT_WRITE, swoole_mysql_onWrite); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_MYSQL | SW_EVENT_ERROR, swoole_mysql_onError); } if (swClient_create(cli, type, 0) < 0) { zend_throw_exception(swoole_mysql_exception_class_entry, "swClient_create failed.", 1 TSRMLS_CC); RETURN_FALSE; } int tcp_nodelay = 1; if (setsockopt(cli->socket->fd, IPPROTO_TCP, TCP_NODELAY, (const void *) &tcp_nodelay, sizeof(int)) == -1) { swoole_php_sys_error(E_WARNING, "setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) failed.", cli->socket->fd); } int ret = cli->connect(cli, connector->host, connector->port, connector->timeout, 1); if ((ret < 0 && errno == EINPROGRESS) || ret == 0) { if (SwooleG.main_reactor->add(SwooleG.main_reactor, cli->socket->fd, PHP_SWOOLE_FD_MYSQL | SW_EVENT_WRITE) < 0) { RETURN_FALSE; } } else { snprintf(buf, sizeof(buf), "connect to mysql server[%s:%d] failed.", connector->host, connector->port); zend_throw_exception(swoole_mysql_exception_class_entry, buf, 2 TSRMLS_CC); RETURN_FALSE; } zend_update_property(swoole_mysql_class_entry_ptr, getThis(), ZEND_STRL("onConnect"), callback TSRMLS_CC); zend_update_property(swoole_mysql_class_entry_ptr, getThis(), ZEND_STRL("serverInfo"), server_info TSRMLS_CC); zend_update_property_long(swoole_mysql_class_entry_ptr, getThis(), ZEND_STRL("sock"), cli->socket->fd TSRMLS_CC); client->buffer = swString_new(SW_BUFFER_SIZE_BIG); client->fd = cli->socket->fd; client->object = getThis(); client->cli = cli; sw_copy_to_stack(client->object, client->_object); sw_zval_add_ref(&client->object); swConnection *_socket = swReactor_get(SwooleG.main_reactor, cli->socket->fd); _socket->object = client; _socket->active = 0; RETURN_TRUE; }
/* {{{ php_url_encode_hash */ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, const char *num_prefix, size_t num_prefix_len, const char *key_prefix, size_t key_prefix_len, const char *key_suffix, size_t key_suffix_len, zval *type, char *arg_sep, int enc_type) { zend_string *key = NULL; char *newprefix, *p; const char *prop_name; size_t arg_sep_len, newprefix_len, prop_len; zend_ulong idx; zval *zdata = NULL; if (!ht) { return FAILURE; } if (GC_IS_RECURSIVE(ht)) { /* Prevent recursion */ return SUCCESS; } if (!arg_sep) { arg_sep = INI_STR("arg_separator.output"); if (!arg_sep || !strlen(arg_sep)) { arg_sep = URL_DEFAULT_ARG_SEP; } } arg_sep_len = strlen(arg_sep); ZEND_HASH_FOREACH_KEY_VAL(ht, idx, key, zdata) { zend_bool is_dynamic = 1; if (Z_TYPE_P(zdata) == IS_INDIRECT) { zdata = Z_INDIRECT_P(zdata); if (Z_ISUNDEF_P(zdata)) { continue; } is_dynamic = 0; } /* handling for private & protected object properties */ if (key) { prop_name = ZSTR_VAL(key); prop_len = ZSTR_LEN(key); if (type != NULL && zend_check_property_access(Z_OBJ_P(type), key, is_dynamic) != SUCCESS) { /* property not visible in this scope */ continue; } if (ZSTR_VAL(key)[0] == '\0' && type != NULL) { const char *tmp; zend_unmangle_property_name_ex(key, &tmp, &prop_name, &prop_len); } else { prop_name = ZSTR_VAL(key); prop_len = ZSTR_LEN(key); } } else { prop_name = NULL; prop_len = 0; } ZVAL_DEREF(zdata); if (Z_TYPE_P(zdata) == IS_ARRAY || Z_TYPE_P(zdata) == IS_OBJECT) { if (key) { zend_string *ekey; if (enc_type == PHP_QUERY_RFC3986) { ekey = php_raw_url_encode(prop_name, prop_len); } else { ekey = php_url_encode(prop_name, prop_len); } newprefix_len = key_suffix_len + ZSTR_LEN(ekey) + key_prefix_len + 3 /* %5B */; newprefix = emalloc(newprefix_len + 1); p = newprefix; if (key_prefix) { memcpy(p, key_prefix, key_prefix_len); p += key_prefix_len; } memcpy(p, ZSTR_VAL(ekey), ZSTR_LEN(ekey)); p += ZSTR_LEN(ekey); zend_string_free(ekey); if (key_suffix) { memcpy(p, key_suffix, key_suffix_len); p += key_suffix_len; } *(p++) = '%'; *(p++) = '5'; *(p++) = 'B'; *p = '\0'; } else { char *ekey; size_t ekey_len; /* Is an integer key */ ekey_len = spprintf(&ekey, 0, ZEND_LONG_FMT, idx); newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 3 /* %5B */; newprefix = emalloc(newprefix_len + 1); p = newprefix; if (key_prefix) { memcpy(p, key_prefix, key_prefix_len); p += key_prefix_len; } memcpy(p, num_prefix, num_prefix_len); p += num_prefix_len; memcpy(p, ekey, ekey_len); p += ekey_len; efree(ekey); if (key_suffix) { memcpy(p, key_suffix, key_suffix_len); p += key_suffix_len; } *(p++) = '%'; *(p++) = '5'; *(p++) = 'B'; *p = '\0'; } if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) { GC_PROTECT_RECURSION(ht); } php_url_encode_hash_ex(HASH_OF(zdata), formstr, NULL, 0, newprefix, newprefix_len, "%5D", 3, (Z_TYPE_P(zdata) == IS_OBJECT ? zdata : NULL), arg_sep, enc_type); if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) { GC_UNPROTECT_RECURSION(ht); } efree(newprefix); } else if (Z_TYPE_P(zdata) == IS_NULL || Z_TYPE_P(zdata) == IS_RESOURCE) { /* Skip these types */ continue; } else { if (formstr->s) { smart_str_appendl(formstr, arg_sep, arg_sep_len); } /* Simple key=value */ smart_str_appendl(formstr, key_prefix, key_prefix_len); if (key) { zend_string *ekey; if (enc_type == PHP_QUERY_RFC3986) { ekey = php_raw_url_encode(prop_name, prop_len); } else { ekey = php_url_encode(prop_name, prop_len); } smart_str_append(formstr, ekey); zend_string_free(ekey); } else { /* Numeric key */ if (num_prefix) { smart_str_appendl(formstr, num_prefix, num_prefix_len); } smart_str_append_long(formstr, idx); } smart_str_appendl(formstr, key_suffix, key_suffix_len); smart_str_appendl(formstr, "=", 1); switch (Z_TYPE_P(zdata)) { case IS_STRING: { zend_string *ekey; if (enc_type == PHP_QUERY_RFC3986) { ekey = php_raw_url_encode(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)); } else { ekey = php_url_encode(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)); } smart_str_append(formstr, ekey); zend_string_free(ekey); } break; case IS_LONG: smart_str_append_long(formstr, Z_LVAL_P(zdata)); break; case IS_FALSE: smart_str_appendl(formstr, "0", sizeof("0")-1); break; case IS_TRUE: smart_str_appendl(formstr, "1", sizeof("1")-1); break; case IS_DOUBLE: { char *ekey; size_t ekey_len; ekey_len = spprintf(&ekey, 0, "%.*G", (int) EG(precision), Z_DVAL_P(zdata)); smart_str_appendl(formstr, ekey, ekey_len); efree(ekey); } break; default: { zend_string *ekey; zend_string *tmp; zend_string *str= zval_get_tmp_string(zdata, &tmp); if (enc_type == PHP_QUERY_RFC3986) { ekey = php_raw_url_encode(ZSTR_VAL(str), ZSTR_LEN(str)); } else { ekey = php_url_encode(ZSTR_VAL(str), ZSTR_LEN(str)); } smart_str_append(formstr, ekey); zend_tmp_string_release(tmp); zend_string_free(ekey); } } } } ZEND_HASH_FOREACH_END();
PHP_METHOD(ExecutionOptions, __construct) { zval* options = NULL; cassandra_execution_options* self = NULL; zval** consistency = NULL; zval** serial_consistency = NULL; zval** page_size = NULL; zval** paging_state_token = NULL; zval** timeout = NULL; zval** arguments = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &options) == FAILURE) { return; } if (!options) return; if (Z_TYPE_P(options) != IS_ARRAY) { INVALID_ARGUMENT(options, "an array"); } self = (cassandra_execution_options*) zend_object_store_get_object(getThis() TSRMLS_CC); if (zend_hash_find(Z_ARRVAL_P(options), "consistency", sizeof("consistency"), (void**)&consistency) == SUCCESS) { if (php_cassandra_get_consistency(*consistency, &self->consistency TSRMLS_CC) == FAILURE) { return; } } if (zend_hash_find(Z_ARRVAL_P(options), "serial_consistency", sizeof("serial_consistency"), (void**)&serial_consistency) == SUCCESS) { if (php_cassandra_get_serial_consistency(*serial_consistency, &self->serial_consistency TSRMLS_CC) == FAILURE) { return; } } if (zend_hash_find(Z_ARRVAL_P(options), "page_size", sizeof("page_size"), (void**)&page_size) == SUCCESS) { if (Z_TYPE_P(*page_size) != IS_LONG || Z_LVAL_P(*page_size) <= 0) { INVALID_ARGUMENT(*page_size, "greater than zero"); } self->page_size = Z_LVAL_P(*page_size); } if (zend_hash_find(Z_ARRVAL_P(options), "paging_state_token", sizeof("paging_state_token"), (void**)&paging_state_token) == SUCCESS) { if (Z_TYPE_P(*paging_state_token) != IS_STRING) { INVALID_ARGUMENT(*paging_state_token, "a string"); } self->paging_state_token = estrndup(Z_STRVAL_P(*paging_state_token), Z_STRLEN_P(*paging_state_token)); self->paging_state_token_size = Z_STRLEN_P(*paging_state_token); } if (zend_hash_find(Z_ARRVAL_P(options), "timeout", sizeof("timeout"), (void**)&timeout) == SUCCESS) { if (!(Z_TYPE_P(*timeout) == IS_LONG && Z_LVAL_P(*timeout) > 0) && !(Z_TYPE_P(*timeout) == IS_DOUBLE && Z_DVAL_P(*timeout) > 0) && !(Z_TYPE_P(*timeout) == IS_NULL)) { INVALID_ARGUMENT(*timeout, "a number of seconds greater than zero or null"); } self->timeout = *timeout; Z_ADDREF_P(self->timeout); } if (zend_hash_find(Z_ARRVAL_P(options), "arguments", sizeof("arguments"), (void**)&arguments) == SUCCESS) { if (Z_TYPE_P(*arguments) != IS_ARRAY) { INVALID_ARGUMENT(*arguments, "an array"); return; } self->arguments = *arguments; Z_ADDREF_P(self->arguments); } }