xref: /PHP-7.2/ext/intl/idn/idn.c (revision c43fc204)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2009 The PHP Group                                     |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Pierre A. Joye <pierre@php.net>                              |
16    |         Gustavo Lopes  <cataphract@php.net>                          |
17    +----------------------------------------------------------------------+
18  */
19 /* $Id$ */
20 
21 /* {{{ includes */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <php.h>
27 
28 #include <unicode/uidna.h>
29 #include <unicode/ustring.h>
30 #include "ext/standard/php_string.h"
31 
32 #include "intl_error.h"
33 #include "intl_convert.h"
34 /* }}} */
35 
36 #ifdef UIDNA_INFO_INITIALIZER
37 #define HAVE_46_API 1 /* has UTS#46 API (introduced in ICU 4.6) */
38 #endif
39 
40 enum {
41 	INTL_IDN_VARIANT_2003 = 0,
42 	INTL_IDN_VARIANT_UTS46
43 };
44 
45 /* {{{ grapheme_register_constants
46  * Register API constants
47  */
idn_register_constants(INIT_FUNC_ARGS)48 void idn_register_constants( INIT_FUNC_ARGS )
49 {
50 	/* OPTIONS */
51 
52 	/* Option to prohibit processing of unassigned codepoints in the input and
53 	   do not check if the input conforms to STD-3 ASCII rules. */
54 	REGISTER_LONG_CONSTANT("IDNA_DEFAULT", UIDNA_DEFAULT, CONST_CS | CONST_PERSISTENT);
55 
56 	/* Option to allow processing of unassigned codepoints in the input */
57 	REGISTER_LONG_CONSTANT("IDNA_ALLOW_UNASSIGNED", UIDNA_ALLOW_UNASSIGNED, CONST_CS | CONST_PERSISTENT);
58 
59 	/* Option to check if input conforms to STD-3 ASCII rules */
60 	REGISTER_LONG_CONSTANT("IDNA_USE_STD3_RULES", UIDNA_USE_STD3_RULES, CONST_CS | CONST_PERSISTENT);
61 
62 #ifdef HAVE_46_API
63 
64 	/* Option to check for whether the input conforms to the BiDi rules.
65 	 * Ignored by the IDNA2003 implementation. (IDNA2003 always performs a BiDi check.) */
66 	REGISTER_LONG_CONSTANT("IDNA_CHECK_BIDI", UIDNA_CHECK_BIDI, CONST_CS | CONST_PERSISTENT);
67 
68 	/* Option to check for whether the input conforms to the CONTEXTJ rules.
69 	 * Ignored by the IDNA2003 implementation. (The CONTEXTJ check is new in IDNA2008.) */
70 	REGISTER_LONG_CONSTANT("IDNA_CHECK_CONTEXTJ", UIDNA_CHECK_CONTEXTJ, CONST_CS | CONST_PERSISTENT);
71 
72 	/* Option for nontransitional processing in ToASCII().
73 	 * By default, ToASCII() uses transitional processing.
74 	 * Ignored by the IDNA2003 implementation. */
75 	REGISTER_LONG_CONSTANT("IDNA_NONTRANSITIONAL_TO_ASCII", UIDNA_NONTRANSITIONAL_TO_ASCII, CONST_CS | CONST_PERSISTENT);
76 
77 	/* Option for nontransitional processing in ToUnicode().
78 	 * By default, ToUnicode() uses transitional processing.
79 	 * Ignored by the IDNA2003 implementation. */
80 	REGISTER_LONG_CONSTANT("IDNA_NONTRANSITIONAL_TO_UNICODE", UIDNA_NONTRANSITIONAL_TO_UNICODE, CONST_CS | CONST_PERSISTENT);
81 #endif
82 
83 	/* VARIANTS */
84 	REGISTER_LONG_CONSTANT("INTL_IDNA_VARIANT_2003", INTL_IDN_VARIANT_2003, CONST_CS | CONST_PERSISTENT);
85 #ifdef HAVE_46_API
86 	REGISTER_LONG_CONSTANT("INTL_IDNA_VARIANT_UTS46", INTL_IDN_VARIANT_UTS46, CONST_CS | CONST_PERSISTENT);
87 #endif
88 
89 #ifdef HAVE_46_API
90 	/* PINFO ERROR CODES */
91 	REGISTER_LONG_CONSTANT("IDNA_ERROR_EMPTY_LABEL", UIDNA_ERROR_EMPTY_LABEL, CONST_CS | CONST_PERSISTENT);
92 	REGISTER_LONG_CONSTANT("IDNA_ERROR_LABEL_TOO_LONG", UIDNA_ERROR_LABEL_TOO_LONG, CONST_CS | CONST_PERSISTENT);
93 	REGISTER_LONG_CONSTANT("IDNA_ERROR_DOMAIN_NAME_TOO_LONG", UIDNA_ERROR_DOMAIN_NAME_TOO_LONG, CONST_CS | CONST_PERSISTENT);
94 	REGISTER_LONG_CONSTANT("IDNA_ERROR_LEADING_HYPHEN", UIDNA_ERROR_LEADING_HYPHEN, CONST_CS | CONST_PERSISTENT);
95 	REGISTER_LONG_CONSTANT("IDNA_ERROR_TRAILING_HYPHEN", UIDNA_ERROR_TRAILING_HYPHEN, CONST_CS | CONST_PERSISTENT);
96 	REGISTER_LONG_CONSTANT("IDNA_ERROR_HYPHEN_3_4", UIDNA_ERROR_HYPHEN_3_4, CONST_CS | CONST_PERSISTENT);
97 	REGISTER_LONG_CONSTANT("IDNA_ERROR_LEADING_COMBINING_MARK", UIDNA_ERROR_LEADING_COMBINING_MARK, CONST_CS | CONST_PERSISTENT);
98 	REGISTER_LONG_CONSTANT("IDNA_ERROR_DISALLOWED", UIDNA_ERROR_DISALLOWED, CONST_CS | CONST_PERSISTENT);
99 	REGISTER_LONG_CONSTANT("IDNA_ERROR_PUNYCODE", UIDNA_ERROR_PUNYCODE, CONST_CS | CONST_PERSISTENT);
100 	REGISTER_LONG_CONSTANT("IDNA_ERROR_LABEL_HAS_DOT", UIDNA_ERROR_LABEL_HAS_DOT, CONST_CS | CONST_PERSISTENT);
101 	REGISTER_LONG_CONSTANT("IDNA_ERROR_INVALID_ACE_LABEL", UIDNA_ERROR_INVALID_ACE_LABEL, CONST_CS | CONST_PERSISTENT);
102 	REGISTER_LONG_CONSTANT("IDNA_ERROR_BIDI", UIDNA_ERROR_BIDI, CONST_CS | CONST_PERSISTENT);
103 	REGISTER_LONG_CONSTANT("IDNA_ERROR_CONTEXTJ", UIDNA_ERROR_CONTEXTJ, CONST_CS | CONST_PERSISTENT);
104 #endif
105 }
106 /* }}} */
107 
108 enum {
109 	INTL_IDN_TO_ASCII = 0,
110 	INTL_IDN_TO_UTF8
111 };
112 
113 /* like INTL_CHECK_STATUS, but as a function and varying the name of the func */
php_intl_idn_check_status(UErrorCode err,const char * msg)114 static int php_intl_idn_check_status(UErrorCode err, const char *msg)
115 {
116 	intl_error_set_code(NULL, err);
117 	if (U_FAILURE(err)) {
118 		char *buff;
119 		spprintf(&buff, 0, "%s: %s",
120 			get_active_function_name(),
121 			msg);
122 		intl_error_set_custom_msg(NULL, buff, 1);
123 		efree(buff);
124 		return FAILURE;
125 	}
126 
127 	return SUCCESS;
128 }
129 
php_intl_bad_args(const char * msg)130 static inline void php_intl_bad_args(const char *msg)
131 {
132 	php_intl_idn_check_status(U_ILLEGAL_ARGUMENT_ERROR, msg);
133 }
134 
135 #ifdef HAVE_46_API
php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS,const zend_string * domain,uint32_t option,int mode,zval * idna_info)136 static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS,
137 		const zend_string *domain, uint32_t option, int mode, zval *idna_info)
138 {
139 	UErrorCode	  status = U_ZERO_ERROR;
140 	UIDNA		  *uts46;
141 	int32_t		  len;
142 	int32_t		  buffer_capac = 255; /* no domain name may exceed this */
143 	zend_string	  *buffer = zend_string_alloc(buffer_capac, 0);
144 	UIDNAInfo	  info = UIDNA_INFO_INITIALIZER;
145 	int			  buffer_used = 0;
146 
147 	uts46 = uidna_openUTS46(option, &status);
148 	if (php_intl_idn_check_status(status, "failed to open UIDNA instance") == FAILURE) {
149 		zend_string_free(buffer);
150 		RETURN_FALSE;
151 	}
152 
153 	if (mode == INTL_IDN_TO_ASCII) {
154 		len = uidna_nameToASCII_UTF8(uts46, ZSTR_VAL(domain), ZSTR_LEN(domain),
155 				ZSTR_VAL(buffer), buffer_capac, &info, &status);
156 	} else {
157 		len = uidna_nameToUnicodeUTF8(uts46, ZSTR_VAL(domain), ZSTR_LEN(domain),
158 				ZSTR_VAL(buffer), buffer_capac, &info, &status);
159 	}
160 	if (len >= 255 || php_intl_idn_check_status(status, "failed to convert name") == FAILURE) {
161 		uidna_close(uts46);
162 		zend_string_free(buffer);
163 		RETURN_FALSE;
164 	}
165 
166 	ZSTR_VAL(buffer)[len] = '\0';
167 	ZSTR_LEN(buffer) = len;
168 
169 	if (info.errors == 0) {
170 		RETVAL_STR(buffer);
171 		buffer_used = 1;
172 	} else {
173 		RETVAL_FALSE;
174 	}
175 
176 	if (idna_info) {
177 		if (buffer_used) { /* used in return_value then */
178 			zval_addref_p(return_value);
179 			add_assoc_zval_ex(idna_info, "result", sizeof("result")-1, return_value);
180 		} else {
181 			zval zv;
182 			ZVAL_NEW_STR(&zv, buffer);
183 			buffer_used = 1;
184 			add_assoc_zval_ex(idna_info, "result", sizeof("result")-1, &zv);
185 		}
186 		add_assoc_bool_ex(idna_info, "isTransitionalDifferent",
187 				sizeof("isTransitionalDifferent")-1, info.isTransitionalDifferent);
188 		add_assoc_long_ex(idna_info, "errors", sizeof("errors")-1, (zend_long)info.errors);
189 	}
190 
191 	if (!buffer_used) {
192 		zend_string_free(buffer);
193 	}
194 
195 	uidna_close(uts46);
196 }
197 #endif
198 
php_intl_idn_to(INTERNAL_FUNCTION_PARAMETERS,const zend_string * domain,uint32_t option,int mode)199 static void php_intl_idn_to(INTERNAL_FUNCTION_PARAMETERS,
200 		const zend_string *domain, uint32_t option, int mode)
201 {
202 	UChar* ustring = NULL;
203 	int ustring_len = 0;
204 	UErrorCode status;
205 	zend_string *u8str;
206 	UChar     converted[MAXPATHLEN];
207 	int32_t   converted_ret_len;
208 
209 	/* convert the string to UTF-16. */
210 	status = U_ZERO_ERROR;
211 	intl_convert_utf8_to_utf16(&ustring, &ustring_len, ZSTR_VAL(domain), ZSTR_LEN(domain), &status);
212 
213 	if (U_FAILURE(status)) {
214 		intl_error_set_code(NULL, status);
215 
216 		/* Set error messages. */
217 		intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 );
218 		if (ustring) {
219 			efree(ustring);
220 		}
221 		RETURN_FALSE;
222 	} else {
223 		UParseError parse_error;
224 
225 		status = U_ZERO_ERROR;
226 #if defined(__clang__)
227 # pragma clang diagnostic push
228 # pragma clang diagnostic ignored "-Wdeprecated-declarations"
229 #elif ZEND_GCC_VERSION >= 4008
230 # pragma GCC diagnostic push
231 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
232 #endif
233 		if (mode == INTL_IDN_TO_ASCII) {
234 			converted_ret_len = uidna_IDNToASCII(ustring, ustring_len, converted, MAXPATHLEN, (int32_t)option, &parse_error, &status);
235 		} else {
236 			converted_ret_len = uidna_IDNToUnicode(ustring, ustring_len, converted, MAXPATHLEN, (int32_t)option, &parse_error, &status);
237 		}
238 #if defined(__clang__)
239 # pragma clang diagnostic pop
240 #elif ZEND_GCC_VERSION >= 4008
241 # pragma GCC diagnostic pop
242 #endif
243 		efree(ustring);
244 
245 		if (U_FAILURE(status)) {
246 			intl_error_set( NULL, status, "idn_to_ascii: cannot convert to ASCII", 0 );
247 			RETURN_FALSE;
248 		}
249 
250 		status = U_ZERO_ERROR;
251 		u8str = intl_convert_utf16_to_utf8(converted, converted_ret_len, &status);
252 
253 		if (!u8str) {
254 			/* Set global error code. */
255 			intl_error_set_code(NULL, status);
256 
257 			/* Set error messages. */
258 			intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 );
259 			RETURN_FALSE;
260 		}
261 	}
262 
263 	/* return the allocated string, not a duplicate */
264 	RETVAL_NEW_STR(u8str);
265 }
266 
php_intl_idn_handoff(INTERNAL_FUNCTION_PARAMETERS,int mode)267 static void php_intl_idn_handoff(INTERNAL_FUNCTION_PARAMETERS, int mode)
268 {
269 	zend_string *domain;
270 	zend_long option = 0,
271 		 variant = INTL_IDN_VARIANT_2003;
272 	zval *idna_info = NULL;
273 
274 	intl_error_reset(NULL);
275 
276 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|llz/",
277 			&domain, &option, &variant, &idna_info) == FAILURE) {
278 		php_intl_bad_args("bad arguments");
279 		RETURN_NULL(); /* don't set FALSE because that's not the way it was before... */
280 	}
281 
282 #ifdef HAVE_46_API
283 	if (variant != INTL_IDN_VARIANT_2003 && variant != INTL_IDN_VARIANT_UTS46) {
284 		php_intl_bad_args("invalid variant, must be one of {"
285 			"INTL_IDNA_VARIANT_2003, INTL_IDNA_VARIANT_UTS46}");
286 		RETURN_FALSE;
287 	}
288 #else
289 	if (variant != INTL_IDN_VARIANT_2003) {
290 		php_intl_bad_args("invalid variant, PHP was compiled against "
291 			"an old version of ICU and only supports INTL_IDN_VARIANT_2003");
292 		RETURN_FALSE;
293 	}
294 #endif
295 
296 	if (ZSTR_LEN(domain) < 1) {
297 		php_intl_bad_args("empty domain name");
298 		RETURN_FALSE;
299 	}
300 	if (ZSTR_LEN(domain) > INT32_MAX - 1) {
301 		php_intl_bad_args("domain name too large");
302 		RETURN_FALSE;
303 	}
304 	/* don't check options; it wasn't checked before */
305 
306 	if (variant == INTL_IDN_VARIANT_2003) {
307 		php_error_docref(NULL, E_DEPRECATED, "INTL_IDNA_VARIANT_2003 is deprecated");
308 	}
309 
310 	if (idna_info != NULL) {
311 		if (variant == INTL_IDN_VARIANT_2003) {
312 			php_error_docref0(NULL, E_NOTICE,
313 				"4 arguments were provided, but INTL_IDNA_VARIANT_2003 only "
314 				"takes 3 - extra argument ignored");
315 		} else {
316 			zval_dtor(idna_info);
317 			array_init(idna_info);
318 		}
319 	}
320 
321 	if (variant == INTL_IDN_VARIANT_2003) {
322 		php_intl_idn_to(INTERNAL_FUNCTION_PARAM_PASSTHRU, domain, (uint32_t)option, mode);
323 	}
324 #ifdef HAVE_46_API
325 	else {
326 		php_intl_idn_to_46(INTERNAL_FUNCTION_PARAM_PASSTHRU, domain, (uint32_t)option, mode, idna_info);
327 	}
328 #endif
329 }
330 
331 /* {{{ proto string idn_to_ascii(string domain[, int options[, int variant[, array &idna_info]]])
332    Converts an Unicode domain to ASCII representation, as defined in the IDNA RFC */
PHP_FUNCTION(idn_to_ascii)333 PHP_FUNCTION(idn_to_ascii)
334 {
335 	php_intl_idn_handoff(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTL_IDN_TO_ASCII);
336 }
337 /* }}} */
338 
339 
340 /* {{{ proto string idn_to_utf8(string domain[, int options[, int variant[, array &idna_info]]])
341    Converts an ASCII representation of the domain to Unicode (UTF-8), as defined in the IDNA RFC */
PHP_FUNCTION(idn_to_utf8)342 PHP_FUNCTION(idn_to_utf8)
343 {
344 	php_intl_idn_handoff(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTL_IDN_TO_UTF8);
345 }
346 /* }}} */
347 
348 
349 /*
350  * Local variables:
351  * tab-width: 4
352  * c-basic-offset: 4
353  * End:
354  * vim600: fdm=marker
355  * vim: noet sw=4 ts=4
356  */
357