Skip to content

Commit 6186b67

Browse files
committed
Add option to specific the expected separator character.
1 parent 0661d03 commit 6186b67

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

ext/filter/logical_filters.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,18 @@ void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
788788
{
789789
char *input = Z_STRVAL_P(value);
790790
int input_len = Z_STRLEN_P(value);
791-
int tokens, length, i, offset;
791+
int tokens, length, i, offset, exp_separator_set, exp_separator_len;
792792
char separator;
793+
char *exp_separator;
793794
long ret = 0;
795+
zval **option_val;
796+
797+
FETCH_STRING_OPTION(exp_separator, "separator");
798+
799+
if (exp_separator_set && exp_separator_len != 1) {
800+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Separator must be exactly one character long");
801+
RETURN_VALIDATION_FAILED;
802+
}
794803

795804
if (14 == input_len) {
796805
/* EUI-64 format: Four hexadecimal digits separated by dots. Less
@@ -813,6 +822,10 @@ void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
813822
RETURN_VALIDATION_FAILED;
814823
}
815824

825+
if (exp_separator_set && separator != exp_separator[0]) {
826+
RETURN_VALIDATION_FAILED;
827+
}
828+
816829
/* Essentially what we now have is a set of tokens each consisting of
817830
* a hexadecimal number followed by a separator character. (With the
818831
* exception of the last token which does not have the separator.)

ext/filter/tests/055.phpt

+25-11
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,32 @@ filter_var() and FILTER_VALIDATE_MAC
55
--FILE--
66
<?php
77
$values = Array(
8-
"01-23-45-67-89-ab",
9-
"01-23-45-67-89-AB",
10-
"01-23-45-67-89-aB",
11-
"01:23:45:67:89:ab",
12-
"01:23:45:67:89:AB",
13-
"01:23:45:67:89:aB",
14-
"01:23:45-67:89:aB",
15-
"xx:23:45:67:89:aB",
16-
"0123.4567.89ab",
8+
array("01-23-45-67-89-ab", null),
9+
array("01-23-45-67-89-ab", array("options" => array("separator" => "-"))),
10+
array("01-23-45-67-89-ab", array("options" => array("separator" => "."))),
11+
array("01-23-45-67-89-ab", array("options" => array("separator" => ":"))),
12+
array("01-23-45-67-89-AB", null),
13+
array("01-23-45-67-89-aB", null),
14+
array("01:23:45:67:89:ab", null),
15+
array("01:23:45:67:89:AB", null),
16+
array("01:23:45:67:89:aB", null),
17+
array("01:23:45-67:89:aB", null),
18+
array("xx:23:45:67:89:aB", null),
19+
array("0123.4567.89ab", null),
20+
array("01-23-45-67-89-ab", array("options" => array("separator" => "--"))),
21+
array("01-23-45-67-89-ab", array("options" => array("separator" => ""))),
1722
);
1823
foreach ($values as $value) {
19-
var_dump(filter_var($value, FILTER_VALIDATE_MAC));
24+
var_dump(filter_var($value[0], FILTER_VALIDATE_MAC, $value[1]));
2025
}
2126

2227
echo "Done\n";
2328
?>
24-
--EXPECT--
29+
--EXPECTF--
2530
string(17) "01-23-45-67-89-ab"
31+
string(17) "01-23-45-67-89-ab"
32+
bool(false)
33+
bool(false)
2634
string(17) "01-23-45-67-89-AB"
2735
string(17) "01-23-45-67-89-aB"
2836
string(17) "01:23:45:67:89:ab"
@@ -31,4 +39,10 @@ string(17) "01:23:45:67:89:aB"
3139
bool(false)
3240
bool(false)
3341
string(14) "0123.4567.89ab"
42+
43+
Warning: filter_var(): Separator must be exactly one character long in %s055.php on line %d
44+
bool(false)
45+
46+
Warning: filter_var(): Separator must be exactly one character long in %s055.php on line %d
47+
bool(false)
3448
Done

0 commit comments

Comments
 (0)