whitespaces -> tabs

no other changes done
This commit is contained in:
Antony Dovgal 2006-03-24 10:25:50 +00:00
parent 9cee8be28e
commit 792a2f6e65

View file

@ -35,383 +35,383 @@ ZEND_API zend_class_entry *unicodeConversionException;
/* {{{ zend_set_converter_error_mode */ /* {{{ zend_set_converter_error_mode */
void zend_set_converter_error_mode(UConverter *conv, uint8_t error_mode) void zend_set_converter_error_mode(UConverter *conv, uint8_t error_mode)
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
switch (error_mode) { switch (error_mode) {
case ZEND_CONV_ERROR_STOP: case ZEND_CONV_ERROR_STOP:
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_STOP, NULL, NULL, NULL, &status); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_STOP, NULL, NULL, NULL, &status);
break; break;
case ZEND_CONV_ERROR_SKIP: case ZEND_CONV_ERROR_SKIP:
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_SKIP, UCNV_SKIP_STOP_ON_ILLEGAL, NULL, NULL, &status); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_SKIP, UCNV_SKIP_STOP_ON_ILLEGAL, NULL, NULL, &status);
break; break;
case ZEND_CONV_ERROR_SUBST: case ZEND_CONV_ERROR_SUBST:
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_SUBSTITUTE, UCNV_SUB_STOP_ON_ILLEGAL, NULL, NULL, &status); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_SUBSTITUTE, UCNV_SUB_STOP_ON_ILLEGAL, NULL, NULL, &status);
break; break;
case ZEND_CONV_ERROR_ESCAPE_UNICODE: case ZEND_CONV_ERROR_ESCAPE_UNICODE:
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_UNICODE, NULL, NULL, &status); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_UNICODE, NULL, NULL, &status);
break; break;
case ZEND_CONV_ERROR_ESCAPE_ICU: case ZEND_CONV_ERROR_ESCAPE_ICU:
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_ICU, NULL, NULL, &status); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_ICU, NULL, NULL, &status);
break; break;
case ZEND_CONV_ERROR_ESCAPE_JAVA: case ZEND_CONV_ERROR_ESCAPE_JAVA:
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_JAVA, NULL, NULL, &status); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_JAVA, NULL, NULL, &status);
break; break;
case ZEND_CONV_ERROR_ESCAPE_XML_DEC: case ZEND_CONV_ERROR_ESCAPE_XML_DEC:
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_XML_DEC, NULL, NULL, &status); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_XML_DEC, NULL, NULL, &status);
break; break;
case ZEND_CONV_ERROR_ESCAPE_XML_HEX: case ZEND_CONV_ERROR_ESCAPE_XML_HEX:
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_XML_HEX, NULL, NULL, &status); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_XML_HEX, NULL, NULL, &status);
break; break;
default: default:
assert(0); assert(0);
break; break;
} }
} }
/* }}} */ /* }}} */
/* {{{ zend_set_converter_subst_char */ /* {{{ zend_set_converter_subst_char */
void zend_set_converter_subst_char(UConverter *conv, UChar *subst_char, int8_t subst_char_len) void zend_set_converter_subst_char(UConverter *conv, UChar *subst_char, int8_t subst_char_len)
{ {
char dest[8]; char dest[8];
int8_t dest_len = 8; int8_t dest_len = 8;
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
UErrorCode temp = U_ZERO_ERROR; UErrorCode temp = U_ZERO_ERROR;
const void *old_context; const void *old_context;
UConverterFromUCallback old_cb; UConverterFromUCallback old_cb;
if (!subst_char_len) if (!subst_char_len)
return; return;
ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_STOP, NULL, &old_cb, &old_context, &temp); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_STOP, NULL, &old_cb, &old_context, &temp);
dest_len = ucnv_fromUChars(conv, dest, dest_len, subst_char, subst_char_len, &status); dest_len = ucnv_fromUChars(conv, dest, dest_len, subst_char, subst_char_len, &status);
ucnv_setFromUCallBack(conv, old_cb, old_context, NULL, NULL, &temp); ucnv_setFromUCallBack(conv, old_cb, old_context, NULL, NULL, &temp);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
zend_error(E_WARNING, "Could not set substitution character for the converter"); zend_error(E_WARNING, "Could not set substitution character for the converter");
return; return;
} }
ucnv_setSubstChars(conv, dest, dest_len, &status); ucnv_setSubstChars(conv, dest, dest_len, &status);
if (status == U_ILLEGAL_ARGUMENT_ERROR) { if (status == U_ILLEGAL_ARGUMENT_ERROR) {
zend_error(E_WARNING, "Substitution character byte sequence is too short or long for this converter"); zend_error(E_WARNING, "Substitution character byte sequence is too short or long for this converter");
return; return;
} }
} }
/* }}} */ /* }}} */
/* {{{ zend_set_converter_encoding */ /* {{{ zend_set_converter_encoding */
int zend_set_converter_encoding(UConverter **converter, const char *encoding) int zend_set_converter_encoding(UConverter **converter, const char *encoding)
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
UConverter *new_converter = NULL; UConverter *new_converter = NULL;
if (!converter) { if (!converter) {
return FAILURE; return FAILURE;
} }
/* /*
* The specified encoding might be the same as converter's existing one, * The specified encoding might be the same as converter's existing one,
* which results in a no-op. * which results in a no-op.
*/ */
if (*converter && encoding && encoding[0]) { if (*converter && encoding && encoding[0]) {
const char *current = ucnv_getName(*converter, &status); const char *current = ucnv_getName(*converter, &status);
status = U_ZERO_ERROR; /* reset error */ status = U_ZERO_ERROR; /* reset error */
if (!ucnv_compareNames(current, encoding)) { if (!ucnv_compareNames(current, encoding)) {
return SUCCESS; return SUCCESS;
} }
} }
/* /*
* If encoding is NULL, ucnv_open() will return a converter based on * If encoding is NULL, ucnv_open() will return a converter based on
* the default platform encoding as determined by ucnv_getDefaultName(). * the default platform encoding as determined by ucnv_getDefaultName().
*/ */
new_converter = ucnv_open(encoding, &status); new_converter = ucnv_open(encoding, &status);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return FAILURE; return FAILURE;
} }
if (*converter) { if (*converter) {
ucnv_close(*converter); ucnv_close(*converter);
} }
*converter = new_converter; *converter = new_converter;
return SUCCESS; return SUCCESS;
} }
/* }}} */ /* }}} */
/* {{{ zend_copy_converter */ /* {{{ zend_copy_converter */
int zend_copy_converter(UConverter **target, UConverter *source) int zend_copy_converter(UConverter **target, UConverter *source)
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
const char *encoding; const char *encoding;
assert(source != NULL); assert(source != NULL);
encoding = ucnv_getName(source, &status); encoding = ucnv_getName(source, &status);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return FAILURE; return FAILURE;
} }
return zend_set_converter_encoding(target, encoding); return zend_set_converter_encoding(target, encoding);
} }
/* }}} */ /* }}} */
/* {{{ zend_convert_to_unicode */ /* {{{ zend_convert_to_unicode */
ZEND_API void zend_convert_to_unicode(UConverter *conv, UChar **target, int *target_len, const char *source, int source_len, UErrorCode *status) ZEND_API void zend_convert_to_unicode(UConverter *conv, UChar **target, int *target_len, const char *source, int source_len, UErrorCode *status)
{ {
UChar *buffer = NULL; UChar *buffer = NULL;
UChar *output; UChar *output;
int32_t buffer_len = 0; int32_t buffer_len = 0;
int32_t converted = 0; int32_t converted = 0;
const char *input = source; const char *input = source;
UConverterType conv_type; UConverterType conv_type;
if (U_FAILURE(*status)) { if (U_FAILURE(*status)) {
return; return;
} }
ucnv_resetToUnicode(conv); ucnv_resetToUnicode(conv);
conv_type = ucnv_getType(conv); conv_type = ucnv_getType(conv);
switch (conv_type) { switch (conv_type) {
case UCNV_SBCS: case UCNV_SBCS:
case UCNV_LATIN_1: case UCNV_LATIN_1:
case UCNV_US_ASCII: case UCNV_US_ASCII:
/* /*
* For single-byte charsets, 1 input byte = 1 output UChar * For single-byte charsets, 1 input byte = 1 output UChar
*/ */
buffer_len = source_len; buffer_len = source_len;
break; break;
default: default:
/* /*
* Initial estimate: 1.25 UChar's for every 2 source bytes + 2 (past a * Initial estimate: 1.25 UChar's for every 2 source bytes + 2 (past a
* certain limit (2)). The rationale behind this is that (atleast * certain limit (2)). The rationale behind this is that (atleast
* in the case of GB2312) it is possible that there are single byte * in the case of GB2312) it is possible that there are single byte
* characters in the input string. By using an GD2312 text as * characters in the input string. By using an GD2312 text as
* example it seemed that a value of 1.25 allowed for as little * example it seemed that a value of 1.25 allowed for as little
* re-allocations as possible without over estimating the buffer * re-allocations as possible without over estimating the buffer
* too much. In case there is a lot of single-byte characters * too much. In case there is a lot of single-byte characters
* around a single multi-byte character this estimation is too low, * around a single multi-byte character this estimation is too low,
* and then the re-allocation routines in the loop below kick in. * and then the re-allocation routines in the loop below kick in.
* There we multiply by 1.33 and add 1 so that it's quite efficient * There we multiply by 1.33 and add 1 so that it's quite efficient
* for smaller input strings without causing too many iterations of * for smaller input strings without causing too many iterations of
* this loop. * this loop.
*/ */
buffer_len = (source_len > 2) ? ((source_len >> 1) + (source_len >> 3) + 2) : source_len; buffer_len = (source_len > 2) ? ((source_len >> 1) + (source_len >> 3) + 2) : source_len;
break; break;
} }
while (1) { while (1) {
buffer = eurealloc(buffer, buffer_len + 1); buffer = eurealloc(buffer, buffer_len + 1);
output = buffer + converted; output = buffer + converted;
ucnv_toUnicode(conv, &output, buffer + buffer_len, &input, source + source_len, NULL, TRUE, status); ucnv_toUnicode(conv, &output, buffer + buffer_len, &input, source + source_len, NULL, TRUE, status);
converted = (int32_t) (output - buffer); converted = (int32_t) (output - buffer);
if (*status == U_BUFFER_OVERFLOW_ERROR) { if (*status == U_BUFFER_OVERFLOW_ERROR) {
buffer_len = (buffer_len * 1.33) + 1; buffer_len = (buffer_len * 1.33) + 1;
*status = U_ZERO_ERROR; *status = U_ZERO_ERROR;
} else { } else {
break; break;
} }
} }
/* /*
* We return the buffer in case of failure anyway. The caller may want to * We return the buffer in case of failure anyway. The caller may want to
* use partially converted string for something. * use partially converted string for something.
*/ */
buffer[converted] = 0; buffer[converted] = 0;
*target = buffer; *target = buffer;
*target_len = converted; *target_len = converted;
} }
/* }}} */ /* }}} */
/* {{{ zend_convert_from_unicode */ /* {{{ zend_convert_from_unicode */
ZEND_API int zend_convert_from_unicode(UConverter *conv, char **target, int *target_len, const UChar *source, int source_len, UErrorCode *status) ZEND_API int zend_convert_from_unicode(UConverter *conv, char **target, int *target_len, const UChar *source, int source_len, UErrorCode *status)
{ {
char *buffer = NULL; char *buffer = NULL;
char *output; char *output;
int32_t buffer_len = 0; int32_t buffer_len = 0;
int32_t converted = 0; int32_t converted = 0;
const UChar *input = source; const UChar *input = source;
if (U_FAILURE(*status)) { if (U_FAILURE(*status)) {
return 0; return 0;
} }
ucnv_resetFromUnicode(conv); ucnv_resetFromUnicode(conv);
buffer_len = ucnv_getMaxCharSize(conv) * source_len; buffer_len = ucnv_getMaxCharSize(conv) * source_len;
while (1) { while (1) {
buffer = erealloc(buffer, buffer_len + 1); buffer = erealloc(buffer, buffer_len + 1);
output = buffer + converted; output = buffer + converted;
ucnv_fromUnicode(conv, &output, buffer + buffer_len, &input, source + source_len, NULL, TRUE, status); ucnv_fromUnicode(conv, &output, buffer + buffer_len, &input, source + source_len, NULL, TRUE, status);
converted = (int32_t) (output - buffer); converted = (int32_t) (output - buffer);
if (*status == U_BUFFER_OVERFLOW_ERROR) { if (*status == U_BUFFER_OVERFLOW_ERROR) {
buffer_len += 64; buffer_len += 64;
*status = U_ZERO_ERROR; *status = U_ZERO_ERROR;
} else { } else {
break; break;
} }
} }
/* /*
* We return the buffer in case of failure anyway. The caller may want to * We return the buffer in case of failure anyway. The caller may want to
* use partially converted string for something. * use partially converted string for something.
*/ */
buffer[converted] = 0; /* NULL-terminate the output string */ buffer[converted] = 0; /* NULL-terminate the output string */
*target = buffer; *target = buffer;
*target_len = converted; *target_len = converted;
return input - source; return input - source;
} }
/* }}} */ /* }}} */
/* {{{ zend_convert_encodings */ /* {{{ zend_convert_encodings */
ZEND_API void zend_convert_encodings(UConverter *target_conv, UConverter *source_conv, ZEND_API void zend_convert_encodings(UConverter *target_conv, UConverter *source_conv,
char **target, int *target_len, char **target, int *target_len,
const char *source, int source_len, UErrorCode *status) const char *source, int source_len, UErrorCode *status)
{ {
char *buffer = NULL; char *buffer = NULL;
char *output; char *output;
const char *input = source; const char *input = source;
int32_t allocated = 0; int32_t allocated = 0;
int32_t converted = 0; int32_t converted = 0;
int8_t null_size; int8_t null_size;
UChar pivot_buf[1024], *pivot, *pivot2; UChar pivot_buf[1024], *pivot, *pivot2;
if (U_FAILURE(*status)) { if (U_FAILURE(*status)) {
return; return;
} }
null_size = ucnv_getMinCharSize(target_conv); null_size = ucnv_getMinCharSize(target_conv);
allocated = source_len + null_size; allocated = source_len + null_size;
ucnv_resetToUnicode(source_conv); ucnv_resetToUnicode(source_conv);
ucnv_resetFromUnicode(target_conv); ucnv_resetFromUnicode(target_conv);
pivot = pivot2 = pivot_buf; pivot = pivot2 = pivot_buf;
while (1) { while (1) {
buffer = (char *) erealloc(buffer, allocated); buffer = (char *) erealloc(buffer, allocated);
output = buffer + converted; output = buffer + converted;
ucnv_convertEx(target_conv, source_conv, &output, buffer + allocated - null_size, ucnv_convertEx(target_conv, source_conv, &output, buffer + allocated - null_size,
&input, source + source_len, pivot_buf, &pivot, &pivot2, pivot_buf + 1024, FALSE, TRUE, status); &input, source + source_len, pivot_buf, &pivot, &pivot2, pivot_buf + 1024, FALSE, TRUE, status);
converted = (int32_t) (output - buffer); converted = (int32_t) (output - buffer);
if (*status == U_BUFFER_OVERFLOW_ERROR) { if (*status == U_BUFFER_OVERFLOW_ERROR) {
allocated += 1024; allocated += 1024;
*status = U_ZERO_ERROR; *status = U_ZERO_ERROR;
} else { } else {
break; break;
} }
} }
memset(buffer + converted, 0, null_size); /* NULL-terminate the output string */ memset(buffer + converted, 0, null_size); /* NULL-terminate the output string */
*target = buffer; *target = buffer;
*target_len = converted; *target_len = converted;
/* Report the conversion error */ /* Report the conversion error */
if (U_FAILURE(*status)) { if (U_FAILURE(*status)) {
zend_error(E_NOTICE, "Error converting from codepage string to Unicode: %s", u_errorName(*status)); zend_error(E_NOTICE, "Error converting from codepage string to Unicode: %s", u_errorName(*status));
} }
} }
/* }}} */ /* }}} */
/* {{{ zend_raise_conversion_error_ex */ /* {{{ zend_raise_conversion_error_ex */
ZEND_API void zend_raise_conversion_error_ex(char *message, UConverter *conv, int error_char_offset, int use_exception TSRMLS_DC) ZEND_API void zend_raise_conversion_error_ex(char *message, UConverter *conv, int error_char_offset, int use_exception TSRMLS_DC)
{ {
UChar err_char[U16_MAX_LENGTH]; UChar err_char[U16_MAX_LENGTH];
int8_t err_char_len = sizeof(err_char); int8_t err_char_len = sizeof(err_char);
UChar32 codepoint; UChar32 codepoint;
const char *conv_name; const char *conv_name;
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
char *reason_fmt = "%s (converter %s failed on character {U+%04X} at offset %d)"; char *reason_fmt = "%s (converter %s failed on character {U+%04X} at offset %d)";
char *no_reason_fmt = "%s"; char *no_reason_fmt = "%s";
char *message_fmt; char *message_fmt;
if (!message) if (!message)
return; return;
ucnv_getInvalidUChars(conv, err_char, &err_char_len, &status); ucnv_getInvalidUChars(conv, err_char, &err_char_len, &status);
conv_name = ucnv_getName(conv, &status); conv_name = ucnv_getName(conv, &status);
/* /*
* UTODO * UTODO
* use some other standard than MIME? or fallback onto IANA? or use * use some other standard than MIME? or fallback onto IANA? or use
* internal converter name? ponder * internal converter name? ponder
*/ */
conv_name = ucnv_getStandardName(conv_name, "MIME", &status); conv_name = ucnv_getStandardName(conv_name, "MIME", &status);
codepoint = (err_char_len < 2) ? err_char[0] : U16_GET_SUPPLEMENTARY(err_char[0], err_char[1]); codepoint = (err_char_len < 2) ? err_char[0] : U16_GET_SUPPLEMENTARY(err_char[0], err_char[1]);
; ;
message_fmt = conv ? reason_fmt : no_reason_fmt; message_fmt = conv ? reason_fmt : no_reason_fmt;
if (use_exception) { if (use_exception) {
zend_throw_exception_ex(unicodeConversionException, 0 TSRMLS_CC, message_fmt, message, conv_name?conv_name:"<unknown>", codepoint, error_char_offset); zend_throw_exception_ex(unicodeConversionException, 0 TSRMLS_CC, message_fmt, message, conv_name?conv_name:"<unknown>", codepoint, error_char_offset);
} else { } else {
zend_error(E_WARNING, message_fmt, message, conv_name?conv_name:"", codepoint, error_char_offset); zend_error(E_WARNING, message_fmt, message, conv_name?conv_name:"", codepoint, error_char_offset);
} }
} }
/* }}} */ /* }}} */
/* {{{ zval_unicode_to_string */ /* {{{ zval_unicode_to_string */
ZEND_API int zval_unicode_to_string(zval *string, UConverter *conv TSRMLS_DC) ZEND_API int zval_unicode_to_string(zval *string, UConverter *conv TSRMLS_DC)
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
int retval = TRUE; int retval = TRUE;
char *s = NULL; char *s = NULL;
int s_len; int s_len;
int num_conv; int num_conv;
UChar *u = Z_USTRVAL_P(string); UChar *u = Z_USTRVAL_P(string);
int u_len = Z_USTRLEN_P(string); int u_len = Z_USTRLEN_P(string);
num_conv = zend_convert_from_unicode(conv, &s, &s_len, u, u_len, &status); num_conv = zend_convert_from_unicode(conv, &s, &s_len, u, u_len, &status);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
int32_t offset = u_countChar32(u, num_conv)-1; int32_t offset = u_countChar32(u, num_conv)-1;
if (s) { if (s) {
efree(s); efree(s);
} }
zend_raise_conversion_error_ex("Could not convert Unicode string to binary string", conv, offset, (UG(from_u_error_mode) & ZEND_CONV_ERROR_EXCEPTION) TSRMLS_CC); zend_raise_conversion_error_ex("Could not convert Unicode string to binary string", conv, offset, (UG(from_u_error_mode) & ZEND_CONV_ERROR_EXCEPTION) TSRMLS_CC);
retval = FAILURE; retval = FAILURE;
} }
ZVAL_STRINGL(string, s, s_len, 0); ZVAL_STRINGL(string, s, s_len, 0);
efree((UChar*)u); efree((UChar*)u);
return retval; return retval;
} }
/* }}} */ /* }}} */
/* {{{ zval_string_to_unicode_ex */ /* {{{ zval_string_to_unicode_ex */
ZEND_API int zval_string_to_unicode_ex(zval *string, UConverter *conv) ZEND_API int zval_string_to_unicode_ex(zval *string, UConverter *conv)
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
int retval = TRUE; int retval = TRUE;
UChar *u = NULL; UChar *u = NULL;
int u_len; int u_len;
char *s = Z_STRVAL_P(string); char *s = Z_STRVAL_P(string);
int s_len = Z_STRLEN_P(string); int s_len = Z_STRLEN_P(string);
Z_TYPE_P(string) = IS_UNICODE; Z_TYPE_P(string) = IS_UNICODE;
zend_convert_to_unicode(conv, &u, &u_len, s, s_len, &status); zend_convert_to_unicode(conv, &u, &u_len, s, s_len, &status);
ZVAL_UNICODEL(string, u, u_len, 0); ZVAL_UNICODEL(string, u, u_len, 0);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
retval = FALSE; retval = FALSE;
} }
efree(s); efree(s);
return retval; return retval;
} }
/* }}} */ /* }}} */
@ -425,20 +425,20 @@ ZEND_API int zval_string_to_unicode(zval *string TSRMLS_DC)
/* {{{ zend_cmp_unicode_and_string */ /* {{{ zend_cmp_unicode_and_string */
ZEND_API int zend_cmp_unicode_and_string(UChar *ustr, char* str, uint len) ZEND_API int zend_cmp_unicode_and_string(UChar *ustr, char* str, uint len)
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
UChar *u = NULL; UChar *u = NULL;
int u_len; int u_len;
int retval = TRUE; int retval = TRUE;
TSRMLS_FETCH(); TSRMLS_FETCH();
zend_convert_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &u, &u_len, str, len, &status); zend_convert_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &u, &u_len, str, len, &status);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
efree(u); efree(u);
return FALSE; return FALSE;
} }
retval = u_memcmp(ustr, u, u_len); retval = u_memcmp(ustr, u, u_len);
efree(u); efree(u);
return retval; return retval;
} }
/* }}} */ /* }}} */
@ -450,145 +450,151 @@ ZEND_API int zend_cmp_unicode_and_string(UChar *ustr, char* str, uint len)
*/ */
ZEND_API int zend_cmp_unicode_and_literal(UChar *ustr, int ulen, char *str, int slen) ZEND_API int zend_cmp_unicode_and_literal(UChar *ustr, int ulen, char *str, int slen)
{ {
int result; int result;
uint len = MIN(ulen, slen); uint len = MIN(ulen, slen);
while (len--) { while (len--) {
result = (int)(uint16_t)*ustr - (int)(uint16_t)*str; result = (int)(uint16_t)*ustr - (int)(uint16_t)*str;
if (result != 0) if (result != 0)
return result; return result;
ustr++; ustr++;
str++; str++;
} }
return ulen - slen; return ulen - slen;
} }
/* }}} */ /* }}} */
/* {{{ zend_is_valid_identifier */ /* {{{ zend_is_valid_identifier */
ZEND_API int zend_is_valid_identifier(UChar *ident, int len) ZEND_API int zend_is_valid_identifier(UChar *ident, int len)
{ {
UChar32 codepoint; UChar32 codepoint;
int32_t i; int32_t i;
int32_t ident_len = len; int32_t ident_len = len;
UProperty id_prop = UCHAR_XID_START; UProperty id_prop = UCHAR_XID_START;
for (i = 0; i < ident_len; ) { for (i = 0; i < ident_len; ) {
U16_NEXT(ident, i, ident_len, codepoint); U16_NEXT(ident, i, ident_len, codepoint);
if (!u_hasBinaryProperty(codepoint, id_prop) && if (!u_hasBinaryProperty(codepoint, id_prop) &&
codepoint != 0x5f) { /* special case for starting '_' */ codepoint != 0x5f) { /* special case for starting '_' */
return 0; return 0;
} }
id_prop = UCHAR_XID_CONTINUE; id_prop = UCHAR_XID_CONTINUE;
} }
return 1; return 1;
} }
/* }}} */ /* }}} */
/* {{{ zend_normalize_string */ /* {{{ zend_normalize_string */
static inline void zend_normalize_string(UChar **dest, int32_t *dest_len, UChar *src, int src_len, UErrorCode *status) static inline void zend_normalize_string(UChar **dest, int32_t *dest_len, UChar *src, int src_len, UErrorCode *status)
{ {
UChar *buffer = NULL; UChar *buffer = NULL;
int32_t buffer_len; int32_t buffer_len;
buffer_len = src_len; buffer_len = src_len;
while (1) { while (1) {
*status = U_ZERO_ERROR; *status = U_ZERO_ERROR;
buffer = eurealloc(buffer, buffer_len+1); buffer = eurealloc(buffer, buffer_len+1);
buffer_len = unorm_normalize(src, src_len, UNORM_NFKC, 0, buffer, buffer_len, status); buffer_len = unorm_normalize(src, src_len, UNORM_NFKC, 0, buffer, buffer_len, status);
if (*status != U_BUFFER_OVERFLOW_ERROR) { if (*status != U_BUFFER_OVERFLOW_ERROR) {
break; break;
} }
} }
if (U_SUCCESS(*status)) { if (U_SUCCESS(*status)) {
buffer[buffer_len] = 0; buffer[buffer_len] = 0;
*dest = buffer; *dest = buffer;
*dest_len = buffer_len; *dest_len = buffer_len;
} else { } else {
efree(buffer); efree(buffer);
} }
} }
/* }}} */ /* }}} */
/* {{{ zend_case_fold_string */ /* {{{ zend_case_fold_string */
ZEND_API void zend_case_fold_string(UChar **dest, int *dest_len, UChar *src, int src_len, uint32_t options, UErrorCode *status) ZEND_API void zend_case_fold_string(UChar **dest, int *dest_len, UChar *src, int src_len, uint32_t options, UErrorCode *status)
{ {
UChar *buffer = NULL; UChar *buffer = NULL;
int32_t buffer_len; int32_t buffer_len;
buffer_len = src_len; buffer_len = src_len;
while (1) { while (1) {
*status = U_ZERO_ERROR; *status = U_ZERO_ERROR;
buffer = eurealloc(buffer, buffer_len+1); buffer = eurealloc(buffer, buffer_len+1);
buffer_len = u_strFoldCase(buffer, buffer_len, src, src_len, options, status); buffer_len = u_strFoldCase(buffer, buffer_len, src, src_len, options, status);
if (*status != U_BUFFER_OVERFLOW_ERROR) { if (*status != U_BUFFER_OVERFLOW_ERROR) {
break; break;
} }
} }
if (U_SUCCESS(*status)) { if (U_SUCCESS(*status)) {
buffer[buffer_len] = 0; buffer[buffer_len] = 0;
*dest = buffer; *dest = buffer;
*dest_len = buffer_len; *dest_len = buffer_len;
} else { } else {
efree(buffer); efree(buffer);
} }
} }
/* }}} */ /* }}} */
/* {{{ zend_normalize_identifier */ /* {{{ zend_normalize_identifier */
ZEND_API int zend_normalize_identifier(UChar **dest, int *dest_len, UChar *ident, int ident_len, zend_bool fold_case) ZEND_API int zend_normalize_identifier(UChar **dest, int *dest_len, UChar *ident, int ident_len, zend_bool fold_case)
{ {
UChar *buffer = NULL; UChar *buffer = NULL;
UChar *orig_ident = ident; UChar *orig_ident = ident;
int32_t buffer_len; int32_t buffer_len;
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
if (unorm_quickCheck(ident, ident_len, UNORM_NFKC, &status) != UNORM_YES) { if (unorm_quickCheck(ident, ident_len, UNORM_NFKC, &status) != UNORM_YES) {
zend_normalize_string(&buffer, &buffer_len, ident, ident_len, &status); zend_normalize_string(&buffer, &buffer_len, ident, ident_len, &status);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return 0; return 0;
} }
ident = buffer; ident = buffer;
ident_len = buffer_len; ident_len = buffer_len;
} }
if (fold_case) { if (fold_case) {
zend_case_fold_string(&buffer, &buffer_len, ident, ident_len, U_FOLD_CASE_DEFAULT, &status); zend_case_fold_string(&buffer, &buffer_len, ident, ident_len, U_FOLD_CASE_DEFAULT, &status);
if (ident != orig_ident) { if (ident != orig_ident) {
efree(ident); efree(ident);
} }
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return 0; return 0;
} }
ident = buffer; ident = buffer;
ident_len = buffer_len; ident_len = buffer_len;
if (unorm_quickCheck(ident, ident_len, UNORM_NFKC, &status) != UNORM_YES) { if (unorm_quickCheck(ident, ident_len, UNORM_NFKC, &status) != UNORM_YES) {
zend_normalize_string(&buffer, &buffer_len, ident, ident_len, &status); zend_normalize_string(&buffer, &buffer_len, ident, ident_len, &status);
if (ident != orig_ident) { if (ident != orig_ident) {
efree(ident); efree(ident);
} }
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return 0; return 0;
} }
ident = buffer; ident = buffer;
ident_len = buffer_len; ident_len = buffer_len;
} }
} }
*dest = ident; *dest = ident;
*dest_len = ident_len; *dest_len = ident_len;
return 1; return 1;
} }
/* }}} */ /* }}} */
void zend_register_unicode_exceptions(TSRMLS_D) void zend_register_unicode_exceptions(TSRMLS_D)
{ {
zend_class_entry ce; zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "UnicodeConversionException", NULL); INIT_CLASS_ENTRY(ce, "UnicodeConversionException", NULL);
unicodeConversionException = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC); unicodeConversionException = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
} }
/* vim: set fdm=marker et sts=4: */ /*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/