U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id) { char *str_id; size_t str_id_len; zend_long index; intl_error_reset(NULL); if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &str_id, &str_id_len, &index) == FAILURE || index < (zend_long)INT32_MIN || index > (zend_long)INT32_MAX) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_get_equivalent_id: bad arguments", 0); RETURN_FALSE; } UErrorCode status = UErrorCode(); UnicodeString id; if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) { intl_error_set(NULL, status, "intltz_get_equivalent_id: could not convert time zone id to UTF-16", 0); RETURN_FALSE; } const UnicodeString result = TimeZone::getEquivalentID(id, (int32_t)index); zend_string *u8str; u8str = intl_convert_utf16_to_utf8(result.getBuffer(), result.length(), &status); INTL_CHECK_STATUS(status, "intltz_get_equivalent_id: " "could not convert resulting time zone id to UTF-16"); RETVAL_NEW_STR(u8str); }
U_CFUNC PHP_FUNCTION(intltz_get_region) { char *str_id; size_t str_id_len; char outbuf[3]; intl_error_reset(NULL); if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str_id, &str_id_len) == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_get_region: bad arguments", 0); RETURN_FALSE; } UErrorCode status = UErrorCode(); UnicodeString id; if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) { intl_error_set(NULL, status, "intltz_get_region: could not convert time zone id to UTF-16", 0); RETURN_FALSE; } int32_t region_len = TimeZone::getRegion(id, outbuf, sizeof(outbuf), status); INTL_CHECK_STATUS(status, "intltz_get_region: Error obtaining region"); RETURN_STRINGL(outbuf, region_len); }
U_CFUNC PHP_FUNCTION(intltz_get_equivalent_id) { char *str_id; int str_id_len; long index; intl_error_reset(NULL TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &str_id, &str_id_len, &index) == FAILURE || index < (long)INT32_MIN || index > (long)INT32_MAX) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_get_equivalent_id: bad arguments", 0 TSRMLS_CC); RETURN_FALSE; } UErrorCode status = UErrorCode(); UnicodeString id; if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) { intl_error_set(NULL, status, "intltz_get_equivalent_id: could not convert time zone id to UTF-16", 0 TSRMLS_CC); RETURN_FALSE; } const UnicodeString result = TimeZone::getEquivalentID(id, (int32_t)index); intl_convert_utf16_to_utf8(&Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), result.getBuffer(), result.length(), &status); INTL_CHECK_STATUS(status, "intltz_get_equivalent_id: " "could not convert resulting time zone id to UTF-16"); Z_TYPE_P(return_value) = IS_STRING; }
U_CFUNC PHP_FUNCTION(intltz_create_time_zone) { char *str_id; int str_id_len; intl_error_reset(NULL TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str_id, &str_id_len) == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_time_zone: bad arguments", 0 TSRMLS_CC); RETURN_NULL(); } UErrorCode status = UErrorCode(); UnicodeString id = UnicodeString(); if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) { intl_error_set(NULL, status, "intltz_create_time_zone: could not convert time zone id to UTF-16", 0 TSRMLS_CC); RETURN_NULL(); } //guaranteed non-null; GMT if timezone cannot be understood TimeZone *tz = TimeZone::createTimeZone(id); timezone_object_construct(tz, return_value, 1 TSRMLS_CC); }
U_CFUNC PHP_FUNCTION(intltz_create_time_zone_id_enumeration) { long zoneType, offset_arg; char *region = NULL; int region_len = 0; int32_t offset, *offsetp = NULL; int arg3isnull = 0; intl_error_reset(NULL TSRMLS_CC); /* must come before zpp because zpp would convert the arg in the stack to 0 */ if (ZEND_NUM_ARGS() == 3) { zval **dummy, **zvoffset; arg3isnull = zend_get_parameters_ex(3, &dummy, &dummy, &zvoffset) != FAILURE && Z_TYPE_PP(zvoffset) == IS_NULL; } if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s!l", &zoneType, ®ion, ®ion_len, &offset_arg) == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_time_zone_id_enumeration: bad arguments", 0 TSRMLS_CC); RETURN_FALSE; } if (zoneType != UCAL_ZONE_TYPE_ANY && zoneType != UCAL_ZONE_TYPE_CANONICAL && zoneType != UCAL_ZONE_TYPE_CANONICAL_LOCATION) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_time_zone_id_enumeration: bad zone type", 0 TSRMLS_CC); RETURN_FALSE; } if (ZEND_NUM_ARGS() == 3) { if (offset_arg < (long)INT32_MIN || offset_arg > (long)INT32_MAX) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_time_zone_id_enumeration: offset out of bounds", 0 TSRMLS_CC); RETURN_FALSE; } if (!arg3isnull) { offset = (int32_t)offset_arg; offsetp = &offset; } //else leave offsetp NULL } StringEnumeration *se; UErrorCode uec = UErrorCode(); se = TimeZone::createTimeZoneIDEnumeration((USystemTimeZoneType)zoneType, region, offsetp, uec); INTL_CHECK_STATUS(uec, "intltz_create_time_zone_id_enumeration: " "Error obtaining time zone id enumeration") IntlIterator_from_StringEnumeration(se, return_value TSRMLS_CC); }
U_CFUNC PHP_FUNCTION(intltz_get_tz_data_version) { intl_error_reset(NULL); if (zend_parse_parameters_none() == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_get_tz_data_version: bad arguments", 0); RETURN_FALSE; } UErrorCode status = UErrorCode(); const char *res = TimeZone::getTZDataVersion(status); INTL_CHECK_STATUS(status, "intltz_get_tz_data_version: " "Error obtaining time zone data version"); RETURN_STRING(res); }
U_CFUNC PHP_FUNCTION(intltz_create_time_zone_id_enumeration) { zend_long zoneType, offset_arg; char *region = NULL; size_t region_len = 0; int32_t offset, *offsetp = NULL; zend_bool arg3isnull = 1; intl_error_reset(NULL); if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|s!l!", &zoneType, ®ion, ®ion_len, &offset_arg, &arg3isnull) == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_time_zone_id_enumeration: bad arguments", 0); RETURN_FALSE; } if (zoneType != UCAL_ZONE_TYPE_ANY && zoneType != UCAL_ZONE_TYPE_CANONICAL && zoneType != UCAL_ZONE_TYPE_CANONICAL_LOCATION) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_time_zone_id_enumeration: bad zone type", 0); RETURN_FALSE; } if (!arg3isnull) { if (offset_arg < (zend_long)INT32_MIN || offset_arg > (zend_long)INT32_MAX) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_create_time_zone_id_enumeration: offset out of bounds", 0); RETURN_FALSE; } offset = (int32_t)offset_arg; offsetp = &offset; } //else leave offsetp NULL StringEnumeration *se; UErrorCode uec = UErrorCode(); se = TimeZone::createTimeZoneIDEnumeration((USystemTimeZoneType)zoneType, region, offsetp, uec); INTL_CHECK_STATUS(uec, "intltz_create_time_zone_id_enumeration: " "Error obtaining time zone id enumeration") IntlIterator_from_StringEnumeration(se, return_value); }
U_CFUNC PHP_FUNCTION(intltz_get_canonical_id) { char *str_id; size_t str_id_len; zval *is_systemid = NULL; intl_error_reset(NULL); if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z", &str_id, &str_id_len, &is_systemid) == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_get_canonical_id: bad arguments", 0); RETURN_FALSE; } UErrorCode status = UErrorCode(); UnicodeString id; if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) { intl_error_set(NULL, status, "intltz_get_canonical_id: could not convert time zone id to UTF-16", 0); RETURN_FALSE; } UnicodeString result; UBool isSystemID; TimeZone::getCanonicalID(id, result, isSystemID, status); INTL_CHECK_STATUS(status, "intltz_get_canonical_id: error obtaining canonical ID"); char *str; int str_len; intl_convert_utf16_to_utf8(&str, &str_len, result.getBuffer(), result.length(), &status); INTL_CHECK_STATUS(status, "intltz_get_canonical_id: could not convert time zone id to UTF-16"); RETVAL_STRINGL(str, str_len); //???? efree(str); if (is_systemid) { /* by-ref argument passed */ ZVAL_DEREF(is_systemid); zval_dtor(is_systemid); ZVAL_BOOL(is_systemid, isSystemID); } }
U_CFUNC PHP_FUNCTION(intltz_count_equivalent_ids) { char *str_id; size_t str_id_len; intl_error_reset(NULL); if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str_id, &str_id_len) == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intltz_count_equivalent_ids: bad arguments", 0); RETURN_FALSE; } UErrorCode status = UErrorCode(); UnicodeString id = UnicodeString(); if (intl_stringFromChar(id, str_id, str_id_len, &status) == FAILURE) { intl_error_set(NULL, status, "intltz_count_equivalent_ids: could not convert time zone id to UTF-16", 0); RETURN_FALSE; } int32_t result = TimeZone::countEquivalentIDs(id); RETURN_LONG((zend_long)result); }
U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo, HashTable *args, UChar **formatted, int32_t *formatted_len) { int arg_count = zend_hash_num_elements(args); std::vector<Formattable> fargs; std::vector<UnicodeString> farg_names; MessageFormat *mf = (MessageFormat *)mfo->mf_data.umsgf; HashTable *types; intl_error& err = INTL_DATA_ERROR(mfo); if (U_FAILURE(err.code)) { return; } types = umsg_get_types(mfo, err); umsg_set_timezone(mfo, err); fargs.resize(arg_count); farg_names.resize(arg_count); int argNum = 0; zval *elem; // Key related variables zend_string *str_index; zend_ulong num_index; ZEND_HASH_FOREACH_KEY_VAL(args, num_index, str_index, elem) { Formattable& formattable = fargs[argNum]; UnicodeString& key = farg_names[argNum]; Formattable::Type argType = Formattable::kObject, //unknown *storedArgType = NULL; if (!U_SUCCESS(err.code)) { break; } /* Process key and retrieve type */ if (str_index == NULL) { /* includes case where index < 0 because it's exposed as unsigned */ if (num_index > (zend_ulong)INT32_MAX) { intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found negative or too large array key", 0); continue; } UChar temp[16]; int32_t len = u_sprintf(temp, "%u", (uint32_t)num_index); key.append(temp, len); storedArgType = (Formattable::Type*)zend_hash_index_find_ptr(types, (zend_ulong)num_index); } else { //string; assumed to be in UTF-8 intl_stringFromChar(key, ZSTR_VAL(str_index), ZSTR_LEN(str_index), &err.code); if (U_FAILURE(err.code)) { char *message; spprintf(&message, 0, "Invalid UTF-8 data in argument key: '%s'", ZSTR_VAL(str_index)); intl_errors_set(&err, err.code, message, 1); efree(message); continue; } storedArgType = (Formattable::Type*)zend_hash_str_find_ptr(types, (char*)key.getBuffer(), key.length()); } if (storedArgType != NULL) { argType = *storedArgType; } /* Convert zval to formattable according to message format type * or (as a fallback) the zval type */ if (argType != Formattable::kObject) { switch (argType) { case Formattable::kString: { string_arg: /* This implicitly converts objects * Note that our vectors will leak if object conversion fails * and PHP ends up with a fatal error and calls longjmp * as a result of that. */ convert_to_string_ex(elem); UnicodeString *text = new UnicodeString(); intl_stringFromChar(*text, Z_STRVAL_P(elem), Z_STRLEN_P(elem), &err.code); if (U_FAILURE(err.code)) { char *message; spprintf(&message, 0, "Invalid UTF-8 data in string argument: " "'%s'", Z_STRVAL_P(elem)); intl_errors_set(&err, err.code, message, 1); efree(message); delete text; continue; } formattable.adoptString(text); break; } case Formattable::kDouble: { double d; if (Z_TYPE_P(elem) == IS_DOUBLE) { d = Z_DVAL_P(elem); } else if (Z_TYPE_P(elem) == IS_LONG) { d = (double)Z_LVAL_P(elem); } else { SEPARATE_ZVAL_IF_NOT_REF(elem); convert_scalar_to_number(elem); d = (Z_TYPE_P(elem) == IS_DOUBLE) ? Z_DVAL_P(elem) : (double)Z_LVAL_P(elem); } formattable.setDouble(d); break; } case Formattable::kLong: { int32_t tInt32 = 0; retry_klong: if (Z_TYPE_P(elem) == IS_DOUBLE) { if (Z_DVAL_P(elem) > (double)INT32_MAX || Z_DVAL_P(elem) < (double)INT32_MIN) { intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found PHP float with absolute value too large for " "32 bit integer argument", 0); } else { tInt32 = (int32_t)Z_DVAL_P(elem); } } else if (Z_TYPE_P(elem) == IS_LONG) { if (Z_LVAL_P(elem) > INT32_MAX || Z_LVAL_P(elem) < INT32_MIN) { intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found PHP integer with absolute value too large " "for 32 bit integer argument", 0); } else { tInt32 = (int32_t)Z_LVAL_P(elem); } } else { SEPARATE_ZVAL_IF_NOT_REF(elem); convert_scalar_to_number(elem); goto retry_klong; } formattable.setLong(tInt32); break; } case Formattable::kInt64: { int64_t tInt64 = 0; retry_kint64: if (Z_TYPE_P(elem) == IS_DOUBLE) { if (Z_DVAL_P(elem) > (double)U_INT64_MAX || Z_DVAL_P(elem) < (double)U_INT64_MIN) { intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found PHP float with absolute value too large for " "64 bit integer argument", 0); } else { tInt64 = (int64_t)Z_DVAL_P(elem); } } else if (Z_TYPE_P(elem) == IS_LONG) { /* assume long is not wider than 64 bits */ tInt64 = (int64_t)Z_LVAL_P(elem); } else { SEPARATE_ZVAL_IF_NOT_REF(elem); convert_scalar_to_number(elem); goto retry_kint64; } formattable.setInt64(tInt64); break; } case Formattable::kDate: { double dd = intl_zval_to_millis(elem, &err, "msgfmt_format"); if (U_FAILURE(err.code)) { char *message; zend_string *u8key; UErrorCode status = UErrorCode(); u8key = intl_charFromString(key, &status); if (u8key) { spprintf(&message, 0, "The argument for key '%s' " "cannot be used as a date or time", ZSTR_VAL(u8key)); intl_errors_set(&err, err.code, message, 1); zend_string_release(u8key); efree(message); } continue; } formattable.setDate(dd); break; } default: intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, "Found unsupported argument type", 0); break; } } else { /* We couldn't find any information about the argument in the pattern, this * means it's an extra argument. So convert it to a number if it's a number or * bool or null and to a string if it's anything else except arrays . */ switch (Z_TYPE_P(elem)) { case IS_DOUBLE: formattable.setDouble(Z_DVAL_P(elem)); break; case IS_TRUE: case IS_FALSE: convert_to_long_ex(elem); /* Intentional fallthrough */ case IS_LONG: formattable.setInt64((int64_t)Z_LVAL_P(elem)); break; case IS_NULL: formattable.setInt64((int64_t)0); break; case IS_STRING: case IS_OBJECT: goto string_arg; default: { char *message; zend_string *u8key; UErrorCode status = UErrorCode(); u8key = intl_charFromString(key, &status); if (u8key) { spprintf(&message, 0, "No strategy to convert the " "value given for the argument with key '%s' " "is available", ZSTR_VAL(u8key)); intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR, message, 1); zend_string_release(u8key); efree(message); } } } } argNum++; } ZEND_HASH_FOREACH_END(); // visiting each argument