8989#define FORMAT_IPV4 4
9090#define FORMAT_IPV6 6
9191
92- static int _php_filter_validate_ipv6 (const char * str , size_t str_len , int ip [8 ]);
92+ static bool _php_filter_validate_ipv6 (const char * str , size_t str_len , int ip [8 ]);
9393
94- static int php_filter_parse_int (const char * str , size_t str_len , zend_long * ret ) { /* {{{ */
94+ static bool php_filter_parse_int (const char * str , size_t str_len , zend_long * ret ) { /* {{{ */
9595 zend_long ctx_value ;
96- int sign = 0 , digit = 0 ;
96+ bool is_negative = false;
97+ int digit = 0 ;
9798 const char * end = str + str_len ;
9899
99100 switch (* str ) {
100101 case '-' :
101- sign = 1 ;
102+ is_negative = true ;
102103 ZEND_FALLTHROUGH ;
103104 case '+' :
104105 str ++ ;
@@ -108,43 +109,43 @@ static int php_filter_parse_int(const char *str, size_t str_len, zend_long *ret)
108109
109110 if (* str == '0' && str + 1 == end ) {
110111 /* Special cases: +0 and -0 */
111- return 1 ;
112+ return true ;
112113 }
113114
114115 /* must start with 1..9*/
115116 if (str < end && * str >= '1' && * str <= '9' ) {
116- ctx_value = (( sign ) ?-1 :1 ) * ((* (str ++ )) - '0' );
117+ ctx_value = (is_negative ?-1 :1 ) * ((* (str ++ )) - '0' );
117118 } else {
118- return -1 ;
119+ return false ;
119120 }
120121
121122 if ((end - str > MAX_LENGTH_OF_LONG - 1 ) /* number too long */
122123 || (SIZEOF_LONG == 4 && (end - str == MAX_LENGTH_OF_LONG - 1 ) && * str > '2' )) {
123124 /* overflow */
124- return -1 ;
125+ return false ;
125126 }
126127
127128 while (str < end ) {
128129 if (* str >= '0' && * str <= '9' ) {
129130 digit = (* (str ++ ) - '0' );
130- if ( (!sign ) && ctx_value <= (ZEND_LONG_MAX - digit )/10 ) {
131+ if ( (!is_negative ) && ctx_value <= (ZEND_LONG_MAX - digit )/10 ) {
131132 ctx_value = (ctx_value * 10 ) + digit ;
132- } else if ( sign && ctx_value >= (ZEND_LONG_MIN + digit )/10 ) {
133+ } else if ( is_negative && ctx_value >= (ZEND_LONG_MIN + digit )/10 ) {
133134 ctx_value = (ctx_value * 10 ) - digit ;
134135 } else {
135- return -1 ;
136+ return false ;
136137 }
137138 } else {
138- return -1 ;
139+ return false ;
139140 }
140141 }
141142
142143 * ret = ctx_value ;
143- return 1 ;
144+ return true ;
144145}
145146/* }}} */
146147
147- static int php_filter_parse_octal (const char * str , size_t str_len , zend_long * ret ) { /* {{{ */
148+ static bool php_filter_parse_octal (const char * str , size_t str_len , zend_long * ret ) { /* {{{ */
148149 zend_ulong ctx_value = 0 ;
149150 const char * end = str + str_len ;
150151
@@ -154,20 +155,20 @@ static int php_filter_parse_octal(const char *str, size_t str_len, zend_long *re
154155
155156 if ((ctx_value > ((zend_ulong )(~(zend_long )0 )) / 8 ) ||
156157 ((ctx_value = ctx_value * 8 ) > ((zend_ulong )(~(zend_long )0 )) - n )) {
157- return -1 ;
158+ return false ;
158159 }
159160 ctx_value += n ;
160161 } else {
161- return -1 ;
162+ return false ;
162163 }
163164 }
164165
165166 * ret = (zend_long )ctx_value ;
166- return 1 ;
167+ return true ;
167168}
168169/* }}} */
169170
170- static int php_filter_parse_hex (const char * str , size_t str_len , zend_long * ret ) { /* {{{ */
171+ static bool php_filter_parse_hex (const char * str , size_t str_len , zend_long * ret ) { /* {{{ */
171172 zend_ulong ctx_value = 0 ;
172173 const char * end = str + str_len ;
173174 zend_ulong n ;
@@ -180,17 +181,17 @@ static int php_filter_parse_hex(const char *str, size_t str_len, zend_long *ret)
180181 } else if (* str >= 'A' && * str <= 'F' ) {
181182 n = ((* (str ++ )) - ('A' - 10 ));
182183 } else {
183- return -1 ;
184+ return false ;
184185 }
185186 if ((ctx_value > ((zend_ulong )(~(zend_long )0 )) / 16 ) ||
186187 ((ctx_value = ctx_value * 16 ) > ((zend_ulong )(~(zend_long )0 )) - n )) {
187- return -1 ;
188+ return false ;
188189 }
189190 ctx_value += n ;
190191 }
191192
192193 * ret = (zend_long )ctx_value ;
193- return 1 ;
194+ return true ;
194195}
195196/* }}} */
196197
@@ -199,9 +200,9 @@ void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
199200 zval * option_val ;
200201 zend_long min_range , max_range , option_flags ;
201202 int min_range_set , max_range_set ;
202- int allow_octal = 0 , allow_hex = 0 ;
203+ bool allow_octal = false , allow_hex = false ;
203204 size_t len ;
204- int error = 0 ;
205+ bool error = false ;
205206 zend_long ctx_value ;
206207 const char * p ;
207208
@@ -217,11 +218,11 @@ void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
217218 }
218219
219220 if (option_flags & FILTER_FLAG_ALLOW_OCTAL ) {
220- allow_octal = 1 ;
221+ allow_octal = true ;
221222 }
222223
223224 if (option_flags & FILTER_FLAG_ALLOW_HEX ) {
224- allow_hex = 1 ;
225+ allow_hex = true ;
225226 }
226227
227228 /* Start the validating loop */
@@ -237,8 +238,8 @@ void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
237238 if (len == 0 ) {
238239 RETURN_VALIDATION_FAILED
239240 }
240- if (php_filter_parse_hex (p , len , & ctx_value ) < 0 ) {
241- error = 1 ;
241+ if (! php_filter_parse_hex (p , len , & ctx_value )) {
242+ error = true ;
242243 }
243244 } else if (allow_octal ) {
244245 /* Support explicit octal prefix notation */
@@ -248,19 +249,19 @@ void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
248249 RETURN_VALIDATION_FAILED
249250 }
250251 }
251- if (php_filter_parse_octal (p , len , & ctx_value ) < 0 ) {
252- error = 1 ;
252+ if (! php_filter_parse_octal (p , len , & ctx_value )) {
253+ error = true ;
253254 }
254255 } else if (len != 0 ) {
255- error = 1 ;
256+ error = true ;
256257 }
257258 } else {
258- if (php_filter_parse_int (p , len , & ctx_value ) < 0 ) {
259- error = 1 ;
259+ if (! php_filter_parse_int (p , len , & ctx_value )) {
260+ error = true ;
260261 }
261262 }
262263
263- if (error > 0 || (min_range_set && (ctx_value < min_range )) || (max_range_set && (ctx_value > max_range ))) {
264+ if (error || (min_range_set && (ctx_value < min_range )) || (max_range_set && (ctx_value > max_range ))) {
264265 RETURN_VALIDATION_FAILED
265266 } else {
266267 zval_ptr_dtor (value );
@@ -359,7 +360,7 @@ void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
359360 double min_range , max_range ;
360361 int min_range_set , max_range_set ;
361362
362- int first , n ;
363+ int n ;
363364
364365 len = Z_STRLEN_P (value );
365366 str = Z_STRVAL_P (value );
@@ -398,7 +399,7 @@ void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
398399 if (str < end && (* str == '+' || * str == '-' )) {
399400 * p ++ = * str ++ ;
400401 }
401- first = 1 ;
402+ bool first = true ;
402403 while (1 ) {
403404 n = 0 ;
404405 while (str < end && * str >= '0' && * str <= '9' ) {
@@ -431,7 +432,7 @@ void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
431432 if (first ?(n < 1 || n > 3 ):(n != 3 )) {
432433 goto error ;
433434 }
434- first = 0 ;
435+ first = false ;
435436 str ++ ;
436437 } else {
437438 goto error ;
@@ -504,7 +505,7 @@ void php_filter_validate_regexp(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
504505 }
505506}
506507
507- static int php_filter_validate_domain_ex (const zend_string * domain , zend_long flags ) /* {{{ */
508+ static bool php_filter_validate_domain_ex (const zend_string * domain , zend_long flags ) /* {{{ */
508509{
509510 const char * e , * s , * t ;
510511 size_t l ;
@@ -524,26 +525,26 @@ static int php_filter_validate_domain_ex(const zend_string *domain, zend_long fl
524525
525526 /* The total length cannot exceed 253 characters (final dot not included) */
526527 if (l > 253 ) {
527- return 0 ;
528+ return false ;
528529 }
529530
530531 /* First char must be alphanumeric */
531532 if (* s == '.' || (hostname && !isalnum ((int )* (unsigned char * )s ))) {
532- return 0 ;
533+ return false ;
533534 }
534535
535536 while (s < e ) {
536537 if (* s == '.' ) {
537538 /* The first and the last character of a label must be alphanumeric */
538539 if (* (s + 1 ) == '.' || (hostname && (!isalnum ((int )* (unsigned char * )(s - 1 )) || !isalnum ((int )* (unsigned char * )(s + 1 ))))) {
539- return 0 ;
540+ return false ;
540541 }
541542
542543 /* Reset label length counter */
543544 i = 1 ;
544545 } else {
545546 if (i > 63 || (hostname && (* s != '-' || * (s + 1 ) == '\0' ) && !isalnum ((int )* (unsigned char * )s ))) {
546- return 0 ;
547+ return false ;
547548 }
548549
549550 i ++ ;
@@ -552,7 +553,7 @@ static int php_filter_validate_domain_ex(const zend_string *domain, zend_long fl
552553 s ++ ;
553554 }
554555
555- return 1 ;
556+ return true ;
556557}
557558/* }}} */
558559
@@ -564,7 +565,7 @@ void php_filter_validate_domain(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
564565}
565566/* }}} */
566567
567- static int is_userinfo_valid (const zend_string * str )
568+ static bool is_userinfo_valid (const zend_string * str )
568569{
569570 const char * p = ZSTR_VAL (str );
570571 while (p - ZSTR_VAL (str ) < ZSTR_LEN (str )) {
@@ -574,10 +575,10 @@ static int is_userinfo_valid(const zend_string *str)
574575 } else if (* p == '%' && p - ZSTR_VAL (str ) <= ZSTR_LEN (str ) - 3 && isdigit (* (p + 1 )) && isxdigit (* (p + 2 ))) {
575576 p += 3 ;
576577 } else {
577- return 0 ;
578+ return false ;
578579 }
579580 }
580- return 1 ;
581+ return true ;
581582}
582583
583584static bool php_filter_is_valid_ipv6_hostname (const zend_string * s )
@@ -718,42 +719,42 @@ void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
718719}
719720/* }}} */
720721
721- static int _php_filter_validate_ipv4 (const char * str , size_t str_len , int * ip ) /* {{{ */
722+ static bool _php_filter_validate_ipv4 (const char * str , size_t str_len , int * ip ) /* {{{ */
722723{
723724 const char * end = str + str_len ;
724725 int num , m ;
725726 int n = 0 ;
726727
727728 while (str < end ) {
728- int leading_zero ;
729+ bool leading_zero ;
729730 if (* str < '0' || * str > '9' ) {
730- return 0 ;
731+ return false ;
731732 }
732733 leading_zero = (* str == '0' );
733734 m = 1 ;
734735 num = ((* (str ++ )) - '0' );
735736 while (str < end && (* str >= '0' && * str <= '9' )) {
736737 num = num * 10 + ((* (str ++ )) - '0' );
737738 if (num > 255 || ++ m > 3 ) {
738- return 0 ;
739+ return false ;
739740 }
740741 }
741742 /* don't allow a leading 0; that introduces octal numbers,
742743 * which we don't support */
743744 if (leading_zero && (num != 0 || m > 1 ))
744- return 0 ;
745+ return false ;
745746 ip [n ++ ] = num ;
746747 if (n == 4 ) {
747748 return str == end ;
748749 } else if (str >= end || * (str ++ ) != '.' ) {
749- return 0 ;
750+ return false ;
750751 }
751752 }
752- return 0 ;
753+ return false ;
753754}
754755/* }}} */
755756
756- static int _php_filter_validate_ipv6 (const char * str , size_t str_len , int ip [8 ]) /* {{{ */
757+ static bool _php_filter_validate_ipv6 (const char * str , size_t str_len , int ip [8 ]) /* {{{ */
757758{
758759 int compressed_pos = -1 ;
759760 int blocks = 0 ;
@@ -797,25 +798,25 @@ static int _php_filter_validate_ipv6(const char *str, size_t str_len, int ip[8])
797798 if (* str == ':' ) {
798799 if (++ str >= end ) {
799800 /* cannot end in : without previous : */
800- return 0 ;
801+ return false ;
801802 }
802803 if (* str == ':' ) {
803804 if (compressed_pos >= 0 ) {
804- return 0 ;
805+ return false ;
805806 }
806807 if (ip && blocks < 8 ) {
807808 ip [blocks ] = -1 ;
808809 }
809810 compressed_pos = blocks ++ ; /* :: means 1 or more 16-bit 0 blocks */
810811 if (++ str == end ) {
811812 if (blocks > 8 ) {
812- return 0 ;
813+ return false ;
813814 }
814815 goto fixup_ip ;
815816 }
816817 } else if ((str - 1 ) == s ) {
817818 /* don't allow leading : without another : following */
818- return 0 ;
819+ return false ;
819820 }
820821 }
821822 num = n = 0 ;
@@ -836,10 +837,10 @@ static int _php_filter_validate_ipv6(const char *str, size_t str_len, int ip[8])
836837 ip [blocks ] = num ;
837838 }
838839 if (n < 1 || n > 4 ) {
839- return 0 ;
840+ return false ;
840841 }
841842 if (++ blocks > 8 )
842- return 0 ;
843+ return false ;
843844 }
844845
845846fixup_ip :
@@ -1005,7 +1006,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
10051006 }
10061007 }
10071008 else if (mode == FORMAT_IPV6 ) {
1008- if (_php_filter_validate_ipv6 (Z_STRVAL_P (value ), Z_STRLEN_P (value ), ip ) < 1 ) {
1009+ if (! _php_filter_validate_ipv6 (Z_STRVAL_P (value ), Z_STRLEN_P (value ), ip )) {
10091010 RETURN_VALIDATION_FAILED
10101011 }
10111012
@@ -1082,7 +1083,7 @@ void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
10821083 /* The current token did not end with e.g. a "." */
10831084 RETURN_VALIDATION_FAILED
10841085 }
1085- if (php_filter_parse_hex (input + offset , length , & ret ) < 0 ) {
1086+ if (! php_filter_parse_hex (input + offset , length , & ret )) {
10861087 /* The current token is no valid hexadecimal digit */
10871088 RETURN_VALIDATION_FAILED
10881089 }
0 commit comments