Skip to content

Commit 740f0df

Browse files
committed
ext/intl: introduce SpoofChecker::areBidiConfusable.
Adding a new more refined spoofchecker method in addition of the existing SpoofChecker::areConfusable which takes in account the text direction left to right and right to left. Adding UBIDI_LTR, UBIDI_RTL, UBIDI_MIXED and UBIDI_NEUTRAL.
1 parent ec9ae1e commit 740f0df

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

ext/intl/spoofchecker/spoofchecker.stub.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class Spoofchecker
3939
/** @cvalue USPOOF_HIDDEN_OVERLAY */
4040
public const int HIDDEN_OVERLAY = UNKNOWN;
4141
#endif
42+
#if U_ICU_VERSION_MAJOR_NUM >= 74
43+
/** @cvalue UBIDI_LTR */
44+
public const int UBIDI_LTR = UNKNOWN;
45+
/** @cvalue UBIDI_RTL */
46+
public const int UBIDI_RTL = UNKNOWN;
47+
/** @cvalue UBIDI_MIXED */
48+
public const int UBIDI_MIXED = UNKNOWN;
49+
/** @cvalue UBIDI_NEUTRAL */
50+
public const int UBIDI_NEUTRAL = UNKNOWN;
51+
#endif
4252

4353
public function __construct() {}
4454

@@ -64,4 +74,10 @@ public function setChecks(int $checks): void {}
6474
/** @tentative-return-type */
6575
public function setRestrictionLevel(int $level): void {}
6676
#endif
77+
#if U_ICU_VERSION_MAJOR_NUM >= 74
78+
/**
79+
* @param int $errorCode
80+
*/
81+
public function areBidiConfusable(int $direction, string $string1, string $string2, &$errorCode = null): bool {}
82+
#endif
6783
}

ext/intl/spoofchecker/spoofchecker_arginfo.h

Lines changed: 48 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/spoofchecker/spoofchecker_main.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,42 @@ PHP_METHOD(Spoofchecker, setRestrictionLevel)
167167
}
168168
/* }}} */
169169
#endif
170+
171+
#if U_ICU_VERSION_MAJOR_NUM >= 74
172+
PHP_METHOD(Spoofchecker, areBidiConfusable)
173+
{
174+
int ret;
175+
char *id1, *id2;
176+
size_t length1, length2;
177+
zend_long direction;
178+
zval *error_code = NULL;
179+
SPOOFCHECKER_METHOD_INIT_VARS;
180+
181+
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "lss|z", &direction, &id1, &length1,
182+
&id2, &length2, &error_code)) {
183+
RETURN_THROWS();
184+
}
185+
186+
SPOOFCHECKER_METHOD_FETCH_OBJECT;
187+
if (direction != UBIDI_LTR && direction != UBIDI_RTL) {
188+
zend_argument_value_error(1, "must be either SpoofChecker::UBIDI_LTR or SpoofChecker::UBIDI_RTL");
189+
RETURN_THROWS();
190+
}
191+
if(length1 > INT32_MAX || length2 > INT32_MAX) {
192+
SPOOFCHECKER_ERROR_CODE(co) = U_BUFFER_OVERFLOW_ERROR;
193+
} else {
194+
ret = uspoof_areBidiConfusableUTF8(co->uspoof, (UBiDiDirection)direction, id1, (int32_t)length1, id2, (int32_t)length2, SPOOFCHECKER_ERROR_CODE_P(co));
195+
}
196+
if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
197+
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
198+
RETURN_TRUE;
199+
}
200+
201+
if (error_code) {
202+
zval_ptr_dtor(error_code);
203+
ZVAL_LONG(Z_REFVAL_P(error_code), ret);
204+
Z_TRY_ADDREF_P(error_code);
205+
}
206+
RETVAL_BOOL(ret != 0);
207+
}
208+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
spoofchecker checks if strings are confusable in a given direction.
3+
--EXTENSIONS--
4+
intl
5+
--SKIPIF--
6+
<?php if(!class_exists("Spoofchecker")) print 'skip'; ?>
7+
<?php if (version_compare(INTL_ICU_VERSION, '74.0') < 0)die('skip for ICU >= 74'); ?>
8+
--FILE--
9+
<?php
10+
$s = new SpoofChecker();
11+
try {
12+
$s->areBidiConfusable(SpoofChecker::UBIDI_RTL + 1, "a", "a");
13+
} catch (ValueError $e) {
14+
echo $e->getMessage() . PHP_EOL;
15+
}
16+
var_dump($s->areBidiConfusable(SpoofChecker::UBIDI_LTR, "\x73\x63", "sc"));
17+
var_dump($s->areBidiConfusable(SpoofChecker::UBIDI_RTL, "sc", "\x73\x63"));
18+
--EXPECT--
19+
Spoofchecker::areBidiConfusable(): Argument #1 ($direction) must be either SpoofChecker::UBIDI_LTR or SpoofChecker::UBIDI_RTL
20+
bool(true)
21+
bool(true)

0 commit comments

Comments
 (0)