intl: use uspoof_check2UTF8 call when available. (#9478)

This commit is contained in:
David CARLIER 2022-09-06 13:07:07 +01:00 committed by GitHub
parent 163c0b5f4c
commit d67df602a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 2 deletions

View File

@ -129,6 +129,13 @@ void spoofchecker_object_destroy(Spoofchecker_object* co)
co->uspoof = NULL;
}
#if U_ICU_VERSION_MAJOR_NUM >= 58
if (co->uspoofres) {
uspoof_closeCheckResult(co->uspoofres);
co->uspoofres = NULL;
}
#endif
intl_error_reset(SPOOFCHECKER_ERROR_P(co));
}
/* }}} */

View File

@ -28,7 +28,10 @@ typedef struct {
intl_error err;
// ICU Spoofchecker
USpoofChecker* uspoof;
USpoofChecker* uspoof;
#if U_ICU_VERSION_MAJOR_NUM >= 58
USpoofCheckResult* uspoofres;
#endif
zend_object zo;
} Spoofchecker_object;

View File

@ -49,6 +49,7 @@ PHP_METHOD(Spoofchecker, __construct)
uspoof_check2 APIs when it became stable, to use extended check result APIs.
Subsequent changes in the unicode security algos are to be watched.*/
uspoof_setRestrictionLevel(co->uspoof, SPOOFCHECKER_DEFAULT_RESTRICTION_LEVEL);
co->uspoofres = uspoof_openCheckResult(SPOOFCHECKER_ERROR_CODE_P(co));
#else
/* Single-script enforcement is on by default. This fails for languages
like Japanese that legally use multiple scripts within a single word,

View File

@ -22,7 +22,7 @@
/* {{{ Checks if a given text contains any suspicious characters */
PHP_METHOD(Spoofchecker, isSuspicious)
{
int ret;
int32_t ret, errmask;
char *text;
size_t text_len;
zval *error_code = NULL;
@ -34,10 +34,21 @@ PHP_METHOD(Spoofchecker, isSuspicious)
SPOOFCHECKER_METHOD_FETCH_OBJECT;
#if U_ICU_VERSION_MAJOR_NUM >= 58
ret = uspoof_check2UTF8(co->uspoof, text, text_len, co->uspoofres, SPOOFCHECKER_ERROR_CODE_P(co));
#else
ret = uspoof_checkUTF8(co->uspoof, text, text_len, NULL, SPOOFCHECKER_ERROR_CODE_P(co));
#endif
if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
#if U_ICU_VERSION_MAJOR_NUM >= 58
errmask = uspoof_getCheckResultChecks(co->uspoofres, SPOOFCHECKER_ERROR_CODE_P(co));
if (errmask != ret) {
php_error_docref(NULL, E_WARNING, "unexpected error (%d), does not relate to the flags passed to setChecks (%d)", ret, errmask);
}
#endif
RETURN_TRUE;
}