1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Hartmut Holzgraefe <hholzgra@php.net>                        |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "php_ctype.h"
24 #include "ctype_arginfo.h"
25 #include "SAPI.h"
26 #include "ext/standard/info.h"
27 
28 #include <ctype.h>
29 
30 #ifdef HAVE_CTYPE
31 
32 static PHP_MINFO_FUNCTION(ctype);
33 
34 /* }}} */
35 
36 /* {{{ ctype_module_entry */
37 zend_module_entry ctype_module_entry = {
38 	STANDARD_MODULE_HEADER,
39 	"ctype",
40 	ext_functions,
41 	NULL,
42 	NULL,
43 	NULL,
44 	NULL,
45 	PHP_MINFO(ctype),
46 	PHP_CTYPE_VERSION,
47 	STANDARD_MODULE_PROPERTIES
48 };
49 /* }}} */
50 
51 #ifdef COMPILE_DL_CTYPE
52 ZEND_GET_MODULE(ctype)
53 #endif
54 
55 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(ctype)56 static PHP_MINFO_FUNCTION(ctype)
57 {
58 	php_info_print_table_start();
59 	php_info_print_table_row(2, "ctype functions", "enabled");
60 	php_info_print_table_end();
61 }
62 /* }}} */
63 
ctype_impl(INTERNAL_FUNCTION_PARAMETERS,int (* iswhat)(int),bool allow_digits,bool allow_minus)64 static void ctype_impl(
65 		INTERNAL_FUNCTION_PARAMETERS, int (*iswhat)(int), bool allow_digits, bool allow_minus) {
66 	zval *c;
67 
68 	ZEND_PARSE_PARAMETERS_START(1, 1);
69 		Z_PARAM_ZVAL(c)
70 	ZEND_PARSE_PARAMETERS_END();
71 
72 	if (Z_TYPE_P(c) == IS_STRING) {
73 		char *p = Z_STRVAL_P(c), *e = Z_STRVAL_P(c) + Z_STRLEN_P(c);
74 		if (e == p) {
75 			RETURN_FALSE;
76 		}
77 		while (p < e) {
78 			if (!iswhat((int)*(unsigned char *)(p++))) {
79 				RETURN_FALSE;
80 			}
81 		}
82 		RETURN_TRUE;
83 	}
84 
85 	php_error_docref(NULL, E_DEPRECATED,
86 		"Argument of type %s will be interpreted as string in the future", zend_zval_type_name(c));
87 	if (Z_TYPE_P(c) == IS_LONG) {
88 		if (Z_LVAL_P(c) <= 255 && Z_LVAL_P(c) >= 0) {
89 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c)));
90 		} else if (Z_LVAL_P(c) >= -128 && Z_LVAL_P(c) < 0) {
91 			RETURN_BOOL(iswhat((int)Z_LVAL_P(c) + 256));
92 		} else if (Z_LVAL_P(c) >= 0) {
93 			RETURN_BOOL(allow_digits);
94 		} else {
95 			RETURN_BOOL(allow_minus);
96 		}
97 	} else {
98 		RETURN_FALSE;
99 	}
100 }
101 
102 /* {{{ Checks for alphanumeric character(s) */
PHP_FUNCTION(ctype_alnum)103 PHP_FUNCTION(ctype_alnum)
104 {
105 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isalnum, 1, 0);
106 }
107 /* }}} */
108 
109 /* {{{ Checks for alphabetic character(s) */
PHP_FUNCTION(ctype_alpha)110 PHP_FUNCTION(ctype_alpha)
111 {
112 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isalpha, 0, 0);
113 }
114 /* }}} */
115 
116 /* {{{ Checks for control character(s) */
PHP_FUNCTION(ctype_cntrl)117 PHP_FUNCTION(ctype_cntrl)
118 {
119 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, iscntrl, 0, 0);
120 }
121 /* }}} */
122 
123 /* {{{ Checks for numeric character(s) */
PHP_FUNCTION(ctype_digit)124 PHP_FUNCTION(ctype_digit)
125 {
126 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isdigit, 1, 0);
127 }
128 /* }}} */
129 
130 /* {{{ Checks for lowercase character(s)  */
PHP_FUNCTION(ctype_lower)131 PHP_FUNCTION(ctype_lower)
132 {
133 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, islower, 0, 0);
134 }
135 /* }}} */
136 
137 /* {{{ Checks for any printable character(s) except space */
PHP_FUNCTION(ctype_graph)138 PHP_FUNCTION(ctype_graph)
139 {
140 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isgraph, 1, 1);
141 }
142 /* }}} */
143 
144 /* {{{ Checks for printable character(s) */
PHP_FUNCTION(ctype_print)145 PHP_FUNCTION(ctype_print)
146 {
147 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isprint, 1, 1);
148 }
149 /* }}} */
150 
151 /* {{{ Checks for any printable character which is not whitespace or an alphanumeric character */
PHP_FUNCTION(ctype_punct)152 PHP_FUNCTION(ctype_punct)
153 {
154 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ispunct, 0, 0);
155 }
156 /* }}} */
157 
158 /* {{{ Checks for whitespace character(s)*/
PHP_FUNCTION(ctype_space)159 PHP_FUNCTION(ctype_space)
160 {
161 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isspace, 0, 0);
162 }
163 /* }}} */
164 
165 /* {{{ Checks for uppercase character(s) */
PHP_FUNCTION(ctype_upper)166 PHP_FUNCTION(ctype_upper)
167 {
168 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isupper, 0, 0);
169 }
170 /* }}} */
171 
172 /* {{{ Checks for character(s) representing a hexadecimal digit */
PHP_FUNCTION(ctype_xdigit)173 PHP_FUNCTION(ctype_xdigit)
174 {
175 	ctype_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, isxdigit, 1, 0);
176 }
177 /* }}} */
178 
179 #endif	/* HAVE_CTYPE */
180