From 6186b676000f74af92b979382f605ed104b6a4bc Mon Sep 17 00:00:00 2001 From: Martin Jansen Date: Sat, 5 Jan 2013 22:46:14 +0100 Subject: [PATCH] Add option to specific the expected separator character. --- ext/filter/logical_filters.c | 15 ++++++++++++++- ext/filter/tests/055.phpt | 36 +++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c index fededcf715..9b436a779e 100644 --- a/ext/filter/logical_filters.c +++ b/ext/filter/logical_filters.c @@ -788,9 +788,18 @@ void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ { char *input = Z_STRVAL_P(value); int input_len = Z_STRLEN_P(value); - int tokens, length, i, offset; + int tokens, length, i, offset, exp_separator_set, exp_separator_len; char separator; + char *exp_separator; long ret = 0; + zval **option_val; + + FETCH_STRING_OPTION(exp_separator, "separator"); + + if (exp_separator_set && exp_separator_len != 1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Separator must be exactly one character long"); + RETURN_VALIDATION_FAILED; + } if (14 == input_len) { /* EUI-64 format: Four hexadecimal digits separated by dots. Less @@ -813,6 +822,10 @@ void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ RETURN_VALIDATION_FAILED; } + if (exp_separator_set && separator != exp_separator[0]) { + RETURN_VALIDATION_FAILED; + } + /* Essentially what we now have is a set of tokens each consisting of * a hexadecimal number followed by a separator character. (With the * exception of the last token which does not have the separator.) diff --git a/ext/filter/tests/055.phpt b/ext/filter/tests/055.phpt index bf94f3515f..688dbb2b54 100644 --- a/ext/filter/tests/055.phpt +++ b/ext/filter/tests/055.phpt @@ -5,24 +5,32 @@ filter_var() and FILTER_VALIDATE_MAC --FILE-- array("separator" => "-"))), + array("01-23-45-67-89-ab", array("options" => array("separator" => "."))), + array("01-23-45-67-89-ab", array("options" => array("separator" => ":"))), + array("01-23-45-67-89-AB", null), + array("01-23-45-67-89-aB", null), + array("01:23:45:67:89:ab", null), + array("01:23:45:67:89:AB", null), + array("01:23:45:67:89:aB", null), + array("01:23:45-67:89:aB", null), + array("xx:23:45:67:89:aB", null), + array("0123.4567.89ab", null), + array("01-23-45-67-89-ab", array("options" => array("separator" => "--"))), + array("01-23-45-67-89-ab", array("options" => array("separator" => ""))), ); foreach ($values as $value) { - var_dump(filter_var($value, FILTER_VALIDATE_MAC)); + var_dump(filter_var($value[0], FILTER_VALIDATE_MAC, $value[1])); } echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- string(17) "01-23-45-67-89-ab" +string(17) "01-23-45-67-89-ab" +bool(false) +bool(false) string(17) "01-23-45-67-89-AB" string(17) "01-23-45-67-89-aB" string(17) "01:23:45:67:89:ab" @@ -31,4 +39,10 @@ string(17) "01:23:45:67:89:aB" bool(false) bool(false) string(14) "0123.4567.89ab" + +Warning: filter_var(): Separator must be exactly one character long in %s055.php on line %d +bool(false) + +Warning: filter_var(): Separator must be exactly one character long in %s055.php on line %d +bool(false) Done -- 2.40.0