]> granicus.if.org Git - php/commitdiff
Add option to specific the expected separator character.
authorMartin Jansen <martin@divbyzero.net>
Sat, 5 Jan 2013 21:46:14 +0000 (22:46 +0100)
committerMartin Jansen <martin@divbyzero.net>
Sun, 3 Feb 2013 12:23:54 +0000 (13:23 +0100)
ext/filter/logical_filters.c
ext/filter/tests/055.phpt

index fededcf715b14c4cd90df137d076b2aacd57929d..9b436a779e6ec06d5dce9792e5dc63852b25036b 100644 (file)
@@ -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.)
index bf94f3515f1756b97464c092b54c7f1a5b5bc9cc..688dbb2b540345a28e811a1f6c420292e5401a6a 100644 (file)
@@ -5,24 +5,32 @@ filter_var() and FILTER_VALIDATE_MAC
 --FILE--
 <?php
 $values = Array(
-       "01-23-45-67-89-ab",
-       "01-23-45-67-89-AB",
-       "01-23-45-67-89-aB",
-       "01:23:45:67:89:ab",
-       "01:23:45:67:89:AB",
-       "01:23:45:67:89:aB",
-       "01:23:45-67:89:aB",
-       "xx:23:45:67:89:aB",
-       "0123.4567.89ab",
+       array("01-23-45-67-89-ab", null),
+       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", 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